From 84be3e47c1e34da0f213eed99c9eca11ff0039f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 16:54:57 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine):=20a=20shifted=20pie?= =?UTF-8?q?ce=20walks=20forward,=20a=20point=20collapse=20is=20real?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A piece whose new run started before its old one collided with the identity run in front of it: apply(29)=29 then apply(30)=15, thirty frames of the domain not invertible -- against the module's own law. piece() now prepends an anchor knot at min(old.lo,new.lo), folding the band between the two los into the same collapse a removal makes, so apply stays one non-decreasing walk. new.lo==new.hi used to be refused as identity while the docs called it the removal shape; only an empty OLD run is identity now, and a literal point collapse is constructible. Docs rewritten to the truth: knots strictly increase on old, non-decreasing on new; one map per member; invert is a linear walk. The shape test now checks monotonicity and inversion over every shape: expansion, compression, both shifts, point collapse. --- crates/engine/src/map.rs | 125 ++++++++++++++++++++++++++------------- 1 file changed, 84 insertions(+), 41 deletions(-) diff --git a/crates/engine/src/map.rs b/crates/engine/src/map.rs index 9957015..61865fb 100644 --- a/crates/engine/src/map.rs +++ b/crates/engine/src/map.rs @@ -3,27 +3,34 @@ //! //! THE ARCHITECTURE LAW: a re-timing op NEVER touches lanes directly. It //! (1) resolves its [`Members`](crate::project) once via `group_of`, (2) -//! builds ONE `TimelineMap` from their geometry, (3) applies that map to -//! every lane it owns -- media clips AND captions, uniformly -- and (4) the -//! caller maps the playhead through the SAME map. The app never re-derives -//! group or time semantics; the engine's gates stay the single questions. +//! builds a `TimelineMap` from each member's OWN geometry -- one piece per +//! clip or caption, that member's own old span onto its own new one -- (3) +//! applies that member's map to only its own lane, media clips AND +//! captions uniformly, and (4) the caller maps the playhead through the +//! SAME per-member map. The app never re-derives group or time semantics; +//! the engine's gates stay the single questions. //! //! The map itself is a sorted list of knots `(old frame, new frame)` with //! affine interpolation between neighbours and identity outside the ends: a //! compression is two knots with a shallower slope between them, a removal //! is a knot pair that collapses a span onto a point, an insertion the same -//! run backwards. Every piece is monotone by construction (knots sorted on -//! both axes), which is what makes [`TimelineMap::invert`] a binary search -//! and the playhead's question a lookup. +//! run backwards, and a piece whose new span starts earlier than its old +//! one (a slow-down pulling a member's head earlier than its own old frame) +//! carries an extra knot that folds the run between the two los into that +//! same kind of collapse, so the piece never doubles back on the identity +//! run before it. Old frames strictly increase knot to knot; new frames +//! only need to not decrease -- a collapse ties two of them together on +//! purpose -- which is what keeps [`TimelineMap::invert`]'s walk a single +//! pass, never a step backward. /// A piecewise-affine old→new frame map, identity outside its knots. Built /// once per edit and read many times; `Clone` for the tests that pin its /// laws, `Debug` for the messages those tests print when they fail. #[derive(Clone, Debug, PartialEq)] pub(crate) struct TimelineMap { - /// Knot points, sorted by `old`, strictly increasing on both axes (the - /// constructor refuses anything else: a map that folded time back on - /// itself would be a map nothing downstream could invert). + /// Knot points, sorted by `old`, strictly increasing on `old` and + /// non-decreasing on `new` -- a collapse ties two knots' `new` value + /// together on purpose, the span it drops entering one new frame. knots: Vec<(u32, u32)>, } @@ -35,20 +42,35 @@ impl TimelineMap { /// One non-identity piece -- `[old_lo, old_hi)` maps onto `[new_lo, /// new_hi)` -- over an identity base. The whole of a compression (a - /// re-rate of a member); a removal is `new` collapsed onto its own lo, - /// and an insertion is that run backwards. Degenerate spans (an empty - /// old, an inverted one) come out identity, because there is nothing in - /// them to move. + /// re-rate of a member); a removal is `new` collapsed onto its own lo + /// (`new_lo == new_hi`, a literal point); an insertion is that run + /// backwards; and a piece whose `new_lo` lands before `old_lo` (a + /// slow-down pulling a member's head earlier than its own old frame) + /// gets an extra anchor knot collapsing the run between the two los + /// onto `new_lo`, so the piece stays non-decreasing instead of doubling + /// back on the identity run before it. An empty old span (`old_lo >= + /// old_hi`) comes out identity, because there is nothing in it to move; + /// an inverted new span clamps its hi up to its lo, the collapse it means. pub(crate) fn piece(old: (u32, u32), new: (u32, u32)) -> Self { - if old.0 >= old.1 || new.0 >= new.1 { + if old.0 >= old.1 { return Self::identity(); } - Self { - // Half-open boundary knots: interpolation runs between them and - // the frame *at* the hi knot is the piece's own to give back - // (the caller clamps inside), never a step past its new end. - knots: vec![(old.0, new.0), (old.1, new.1)], + let new = (new.0, new.1.max(new.0)); + let mut knots = Vec::with_capacity(3); + // The anchor: without it, frames below `old_lo` stay identity and + // land ON the piece's own new_lo image the moment old_lo is + // crossed -- a decreasing step, the non-monotone bug a shift-left + // piece used to be. Folding the run into the same collapse a + // removal already does keeps `apply` a single non-decreasing walk. + if new.0 < old.0 { + knots.push((new.0, new.0)); } + // Half-open boundary knots: interpolation runs between them and + // the frame *at* the hi knot is the piece's own to give back + // (the caller clamps inside), never a step past its new end. + knots.push((old.0, new.0)); + knots.push((old.1, new.1)); + Self { knots } } /// Where `frame` lands. Affine between knots, identity outside them, @@ -181,32 +203,53 @@ mod tests { assert_eq!(m.shifted_total(), -99); } - /// A SHIFTED piece -- one whose new span starts before its old one, the - /// shape a removal cut into the head of the timeline gives -- is the case - /// `invert`'s below-the-knot run exists for: identity before the piece, - /// exact on the knots, image-total through the shift, and the frames the - /// shift took below the piece's old lo their own inverses. - #[test] - fn a_shifted_piece_inverts_on_its_own_run() { - let m = TimelineMap::piece((30, 60), (15, 30)); - assert_eq!(m.apply(0), 0, "identity before the piece"); - assert_eq!(m.apply(29), 29); - assert_eq!(m.apply(30), 15, "the lo knot is exact"); - assert_eq!(m.apply(59), 29, "the last frame of the piece"); - assert_eq!(m.apply(60), 30, "identity at the shift the piece left"); - assert_eq!(m.shifted_total(), -30); - for n in 0..60 { + /// The laws every piece keeps, whatever shape cut it: `apply` walking + /// old frames up never goes back down, and every new frame's own + /// inverse answers back exactly through `apply` again -- the right + /// inverse a many-to-one piece (a compression, a collapse, the run a + /// shift folds into its own anchor) can still give, even where the + /// left inverse can't (many old frames landing on one new frame means + /// only one of them gets its own value back). Old frames strictly + /// below `min(old_lo, new_lo)`, and at or past `old_hi`, sit outside + /// the piece's own collapse -- pure identity or a pure shift, both + /// exact bijections -- so those get the stronger left-inverse check. + fn assert_piece_laws(old: (u32, u32), new: (u32, u32), span: u32) { + let m = TimelineMap::piece(old, new); + let mut last = 0; + // The image `apply` actually produces over the span -- an + // expansion skips new frames between old ones (no old frame answers + // them), so the right-inverse law is only fair to ask of frames + // `apply` really lands on, never the whole new axis blind. + let mut image = std::collections::BTreeSet::new(); + for f in 0..span { + let now = m.apply(f); + assert!(now >= last, "{old:?}->{new:?}: frame {f} went back to {now} from {last}"); + image.insert(now); + last = now; + } + for n in image { let back = m.invert(n).expect("the image is total"); - assert_eq!(m.apply(back), n, "new frame {n} round trips"); + assert_eq!(m.apply(back), n, "{old:?}->{new:?}: new frame {n} round trips"); } - // ...and the run the shift opened below the piece's old lo is - // identity in both directions -- the frames `invert` used to walk - // into the piece's own lerp from the wrong end. - for f in 0..15 { - assert_eq!(m.invert(f), Some(f), "frame {f} is its own inverse"); + let lo = old.0.min(new.0); + for f in (0..lo).chain(old.1 + 1..span) { + assert_eq!(m.invert(m.apply(f)), Some(f), "{old:?}->{new:?}: frame {f} outside the piece round trips"); } } + /// Every piece shape the engine cuts, run through the laws above: an + /// expansion, a compression, a shift-left (the shape a slow-down's + /// caption overhang gives, and the one that used to go non-monotone), + /// a shift-right, and a collapse to a literal point. + #[test] + fn a_piece_holds_its_laws_on_every_shape() { + assert_piece_laws((100, 200), (100, 300), 500); + assert_piece_laws((100, 200), (100, 150), 500); + assert_piece_laws((30, 60), (15, 30), 200); + assert_piece_laws((30, 60), (45, 90), 200); + assert_piece_laws((100, 200), (100, 100), 500); + } + /// Identity is the base: everything maps to itself, in both directions, /// and the timeline's length never moved. #[test]