Tanoda
HermiteSpline2.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 HermiteSpline2 {
25 public float t0, t1;
26 public Vector2 pos0, pos1;
27 public Vector2 vel0, vel1;
28
34 public HermiteSpline2(Vector2 pos0, Vector2 pos1) {
35 t0 = 0;
36 t1 = 1;
37
38 vel0 = default(Vector2);
39 vel1 = default(Vector2);
40
41 this.pos0 = pos0;
42 this.pos1 = pos1;
43 }
44
50 public HermiteSpline2(Vector2 pos0, Vector2 pos1, Vector2 vel0, Vector2 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 HermiteSpline2(Vector2 pos0, Vector2 pos1, Vector2 vel0, Vector2 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 HermiteSpline2(float t0, float t1, Vector2 pos0, Vector2 pos1, Vector2 vel0, Vector2 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 Vector2 PositionAt(float t) {
97 float i = Mathf.Clamp01((t - t0) / (t1 - t0));
98 float i2 = i * i;
99 float i3 = i2 * i;
100
101 Vector2 h00 = (2 * i3 - 3 * i2 + 1) * pos0;
102 Vector2 h10 = (i3 - 2 * i2 + i) * (t1 - t0) * vel0;
103 Vector2 h01 = (-2 * i3 + 3 * i2) * pos1;
104 Vector2 h11 = (i3 - i2) * (t1 - t0) * vel1;
105
106 return h00 + h10 + h01 + h11;
107 }
108
113 public Vector2 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 Vector2 h00_ = (i3_ * 2 - i2_ * 3) * pos0;
130 Vector2 h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
131 Vector2 h01_ = (i2_ * 3 - 2 * i3_) * pos1;
132 Vector2 h11_ = (i3_ - i2_) * C00 * vel1;
133
134 return h00_ + h01_ + h10_ + h11_;
135 }
136
141 public void PositionAndVelAt(float t, out Vector2 position, out Vector2 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 Vector2 h00 = (2 * i3 - 3 * i2 + 1) * pos0;
159 Vector2 h00_ = (i3_ * 2 - i2_ * 3) * pos0;
160
161 Vector2 h10 = (i3 - 2 * i2 + i) * C00 * vel0;
162 Vector2 h10_ = (i3_ - 2 * i2_ + i_) * C00 * vel0;
163
164 Vector2 h01 = (3 * i2 - 2 * i3) * pos1;
165 Vector2 h01_ = (i2_ * 3 - 2 * i3_) * pos1;
166
167 Vector2 h11 = (i3 - i2) * C00 * vel1;
168 Vector2 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....
Vector2 PositionAt(float t)
Gets the position at time t along this spline. The time is clamped within the t0 - t1 range.
HermiteSpline2(Vector2 pos0, Vector2 pos1, Vector2 vel0, Vector2 vel1, float length)
Constructs a spline by specifying the positions and velocities of the two endpoints....
Vector2 VelocityAt(float t)
Gets the first derivative of position at time t. The time is clamped within the t0 - t1 range.
HermiteSpline2(float t0, float t1, Vector2 pos0, Vector2 pos1, Vector2 vel0, Vector2 vel1)
Constructs a spline by specifying the positions, velocities, and times of the endpoints.
HermiteSpline2(Vector2 pos0, Vector2 pos1, Vector2 vel0, Vector2 vel1)
Constructs a spline by specifying the positions and velocities of the two endpoints....
void PositionAndVelAt(float t, out Vector2 position, out Vector2 velocity)
Gets both the position and the first derivative of position at time ti. The time is clamped within th...
HermiteSpline2(Vector2 pos0, Vector2 pos1)
Constructs a spline by specifying the positions of the two endpoints. The velocity at each endpoint i...