BodyComparer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. using Valve.VR;
  6. public class BodyComparer : MonoBehaviour
  7. {
  8. public BodySourceView bsv;
  9. public ViveInput viveInput;
  10. private List<JointsData> jointsDataDemo = new List<JointsData>();
  11. private List<float> recordingTimesDemo = new List<float>();
  12. // For saving to CSV
  13. private List<List<Vector3>> posJointsDemos = new List<List<Vector3>>();
  14. private List<List<Vector3>> posJointsBodys = new List<List<Vector3>>();
  15. private List<float> csvTimes = new List<float>();
  16. public void SetDataDemo(List<JointsData> jointsDataDemo, List<float> recordingTimesDemo)
  17. {
  18. this.jointsDataDemo = jointsDataDemo;
  19. this.recordingTimesDemo = recordingTimesDemo;
  20. }
  21. public IEnumerator StartCompare()
  22. {
  23. if (bsv.body == null)
  24. {
  25. yield return new WaitForSeconds(3);
  26. yield break;
  27. }
  28. for (int i = 0; i < jointsDataDemo.Count; i++)
  29. {
  30. Transform body = bsv.body.transform;
  31. List<Vector3> oneBodyDemo = new List<Vector3>();
  32. List<Vector3> oneBodyReal = new List<Vector3>();
  33. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  34. {
  35. // Skip these joints
  36. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  37. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  38. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  39. continue;
  40. JointsData jd = jointsDataDemo[i];
  41. Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  42. Vector3 posJointBody = body.GetChild((int)jt).position;
  43. // Save positions demo and real body
  44. oneBodyDemo.Add(posJointDemo);
  45. oneBodyReal.Add(posJointBody);
  46. float distance = (posJointBody - posJointDemo).magnitude;
  47. if (distance > 0.15f)
  48. {
  49. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.red;
  50. // TODO: Haptic feedback
  51. //SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  52. //SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  53. //SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftFoot].Execute(0, 0.01f, 10, 1);
  54. //SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightFoot].Execute(0, 0.01f, 10, 1);
  55. }
  56. else
  57. {
  58. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  59. }
  60. }
  61. // Save it to csv later
  62. posJointsDemos.Add(oneBodyDemo);
  63. posJointsBodys.Add(oneBodyReal);
  64. csvTimes.Add(viveInput.csvTime);
  65. float waitTime = 0.01f;
  66. if (i < recordingTimesDemo.Count - 1)
  67. {
  68. waitTime = recordingTimesDemo[i + 1] - recordingTimesDemo[i];
  69. }
  70. yield return new WaitForSeconds(waitTime);
  71. }
  72. // Make all joints white
  73. Transform bodyEnd = bsv.body.transform;
  74. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  75. {
  76. // Skip these joints
  77. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  78. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  79. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  80. continue;
  81. bodyEnd.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  82. }
  83. }
  84. public void WriteCSV()
  85. {
  86. Debug.Log("Save to CSV");
  87. CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
  88. }
  89. }