[4de1c7]: / app / resources / LeapSDK / v3_python27 / src / CopyFromLeapCExtensions.cs

Download this file

144 lines (124 with data), 6.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/******************************************************************************\
* Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved. *
* Leap Motion proprietary and confidential. Not for distribution. *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
\******************************************************************************/
namespace LeapInternal
{
using Leap;
public static class CopyFromLeapCExtensions
{
/**
* Copies the data from an internal tracking message into a frame.
*
* @param trackingMsg The internal tracking message with the data to be copied into this frame.
*/
public static Frame CopyFrom(this Frame frame, ref LEAP_TRACKING_EVENT trackingMsg)
{
frame.Id = (long)trackingMsg.info.frame_id;
frame.Timestamp = (long)trackingMsg.info.timestamp;
frame.CurrentFramesPerSecond = trackingMsg.framerate;
frame.InteractionBox = new InteractionBox(trackingMsg.interaction_box_center.ToLeapVector(),
trackingMsg.interaction_box_size.ToLeapVector());
frame.ResizeHandList((int)trackingMsg.nHands);
for (int i = frame.Hands.Count; i-- != 0; )
{
LEAP_HAND hand;
StructMarshal<LEAP_HAND>.ArrayElementToStruct(trackingMsg.pHands, i, out hand);
frame.Hands[i].CopyFrom(ref hand, frame.Id);
}
return frame;
}
/**
* Copies the data from an internal hand definition into a hand.
*
* @param leapHand The internal hand definition to be copied into this hand.
* @param frameId The frame id of the frame this hand belongs to.
*/
public static Hand CopyFrom(this Hand hand, ref LEAP_HAND leapHand, long frameId)
{
hand.FrameId = frameId;
hand.Id = (int)leapHand.id;
hand.Arm.CopyFrom(leapHand.arm, Bone.BoneType.TYPE_INVALID);
hand.Confidence = leapHand.confidence;
hand.GrabStrength = leapHand.grab_strength;
hand.GrabAngle = leapHand.grab_angle;
hand.PinchStrength = leapHand.pinch_strength;
hand.PinchDistance = leapHand.pinch_distance;
hand.PalmWidth = leapHand.palm.width;
hand.IsLeft = leapHand.type == eLeapHandType.eLeapHandType_Left;
hand.TimeVisible = leapHand.visible_time;
hand.PalmPosition = leapHand.palm.position.ToLeapVector();
hand.StabilizedPalmPosition = leapHand.palm.stabilized_position.ToLeapVector();
hand.PalmVelocity = leapHand.palm.velocity.ToLeapVector();
hand.PalmNormal = leapHand.palm.normal.ToLeapVector();
hand.Rotation = leapHand.palm.orientation.ToLeapQuaternion();
hand.Direction = leapHand.palm.direction.ToLeapVector();
hand.WristPosition = hand.Arm.NextJoint;
hand.Fingers[0].CopyFrom(leapHand.thumb, Leap.Finger.FingerType.TYPE_THUMB, hand.Id, hand.TimeVisible);
hand.Fingers[1].CopyFrom(leapHand.index, Leap.Finger.FingerType.TYPE_INDEX, hand.Id, hand.TimeVisible);
hand.Fingers[2].CopyFrom(leapHand.middle, Leap.Finger.FingerType.TYPE_MIDDLE, hand.Id, hand.TimeVisible);
hand.Fingers[3].CopyFrom(leapHand.ring, Leap.Finger.FingerType.TYPE_RING, hand.Id, hand.TimeVisible);
hand.Fingers[4].CopyFrom(leapHand.pinky, Leap.Finger.FingerType.TYPE_PINKY, hand.Id, hand.TimeVisible);
return hand;
}
/**
* Copies the data from an internal finger definition into a finger.
*
* @param leapBone The internal finger definition to be copied into this finger.
* @param type The finger type of this finger.
* @param frameId The frame id of the frame this finger belongs to.
* @param handId The hand id of the hand this finger belongs to.
* @param timeVisible The time in seconds that this finger has been visible.
*/
public static Finger CopyFrom(this Finger finger, LEAP_DIGIT leapBone, Finger.FingerType type, int handId, float timeVisible)
{
finger.Id = (handId * 10) + leapBone.finger_id;
finger.HandId = handId;
finger.TimeVisible = timeVisible;
Bone metacarpal = finger.bones[0];
Bone proximal = finger.bones[1];
Bone intermediate = finger.bones[2];
Bone distal = finger.bones[3];
metacarpal.CopyFrom(leapBone.metacarpal, Leap.Bone.BoneType.TYPE_METACARPAL);
proximal.CopyFrom(leapBone.proximal, Leap.Bone.BoneType.TYPE_PROXIMAL);
intermediate.CopyFrom(leapBone.intermediate, Leap.Bone.BoneType.TYPE_INTERMEDIATE);
distal.CopyFrom(leapBone.distal, Leap.Bone.BoneType.TYPE_DISTAL);
finger.TipPosition = distal.NextJoint;
finger.TipVelocity = leapBone.tip_velocity.ToLeapVector();
finger.Direction = intermediate.Direction;
finger.StabilizedTipPosition = leapBone.stabilized_tip_position.ToLeapVector();
finger.Width = intermediate.Width;
finger.Length = proximal.Length + intermediate.Length + (distal.Length * 0.77f); //0.77 is used in platform code for this calculation
finger.IsExtended = leapBone.is_extended != 0;
finger.Type = type;
return finger;
}
/**
* Copies the data from an internal bone definition into a bone.
*
* @param leapBone The internal bone definition to be copied into this bone.
* @param type The bone type of this bone.
*/
public static Bone CopyFrom(this Bone bone, LEAP_BONE leapBone, Bone.BoneType type)
{
bone.Type = type;
bone.PrevJoint = leapBone.prev_joint.ToLeapVector();
bone.NextJoint = leapBone.next_joint.ToLeapVector();
bone.Direction = (bone.NextJoint - bone.PrevJoint);
bone.Length = bone.Direction.Magnitude;
if (bone.Length < float.Epsilon)
{
bone.Direction = Vector.Zero;
} else {
bone.Direction /= bone.Length;
}
bone.Center = (bone.PrevJoint + bone.NextJoint) / 2.0f;
bone.Rotation = leapBone.rotation.ToLeapQuaternion();
bone.Width = leapBone.width;
return bone;
}
}
}