Tanoda
FingerModel.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System.Collections;
11using Leap;
12using Leap.Unity;
13
14namespace Leap.Unity{
26 public abstract class FingerModel : MonoBehaviour {
27
29 public const int NUM_BONES = 4;
30
32 public const int NUM_JOINTS = 3;
33
35
36 // Unity references
38 public Transform[] bones = new Transform[NUM_BONES];
40 public Transform[] joints = new Transform[NUM_BONES - 1];
41
42 // Leap references
44 protected Hand hand_;
46 protected Finger finger_;
47
53 public void SetLeapHand(Hand hand) {
54 hand_ = hand;
55 if (hand_ != null) {
56 finger_ = hand.Fingers[(int)fingerType];
57 }
58 }
59
61 public Hand GetLeapHand() { return hand_; }
63 public Finger GetLeapFinger() { return finger_; }
64
69 public virtual void InitFinger() {
71 }
72
80 public abstract void UpdateFinger();
81
83 public Vector3 GetTipPosition() {
84 if (finger_ != null) {
85 Vector3 local_tip = finger_.Bone ((Bone.BoneType.TYPE_DISTAL)).NextJoint.ToVector3();
86 return local_tip;
87 }
88 if (bones [NUM_BONES - 1] && joints [NUM_JOINTS - 2]) {
89 return 2f*bones [NUM_BONES - 1].position - joints [NUM_JOINTS - 2].position;
90 }
91 return Vector3.zero;
92 }
93
95 public Vector3 GetJointPosition(int joint) {
96 if (joint >= NUM_BONES) {
97 return GetTipPosition ();
98 }
99 if (finger_ != null) {
100 Vector3 local_position = finger_.Bone((Bone.BoneType)(joint)).PrevJoint.ToVector3();
101 return local_position;
102 }
103 if (joints [joint]) {
104 return joints[joint].position;
105 }
106 return Vector3.zero;
107 }
108
110 public Ray GetRay() {
111 Ray ray = new Ray(GetTipPosition(), GetBoneDirection(NUM_BONES - 1));
112 return ray;
113 }
114
116 public Vector3 GetBoneCenter(int bone_type) {
117 if (finger_ != null) {
118 Bone bone = finger_.Bone ((Bone.BoneType)(bone_type));
119 return bone.Center.ToVector3();
120 }
121 if (bones [bone_type]) {
122 return bones[bone_type].position;
123 }
124 return Vector3.zero;
125 }
126
128 public Vector3 GetBoneDirection(int bone_type) {
129 if (finger_ != null) {
130 Vector3 direction = GetJointPosition (bone_type + 1) - GetJointPosition (bone_type);
131 return direction.normalized;
132 }
133 if (bones[bone_type]) {
134 return bones[bone_type].forward;
135 }
136 return Vector3.forward;
137 }
138
140 public Quaternion GetBoneRotation(int bone_type) {
141 if (finger_ != null) {
142 Quaternion local_rotation = finger_.Bone ((Bone.BoneType)(bone_type)).Rotation.ToQuaternion();
143 return local_rotation;
144 }
145 if (bones[bone_type]) {
146 return bones[bone_type].rotation;
147 }
148 return Quaternion.identity;
149 }
150
152 public float GetBoneLength(int bone_type) {
153 return finger_.Bone((Bone.BoneType)(bone_type)).Length;
154 }
155
157 public float GetBoneWidth(int bone_type) {
158 return finger_.Bone((Bone.BoneType)(bone_type)).Width;
159 }
160
166 public float GetFingerJointStretchMecanim(int joint_type) {
167 // The successive actions of local rotations on a vector yield the global rotation,
168 // so the inverse of the parent rotation appears on the left.
169 Quaternion jointRotation = Quaternion.identity;
170 if (finger_ != null) {
171 jointRotation = Quaternion.Inverse(finger_.Bone((Bone.BoneType)(joint_type)).Rotation.ToQuaternion())
172 * finger_.Bone ((Bone.BoneType)(joint_type + 1)).Rotation.ToQuaternion();
173 } else if (bones [joint_type] && bones [joint_type + 1]) {
174 jointRotation = Quaternion.Inverse (GetBoneRotation (joint_type)) * GetBoneRotation (joint_type + 1);
175 }
176 // Stretch is a rotation around the X axis of the base bone
177 // Positive stretch opens joints
178 float stretchAngle = -jointRotation.eulerAngles.x;
179 if (stretchAngle <= -180f) {
180 stretchAngle += 360f;
181 }
182 // NOTE: eulerAngles range is [0, 360) so stretchAngle > +180f will not occur.
183 return stretchAngle;
184 }
185
193 // The successive actions of local rotations on a vector yield the global rotation,
194 // so the inverse of the parent rotation appears on the left.
195 Quaternion jointRotation = Quaternion.identity;
196 if (finger_ != null) {
197 jointRotation = Quaternion.Inverse (finger_.Bone ((Bone.BoneType)(0)).Rotation.ToQuaternion())
198 * finger_.Bone ((Bone.BoneType)(1)).Rotation.ToQuaternion();
199 } else if (bones [0] && bones [1]) {
200 jointRotation = Quaternion.Inverse (GetBoneRotation (0)) * GetBoneRotation (1);
201 }
202 // Spread is a rotation around the Y axis of the base bone when joint_type = 0
203 float spreadAngle = 0f;
205 if (finger_ != null) {
207 }
208
209 if (fType == Finger.FingerType.TYPE_INDEX ||
210 fType == Finger.FingerType.TYPE_MIDDLE) {
211 spreadAngle = jointRotation.eulerAngles.y;
212 if (spreadAngle > 180f) {
213 spreadAngle -= 360f;
214 }
215 // NOTE: eulerAngles range is [0, 360) so spreadAngle <= -180f will not occur.
216 }
217 if (fType == Finger.FingerType.TYPE_THUMB ||
218 fType == Finger.FingerType.TYPE_RING ||
219 fType == Finger.FingerType.TYPE_PINKY) {
220 spreadAngle = -jointRotation.eulerAngles.y;
221 if (spreadAngle <= -180f) {
222 spreadAngle += 360f;
223 }
224 // NOTE: eulerAngles range is [0, 360) so spreadAngle > +180f will not occur.
225 }
226 return spreadAngle;
227 }
228 }
229}
The Bone class represents a tracked bone.
Definition: Bone.cs:26
LeapQuaternion Rotation
The orientation of this Bone as a Quaternion.
Definition: Bone.cs:126
float Width
The average width of the flesh around the bone.
Definition: Bone.cs:114
BoneType
Enumerates the type of bones.
Definition: Bone.cs:168
Vector Center
The midpoint of the bone.
Definition: Bone.cs:96
Vector PrevJoint
The base of the bone, closest to the wrist. In anatomical terms, this is the proximal end of the bone...
Definition: Bone.cs:83
float Length
The estimated length of the bone.
Definition: Bone.cs:108
Vector NextJoint
The end of the bone, closest to the finger tip. In anatomical terms, this is the distal end of the bo...
Definition: Bone.cs:90
The Finger class represents a tracked finger.
Definition: Finger.cs:20
Bone Bone(Bone.BoneType boneIx)
The bone at a given bone index on this finger.
Definition: Finger.cs:79
Finger.FingerType Type
The type of this finger.
Definition: Finger.cs:95
FingerType
Enumerates the names of the fingers.
Definition: Finger.cs:167
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
List< Finger > Fingers
The list of Finger objects detected in this frame that are attached to this hand, given in order from...
Definition: Hand.cs:159
float GetFingerJointStretchMecanim(int joint_type)
Definition: FingerModel.cs:166
Vector3 GetTipPosition()
Definition: FingerModel.cs:83
Quaternion GetBoneRotation(int bone_type)
Definition: FingerModel.cs:140
Finger.FingerType fingerType
Definition: FingerModel.cs:34
virtual void InitFinger()
Definition: FingerModel.cs:69
void SetLeapHand(Hand hand)
Definition: FingerModel.cs:53
Vector3 GetBoneCenter(int bone_type)
Definition: FingerModel.cs:116
Vector3 GetBoneDirection(int bone_type)
Definition: FingerModel.cs:128
float GetFingerJointSpreadMecanim()
Definition: FingerModel.cs:192
abstract void UpdateFinger()
Vector3 GetJointPosition(int joint)
Definition: FingerModel.cs:95
float GetBoneLength(int bone_type)
Definition: FingerModel.cs:152
float GetBoneWidth(int bone_type)
Definition: FingerModel.cs:157