Tanoda
VectorHandCurves.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;
10
11namespace Leap.Unity.Recording {
12 using Leap.Unity.Encoding;
13
15 [System.Serializable]
16 public struct VectorHandCurves {
17
18 [SerializeField]
19 private AnimationCurve _backingIsTrackedCurve;
21 public AnimationCurve isTrackedCurve {
22 get {
23 if (_backingIsTrackedCurve == null) {
24 _backingIsTrackedCurve = new AnimationCurve();
25 }
26 return _backingIsTrackedCurve;
27 }
28 set {
29 _backingIsTrackedCurve = value;
30 }
31 }
32
35
38
39 [SerializeField]
40 private Vector3Curves[] _backingJointPositionCurves;
43 get {
44 if (_backingJointPositionCurves == null) {
45 _backingJointPositionCurves = new Vector3Curves[VectorHand.NUM_JOINT_POSITIONS];
46 for (int i = 0; i < _backingJointPositionCurves.Length; i++) {
47 _backingJointPositionCurves[i] = new Vector3Curves();
48 }
49 }
50 return _backingJointPositionCurves;
51 }
52 set {
53 _backingJointPositionCurves = value;
54 }
55 }
56
60 private Hand _lastHand;
61
66 public void AddKeyframes(float time, Hand hand) {
67 bool isTracked = hand != null;
68
69 isTrackedCurve.AddBooleanKey(time, isTracked);
70
71 if (isTracked) {
72 VectorHand vectorHand = Pool<VectorHand>.Spawn();
73 try {
74 vectorHand.Encode(hand);
75
76 palmPosCurves.AddKeyframes(time, vectorHand.palmPos);
77 palmRotCurves.AddKeyframes(time, vectorHand.palmRot);
78
79 for (int i = 0; i < VectorHand.NUM_JOINT_POSITIONS; i++) {
80 jointPositionCurves[i].AddKeyframes(time, vectorHand.jointPositions[i]);
81 }
82 }
83 finally {
84 Pool<VectorHand>.Recycle(vectorHand);
85 }
86 }
87 }
88
89#if UNITY_EDITOR
93 public void Compress() {
94 AnimationCurveUtil.Compress(isTrackedCurve, maxDelta: 0.001f);
95
96 palmPosCurves.Compress();
97 palmRotCurves.Compress();
98
99 for (int i = 0; i < jointPositionCurves.Length; i++) {
100 jointPositionCurves[i].Compress(maxDistanceError: 0.001f);
101 }
102 }
103#endif
104
111 public bool Sample(float time, Hand intoHand, bool isLeft) {
112 bool isTracked = isTrackedCurve.Evaluate(time) > 0.5f;
113
114 VectorHand vectorHand = Pool<VectorHand>.Spawn();
115 try {
116 vectorHand.isLeft = isLeft;
117 vectorHand.palmPos = palmPosCurves.Evaluate(time);
118 vectorHand.palmRot = palmRotCurves.Evaluate(time);
119
120 for (int i = 0; i < VectorHand.NUM_JOINT_POSITIONS; i++) {
121 vectorHand.jointPositions[i] = jointPositionCurves[i].Evaluate(time);
122 }
123
124 vectorHand.Decode(intoHand);
125
126 // Fill temporal data if we have a hand from the previous sampling.
127 if (_lastHand != null) {
128 intoHand.FillTemporalData(_lastHand, Time.deltaTime);
129
130 _lastHand.CopyFrom(intoHand);
131 }
132 else {
133 _lastHand = new Hand();
134 }
135 }
136 finally {
137 Pool<VectorHand>.Recycle(vectorHand);
138 }
139
140 return isTracked;
141 }
142
143 }
144
145}
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
A Vector-based encoding of a Leap Hand.
Definition: VectorHand.cs:36
void Encode(Hand fromHand)
Definition: VectorHand.cs:88
void Decode(Hand intoHand)
Definition: VectorHand.cs:109
AnimationCurve data for an animated Quaternion.
void AddKeyframes(float time, Quaternion value)
AnimationCurve data for an animated Vector3.
void AddKeyframes(float time, Vector3 value)
AnimationCurve data for an animated VectorHand representation.
Vector3Curves palmPosCurves
AnimationCurve data for palm position.
void AddKeyframes(float time, Hand hand)
Adds keyframe data into these VectorHandCurves at the specified time using the provided hand data.
AnimationCurve isTrackedCurve
AnimationCurve for whether the hand is tracked.
Vector3Curves[] jointPositionCurves
AnimationCurve data for the hand's fingers.
bool Sample(float time, Hand intoHand, bool isLeft)
Samples hand curve data into the provided hand object at the specified time.
QuaternionCurves palmRotCurves
AnimationCurve data for palm rotation.