Tanoda
HermiteSpline3.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;
10using UnityEngine;
11
12namespace Leap.Unity.Animation {
13
23 [Serializable]
24 public struct HermiteSpline3 {
25 public float t0, t1;
26 public Vector3 pos0, pos1;
27 public Vector3 vel0, vel1;
28
34 public HermiteSpline3(Vector3 pos0, Vector3 pos1) {
35 t0 = 0;
36 t1 = 1;
37
38 vel0 = default(Vector3);
39 vel1 = default(Vector3);
40
41 this.pos0 = pos0;
42 this.pos1 = pos1;
43 }
44
50 public HermiteSpline3(Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1) {
51 t0 = 0;
52 t1 = 1;
53
54 this.vel0 = vel0;
55 this.vel1 = vel1;
56
57 this.pos0 = pos0;
58 this.pos1 = pos1;
59 }
60
66 public HermiteSpline3(Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1, float length) {
67 t0 = 0;
68 t1 = length;
69
70 this.vel0 = vel0;
71 this.vel1 = vel1;
72
73 this.pos0 = pos0;
74 this.pos1 = pos1;
75 }
76
81 public HermiteSpline3(float t0, float t1, Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1) {
82 this.t0 = t0;
83 this.t1 = t1;
84
85 this.vel0 = vel0;
86 this.vel1 = vel1;
87
88 this.pos0 = pos0;
89 this.pos1 = pos1;
90 }
91
97 public Vector3 PositionAt(float t) {
98 if (t > t1) {
99 float i = ((t - t0) / (t1 - t0)) - 1f;
100 return pos1 + (vel1 * i);
101 } else {
102 float i = Mathf.Clamp01((t - t0) / (t1 - t0));
103 float i2 = i * i;
104 float i3 = i2 * i;
105
106 Vector3 h00 = (2 * i3 - 3 * i2 + 1) * pos0;
107 Vector3 h10 = (i3 - 2 * i2 + i) * (t1 - t0) * vel0;
108 Vector3 h01 = (-2 * i3 + 3 * i2) * pos1;
109 Vector3 h11 = (i3 - i2) * (t1 - t0) * vel1;
110
111 return h00 + h10 + h01 + h11;
112 }
113 }
114
120 public Vector3 VelocityAt(float t) {
121 float C00 = t1 - t0;
122 float C1 = 1.0f / C00;
123
124 float i, i2;
125 float i_, i2_, i3_;
126 {
127 i = Mathf.Clamp01((t - t0) * C1);
128 i_ = C1;
129
130 i2 = i * i;
131 i2_ = 2 * i * i_;
132
133 i3_ = i2_ * i + i_ * i2;
134 }
135
136 Vector3 h00_ = (i3_ * 2 - i2_ * 3) * pos0;
137 Vector3 h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
138 Vector3 h01_ = (i2_ * 3 - 2 * i3_) * pos1;
139 Vector3 h11_ = (i3_ - i2_) * C00 * vel1;
140
141 return h00_ + h01_ + h10_ + h11_;
142 }
143
149 public void PositionAndVelAt(float t, out Vector3 position, out Vector3 velocity) {
150 float C00 = t1 - t0;
151 float C1 = 1.0f / C00;
152
153 float i, i2, i3;
154 float i_, i2_, i3_;
155 {
156 i = Mathf.Clamp01((t - t0) * C1);
157 i_ = C1;
158
159 i2 = i * i;
160 i2_ = 2 * i * i_;
161
162 i3 = i2 * i;
163 i3_ = i2_ * i + i_ * i2;
164 }
165
166 Vector3 h00 = (2 * i3 - 3 * i2 + 1) * pos0;
167 Vector3 h00_ = (i3_ * 2 - i2_ * 3) * pos0;
168
169 Vector3 h10 = (i3 - 2 * i2 + i) * C00 * vel0;
170 Vector3 h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
171
172 Vector3 h01 = (3 * i2 - 2 * i3) * pos1;
173 Vector3 h01_ = (i2_ * 3 - 2 * i3_) * pos1;
174
175 Vector3 h11 = (i3 - i2) * C00 * vel1;
176 Vector3 h11_ = (i3_ - i2_) * C00 * vel1;
177
178 position = h00 + h01 + h10 + h11;
179 velocity = h00_ + h01_ + h10_ + h11_;
180 }
181 }
182}
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...
HermiteSpline3(Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1)
Constructs a spline by specifying the positions and velocities of the two endpoints....
Vector3 PositionAt(float t)
Gets the position at time t along this spline. The time is clamped to t0, but can extrapolate beyon...
HermiteSpline3(Vector3 pos0, Vector3 pos1)
Constructs a spline by specifying the positions of the two endpoints. The velocity at each endpoint i...
HermiteSpline3(Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1, float length)
Constructs a spline by specifying the positions and velocities of the two endpoints....
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...
HermiteSpline3(float t0, float t1, Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1)
Constructs a spline by specifying the positions, velocities, and times of the endpoints.