BodyComparer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (jt == Kinect.JointType.WristLeft)
  52. {
  53. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  54. }
  55. else if (jt == Kinect.JointType.WristRight)
  56. {
  57. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  58. }
  59. else if (jt == Kinect.JointType.AnkleLeft)
  60. {
  61. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftFoot].Execute(0, 0.01f, 10, 1);
  62. }
  63. else if (jt == Kinect.JointType.AnkleRight)
  64. {
  65. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightFoot].Execute(0, 0.01f, 10, 1);
  66. }
  67. }
  68. else
  69. {
  70. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  71. }
  72. }
  73. // Save it to csv later
  74. posJointsDemos.Add(oneBodyDemo);
  75. posJointsBodys.Add(oneBodyReal);
  76. csvTimes.Add(viveInput.csvTime);
  77. float waitTime = 0.01f;
  78. if (i < recordingTimesDemo.Count - 1)
  79. {
  80. waitTime = recordingTimesDemo[i + 1] - recordingTimesDemo[i];
  81. }
  82. yield return new WaitForSeconds(waitTime);
  83. }
  84. // Make all joints white
  85. Transform bodyEnd = bsv.body.transform;
  86. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  87. {
  88. // Skip these joints
  89. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  90. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  91. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  92. continue;
  93. bodyEnd.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  94. }
  95. }
  96. public void WriteCSV()
  97. {
  98. Debug.Log("Save to CSV");
  99. CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
  100. }
  101. }