Tanoda
PoseSplineSequence.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
10using System;
11using UnityEngine;
12
13namespace Leap.Unity.Animation {
14
15 [Serializable]
16 public struct PoseSplineSequence : IIndexable<HermitePoseSpline>,
17 ISpline<Pose, Movement>,
18 ISpline<Vector3, Vector3> {
20 public bool allowExtrapolation;
21
23 bool allowExtrapolation = false) {
24 this.splines = splines;
25 this.allowExtrapolation = allowExtrapolation;
26 }
27
28 public HermitePoseSpline this[int idx] {
29 get { return splines[idx]; }
30 }
31
32 public int Count { get { return splines.Length; } }
33
34 public Pose PoseAt(float t) {
35 Pose pose;
36 Movement unusedMovement;
37 PoseAndMovementAt(t, out pose, out unusedMovement);
38 return pose;
39 }
40
41 public Movement MovementAt(float t) {
42 Pose unusePose;
43 Movement movement;
44 PoseAndMovementAt(t, out unusePose, out movement);
45 return movement;
46 }
47
48 public void PoseAndMovementAt(float t, out Pose pose, out Movement movement) {
49 var minT = splines[0].minT;
50 var maxT = splines[splines.Length - 1].maxT;
51
52 pose = Pose.identity;
53 movement = Movement.identity;
54
55 var dt = 0f;
56 Pose poseOrigin; Movement extrapMovement;
57 if (t < minT) {
59 splines[0].PoseAndMovementAt(minT, out poseOrigin, out extrapMovement);
60 dt = t - minT;
61 pose = poseOrigin.Integrated(extrapMovement, dt);
62 movement = extrapMovement;
63 return;
64 }
65 else {
66 t = minT;
67 }
68 }
69 else if (t > maxT) {
71 splines[splines.Length - 1].PoseAndMovementAt(maxT, out poseOrigin, out extrapMovement);
72 dt = t - maxT;
73 pose = poseOrigin.Integrated(extrapMovement, dt);
74 movement = extrapMovement;
75 return;
76 }
77 else {
78 t = maxT;
79 }
80 }
81
82 foreach (var spline in splines) {
83 if (t >= spline.minT && t <= spline.maxT) {
84 pose = spline.PoseAt(t);
85 movement = spline.MovementAt(t);
86 return;
87 }
88 }
89
90 Debug.LogError("PoseSplineSequence couldn't evaluate T: " + t);
91 }
92
93 #region ISpline<Pose, Movement>
94
95 public float minT {
96 get {
97 if (splines == null || splines.Length == 0) {
98 return 0;
99 }
100 else {
101 return splines[0].minT;
102 }
103 }
104 }
105
106 public float maxT {
107 get {
108 if (splines == null || splines.Length == 0) {
109 return 0;
110 }
111 else {
112 return splines[splines.Length - 1].maxT;
113 }
114 }
115 }
116
117 public Pose ValueAt(float t) {
118 return PoseAt(t);
119 }
120
121 public Movement DerivativeAt(float t) {
122 return MovementAt(t);
123 }
124
125 public void ValueAndDerivativeAt(float t, out Pose value, out Movement deltaValuePerSec) {
126 PoseAndMovementAt(t, out value, out deltaValuePerSec);
127 }
128
129 #endregion
130
131 #region ISpline<Vector3, Vector3>
132
133 float ISpline<Vector3, Vector3>.minT { get { return minT; } }
134
135 float ISpline<Vector3, Vector3>.maxT { get { return maxT; } }
136
138 return PoseAt(t).position;
139 }
140
142 return MovementAt(t).velocity;
143 }
144
146 out Vector3 value,
147 out Vector3 deltaValuePerT) {
148 Pose pose;
149 Movement movement;
150 PoseAndMovementAt(t, out pose, out movement);
151
152 value = pose.position;
153 deltaValuePerT = movement.velocity;
154 }
155
156 #endregion
157
158 }
159
160 public static class PoseSplineSequenceExtensions {
161 public static void DrawPoseSplineSequence(this RuntimeGizmoDrawer drawer,
162 PoseSplineSequence poseSplines,
163 bool drawPoses = true,
164 bool drawSegments = true) {
165 for (int i = 0; i < poseSplines.Count; i++) {
166 drawer.DrawPoseSpline(poseSplines[i],
167 drawPoses: drawPoses,
168 drawSegments: drawSegments);
169 }
170 }
171 }
172
173}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
dXType DerivativeAt(float t)
void ValueAndDerivativeAt(float t, out XType value, out dXType deltaValuePerT)
This easy-to-implement interface represents the ability to index into a collection of elements of typ...
Definition: IIndexable.cs:21
Represents a spline for poses – positions and rotations – that travel from one position and rotation ...
void PoseAndMovementAt(float t, out Pose pose, out Movement movement)
Gets both the rotation and the first derivative of rotation at time t. The time is clamped within the...
void PoseAndMovementAt(float t, out Pose pose, out Movement movement)
void ValueAndDerivativeAt(float t, out Pose value, out Movement deltaValuePerSec)
PoseSplineSequence(HermitePoseSpline[] splines, bool allowExtrapolation=false)
float ISpline< Vector3, Vector3 >. maxT
float ISpline< Vector3, Vector3 >. minT
static readonly Movement identity
Definition: Movement.cs:26
Vector3 velocity
The linear velocity of this Movement.
Definition: Movement.cs:18
A position and rotation. You can multiply two poses; this acts like Matrix4x4 multiplication,...
Definition: Pose.cs:21
static readonly Pose identity
Definition: Pose.cs:35
Vector3 position
Definition: Pose.cs:23