HermiteSpline3.cs
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
using UnityEngine;
namespace Leap.Unity.Animation {
/// <summary>
/// Represents a spline that travels from one point in space
/// to another over a specified time frame. The two endpoints
/// are specified, as well as the instantaneous velocity at
/// these two endpoints.
///
/// You may ask for the position or the velocity at any time
/// during the splines duration.
/// </summary>
[Serializable]
public struct HermiteSpline3 {
public float t0, t1;
public Vector3 pos0, pos1;
public Vector3 vel0, vel1;
/// <summary>
/// Constructs a spline by specifying the positions of the
/// two endpoints. The velocity at each endpoint is zero,
/// and the time range of the spline is 0 to 1.
/// </summary>
public HermiteSpline3(Vector3 pos0, Vector3 pos1) {
t0 = 0;
t1 = 1;
vel0 = default(Vector3);
vel1 = default(Vector3);
this.pos0 = pos0;
this.pos1 = pos1;
}
/// <summary>
/// Constructs a spline by specifying the positions and
/// velocities of the two endpoints. The time range of
/// the spline is 0 to 1.
/// </summary>
public HermiteSpline3(Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1) {
t0 = 0;
t1 = 1;
this.vel0 = vel0;
this.vel1 = vel1;
this.pos0 = pos0;
this.pos1 = pos1;
}
/// <summary>
/// Constructs a spline by specifying the positions and
/// velocities of the two endpoints. The time range of
/// the spline is 0 to length.
/// </summary>
public HermiteSpline3(Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1, float length) {
t0 = 0;
t1 = length;
this.vel0 = vel0;
this.vel1 = vel1;
this.pos0 = pos0;
this.pos1 = pos1;
}
/// <summary>
/// Constructs a spline by specifying the positions,
/// velocities, and times of the endpoints.
/// </summary>
public HermiteSpline3(float t0, float t1, Vector3 pos0, Vector3 pos1, Vector3 vel0, Vector3 vel1) {
this.t0 = t0;
this.t1 = t1;
this.vel0 = vel0;
this.vel1 = vel1;
this.pos0 = pos0;
this.pos1 = pos1;
}
/// <summary>
/// Gets the position at time t along this spline.
/// The time is clamped within the t0 - t1 range.
/// </summary>
public Vector3 PositionAt(float t) {
float i = Mathf.Clamp01((t - t0) / (t1 - t0));
float i2 = i * i;
float i3 = i2 * i;
Vector3 h00 = (2 * i3 - 3 * i2 + 1) * pos0;
Vector3 h10 = (i3 - 2 * i2 + i) * (t1 - t0) * vel0;
Vector3 h01 = (-2 * i3 + 3 * i2) * pos1;
Vector3 h11 = (i3 - i2) * (t1 - t0) * vel1;
return h00 + h10 + h01 + h11;
}
/// <summary>
/// Gets the first derivative of position at time t.
/// The time is clamped within the t0 - t1 range.
/// </summary>
public Vector3 VelocityAt(float t) {
float C00 = t1 - t0;
float C1 = 1.0f / C00;
float i, i2;
float i_, i2_, i3_;
{
i = Mathf.Clamp01((t - t0) * C1);
i_ = C1;
i2 = i * i;
i2_ = 2 * i * i_;
i3_ = i2_ * i + i_ * i2;
}
Vector3 h00_ = (i3_ * 2 - i2_ * 3) * pos0;
Vector3 h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
Vector3 h01_ = (i2_ * 3 - 2 * i3_) * pos1;
Vector3 h11_ = (i3_ - i2_) * C00 * vel1;
return h00_ + h01_ + h10_ + h11_;
}
/// <summary>
/// Gets both the position and the first derivative of position
/// at time ti. The time is clamped within the t0 - t1 range.
/// </summary>
public void PositionAndVelAt(float t, out Vector3 position, out Vector3 velocity) {
float C00 = t1 - t0;
float C1 = 1.0f / C00;
float i, i2, i3;
float i_, i2_, i3_;
{
i = Mathf.Clamp01((t - t0) * C1);
i_ = C1;
i2 = i * i;
i2_ = 2 * i * i_;
i3 = i2 * i;
i3_ = i2_ * i + i_ * i2;
}
Vector3 h00 = (2 * i3 - 3 * i2 + 1) * pos0;
Vector3 h00_ = (i3_ * 2 - i2_ * 3) * pos0;
Vector3 h10 = (i3 - 2 * i2 + i) * C00 * vel0;
Vector3 h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
Vector3 h01 = (3 * i2 - 2 * i3) * pos1;
Vector3 h01_ = (i2_ * 3 - 2 * i3_) * pos1;
Vector3 h11 = (i3 - i2) * C00 * vel1;
Vector3 h11_ = (i3_ - i2_) * C00 * vel1;
position = h00 + h01 + h10 + h11;
velocity = h00_ + h01_ + h10_ + h11_;
}
}
}