fastanim-core 0.1.0

fastanim core model: geometry, mobjects, animations and timeline
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
//! `TransformDiff` (SPEC §7): turns an edit script between two keyed sequences of parts into
//! choreographed animation, so unchanged parts slide, moved parts arc and only real changes
//! fade or morph.

use std::f32::consts::PI;
use std::hash::Hash;
use std::ops::{Deref, Range};

use fastanim_diff::{Algorithm, DiffOptions, Differ, Op, expand};
use kurbo::{Affine, Point, Vec2};

use crate::Interpolate;
use crate::anim::{Animation, RateFn};
use crate::color::{BLUE, Color, GREEN, GREY, ORANGE, RED, YELLOW};
use crate::geom::align;
use crate::mobject::{MobjectId, SceneState, VState};
use crate::timeline::Scene;

/// Mobjects in the scene grouped into keyed parts, e.g. a text's glyphs grouped into tokens.
/// Derefs to the ids, so it can be passed wherever `&[MobjectId]` is expected.
#[derive(Debug, Clone, PartialEq)]
pub struct Group<K> {
    /// The mobjects, in order.
    pub ids: Vec<MobjectId>,
    /// Each part's key and its range of [`ids`](Group::ids); contiguous and in order.
    pub parts: Vec<(K, Range<usize>)>,
    /// Runs of [`parts`](Group::parts) forming lines, diffed coarse-to-fine (SPEC §5.6);
    /// contiguous and in order. Empty means one line.
    pub lines: Vec<Range<usize>>,
}

/// Shapes grouped into keyed parts and lines, not yet in a scene: what a [`Group`] is added
/// from or transforms into. Fields are as in [`Group`].
#[derive(Debug, Clone, PartialEq)]
pub struct Layout<K> {
    /// The shapes, in order.
    pub states: Vec<VState>,
    /// Each part's key and its range of [`states`](Layout::states).
    pub parts: Vec<(K, Range<usize>)>,
    /// Runs of parts forming lines; empty means one line.
    pub lines: Vec<Range<usize>>,
}

impl<K: Clone> Group<K> {
    /// Adds the layout's shapes to the scene as one group.
    pub fn add(s: &mut Scene, layout: &Layout<K>) -> Self {
        Self {
            ids: layout.states.iter().map(|m| s.add(m.clone())).collect(),
            parts: layout.parts.clone(),
            lines: layout.lines.clone(),
        }
    }
}

impl<K> Deref for Group<K> {
    type Target = [MobjectId];
    fn deref(&self) -> &[MobjectId] {
        &self.ids
    }
}

/// When each kind of op animates within the clip.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Phasing {
    /// Deletes, then everything that stays, then inserts, one after another.
    Sequential,
    /// The phases overlap (SPEC §7.3): deletes first, inserts last.
    #[default]
    Overlapped,
}

/// How replaced parts change.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ReplaceStyle {
    /// Aligned path morph.
    #[default]
    Morph,
    /// The old shape fades out, then the new one fades in.
    CrossFade,
}

/// Choreography of a [`TransformDiff`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DiffStyle {
    /// When each kind of op runs.
    pub phasing: Phasing,
    /// Stagger between mobjects within a phase, as a fraction of one mobject's duration.
    pub lag_ratio: f32,
    /// Moved parts travel along an arc of this angle (radians); 0 is a straight line.
    pub move_arc: f64,
    /// How replaced parts change.
    pub replace: ReplaceStyle,
    /// Briefly tint inserted and replaced parts.
    pub highlight_changes: bool,
    /// Color parts by op while the clip plays (SPEC §7.4): green insert, red delete, blue move,
    /// amber replace, grey equal.
    pub debug: bool,
}

impl Default for DiffStyle {
    fn default() -> Self {
        Self {
            phasing: Phasing::Overlapped,
            lag_ratio: 0.05,
            move_arc: std::f64::consts::FRAC_PI_3,
            replace: ReplaceStyle::Morph,
            highlight_changes: false,
            debug: false,
        }
    }
}

/// What happens to one mobject.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Delete,
    Equal,
    Move,
    Replace,
    Insert,
}

impl Kind {
    /// Phase as a fraction of the clip.
    fn phase(self, phasing: Phasing) -> (f32, f32) {
        match (phasing, self) {
            (Phasing::Overlapped, Kind::Delete) => (0.0, 0.35),
            (Phasing::Overlapped, Kind::Equal | Kind::Move) => (0.2, 0.8),
            (Phasing::Overlapped, Kind::Replace) => (0.3, 0.85),
            (Phasing::Overlapped, Kind::Insert) => (0.6, 1.0),
            (Phasing::Sequential, Kind::Delete) => (0.0, 1.0 / 3.0),
            (Phasing::Sequential, Kind::Equal | Kind::Move | Kind::Replace) => {
                (1.0 / 3.0, 2.0 / 3.0)
            }
            (Phasing::Sequential, Kind::Insert) => (2.0 / 3.0, 1.0),
        }
    }

    fn debug_color(self) -> Color {
        match self {
            Kind::Delete => RED,
            Kind::Equal => GREY,
            Kind::Move => BLUE,
            Kind::Replace => ORANGE,
            Kind::Insert => GREEN,
        }
    }
}

struct Track {
    id: MobjectId,
    kind: Kind,
    /// `None` for deletes: the mobject leaves the scene.
    target: Option<VState>,
    /// Aligned start and end, set by `plan`.
    ends: Option<(VState, VState)>,
    /// Start and length as fractions of the clip, set by `plan`.
    window: (f32, f32),
}

/// Animates a [`Group`] into a new arrangement planned by diffing part keys; made by
/// [`Group::transform_diff`].
pub struct TransformDiff {
    tracks: Vec<Track>,
    ops: Vec<Op>,
    style: DiffStyle,
}

impl<K: Eq + Hash + Clone> Group<K> {
    /// Plans the morph of this group into `to` and updates the group to describe the result.
    /// Play the returned animation next.
    ///
    /// Parts are matched by key, line by line first with patience diff when there are lines,
    /// then with Myers' diff (see [`diff`](Group::diff)). New mobjects are added now,
    /// invisible; deleted ones leave the scene when the animation ends.
    pub fn transform_diff<C: Eq + Hash>(
        &mut self,
        s: &mut Scene,
        to: &Layout<K>,
        class: impl Fn(&K) -> Option<C>,
    ) -> TransformDiff {
        let ops = self.diff(s.state(), to, class);
        self.transform_ops(s, to, ops)
    }

    /// The edit script from this group's parts to `to`'s.
    ///
    /// Lines are diffed first with patience diff, keyed by their parts' keys; parts of equal
    /// or moved lines pair in order, and replaced runs of lines are diffed part by part. Moves and replacements
    /// pair by distance, and non-adjacent deletes and inserts of the same `class` pair into a
    /// replacement (e.g. `+` → `−`).
    pub fn diff<C: Eq + Hash>(
        &self,
        state: &SceneState,
        to: &Layout<K>,
        class: impl Fn(&K) -> Option<C>,
    ) -> Vec<Op> {
        let ca: Vec<Point> = (self.parts.iter())
            .map(|(_, r)| center(self.ids[r.clone()].iter().map(|id| &state[id])))
            .collect();
        let cb: Vec<Point> = (to.parts.iter())
            .map(|(_, r)| center(to.states[r.clone()].iter()))
            .collect();
        #[allow(clippy::single_range_in_vec_init, reason = "one line of all parts")]
        let lines = |l: &[Range<usize>], n: usize| match l {
            [] => vec![0..n],
            l => l.to_vec(),
        };
        let (la, lb) = (
            lines(&self.lines, self.parts.len()),
            lines(&to.lines, to.parts.len()),
        );
        let keys = |parts: &[(K, Range<usize>)], l: &[Range<usize>]| -> Vec<Vec<K>> {
            (l.iter())
                .map(|r| parts[r.clone()].iter().map(|(k, _)| k.clone()).collect())
                .collect()
        };
        let (ka, kb) = (keys(&self.parts, &la), keys(&to.parts, &lb));
        let outer = Differ::new(&ka, &kb, |k| k.clone())
            .options(DiffOptions {
                algorithm: Algorithm::Patience,
                ..DiffOptions::default()
            })
            .run();
        expand(&outer, &la, &lb, |ra, rb| {
            let (ao, bo) = (ra.start, rb.start);
            Differ::new(&self.parts[ra], &to.parts[rb], |(k, _)| k.clone())
                .cost(|i, j| ca[ao + i].distance(cb[bo + j]))
                .class(|(k, _)| class(k))
                .run()
        })
    }

    /// Like [`transform_diff`](Group::transform_diff), with the edit script given, e.g. a
    /// [`diff`](Group::diff) with ops rewritten.
    pub fn transform_ops(&mut self, s: &mut Scene, to: &Layout<K>, ops: Vec<Op>) -> TransformDiff {
        let glyphs = |parts: &[(K, Range<usize>)], r: Range<usize>| -> Vec<usize> {
            parts[r].iter().flat_map(|(_, g)| g.clone()).collect()
        };
        let mut tracks = Vec::new();
        let mut new_ids = vec![None; to.states.len()];
        for op in &ops {
            let (a, b, kind) = match op {
                Op::Equal { a, b } => (*a..a + 1, *b..b + 1, Kind::Equal),
                Op::Move { a, b } => (*a..a + 1, *b..b + 1, Kind::Move),
                Op::Replace { a, b } => (a.clone(), b.clone(), Kind::Replace),
                Op::Delete { a } => (*a..a + 1, 0..0, Kind::Delete),
                Op::Insert { b } => (0..0, *b..b + 1, Kind::Insert),
            };
            let (a, b) = (glyphs(&self.parts, a), glyphs(&to.parts, b));
            // Pair mobjects in order; leftovers fade out or in.
            for k in 0..a.len().max(b.len()) {
                let (id, kind) = match (a.get(k), b.get(k)) {
                    (Some(&i), Some(_)) => (self.ids[i], kind),
                    (Some(&i), None) => (self.ids[i], Kind::Delete),
                    (None, Some(&j)) => (
                        s.add(VState {
                            opacity: 0.0,
                            ..to.states[j].clone()
                        }),
                        Kind::Insert,
                    ),
                    (None, None) => unreachable!(),
                };
                let target = b.get(k).map(|&j| {
                    new_ids[j] = Some(id);
                    to.states[j].clone()
                });
                tracks.push(Track {
                    id,
                    kind,
                    target,
                    ends: None,
                    window: (0.0, 1.0),
                });
            }
        }

        self.ids = new_ids
            .into_iter()
            .map(|id| id.expect("ops cover every target part"))
            .collect();
        self.parts = to.parts.clone();
        self.lines = to.lines.clone();
        TransformDiff {
            tracks,
            ops,
            style: DiffStyle::default(),
        }
    }
}

/// Center of the joint bounding box; the origin when empty.
pub(crate) fn center<'a>(states: impl Iterator<Item = &'a VState>) -> Point {
    states
        .filter_map(|m| m.path.bbox())
        .reduce(|a, b| a.union(b))
        .map_or(Point::ORIGIN, |b| b.center())
}

/// Shrunk to a fifth about its center and transparent: where inserts come from and deletes go.
fn vanished(m: &VState) -> VState {
    VState {
        opacity: 0.0,
        ..m.clone().scale(0.2)
    }
}

impl TransformDiff {
    /// Sets the choreography.
    pub fn style(self, style: DiffStyle) -> Self {
        Self { style, ..self }
    }

    /// Colors parts by op while the clip plays; see [`DiffStyle::debug`].
    pub fn debug(mut self) -> Self {
        self.style.debug = true;
        self
    }

    /// The edit script between the parts, for inspection and tests.
    pub fn ops(&self) -> &[Op] {
        &self.ops
    }
}

impl Animation for TransformDiff {
    fn plan(&mut self, state: &SceneState) {
        // Stagger like `write`: within a phase, each mobject starts `lag_ratio` of its duration
        // after the previous one.
        let phasing = self.style.phasing;
        // Kinds sharing a phase are staggered together (and recomputed identically per kind).
        for kind in [
            Kind::Delete,
            Kind::Equal,
            Kind::Move,
            Kind::Replace,
            Kind::Insert,
        ] {
            let (s, e) = kind.phase(phasing);
            let in_phase = |t: &&mut Track| t.kind.phase(phasing) == (s, e);
            let n = self.tracks.iter_mut().filter(in_phase).count();
            let lag = self.style.lag_ratio;
            let w = (e - s) / (1.0 + n.saturating_sub(1) as f32 * lag);
            let mut k = 0.0;
            for t in self.tracks.iter_mut().filter(in_phase) {
                t.window = (s + k * lag * w, w);
                k += 1.0;
            }
        }
        for t in &mut self.tracks {
            let from = state[&t.id].clone();
            t.ends = Some(match (&t.target, t.kind) {
                (None, _) => (from.clone(), vanished(&from)),
                (Some(to), Kind::Insert) => (vanished(to), to.clone()),
                (Some(to), _) => {
                    let (a, b) = align(&from.path, &to.path);
                    (
                        VState { path: a, ..from },
                        VState {
                            path: b,
                            ..to.clone()
                        },
                    )
                }
            });
        }
    }

    fn sample(&self, alpha: f32, state: &mut SceneState) {
        for t in &self.tracks {
            // Exact endpoint: alignment re-segments paths, and deletes leave the scene.
            if alpha >= 1.0 {
                match &t.target {
                    Some(to) => state.insert(t.id, to.clone()),
                    None => state.remove(&t.id),
                };
                continue;
            }
            let (a, b) = t.ends.as_ref().expect("sample before plan");
            let p = RateFn::Smooth.apply((alpha - t.window.0) / t.window.1);
            let mut m = if t.kind == Kind::Replace && self.style.replace == ReplaceStyle::CrossFade
            {
                let (m, f) = if p < 0.5 {
                    (a, 1.0 - 2.0 * p)
                } else {
                    (b, 2.0 * p - 1.0)
                };
                VState {
                    opacity: m.opacity * f,
                    ..m.clone()
                }
            } else {
                VState::lerp(a, b, p)
            };
            if t.kind == Kind::Move && self.style.move_arc != 0.0 {
                // ponytail: a parabolic bow with the arc's sagitta, not a true circular arc.
                // Moving right bows up and moving left bows down, so swapping parts pass on
                // opposite sides.
                let d = b.path.center() - a.path.center();
                let bow = Vec2::new(-d.y, d.x) * (0.5 * (self.style.move_arc / 4.0).tan());
                let h = f64::from(4.0 * p * (1.0 - p));
                m = m.transform(Affine::translate(bow * h));
            }
            if self.style.debug {
                m.fill = t.kind.debug_color().with_alpha(m.fill.a.max(0.5));
            } else if self.style.highlight_changes && matches!(t.kind, Kind::Insert | Kind::Replace)
            {
                m.fill = Color::lerp(&m.fill, &YELLOW.with_alpha(m.fill.a), 0.8 * (PI * p).sin());
            }
            state.insert(t.id, m);
        }
    }

    fn rate_fn(&self) -> RateFn {
        // Each mobject eases within its own phase.
        RateFn::Linear
    }
}