BodyComparer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. private bool isComparing;
  17. public bool endStepsPressed;
  18. private void Update()
  19. {
  20. if (!isComparing)
  21. return;
  22. if (SteamVR_Actions.default_GrabPinch.GetStateDown(SteamVR_Input_Sources.Any) || Input.GetKeyDown(KeyCode.Space))
  23. {
  24. isComparing = false;
  25. endStepsPressed = true;
  26. }
  27. }
  28. public void SetDataDemo(List<JointsData> jointsDataDemo, List<float> recordingTimesDemo)
  29. {
  30. this.jointsDataDemo = jointsDataDemo;
  31. this.recordingTimesDemo = recordingTimesDemo;
  32. }
  33. public IEnumerator StartCompare()
  34. {
  35. if (bsv.body == null)
  36. {
  37. yield return new WaitForSeconds(3);
  38. yield break;
  39. }
  40. isComparing = true;
  41. for (int i = 0; i < jointsDataDemo.Count; i++)
  42. {
  43. Transform body = bsv.body.transform;
  44. List<Vector3> oneBodyDemo = new List<Vector3>();
  45. List<Vector3> oneBodyReal = new List<Vector3>();
  46. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  47. {
  48. // Skip these joints
  49. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  50. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  51. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  52. continue;
  53. JointsData jd = jointsDataDemo[i];
  54. Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  55. Vector3 posJointBody = body.GetChild((int)jt).position;
  56. // Save positions demo and real body
  57. oneBodyDemo.Add(posJointDemo);
  58. oneBodyReal.Add(posJointBody);
  59. float distance = (posJointBody - posJointDemo).magnitude;
  60. if (distance > 0.15f)
  61. {
  62. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.red;
  63. // TODO: Haptic feedback
  64. if (jt == Kinect.JointType.WristLeft)
  65. {
  66. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  67. }
  68. else if (jt == Kinect.JointType.WristRight)
  69. {
  70. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  71. }
  72. else if (jt == Kinect.JointType.AnkleLeft)
  73. {
  74. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftFoot].Execute(0, 0.01f, 10, 1);
  75. }
  76. else if (jt == Kinect.JointType.AnkleRight)
  77. {
  78. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightFoot].Execute(0, 0.01f, 10, 1);
  79. }
  80. }
  81. else
  82. {
  83. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  84. }
  85. }
  86. // Save it to csv later
  87. posJointsDemos.Add(oneBodyDemo);
  88. posJointsBodys.Add(oneBodyReal);
  89. csvTimes.Add(viveInput.csvTime);
  90. // If pressed stop, end immediately and save last to csv
  91. if (!isComparing)
  92. {
  93. for (int j = i + 1; j < jointsDataDemo.Count; j++)
  94. {
  95. List<Vector3> oneBodyDemo2 = new List<Vector3>();
  96. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  97. {
  98. // Skip these joints
  99. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  100. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  101. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  102. continue;
  103. JointsData jd = jointsDataDemo[j];
  104. Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  105. // Save positions demo and real body
  106. oneBodyDemo2.Add(posJointDemo);
  107. }
  108. // Save it to csv later
  109. posJointsDemos.Add(oneBodyDemo2);
  110. posJointsBodys.Add(oneBodyReal);
  111. csvTimes.Add(viveInput.csvTime);
  112. }
  113. // Make all joints white
  114. Transform bodyEnd2 = bsv.body.transform;
  115. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  116. {
  117. // Skip these joints
  118. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  119. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  120. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  121. continue;
  122. bodyEnd2.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  123. }
  124. yield break;
  125. }
  126. float waitTime = 0;
  127. if (isComparing && i < recordingTimesDemo.Count - 1)
  128. {
  129. waitTime = recordingTimesDemo[i + 1] - recordingTimesDemo[i];
  130. }
  131. yield return new WaitForSeconds(waitTime);
  132. }
  133. isComparing = false;
  134. // Make all joints white
  135. Transform bodyEnd = bsv.body.transform;
  136. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  137. {
  138. // Skip these joints
  139. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  140. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  141. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  142. continue;
  143. bodyEnd.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  144. }
  145. }
  146. public void WriteCSV()
  147. {
  148. Debug.Log("Save to CSV");
  149. CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
  150. }
  151. }