BodyComparer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // TODO: Testing (delete later)
  17. private void Update()
  18. {
  19. if (Input.GetKeyDown(KeyCode.S))
  20. {
  21. Debug.Log("Save to CSV");
  22. CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
  23. }
  24. }
  25. public void SetDataDemo(List<JointsData> jointsDataDemo, List<float> recordingTimesDemo)
  26. {
  27. this.jointsDataDemo = jointsDataDemo;
  28. this.recordingTimesDemo = recordingTimesDemo;
  29. }
  30. public IEnumerator StartCompare()
  31. {
  32. if (bsv.body == null)
  33. yield break;
  34. for (int i = 0; i < jointsDataDemo.Count; i++)
  35. {
  36. Transform body = bsv.body.transform;
  37. List<Vector3> oneBodyDemo = new List<Vector3>();
  38. List<Vector3> oneBodyReal = new List<Vector3>();
  39. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  40. {
  41. // Skip these joints
  42. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  43. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  44. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  45. continue;
  46. JointsData jd = jointsDataDemo[i];
  47. Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  48. Vector3 posJointBody = body.GetChild((int)jt).position;
  49. // Save positions demo and real body
  50. oneBodyDemo.Add(posJointDemo);
  51. oneBodyReal.Add(posJointBody);
  52. float distance = (posJointBody - posJointDemo).magnitude;
  53. if (distance > 0.15f)
  54. {
  55. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.red;
  56. // TODO: Haptic feedback
  57. //SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  58. //SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  59. }
  60. else
  61. {
  62. body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  63. }
  64. }
  65. // Save it to csv later
  66. posJointsDemos.Add(oneBodyDemo);
  67. posJointsBodys.Add(oneBodyReal);
  68. csvTimes.Add(viveInput.csvTime);
  69. float waitTime = 0.01f;
  70. if (i < recordingTimesDemo.Count - 1)
  71. {
  72. waitTime = recordingTimesDemo[i + 1] - recordingTimesDemo[i];
  73. }
  74. yield return new WaitForSeconds(waitTime);
  75. }
  76. // Make all joints white
  77. Transform bodyEnd = bsv.body.transform;
  78. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  79. {
  80. // Skip these joints
  81. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  82. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  83. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  84. continue;
  85. bodyEnd.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
  86. }
  87. }
  88. public void WriteCSV()
  89. {
  90. CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
  91. }
  92. }