BodyComparer.cs 7.5 KB

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