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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Piecewise spline curve evaluation for animations, tweens, and particle parameter ramps.
//!
//! Inspired by Fyrox's `fyrox-math::curve::Curve`, adapted for `no_std` embedded systems.
//!
//! # Example
//! ```
//! use embedded_3dgfx::curve::{Curve, CurveInterpolation, CurveKey};
//!
//! static BOUNCE_CURVE: Curve<'static, f32> = Curve::new(&[
//! CurveKey::new(0.0, 0.0, CurveInterpolation::Hermite { in_tangent: 0.0, out_tangent: 2.0 }),
//! CurveKey::new(0.5, 1.0, CurveInterpolation::Hermite { in_tangent: 0.0, out_tangent: 0.0 }),
//! CurveKey::new(1.0, 0.0, CurveInterpolation::Hermite { in_tangent: -2.0, out_tangent: 0.0 }),
//! ]);
//!
//! let val = BOUNCE_CURVE.sample(0.25);
//! ```
/// Interpolation mode between curve keyframes.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum CurveInterpolation {
/// Holds the constant value of the key until the next key.
Step,
/// Linear interpolation between keys.
#[default]
Linear,
/// Cubic Hermite spline interpolation with incoming and outgoing tangents.
Hermite {
/// Incoming tangent slope.
in_tangent: f32,
/// Outgoing tangent slope.
out_tangent: f32,
},
}
/// A keyframe on a parametric curve.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CurveKey<T> {
/// Time/position along the curve.
pub time: f32,
/// Value at this keyframe.
pub value: T,
/// Interpolation curve to the next keyframe.
pub interpolation: CurveInterpolation,
}
impl<T> CurveKey<T> {
/// Create a new curve keyframe.
pub const fn new(time: f32, value: T, interpolation: CurveInterpolation) -> Self {
Self {
time,
value,
interpolation,
}
}
/// Create a linear keyframe.
pub const fn linear(time: f32, value: T) -> Self {
Self {
time,
value,
interpolation: CurveInterpolation::Linear,
}
}
/// Create a step keyframe.
pub const fn step(time: f32, value: T) -> Self {
Self {
time,
value,
interpolation: CurveInterpolation::Step,
}
}
}
/// A piecewise parametric curve backed by a sorted slice of keyframes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Curve<'a, T> {
keys: &'a [CurveKey<T>],
}
impl<'a, T> Curve<'a, T> {
/// Create a new curve from a slice of keys.
pub const fn new(keys: &'a [CurveKey<T>]) -> Self {
Self { keys }
}
/// Returns the number of keyframes.
#[inline]
pub const fn len(&self) -> usize {
self.keys.len()
}
/// Returns true if the curve has no keyframes.
#[inline]
pub const fn is_empty(&self) -> bool {
self.keys.is_empty()
}
}
impl<'a> Curve<'a, f32> {
/// Sample the curve at the specified `time`.
pub fn sample(&self, time: f32) -> f32 {
if self.keys.is_empty() {
return 0.0;
}
if self.keys.len() == 1 {
return self.keys[0].value;
}
// Before first key
if time <= self.keys[0].time {
return self.keys[0].value;
}
// At or after last key
if time >= self.keys[self.keys.len() - 1].time {
return self.keys[self.keys.len() - 1].value;
}
// Find surrounding key segment
for i in 0..self.keys.len() - 1 {
let left = &self.keys[i];
let right = &self.keys[i + 1];
if time >= left.time && time <= right.time {
let dt = right.time - left.time;
if dt <= 1e-6 {
return left.value;
}
let t = (time - left.time) / dt;
return match left.interpolation {
CurveInterpolation::Step => left.value,
CurveInterpolation::Linear => left.value + t * (right.value - left.value),
CurveInterpolation::Hermite { out_tangent, .. } => {
let in_tangent = match right.interpolation {
CurveInterpolation::Hermite { in_tangent, .. } => in_tangent,
_ => 0.0,
};
// Cubic Hermite basis functions
let t2 = t * t;
let t3 = t2 * t;
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
h00 * left.value
+ h10 * dt * out_tangent
+ h01 * right.value
+ h11 * dt * in_tangent
}
};
}
}
self.keys[self.keys.len() - 1].value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_linear_curve() {
let keys = [
CurveKey::linear(0.0, 0.0),
CurveKey::linear(1.0, 10.0),
CurveKey::linear(2.0, 5.0),
];
let curve = Curve::new(&keys);
assert_eq!(curve.sample(0.0), 0.0);
assert_eq!(curve.sample(0.5), 5.0);
assert_eq!(curve.sample(1.0), 10.0);
assert_eq!(curve.sample(1.5), 7.5);
assert_eq!(curve.sample(2.0), 5.0);
}
#[test]
fn test_hermite_curve() {
let keys = [
CurveKey::new(
0.0,
0.0,
CurveInterpolation::Hermite {
in_tangent: 0.0,
out_tangent: 0.0,
},
),
CurveKey::new(
1.0,
1.0,
CurveInterpolation::Hermite {
in_tangent: 0.0,
out_tangent: 0.0,
},
),
];
let curve = Curve::new(&keys);
assert_eq!(curve.sample(0.0), 0.0);
assert_eq!(curve.sample(1.0), 1.0);
// Smooth S-curve at midpoint should be 0.5
assert!((curve.sample(0.5) - 0.5).abs() < 1e-5);
}
#[test]
fn test_curve_empty_single_step_zero_dt_and_len() {
let empty: Curve<'_, f32> = Curve::new(&[]);
assert!(empty.is_empty());
assert_eq!(empty.len(), 0);
assert_eq!(empty.sample(0.5), 0.0);
let single = [CurveKey::linear(1.0, 7.0)];
let one = Curve::new(&single);
assert_eq!(one.len(), 1);
assert!(!one.is_empty());
assert_eq!(one.sample(-100.0), 7.0);
assert_eq!(one.sample(100.0), 7.0);
let step_keys = [CurveKey::step(0.0, 2.0), CurveKey::step(1.0, 4.0)];
let step_curve = Curve::new(&step_keys);
assert_eq!(step_curve.sample(0.5), 2.0);
let hermite_to_linear = [
CurveKey::new(
0.0,
3.0,
CurveInterpolation::Hermite {
in_tangent: 0.0,
out_tangent: 0.5,
},
),
CurveKey::linear(1.0, 11.0),
];
let curve = Curve::new(&hermite_to_linear);
let mid = curve.sample(0.5);
assert!(mid > 3.0 && mid < 11.0);
}
}