spanda 0.9.3

A general-purpose animation library for Rust — tweening, keyframes, timelines, and physics.
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Shape morphing — lerp between two sets of 2D control points.
//!
//! `MorphPath` animates a smooth transition between two shapes defined as
//! sequences of `[f32; 2]` points. Each point in the source shape is linearly
//! interpolated toward its corresponding point in the target shape.
//!
//! If the two shapes have different point counts, use [`resample`] to
//! normalise them, or let the builder handle it automatically.
//!
//! # Example
//!
//! ```rust
//! use spanda::morph::MorphPath;
//! use spanda::easing::Easing;
//! use spanda::traits::Update;
//!
//! let triangle = vec![[0.0, 0.0], [50.0, 100.0], [100.0, 0.0]];
//! let square   = vec![[0.0, 0.0], [0.0, 100.0], [100.0, 100.0]];
//!
//! let mut morph = MorphPath::new(triangle, square)
//!     .duration(1.0)
//!     .easing(Easing::EaseInOutCubic)
//!     .build();
//!
//! morph.update(0.5);
//! let points = morph.value();
//! assert_eq!(points.len(), 3);
//! ```

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use num_traits::Float as _;

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

use crate::easing::Easing;
use crate::traits::Update;

/// Animated shape morph between two sets of 2D points.
#[derive(Clone, Debug)]
pub struct MorphPath {
    from_points: Vec<[f32; 2]>,
    to_points: Vec<[f32; 2]>,
    duration: f32,
    easing: Easing,
    elapsed: f32,
    completed: bool,
}

/// Builder for [`MorphPath`].
#[derive(Debug)]
pub struct MorphPathBuilder {
    from_points: Vec<[f32; 2]>,
    to_points: Vec<[f32; 2]>,
    duration: f32,
    easing: Easing,
    shape_index: ShapeIndex,
}

impl MorphPath {
    /// Start building a morph from `from` points to `to` points.
    ///
    /// If the two point arrays have different lengths, the shorter one is
    /// automatically resampled to match the longer.
    #[allow(clippy::new_ret_no_self)]
    pub fn new(from: Vec<[f32; 2]>, to: Vec<[f32; 2]>) -> MorphPathBuilder {
        MorphPathBuilder {
            from_points: from,
            to_points: to,
            duration: 1.0,
            easing: Easing::Linear,
            shape_index: ShapeIndex::None,
        }
    }

    /// Current interpolated points at the current progress.
    pub fn value(&self) -> Vec<[f32; 2]> {
        let raw_t = if self.duration > 0.0 {
            (self.elapsed / self.duration).clamp(0.0, 1.0)
        } else {
            1.0
        };
        let t = self.easing.apply(raw_t);

        self.from_points
            .iter()
            .zip(self.to_points.iter())
            .map(|(a, b)| [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t])
            .collect()
    }

    /// Raw progress `0.0..=1.0` (before easing).
    pub fn progress(&self) -> f32 {
        if self.duration > 0.0 {
            (self.elapsed / self.duration).clamp(0.0, 1.0)
        } else {
            1.0
        }
    }

    /// Whether the morph animation has completed.
    pub fn is_complete(&self) -> bool {
        self.completed
    }

    /// Reset the animation to the beginning.
    pub fn reset(&mut self) {
        self.elapsed = 0.0;
        self.completed = false;
    }

    /// Jump to a specific progress value `t` (0.0..=1.0).
    pub fn seek(&mut self, t: f32) {
        self.elapsed = t.clamp(0.0, 1.0) * self.duration;
        self.completed = t >= 1.0;
    }
}

impl Update for MorphPath {
    fn update(&mut self, dt: f32) -> bool {
        if self.completed {
            return false;
        }
        self.elapsed += dt;
        if self.elapsed >= self.duration {
            self.elapsed = self.duration;
            self.completed = true;
        }
        !self.completed
    }
}

impl MorphPathBuilder {
    /// Set animation duration in seconds (default: 1.0).
    pub fn duration(mut self, d: f32) -> Self {
        self.duration = d;
        self
    }

    /// Set the easing curve (default: Linear).
    pub fn easing(mut self, e: Easing) -> Self {
        self.easing = e;
        self
    }

    /// Set the shape index for point correspondence.
    ///
    /// GSAP equivalent: `shapeIndex` property.
    ///
    /// Controls which points in the source shape map to which points in
    /// the target shape. Use `ShapeIndex::Auto` for automatic detection
    /// of the best rotation, or `ShapeIndex::Offset(n)` for manual control.
    pub fn shape_index(mut self, index: ShapeIndex) -> Self {
        self.shape_index = index;
        self
    }

    /// Build the `MorphPath`. Auto-resamples if point counts differ.
    pub fn build(mut self) -> MorphPath {
        let from_len = self.from_points.len();
        let to_len = self.to_points.len();

        // Resample to match point counts
        if from_len != to_len && from_len > 0 && to_len > 0 {
            let target = from_len.max(to_len);
            if from_len < target {
                self.from_points = resample(&self.from_points, target);
            } else {
                self.to_points = resample(&self.to_points, target);
            }
        }

        // Apply shape index rotation to target points
        let to_points = match self.shape_index {
            ShapeIndex::Auto => {
                let best = ShapeIndex::auto(&self.from_points, &self.to_points);
                best.apply(&self.to_points)
            }
            other => other.apply(&self.to_points),
        };

        MorphPath {
            from_points: self.from_points,
            to_points,
            duration: self.duration,
            easing: self.easing,
            elapsed: 0.0,
            completed: false,
        }
    }
}

/// Resample a polyline to `target_count` evenly-spaced points along its arc length.
///
/// Preserves the first and last endpoints. If `points` has fewer than 2 entries
/// or `target_count` is 0, returns `points` unchanged (or empty).
pub fn resample(points: &[[f32; 2]], target_count: usize) -> Vec<[f32; 2]> {
    if points.len() < 2 || target_count < 2 {
        return if target_count == 1 && !points.is_empty() {
            vec![points[0]]
        } else {
            points.to_vec()
        };
    }

    // Build cumulative arc-length table
    let mut lengths = Vec::with_capacity(points.len());
    lengths.push(0.0_f32);
    for i in 1..points.len() {
        let dx = points[i][0] - points[i - 1][0];
        let dy = points[i][1] - points[i - 1][1];
        let seg_len = (dx * dx + dy * dy).sqrt();
        lengths.push(lengths[i - 1] + seg_len);
    }

    let total_len = *lengths.last().unwrap();
    if total_len < 1e-10 {
        return vec![points[0]; target_count];
    }

    let mut result = Vec::with_capacity(target_count);
    for i in 0..target_count {
        let target_dist = total_len * (i as f32 / (target_count - 1) as f32);

        // Binary search for the segment containing target_dist
        let seg = match lengths.binary_search_by(|l| l.partial_cmp(&target_dist).unwrap()) {
            Ok(idx) => idx.min(points.len() - 2),
            Err(idx) => {
                if idx == 0 {
                    0
                } else {
                    (idx - 1).min(points.len() - 2)
                }
            }
        };

        let seg_start = lengths[seg];
        let seg_end = lengths[seg + 1];
        let seg_len = seg_end - seg_start;

        let local_t = if seg_len > 1e-10 {
            (target_dist - seg_start) / seg_len
        } else {
            0.0
        };

        result.push([
            points[seg][0] + (points[seg + 1][0] - points[seg][0]) * local_t,
            points[seg][1] + (points[seg + 1][1] - points[seg][1]) * local_t,
        ]);
    }

    result
}

// ── ShapeIndex ──────────────────────────────────────────────────────────────

/// Controls point correspondence during shape morphing.
///
/// GSAP equivalent: `shapeIndex` property in MorphSVGPlugin.
///
/// When morphing between two shapes, `ShapeIndex` determines which point
/// in the source shape corresponds to which point in the target shape.
/// This can dramatically affect the visual quality of the morph.
///
/// # Example
///
/// ```rust
/// use spanda::morph::{MorphPath, ShapeIndex};
///
/// let triangle = vec![[0.0, 0.0], [50.0, 100.0], [100.0, 0.0]];
/// let square   = vec![[0.0, 0.0], [0.0, 100.0], [100.0, 100.0], [100.0, 0.0]];
///
/// // Use auto-detection for best point alignment
/// let index = ShapeIndex::auto(&triangle, &square);
///
/// let morph = MorphPath::new(triangle, square)
///     .shape_index(index)
///     .duration(1.0)
///     .build();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ShapeIndex {
    /// No rotation - use points as-is (default).
    #[default]
    None,
    /// Rotate target points by this index offset.
    ///
    /// For a shape with N points, `Offset(k)` maps source point 0 to
    /// target point k, source point 1 to target point (k+1) % N, etc.
    Offset(i32),
    /// Automatically find the best rotation to minimize morph distance.
    Auto,
}

impl ShapeIndex {
    /// Compute the best rotation offset for two shapes.
    ///
    /// Finds the rotation that minimizes the total distance between
    /// corresponding points. Both shapes should have the same number
    /// of points (resample first if needed).
    pub fn auto(from: &[[f32; 2]], to: &[[f32; 2]]) -> Self {
        if from.is_empty() || to.is_empty() || from.len() != to.len() {
            return ShapeIndex::None;
        }

        let n = from.len();
        let mut best_offset = 0;
        let mut best_dist = f32::MAX;

        for offset in 0..n {
            let mut total_dist = 0.0_f32;
            for (i, from_pt) in from.iter().enumerate() {
                let to_idx = (i + offset) % n;
                let dx = to[to_idx][0] - from_pt[0];
                let dy = to[to_idx][1] - from_pt[1];
                total_dist += dx * dx + dy * dy;
            }

            if total_dist < best_dist {
                best_dist = total_dist;
                best_offset = offset;
            }
        }

        if best_offset == 0 {
            ShapeIndex::None
        } else {
            ShapeIndex::Offset(best_offset as i32)
        }
    }

    /// Apply the shape index rotation to a set of points.
    ///
    /// Returns a new vector with points rotated according to the index.
    pub fn apply(&self, points: &[[f32; 2]]) -> Vec<[f32; 2]> {
        if points.is_empty() {
            return Vec::new();
        }

        match self {
            ShapeIndex::None => points.to_vec(),
            ShapeIndex::Offset(k) => {
                let n = points.len() as i32;
                let k = k.rem_euclid(n) as usize;
                let mut result = Vec::with_capacity(points.len());
                for i in 0..points.len() {
                    result.push(points[(i + k) % points.len()]);
                }
                result
            }
            ShapeIndex::Auto => {
                // Auto requires both shapes - caller should use ShapeIndex::auto() first
                points.to_vec()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn morph_at_t0_returns_from() {
        let from = vec![[0.0, 0.0], [10.0, 10.0]];
        let to = vec![[100.0, 100.0], [200.0, 200.0]];
        let morph = MorphPath::new(from.clone(), to).duration(1.0).build();
        let val = morph.value();
        assert!((val[0][0] - 0.0).abs() < 1e-6);
        assert!((val[1][1] - 10.0).abs() < 1e-6);
    }

    #[test]
    fn morph_at_t1_returns_to() {
        let from = vec![[0.0, 0.0], [10.0, 10.0]];
        let to = vec![[100.0, 100.0], [200.0, 200.0]];
        let mut morph = MorphPath::new(from, to.clone()).duration(1.0).build();
        morph.update(1.0);
        let val = morph.value();
        assert!((val[0][0] - 100.0).abs() < 1e-6);
        assert!((val[1][1] - 200.0).abs() < 1e-6);
    }

    #[test]
    fn morph_midpoint() {
        let from = vec![[0.0, 0.0]];
        let to = vec![[100.0, 200.0]];
        let mut morph = MorphPath::new(from, to).duration(1.0).build();
        morph.update(0.5);
        let val = morph.value();
        assert!((val[0][0] - 50.0).abs() < 1e-5);
        assert!((val[0][1] - 100.0).abs() < 1e-5);
    }

    #[test]
    fn morph_auto_resample_mismatched_lengths() {
        let from = vec![[0.0, 0.0], [100.0, 0.0]];
        let to = vec![[0.0, 0.0], [50.0, 50.0], [100.0, 0.0]];
        let morph = MorphPath::new(from, to).duration(1.0).build();
        // Both should now have 3 points
        let val = morph.value();
        assert_eq!(val.len(), 3);
    }

    #[test]
    fn morph_update_returns_false_when_done() {
        let from = vec![[0.0, 0.0]];
        let to = vec![[10.0, 10.0]];
        let mut morph = MorphPath::new(from, to).duration(0.5).build();
        assert!(morph.update(0.3));
        assert!(!morph.update(0.3));
        assert!(morph.is_complete());
    }

    #[test]
    fn morph_reset() {
        let from = vec![[0.0, 0.0]];
        let to = vec![[10.0, 10.0]];
        let mut morph = MorphPath::new(from, to).duration(0.5).build();
        morph.update(1.0);
        assert!(morph.is_complete());
        morph.reset();
        assert!(!morph.is_complete());
        assert!((morph.value()[0][0]).abs() < 1e-6);
    }

    #[test]
    fn resample_preserves_endpoints() {
        let pts = vec![[0.0, 0.0], [50.0, 50.0], [100.0, 0.0]];
        let resampled = resample(&pts, 5);
        assert_eq!(resampled.len(), 5);
        assert!((resampled[0][0] - 0.0).abs() < 1e-5);
        assert!((resampled[0][1] - 0.0).abs() < 1e-5);
        assert!((resampled[4][0] - 100.0).abs() < 1e-5);
        assert!((resampled[4][1] - 0.0).abs() < 1e-5);
    }

    #[test]
    fn resample_single_point() {
        let pts = vec![[42.0, 17.0]];
        let resampled = resample(&pts, 1);
        assert_eq!(resampled.len(), 1);
        assert!((resampled[0][0] - 42.0).abs() < 1e-6);
    }
}