BodyComparer.cs 3.9 KB

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