forked from Kezii/embedded-gfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.rs
More file actions
265 lines (222 loc) · 7.55 KB
/
Copy pathanimation.rs
File metadata and controls
265 lines (222 loc) · 7.55 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Vertex Animation System
//!
//! Provides keyframe-based vertex animation through linear interpolation.
//! Perfect for animated characters, waving flags, pulsing objects, etc.
/// A single keyframe containing vertex positions
#[derive(Debug, Clone, Copy)]
pub struct Keyframe<'a> {
pub vertices: &'a [[f32; 3]],
pub time: f32,
}
/// Vertex animation with multiple keyframes
#[derive(Debug)]
pub struct VertexAnimation<'a> {
keyframes: &'a [Keyframe<'a>],
looping: bool,
}
impl<'a> VertexAnimation<'a> {
/// Create a new vertex animation from keyframes
///
/// # Arguments
/// * `keyframes` - Array of keyframes with vertex positions and timestamps
/// * `looping` - Whether the animation should loop
///
/// # Panics
/// Panics if keyframes array is empty or if keyframes have inconsistent vertex counts
pub fn new(keyframes: &'a [Keyframe<'a>], looping: bool) -> Self {
assert!(!keyframes.is_empty(), "Keyframes array cannot be empty");
// Verify all keyframes have the same vertex count
let vertex_count = keyframes[0].vertices.len();
for kf in keyframes.iter() {
assert_eq!(
kf.vertices.len(),
vertex_count,
"All keyframes must have the same number of vertices"
);
}
Self { keyframes, looping }
}
/// Sample the animation at a given time
///
/// Returns interpolated vertex positions for the given time.
/// Uses a temporary buffer to store interpolated vertices.
///
/// # Arguments
/// * `time` - Current animation time
/// * `output` - Output buffer for interpolated vertices (must match keyframe vertex count)
pub fn sample(&self, time: f32, output: &mut [[f32; 3]]) {
assert_eq!(
output.len(),
self.keyframes[0].vertices.len(),
"Output buffer size must match keyframe vertex count"
);
// Handle edge cases
if self.keyframes.len() == 1 {
output.copy_from_slice(self.keyframes[0].vertices);
return;
}
// Get animation duration
let duration = self.keyframes.last().unwrap().time;
// Handle looping
let t = if self.looping {
if duration > 0.0 { time % duration } else { 0.0 }
} else {
time.clamp(0.0, duration)
};
// Find the two keyframes to interpolate between (binary search).
let mut kf1_idx = self
.keyframes
.partition_point(|kf| kf.time <= t)
.saturating_sub(1);
if self.keyframes[kf1_idx].time > t {
kf1_idx = 0;
}
let kf2_idx = (kf1_idx + 1).min(self.keyframes.len() - 1);
// If we're at or past the last keyframe
if kf1_idx == self.keyframes.len() - 1 {
output.copy_from_slice(self.keyframes[kf1_idx].vertices);
return;
}
let kf1 = &self.keyframes[kf1_idx];
let kf2 = &self.keyframes[kf2_idx];
// Calculate interpolation factor
let alpha = if kf2.time > kf1.time {
(t - kf1.time) / (kf2.time - kf1.time)
} else {
0.0
};
// Interpolate vertices
for (i, out_vertex) in output.iter_mut().enumerate() {
let v1 = &kf1.vertices[i];
let v2 = &kf2.vertices[i];
out_vertex[0] = v1[0] + alpha * (v2[0] - v1[0]);
out_vertex[1] = v1[1] + alpha * (v2[1] - v1[1]);
out_vertex[2] = v1[2] + alpha * (v2[2] - v1[2]);
}
}
/// Get the total duration of the animation
pub fn duration(&self) -> f32 {
self.keyframes.last().map(|kf| kf.time).unwrap_or(0.0)
}
/// Get the number of keyframes
pub fn keyframe_count(&self) -> usize {
self.keyframes.len()
}
}
#[cfg(test)]
mod tests {
extern crate std;
use super::*;
#[test]
fn test_single_keyframe() {
let vertices = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]];
let kf = Keyframe {
vertices: &vertices,
time: 0.0,
};
let keyframes = [kf];
let anim = VertexAnimation::new(&keyframes, false);
let mut output = [[0.0, 0.0, 0.0]; 2];
anim.sample(0.5, &mut output);
assert_eq!(output[0], [0.0, 0.0, 0.0]);
assert_eq!(output[1], [1.0, 0.0, 0.0]);
}
#[test]
fn test_two_keyframe_interpolation() {
let vertices1 = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]];
let vertices2 = [[0.0, 2.0, 0.0], [1.0, 2.0, 0.0]];
let kf1 = Keyframe {
vertices: &vertices1,
time: 0.0,
};
let kf2 = Keyframe {
vertices: &vertices2,
time: 1.0,
};
let keyframes = [kf1, kf2];
let anim = VertexAnimation::new(&keyframes, false);
// Sample at halfway point
let mut output = [[0.0, 0.0, 0.0]; 2];
anim.sample(0.5, &mut output);
assert_eq!(output[0], [0.0, 1.0, 0.0]);
assert_eq!(output[1], [1.0, 1.0, 0.0]);
}
#[test]
fn test_looping_animation() {
let vertices1 = [[0.0, 0.0, 0.0]];
let vertices2 = [[1.0, 0.0, 0.0]];
let kf1 = Keyframe {
vertices: &vertices1,
time: 0.0,
};
let kf2 = Keyframe {
vertices: &vertices2,
time: 1.0,
};
let keyframes = [kf1, kf2];
let anim = VertexAnimation::new(&keyframes, true);
// Sample past the end - should loop
let mut output = [[0.0, 0.0, 0.0]; 1];
anim.sample(1.5, &mut output);
// Should be halfway between keyframes (0.5 after wrapping)
assert_eq!(output[0], [0.5, 0.0, 0.0]);
}
#[test]
fn test_clamping_non_looping() {
let vertices1 = [[0.0, 0.0, 0.0]];
let vertices2 = [[1.0, 0.0, 0.0]];
let kf1 = Keyframe {
vertices: &vertices1,
time: 0.0,
};
let kf2 = Keyframe {
vertices: &vertices2,
time: 1.0,
};
let keyframes = [kf1, kf2];
let anim = VertexAnimation::new(&keyframes, false);
// Sample past the end - should clamp to last keyframe
let mut output = [[0.0, 0.0, 0.0]; 1];
anim.sample(2.0, &mut output);
assert_eq!(output[0], [1.0, 0.0, 0.0]);
}
#[test]
fn test_duration() {
let vertices1 = [[0.0, 0.0, 0.0]];
let vertices2 = [[1.0, 0.0, 0.0]];
let kf1 = Keyframe {
vertices: &vertices1,
time: 0.0,
};
let kf2 = Keyframe {
vertices: &vertices2,
time: 2.5,
};
let keyframes = [kf1, kf2];
let anim = VertexAnimation::new(&keyframes, false);
assert_eq!(anim.duration(), 2.5);
assert_eq!(anim.keyframe_count(), 2);
}
#[test]
#[should_panic(expected = "Keyframes array cannot be empty")]
fn test_empty_keyframes_panics() {
let keyframes: &[Keyframe] = &[];
let _anim = VertexAnimation::new(keyframes, false);
}
#[test]
#[should_panic(expected = "All keyframes must have the same number of vertices")]
fn test_inconsistent_vertex_count_panics() {
let vertices1 = [[0.0, 0.0, 0.0]];
let vertices2 = [[1.0, 0.0, 0.0], [2.0, 0.0, 0.0]];
let kf1 = Keyframe {
vertices: &vertices1,
time: 0.0,
};
let kf2 = Keyframe {
vertices: &vertices2,
time: 1.0,
};
let keyframes = [kf1, kf2];
let _anim = VertexAnimation::new(&keyframes, false);
}
}