Tanoda
HermiteSpline.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 HermiteSpline {
25 public float t0, t1;
26 public float pos0, pos1;
27 public float vel0, vel1;
28
34 public HermiteSpline(float pos0, float pos1) {
35 t0 = 0;
36 t1 = 1;
37
38 vel0 = default(float);
39 vel1 = default(float);
40
41 this.pos0 = pos0;
42 this.pos1 = pos1;
43 }
44
50 public HermiteSpline(float pos0, float pos1, float vel0, float 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 HermiteSpline(float pos0, float pos1, float vel0, float 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 HermiteSpline(float t0, float t1, float pos0, float pos1, float vel0, float 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
96 public float PositionAt(float t) {
97 float i = Mathf.Clamp01((t - t0) / (t1 - t0));
98 float i2 = i * i;
99 float i3 = i2 * i;
100
101 float h00 = (2 * i3 - 3 * i2 + 1) * pos0;
102 float h10 = (i3 - 2 * i2 + i) * (t1 - t0) * vel0;
103 float h01 = (-2 * i3 + 3 * i2) * pos1;
104 float h11 = (i3 - i2) * (t1 - t0) * vel1;
105
106 return h00 + h10 + h01 + h11;
107 }
108
113 public float VelocityAt(float t) {
114 float C00 = t1 - t0;
115 float C1 = 1.0f / C00;
116
117 float i, i2;
118 float i_, i2_, i3_;
119 {
120 i = Mathf.Clamp01((t - t0) * C1);
121 i_ = C1;
122
123 i2 = i * i;
124 i2_ = 2 * i * i_;
125
126 i3_ = i2_ * i + i_ * i2;
127 }
128
129 float h00_ = (i3_ * 2 - i2_ * 3) * pos0;
130 float h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
131 float h01_ = (i2_ * 3 - 2 * i3_) * pos1;
132 float h11_ = (i3_ - i2_) * C00 * vel1;
133
134 return h00_ + h01_ + h10_ + h11_;
135 }
136
141 public void PositionAndVelAt(float t, out float position, out float velocity) {
142 float C00 = t1 - t0;
143 float C1 = 1.0f / C00;
144
145 float i, i2, i3;
146 float i_, i2_, i3_;
147 {
148 i = Mathf.Clamp01((t - t0) * C1);
149 i_ = C1;
150
151 i2 = i * i;
152 i2_ = 2 * i * i_;
153
154 i3 = i2 * i;
155 i3_ = i2_ * i + i_ * i2;
156 }
157
158 float h00 = (2 * i3 - 3 * i2 + 1) * pos0;
159 float h00_ = (i3_ * 2 - i2_ * 3) * pos0;
160
161 float h10 = (i3 - 2 * i2 + i) * C00 * vel0;
162 float h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
163
164 float h01 = (3 * i2 - 2 * i3) * pos1;
165 float h01_ = (i2_ * 3 - 2 * i3_) * pos1;
166
167 float h11 = (i3 - i2) * C00 * vel1;
168 float h11_ = (i3_ - i2_) * C00 * vel1;
169
170 position = h00 + h01 + h10 + h11;
171 velocity = h00_ + h01_ + h10_ + h11_;
172 }
173 }
174}
Represents a spline that travels from one point in space to another over a specified time frame....
HermiteSpline(float pos0, float pos1)
Constructs a spline by specifying the positions of the two endpoints. The velocity at each endpoint i...
HermiteSpline(float t0, float t1, float pos0, float pos1, float vel0, float vel1)
Constructs a spline by specifying the positions, velocities, and times of the endpoints.
HermiteSpline(float pos0, float pos1, float vel0, float vel1)
Constructs a spline by specifying the positions and velocities of the two endpoints....
float VelocityAt(float t)
Gets the first derivative of position at time t. The time is clamped within the t0 - t1 range.
HermiteSpline(float pos0, float pos1, float vel0, float vel1, float length)
Constructs a spline by specifying the positions and velocities of the two endpoints....
float PositionAt(float t)
Gets the position at time t along this spline. The time is clamped within the t0 - t1 range.
void PositionAndVelAt(float t, out float position, out float velocity)
Gets both the position and the first derivative of position at time ti. The time is clamped within th...