A shifted piece walks forward, a point collapse is real
Browse files at this commit84be3e47c1e34da0f213eed99c9eca11ff0039f3Parent: b1daa98- fix
- a shifted piece walks forward, a point collapse is real
1 changed file +84 -41
- crates/engine/src/map.rs +84 -41
crates/engine/src/map.rsmodified+84 -41
| @@ -3,27 +3,34 @@ | |||
| 3 | //! | 3 | //! |
| 4 | //! THE ARCHITECTURE LAW: a re-timing op NEVER touches lanes directly. It | 4 | //! THE ARCHITECTURE LAW: a re-timing op NEVER touches lanes directly. It |
| 5 | //! (1) resolves its [`Members`](crate::project) once via `group_of`, (2) | 5 | //! (1) resolves its [`Members`](crate::project) once via `group_of`, (2) |
| 6 | -//! builds ONE `TimelineMap` from their geometry, (3) applies that map to | 6 | +//! builds a `TimelineMap` from each member's OWN geometry -- one piece per |
| 7 | -//! every lane it owns -- media clips AND captions, uniformly -- and (4) the | 7 | +//! clip or caption, that member's own old span onto its own new one -- (3) |
| 8 | -//! caller maps the playhead through the SAME map. The app never re-derives | 8 | +//! applies that member's map to only its own lane, media clips AND |
| 9 | -//! group or time semantics; the engine's gates stay the single questions. | 9 | +//! captions uniformly, and (4) the caller maps the playhead through the |
| 10 | +//! SAME per-member map. The app never re-derives group or time semantics; | ||
| 11 | +//! the engine's gates stay the single questions. | ||
| 10 | //! | 12 | //! |
| 11 | //! The map itself is a sorted list of knots `(old frame, new frame)` with | 13 | //! The map itself is a sorted list of knots `(old frame, new frame)` with |
| 12 | //! affine interpolation between neighbours and identity outside the ends: a | 14 | //! affine interpolation between neighbours and identity outside the ends: a |
| 13 | //! compression is two knots with a shallower slope between them, a removal | 15 | //! compression is two knots with a shallower slope between them, a removal |
| 14 | //! is a knot pair that collapses a span onto a point, an insertion the same | 16 | //! is a knot pair that collapses a span onto a point, an insertion the same |
| 15 | -//! run backwards. Every piece is monotone by construction (knots sorted on | 17 | +//! run backwards, and a piece whose new span starts earlier than its old |
| 16 | -//! both axes), which is what makes [`TimelineMap::invert`] a binary search | 18 | +//! one (a slow-down pulling a member's head earlier than its own old frame) |
| 17 | -//! and the playhead's question a lookup. | 19 | +//! carries an extra knot that folds the run between the two los into that |
| 20 | +//! same kind of collapse, so the piece never doubles back on the identity | ||
| 21 | +//! run before it. Old frames strictly increase knot to knot; new frames | ||
| 22 | +//! only need to not decrease -- a collapse ties two of them together on | ||
| 23 | +//! purpose -- which is what keeps [`TimelineMap::invert`]'s walk a single | ||
| 24 | +//! pass, never a step backward. | ||
| 18 | 25 | ||
| 19 | /// A piecewise-affine old→new frame map, identity outside its knots. Built | 26 | /// A piecewise-affine old→new frame map, identity outside its knots. Built |
| 20 | /// once per edit and read many times; `Clone` for the tests that pin its | 27 | /// once per edit and read many times; `Clone` for the tests that pin its |
| 21 | /// laws, `Debug` for the messages those tests print when they fail. | 28 | /// laws, `Debug` for the messages those tests print when they fail. |
| 22 | #[derive(Clone, Debug, PartialEq)] | 29 | #[derive(Clone, Debug, PartialEq)] |
| 23 | pub(crate) struct TimelineMap { | 30 | pub(crate) struct TimelineMap { |
| 24 | - /// Knot points, sorted by `old`, strictly increasing on both axes (the | 31 | + /// Knot points, sorted by `old`, strictly increasing on `old` and |
| 25 | - /// constructor refuses anything else: a map that folded time back on | 32 | + /// non-decreasing on `new` -- a collapse ties two knots' `new` value |
| 26 | - /// itself would be a map nothing downstream could invert). | 33 | + /// together on purpose, the span it drops entering one new frame. |
| 27 | knots: Vec<(u32, u32)>, | 34 | knots: Vec<(u32, u32)>, |
| 28 | } | 35 | } |
| 29 | 36 | ||
| @@ -35,20 +42,35 @@ impl TimelineMap { | |||
| 35 | 42 | ||
| 36 | /// One non-identity piece -- `[old_lo, old_hi)` maps onto `[new_lo, | 43 | /// One non-identity piece -- `[old_lo, old_hi)` maps onto `[new_lo, |
| 37 | /// new_hi)` -- over an identity base. The whole of a compression (a | 44 | /// new_hi)` -- over an identity base. The whole of a compression (a |
| 38 | - /// re-rate of a member); a removal is `new` collapsed onto its own lo, | 45 | + /// re-rate of a member); a removal is `new` collapsed onto its own lo |
| 39 | - /// and an insertion is that run backwards. Degenerate spans (an empty | 46 | + /// (`new_lo == new_hi`, a literal point); an insertion is that run |
| 40 | - /// old, an inverted one) come out identity, because there is nothing in | 47 | + /// backwards; and a piece whose `new_lo` lands before `old_lo` (a |
| 41 | - /// them to move. | 48 | + /// slow-down pulling a member's head earlier than its own old frame) |
| 49 | + /// gets an extra anchor knot collapsing the run between the two los | ||
| 50 | + /// onto `new_lo`, so the piece stays non-decreasing instead of doubling | ||
| 51 | + /// back on the identity run before it. An empty old span (`old_lo >= | ||
| 52 | + /// old_hi`) comes out identity, because there is nothing in it to move; | ||
| 53 | + /// an inverted new span clamps its hi up to its lo, the collapse it means. | ||
| 42 | pub(crate) fn piece(old: (u32, u32), new: (u32, u32)) -> Self { | 54 | pub(crate) fn piece(old: (u32, u32), new: (u32, u32)) -> Self { |
| 43 | - if old.0 >= old.1 || new.0 >= new.1 { | 55 | + if old.0 >= old.1 { |
| 44 | return Self::identity(); | 56 | return Self::identity(); |
| 45 | } | 57 | } |
| 46 | - Self { | 58 | + let new = (new.0, new.1.max(new.0)); |
| 47 | - // Half-open boundary knots: interpolation runs between them and | 59 | + let mut knots = Vec::with_capacity(3); |
| 48 | - // the frame *at* the hi knot is the piece's own to give back | 60 | + // The anchor: without it, frames below `old_lo` stay identity and |
| 49 | - // (the caller clamps inside), never a step past its new end. | 61 | + // land ON the piece's own new_lo image the moment old_lo is |
| 50 | - knots: vec![(old.0, new.0), (old.1, new.1)], | 62 | + // crossed -- a decreasing step, the non-monotone bug a shift-left |
| 63 | + // piece used to be. Folding the run into the same collapse a | ||
| 64 | + // removal already does keeps `apply` a single non-decreasing walk. | ||
| 65 | + if new.0 < old.0 { | ||
| 66 | + knots.push((new.0, new.0)); | ||
| 51 | } | 67 | } |
| 68 | + // Half-open boundary knots: interpolation runs between them and | ||
| 69 | + // the frame *at* the hi knot is the piece's own to give back | ||
| 70 | + // (the caller clamps inside), never a step past its new end. | ||
| 71 | + knots.push((old.0, new.0)); | ||
| 72 | + knots.push((old.1, new.1)); | ||
| 73 | + Self { knots } | ||
| 52 | } | 74 | } |
| 53 | 75 | ||
| 54 | /// Where `frame` lands. Affine between knots, identity outside them, | 76 | /// Where `frame` lands. Affine between knots, identity outside them, |
| @@ -181,32 +203,53 @@ mod tests { | |||
| 181 | assert_eq!(m.shifted_total(), -99); | 203 | assert_eq!(m.shifted_total(), -99); |
| 182 | } | 204 | } |
| 183 | 205 | ||
| 184 | - /// A SHIFTED piece -- one whose new span starts before its old one, the | 206 | + /// The laws every piece keeps, whatever shape cut it: `apply` walking |
| 185 | - /// shape a removal cut into the head of the timeline gives -- is the case | 207 | + /// old frames up never goes back down, and every new frame's own |
| 186 | - /// `invert`'s below-the-knot run exists for: identity before the piece, | 208 | + /// inverse answers back exactly through `apply` again -- the right |
| 187 | - /// exact on the knots, image-total through the shift, and the frames the | 209 | + /// inverse a many-to-one piece (a compression, a collapse, the run a |
| 188 | - /// shift took below the piece's old lo their own inverses. | 210 | + /// shift folds into its own anchor) can still give, even where the |
| 189 | - #[test] | 211 | + /// left inverse can't (many old frames landing on one new frame means |
| 190 | - fn a_shifted_piece_inverts_on_its_own_run() { | 212 | + /// only one of them gets its own value back). Old frames strictly |
| 191 | - let m = TimelineMap::piece((30, 60), (15, 30)); | 213 | + /// below `min(old_lo, new_lo)`, and at or past `old_hi`, sit outside |
| 192 | - assert_eq!(m.apply(0), 0, "identity before the piece"); | 214 | + /// the piece's own collapse -- pure identity or a pure shift, both |
| 193 | - assert_eq!(m.apply(29), 29); | 215 | + /// exact bijections -- so those get the stronger left-inverse check. |
| 194 | - assert_eq!(m.apply(30), 15, "the lo knot is exact"); | 216 | + fn assert_piece_laws(old: (u32, u32), new: (u32, u32), span: u32) { |
| 195 | - assert_eq!(m.apply(59), 29, "the last frame of the piece"); | 217 | + let m = TimelineMap::piece(old, new); |
| 196 | - assert_eq!(m.apply(60), 30, "identity at the shift the piece left"); | 218 | + let mut last = 0; |
| 197 | - assert_eq!(m.shifted_total(), -30); | 219 | + // The image `apply` actually produces over the span -- an |
| 198 | - for n in 0..60 { | 220 | + // expansion skips new frames between old ones (no old frame answers |
| 221 | + // them), so the right-inverse law is only fair to ask of frames | ||
| 222 | + // `apply` really lands on, never the whole new axis blind. | ||
| 223 | + let mut image = std::collections::BTreeSet::new(); | ||
| 224 | + for f in 0..span { | ||
| 225 | + let now = m.apply(f); | ||
| 226 | + assert!(now >= last, "{old:?}->{new:?}: frame {f} went back to {now} from {last}"); | ||
| 227 | + image.insert(now); | ||
| 228 | + last = now; | ||
| 229 | + } | ||
| 230 | + for n in image { | ||
| 199 | let back = m.invert(n).expect("the image is total"); | 231 | let back = m.invert(n).expect("the image is total"); |
| 200 | - assert_eq!(m.apply(back), n, "new frame {n} round trips"); | 232 | + assert_eq!(m.apply(back), n, "{old:?}->{new:?}: new frame {n} round trips"); |
| 201 | } | 233 | } |
| 202 | - // ...and the run the shift opened below the piece's old lo is | 234 | + let lo = old.0.min(new.0); |
| 203 | - // identity in both directions -- the frames `invert` used to walk | 235 | + for f in (0..lo).chain(old.1 + 1..span) { |
| 204 | - // into the piece's own lerp from the wrong end. | 236 | + assert_eq!(m.invert(m.apply(f)), Some(f), "{old:?}->{new:?}: frame {f} outside the piece round trips"); |
| 205 | - for f in 0..15 { | ||
| 206 | - assert_eq!(m.invert(f), Some(f), "frame {f} is its own inverse"); | ||
| 207 | } | 237 | } |
| 208 | } | 238 | } |
| 209 | 239 | ||
| 240 | + /// Every piece shape the engine cuts, run through the laws above: an | ||
| 241 | + /// expansion, a compression, a shift-left (the shape a slow-down's | ||
| 242 | + /// caption overhang gives, and the one that used to go non-monotone), | ||
| 243 | + /// a shift-right, and a collapse to a literal point. | ||
| 244 | + #[test] | ||
| 245 | + fn a_piece_holds_its_laws_on_every_shape() { | ||
| 246 | + assert_piece_laws((100, 200), (100, 300), 500); | ||
| 247 | + assert_piece_laws((100, 200), (100, 150), 500); | ||
| 248 | + assert_piece_laws((30, 60), (15, 30), 200); | ||
| 249 | + assert_piece_laws((30, 60), (45, 90), 200); | ||
| 250 | + assert_piece_laws((100, 200), (100, 100), 500); | ||
| 251 | + } | ||
| 252 | + | ||
| 210 | /// Identity is the base: everything maps to itself, in both directions, | 253 | /// Identity is the base: everything maps to itself, in both directions, |
| 211 | /// and the timeline's length never moved. | 254 | /// and the timeline's length never moved. |
| 212 | #[test] | 255 | #[test] |