BodyComparer.cs 7.2 KB

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