BodyComparer.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  49. {
  50. // Skip these joints
  51. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  52. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  53. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  54. continue;
  55. JointsData jd = jointsDataDemo[i];
  56. Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  57. Vector3 posJointBody = body.GetChild((int)jt).position;
  58. // Save positions demo and real body
  59. oneBodyDemo.Add(posJointDemo);
  60. oneBodyReal.Add(posJointBody);
  61. float distance = (posJointBody - posJointDemo).magnitude;
  62. if (distance > 0.15f)
  63. {
  64. if (mc.feedback == ModeController.Feedback.ColorFeedback)
  65. {
  66. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.red;
  67. } else if (mc.feedback == ModeController.Feedback.HapticFeedback)
  68. {
  69. // TODO: Haptic feedback
  70. if (jt == Kinect.JointType.WristLeft)
  71. {
  72. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.1f, 10, 1);
  73. }
  74. else if (jt == Kinect.JointType.WristRight)
  75. {
  76. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.1f, 10, 1);
  77. }
  78. else if (jt == Kinect.JointType.AnkleLeft)
  79. {
  80. haptics.Vibrate(15, 1, 100);
  81. }
  82. else if (jt == Kinect.JointType.AnkleRight)
  83. {
  84. haptics.Vibrate(16, 1, 100);
  85. }
  86. }
  87. }
  88. else
  89. {
  90. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  91. }
  92. }
  93. // Save it to csv later
  94. posJointsDemos.Add(oneBodyDemo);
  95. posJointsBodys.Add(oneBodyReal);
  96. csvTimes.Add(viveInput.csvTime);
  97. // If pressed stop, end immediately and save last to csv
  98. if (!isComparing)
  99. {
  100. for (int j = i + 1; j < jointsDataDemo.Count; j++)
  101. {
  102. List<Vector3> oneBodyDemo2 = new List<Vector3>();
  103. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  104. {
  105. // Skip these joints
  106. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  107. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  108. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  109. continue;
  110. JointsData jd = jointsDataDemo[j];
  111. Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  112. // Save positions demo and real body
  113. oneBodyDemo2.Add(posJointDemo);
  114. }
  115. // Save it to csv later
  116. posJointsDemos.Add(oneBodyDemo2);
  117. posJointsBodys.Add(oneBodyReal);
  118. csvTimes.Add(viveInput.csvTime);
  119. }
  120. // Make all joints white
  121. Transform bodyEnd2 = bsv.body.transform;
  122. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  123. {
  124. // Skip these joints
  125. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  126. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  127. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  128. continue;
  129. bodyEnd2.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  130. }
  131. yield break;
  132. }
  133. float waitTime = 0;
  134. if (isComparing && i < recordingTimesDemo.Count - 1)
  135. {
  136. waitTime = recordingTimesDemo[i + 1] - recordingTimesDemo[i];
  137. }
  138. yield return new WaitForSeconds(waitTime);
  139. }
  140. isComparing = false;
  141. // Make all joints white
  142. Transform bodyEnd = bsv.body.transform;
  143. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  144. {
  145. // Skip these joints
  146. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  147. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  148. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  149. continue;
  150. bodyEnd.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  151. }
  152. }
  153. public void WriteCSV()
  154. {
  155. Debug.Log("Save to CSV");
  156. CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes, mc);
  157. }
  158. }