Tanoda
HermitePoseSpline.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 System;
11using UnityEngine;
12
13namespace Leap.Unity.Animation {
14
24 [Serializable]
25 public struct HermitePoseSpline : ISpline<Pose, Movement>,
26 ISpline<Vector3, Vector3> {
27
30
36 public HermitePoseSpline(Pose pose0, Pose pose1) {
37 pSpline = new HermiteSpline3(pose0.position, pose1.position);
39 }
40
45 public HermitePoseSpline(Pose pose0, Pose pose1, Movement move0, Movement move1) {
46 pSpline = new HermiteSpline3(pose0.position, pose1.position,
47 move0.velocity, move1.velocity);
49 move0.angularVelocity, move1.angularVelocity);
50 }
51
56 public HermitePoseSpline(Pose pose0, Pose pose1,
57 Movement move0, Movement move1,
58 float duration) {
59 pSpline = new HermiteSpline3(pose0.position, pose1.position,
60 move0.velocity, move1.velocity,
61 duration);
64 duration);
65 }
66
71 public HermitePoseSpline(float t0, float t1,
72 Pose pose0, Pose pose1,
73 Movement move0, Movement move1) {
74 pSpline = new HermiteSpline3(t0, t1,
75 pose0.position, pose1.position,
76 move0.velocity, move1.velocity);
78 pose0.rotation, pose1.rotation,
79 move0.angularVelocity, move1.angularVelocity);
80 }
81
86 public Vector3 PositionAt(float t) {
87 return pSpline.PositionAt(t);
88 }
89
94 public Quaternion RotationAt(float t) {
95 return qSpline.RotationAt(t);
96 }
97
102 public Pose PoseAt(float t) {
103 return new Pose(PositionAt(t), RotationAt(t));
104 }
105
110 public Vector3 VelocityAt(float t) {
111 return pSpline.VelocityAt(t);
112 }
113
118 public Vector3 AngularVelocityAt(float t) {
119 return qSpline.AngularVelocityAt(t);
120 }
121
122 public Movement MovementAt(float t) {
123 return new Movement(VelocityAt(t), AngularVelocityAt(t));
124 }
125
130 public void PositionAndVelAt(float t, out Vector3 position, out Vector3 velocity) {
131 pSpline.PositionAndVelAt(t, out position, out velocity);
132 }
133
139 public void RotationAndAngVelAt(float t, out Quaternion rotation,
140 out Vector3 angularVelocity) {
141 qSpline.RotationAndAngVelAt(t, out rotation, out angularVelocity);
142 }
143
144
154 public void PoseAndMovementAt(float t, out Pose pose,
155 out Movement movement) {
156 Vector3 pos, vel, angVel;
157 Quaternion rot;
158 pSpline.PositionAndVelAt(t, out pos, out vel);
159 qSpline.RotationAndAngVelAt(t, out rot, out angVel);
160 pose = new Pose(pos, rot);
161 movement = new Movement(vel, angVel);
162 }
163
164 #region ISpline<Pose, Movement>
165
166 public float minT { get { return pSpline.t0; } }
167
168 public float maxT { get { return pSpline.t1; } }
169
170 public Pose ValueAt(float t) {
171 return PoseAt(t);
172 }
173
174 public Movement DerivativeAt(float t) {
175 return MovementAt(t);
176 }
177
178 public void ValueAndDerivativeAt(float t, out Pose value, out Movement deltaValuePerSec) {
179 PoseAndMovementAt(t, out value, out deltaValuePerSec);
180 }
181
182 #endregion
183
184 #region ISpline<Vector3, Vector3>
185
186 float ISpline<Vector3, Vector3>.minT { get { return pSpline.t0; } }
187
188 float ISpline<Vector3, Vector3>.maxT { get { return pSpline.t1; } }
189
191 return PoseAt(t).position;
192 }
193
195 return MovementAt(t).velocity;
196 }
197
199 out Vector3 value,
200 out Vector3 deltaValuePerT) {
201 Pose pose;
202 Movement movement;
203 PoseAndMovementAt(t, out pose, out movement);
204
205 value = pose.position;
206 deltaValuePerT = movement.velocity;
207 }
208
209 #endregion
210
211 }
212
213 public static class HermitePoseSplineExtensions {
214
215 public static void DrawPoseSpline(this RuntimeGizmos.RuntimeGizmoDrawer drawer,
216 HermitePoseSpline spline,
217 Color? color = null,
218 float poseGizmoScale = 0.02f,
219 int splineResolution = 32,
220 int drawPosePeriod = 8,
221 bool drawPoses = true,
222 bool drawSegments = true) {
223 if (!color.HasValue) {
224 color = LeapColor.brown.WithAlpha(0.4f);
225 }
226 drawer.color = color.Value;
227
228 var tWidth = spline.maxT - spline.minT;
229
230 Vector3? prevPos = null;
231 int counter = 0;
232 float tStep = (1f / splineResolution) * tWidth;
233 for (float t = spline.minT; t <= spline.minT + tWidth; t += tStep) {
234 var pose = spline.PoseAt(t);
235
236 if (counter % drawPosePeriod == 0 && drawPoses) {
237 drawer.DrawPose(pose, 0.02f);
238 }
239
240 if (prevPos.HasValue && drawSegments) {
241 drawer.DrawLine(prevPos.Value, pose.position);
242 }
243
244 prevPos = pose.position;
245 counter++;
246 }
247 }
248
249 }
250}
UnityEngine.Color Color
Definition: TestScript.cs:32
dXType DerivativeAt(float t)
void ValueAndDerivativeAt(float t, out XType value, out dXType deltaValuePerT)
Represents a spline for poses – positions and rotations – that travel from one position and rotation ...
Quaternion RotationAt(float t)
Gets the rotation at time t along this spline. The time is clamped within the t0 - t1 range.
Pose PoseAt(float t)
Gets the pose at time t along this spline. The time is clamped within the t0 - t1 range.
void RotationAndAngVelAt(float t, out Quaternion rotation, out Vector3 angularVelocity)
Gets both the rotation and the first derivative of rotation at time t. The time is clamped within the...
HermitePoseSpline(Pose pose0, Pose pose1, Movement move0, Movement move1)
Constructs a spline by specifying the poses and movements of the two endpoints. The time range of the...
void PositionAndVelAt(float t, out Vector3 position, out Vector3 velocity)
Gets both the position and the first derivative of position at time t. The time is clamped within the...
Vector3 AngularVelocityAt(float t)
Gets the first derivative of rotation at time t. The time is clamped within the t0 - t1 range....
Vector3 PositionAt(float t)
Gets the position at time t along this spline. The time is clamped within the t0 - t1 range.
HermitePoseSpline(Pose pose0, Pose pose1)
Constructs a spline by specifying the poses of the two endpoints. The velocity and angular velocity a...
HermitePoseSpline(Pose pose0, Pose pose1, Movement move0, Movement move1, float duration)
Constructs a spline by specifying the positions and velocities of the two endpoints....
Vector3 VelocityAt(float t)
Gets the first derivative of position at time t. The time is clamped within the t0 - t1 range.
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 ValueAndDerivativeAt(float t, out Pose value, out Movement deltaValuePerSec)
HermitePoseSpline(float t0, float t1, Pose pose0, Pose pose1, Movement move0, Movement move1)
Constructs a spline by specifying the positions, velocities, and times of the endpoints.
Represents a spline for the rotation of a rigid body from one orientation in space to another over a ...
void RotationAndAngVelAt(float t, out Quaternion rotation, out Vector3 angularVelocity)
Gets both the rotation and the first derivative of rotation at time t. The time is clamped within the...
Vector3 AngularVelocityAt(float t)
Gets the first derivative of rotation at time t. The time is clamped within the t0 - t1 range....
Quaternion RotationAt(float t)
Gets the rotation at time t along this spline. The time is clamped within the t0 - t1 range.
Represents a spline that travels from one point in space to another over a specified time frame....
Vector3 VelocityAt(float t)
Gets the first derivative of position at time t. The time is clamped within the t0 - t1 range (thus v...
Vector3 PositionAt(float t)
Gets the position at time t along this spline. The time is clamped to t0, but can extrapolate beyon...
void PositionAndVelAt(float t, out Vector3 position, out Vector3 velocity)
Gets both the position and the first derivative of position at time ti. The time is clamped within th...
Vector3 angularVelocity
Angular velocity expressed as an angle-axis vector with angle equal to the length of the vector in de...
Definition: Movement.cs:24
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
Quaternion rotation
Definition: Pose.cs:24
Vector3 position
Definition: Pose.cs:23