BodyComparer.cs 7.7 KB

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