TestHandFactory.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using Leap.Unity;
  4. using UnityEngine;
  5. namespace Leap
  6. {
  7. public class TestHandFactory
  8. {
  9. public static Frame MakeTestFrame(int frameId, bool leftHandIncluded, bool rightHandIncluded)
  10. {
  11. Frame frame = new Frame((long)frameId, 0L, 120f, default(InteractionBox), new List<Hand>());
  12. if (leftHandIncluded)
  13. {
  14. frame.Hands.Add(TestHandFactory.MakeTestHand(frameId, 10, true));
  15. }
  16. if (rightHandIncluded)
  17. {
  18. frame.Hands.Add(TestHandFactory.MakeTestHand(frameId, 20, false));
  19. }
  20. return frame;
  21. }
  22. public static Hand MakeTestHand(int frameId, int handId, bool isLeft)
  23. {
  24. List<Finger> list = new List<Finger>(5);
  25. list.Add(TestHandFactory.MakeThumb(frameId, handId, isLeft));
  26. list.Add(TestHandFactory.MakeIndexFinger(frameId, handId, isLeft));
  27. list.Add(TestHandFactory.MakeMiddleFinger(frameId, handId, isLeft));
  28. list.Add(TestHandFactory.MakeRingFinger(frameId, handId, isLeft));
  29. list.Add(TestHandFactory.MakePinky(frameId, handId, isLeft));
  30. Vector vector = new Vector(-7.05809927f, 4f, 50f);
  31. Vector vector2 = vector + 250f * Vector.Backward;
  32. Arm arm = new Arm(vector2, vector, (vector2 + vector) / 2f, Vector.Forward, 250f, 41f, LeapQuaternion.Identity);
  33. Hand hand = new Hand((long)frameId, handId, 1f, 0f, 0f, 0f, 0f, 85f, isLeft, 0f, arm, list, new Vector(0f, 0f, 0f), new Vector(0f, 0f, 0f), new Vector(0f, 0f, 0f), Vector.Down, Vector.Forward, new Vector(-4.36385775f, 6.5f, 31.0111351f));
  34. LeapTransform identity = LeapTransform.Identity;
  35. identity.rotation = TestHandFactory.AngleAxis(3.14159274f, Vector.Forward);
  36. if (isLeft)
  37. {
  38. identity.translation = new Vector(80f, 120f, 0f);
  39. }
  40. else
  41. {
  42. identity.translation = new Vector(-80f, 120f, 0f);
  43. identity.MirrorX();
  44. }
  45. return hand.TransformedCopy(identity);
  46. }
  47. private static LeapQuaternion AngleAxis(float angle, Vector axis)
  48. {
  49. if (!axis.MagnitudeSquared.NearlyEquals(1f, 1.1920929E-07f))
  50. {
  51. throw new ArgumentException("Axis must be a unit vector.");
  52. }
  53. float num = Mathf.Sin(angle / 2f);
  54. LeapQuaternion leapQuaternion = new LeapQuaternion(num * axis.x, num * axis.y, num * axis.z, Mathf.Cos(angle / 2f));
  55. return leapQuaternion.Normalized;
  56. }
  57. private static LeapQuaternion RotationBetween(Vector fromDirection, Vector toDirection)
  58. {
  59. float num = Mathf.Sqrt(2f + 2f * fromDirection.Dot(toDirection));
  60. Vector vector = 1f / num * fromDirection.Cross(toDirection);
  61. return new LeapQuaternion(vector.x, vector.y, vector.z, 0.5f * num);
  62. }
  63. private static Finger MakeThumb(int frameId, int handId, bool isLeft)
  64. {
  65. Vector position = new Vector(19.33826f, -6f, 53.1684837f);
  66. Vector forward = new Vector(0.6363291f, -0.5f, -0.8997871f);
  67. Vector up = new Vector(0.804793954f, 0.447213918f, 0.390264541f);
  68. float[] jointLengths = new float[]
  69. {
  70. 0f,
  71. 46.22f,
  72. 31.57f,
  73. 21.67f
  74. };
  75. return TestHandFactory.MakeFinger(Finger.FingerType.TYPE_THUMB, position, forward, up, jointLengths, frameId, handId, handId, isLeft);
  76. }
  77. private static Finger MakeIndexFinger(int frameId, int handId, bool isLeft)
  78. {
  79. Vector position = new Vector(23.1812859f, 2f, -23.1493454f);
  80. Vector forward = new Vector(0.16604431f, -0.148340449f, -0.974897146f);
  81. Vector up = new Vector(0.0249066465f, 0.988936365f, -0.146234572f);
  82. float[] jointLengths = new float[]
  83. {
  84. 68.12f,
  85. 39.78f,
  86. 22.38f,
  87. 15.82f
  88. };
  89. return TestHandFactory.MakeFinger(Finger.FingerType.TYPE_INDEX, position, forward, up, jointLengths, frameId, handId, handId + 1, isLeft);
  90. }
  91. private static Finger MakeMiddleFinger(int frameId, int handId, bool isLeft)
  92. {
  93. Vector position = new Vector(2.78877831f, 4f, -23.2521057f);
  94. Vector forward = new Vector(0.0295207854f, -0.148340449f, -0.988495648f);
  95. Vector up = new Vector(-0.145765275f, 0.977715969f, -0.151075974f);
  96. float[] jointLengths = new float[]
  97. {
  98. 64.6f,
  99. 44.63f,
  100. 26.33f,
  101. 17.4f
  102. };
  103. return TestHandFactory.MakeFinger(Finger.FingerType.TYPE_MIDDLE, position, forward, up, jointLengths, frameId, handId, handId + 2, isLeft);
  104. }
  105. private static Finger MakeRingFinger(int frameId, int handId, bool isLeft)
  106. {
  107. Vector position = new Vector(-17.4471684f, 4f, -17.2791443f);
  108. Vector forward = new Vector(-0.121317938f, -0.148340344f, -0.9814668f);
  109. Vector up = new Vector(-0.216910467f, 0.968834937f, -0.1196191f);
  110. float[] jointLengths = new float[]
  111. {
  112. 58f,
  113. 41.37f,
  114. 25.65f,
  115. 17.3f
  116. };
  117. return TestHandFactory.MakeFinger(Finger.FingerType.TYPE_RING, position, forward, up, jointLengths, frameId, handId, handId + 3, isLeft);
  118. }
  119. private static Finger MakePinky(int frameId, int handId, bool isLeft)
  120. {
  121. Vector position = new Vector(-35.33744f, 0f, -9.728714f);
  122. Vector forward = new Vector(-0.259328932f, -0.105851226f, -0.959970832f);
  123. Vector up = new Vector(-0.353350222f, 0.9354595f, -0.007693566f);
  124. float[] jointLengths = new float[]
  125. {
  126. 53.69f,
  127. 32.74f,
  128. 18.11f,
  129. 15.96f
  130. };
  131. return TestHandFactory.MakeFinger(Finger.FingerType.TYPE_PINKY, position, forward, up, jointLengths, frameId, handId, handId + 4, isLeft);
  132. }
  133. private static Finger MakeFinger(Finger.FingerType name, Vector position, Vector forward, Vector up, float[] jointLengths, int frameId, int handId, int fingerId, bool isLeft)
  134. {
  135. forward = forward.Normalized;
  136. up = up.Normalized;
  137. Bone[] array = new Bone[5];
  138. float num = -jointLengths[0];
  139. Bone bone = TestHandFactory.MakeBone(Bone.BoneType.TYPE_METACARPAL, position + forward * num, jointLengths[0], 8f, forward, up, isLeft);
  140. num += jointLengths[0];
  141. array[0] = bone;
  142. Bone bone2 = TestHandFactory.MakeBone(Bone.BoneType.TYPE_PROXIMAL, position + forward * num, jointLengths[1], 8f, forward, up, isLeft);
  143. num += jointLengths[1];
  144. array[1] = bone2;
  145. Bone bone3 = TestHandFactory.MakeBone(Bone.BoneType.TYPE_INTERMEDIATE, position + forward * num, jointLengths[2], 8f, forward, up, isLeft);
  146. num += jointLengths[2];
  147. array[2] = bone3;
  148. Bone bone4 = TestHandFactory.MakeBone(Bone.BoneType.TYPE_DISTAL, position + forward * num, jointLengths[3], 8f, forward, up, isLeft);
  149. array[3] = bone4;
  150. return new Finger((long)frameId, handId, fingerId, 0f, position, new Vector(0f, 0f, 0f), forward, position, 8f, jointLengths[1] + jointLengths[2] + jointLengths[3], true, name, array[0], array[1], array[2], array[3]);
  151. }
  152. private static Bone MakeBone(Bone.BoneType name, Vector proximalPosition, float length, float width, Vector direction, Vector up, bool isLeft)
  153. {
  154. LeapQuaternion rotation = Quaternion.LookRotation(-direction.ToVector3(), up.ToVector3()).ToLeapQuaternion();
  155. return new Bone(proximalPosition, proximalPosition + direction * length, Vector.Lerp(proximalPosition, proximalPosition + direction * length, 0.5f), direction, length, width, name, rotation);
  156. }
  157. }
  158. }