Tanoda
CopyFromLeapCExtensions.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
9namespace LeapInternal {
10 using Leap;
11
12 public static class CopyFromLeapCExtensions {
13
19 public static Frame CopyFrom(this Frame frame, ref LEAP_TRACKING_EVENT trackingMsg) {
20 frame.Id = (long)trackingMsg.info.frame_id;
21 frame.Timestamp = (long)trackingMsg.info.timestamp;
22 frame.CurrentFramesPerSecond = trackingMsg.framerate;
23
24 frame.ResizeHandList((int)trackingMsg.nHands);
25
26 for (int i = frame.Hands.Count; i-- != 0;) {
27 LEAP_HAND hand;
28 StructMarshal<LEAP_HAND>.ArrayElementToStruct(trackingMsg.pHands, i, out hand);
29 frame.Hands[i].CopyFrom(ref hand, frame.Id);
30 }
31
32 return frame;
33 }
34
41 public static Hand CopyFrom(this Hand hand, ref LEAP_HAND leapHand, long frameId) {
42 hand.FrameId = frameId;
43 hand.Id = (int)leapHand.id;
44
45 hand.Arm.CopyFrom(leapHand.arm, Bone.BoneType.TYPE_INVALID);
46
47 hand.Confidence = leapHand.confidence;
48 hand.GrabStrength = leapHand.grab_strength;
49 hand.GrabAngle = leapHand.grab_angle;
50 hand.PinchStrength = leapHand.pinch_strength;
51 hand.PinchDistance = leapHand.pinch_distance;
52 hand.PalmWidth = leapHand.palm.width;
53 hand.IsLeft = leapHand.type == eLeapHandType.eLeapHandType_Left;
54 hand.TimeVisible = (float)(leapHand.visible_time * 1e-6);
55 hand.PalmPosition = leapHand.palm.position.ToLeapVector();
56 hand.StabilizedPalmPosition = leapHand.palm.stabilized_position.ToLeapVector();
57 hand.PalmVelocity = leapHand.palm.velocity.ToLeapVector();
58 hand.PalmNormal = leapHand.palm.normal.ToLeapVector();
59 hand.Rotation = leapHand.palm.orientation.ToLeapQuaternion();
60 hand.Direction = leapHand.palm.direction.ToLeapVector();
61 hand.WristPosition = hand.Arm.NextJoint;
62
63 hand.Fingers[0].CopyFrom(leapHand.thumb, Leap.Finger.FingerType.TYPE_THUMB, hand.Id, hand.TimeVisible);
64 hand.Fingers[1].CopyFrom(leapHand.index, Leap.Finger.FingerType.TYPE_INDEX, hand.Id, hand.TimeVisible);
65 hand.Fingers[2].CopyFrom(leapHand.middle, Leap.Finger.FingerType.TYPE_MIDDLE, hand.Id, hand.TimeVisible);
66 hand.Fingers[3].CopyFrom(leapHand.ring, Leap.Finger.FingerType.TYPE_RING, hand.Id, hand.TimeVisible);
67 hand.Fingers[4].CopyFrom(leapHand.pinky, Leap.Finger.FingerType.TYPE_PINKY, hand.Id, hand.TimeVisible);
68
69 return hand;
70 }
71
81 public static Finger CopyFrom(this Finger finger, LEAP_DIGIT leapBone, Finger.FingerType type, int handId, float timeVisible) {
82 finger.Id = (handId * 10) + leapBone.finger_id;
83 finger.HandId = handId;
84 finger.TimeVisible = timeVisible;
85
86 Bone metacarpal = finger.bones[0];
87 Bone proximal = finger.bones[1];
88 Bone intermediate = finger.bones[2];
89 Bone distal = finger.bones[3];
90
91 metacarpal.CopyFrom(leapBone.metacarpal, Leap.Bone.BoneType.TYPE_METACARPAL);
92 proximal.CopyFrom(leapBone.proximal, Leap.Bone.BoneType.TYPE_PROXIMAL);
93 intermediate.CopyFrom(leapBone.intermediate, Leap.Bone.BoneType.TYPE_INTERMEDIATE);
94 distal.CopyFrom(leapBone.distal, Leap.Bone.BoneType.TYPE_DISTAL);
95
96 finger.TipPosition = distal.NextJoint;
97 finger.Direction = intermediate.Direction;
98 finger.Width = intermediate.Width;
99 finger.Length = (leapBone.finger_id == 0 ? 0.0f : 0.5f * proximal.Length) + intermediate.Length + 0.77f * distal.Length; //The values 0.5 for proximal and 0.77 for distal are used in platform code for this calculation
100 finger.IsExtended = leapBone.is_extended != 0;
101 finger.Type = type;
102
103 return finger;
104 }
105
112 public static Bone CopyFrom(this Bone bone, LEAP_BONE leapBone, Bone.BoneType type) {
113 bone.Type = type;
114 bone.PrevJoint = leapBone.prev_joint.ToLeapVector();
115 bone.NextJoint = leapBone.next_joint.ToLeapVector();
116 bone.Direction = (bone.NextJoint - bone.PrevJoint);
117 bone.Length = bone.Direction.Magnitude;
118
119 if (bone.Length < float.Epsilon) {
120 bone.Direction = Vector.Zero;
121 } else {
122 bone.Direction /= bone.Length;
123 }
124
125 bone.Center = (bone.PrevJoint + bone.NextJoint) / 2.0f;
126 bone.Rotation = leapBone.rotation.ToLeapQuaternion();
127 bone.Width = leapBone.width;
128
129 return bone;
130 }
131 }
132}
The Bone class represents a tracked bone.
Definition: Bone.cs:26
BoneType
Enumerates the type of bones.
Definition: Bone.cs:168
The Finger class represents a tracked finger.
Definition: Finger.cs:20
FingerType
Enumerates the names of the fingers.
Definition: Finger.cs:167