diff --git a/crates/engine/src/map.rs b/crates/engine/src/map.rs index 89a5dbf..9957015 100644 --- a/crates/engine/src/map.rs +++ b/crates/engine/src/map.rs @@ -87,8 +87,16 @@ impl TimelineMap { if self.knots.is_empty() { return Some(frame); } - // Before the first knot the map was the identity on `[0, old_lo)`, - // which on the new axis is the straight run from 0 to the knot. + // Below the first knot the map is identity on the old axis *up to + // the piece's old lo*, which on the new axis is the run from 0 to + // the piece's new lo. A SHIFTED piece (new lo < old lo) puts part of + // that run inside the piece's own destination, so the lerp through + // the knot is right; an unshifted one has new lo == old lo and the + // lerp degenerates to identity -- either way the single run covers + // it, and a frame below min(old lo, new lo) is identity outright. + if frame < self.knots[0].0.min(self.knots[0].1) { + return Some(frame); + } if frame < self.knots[0].1 { return Some(lerp((0, 0), self.knots[0], frame)); } @@ -173,6 +181,32 @@ 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 { + let back = m.invert(n).expect("the image is total"); + assert_eq!(m.apply(back), n, "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"); + } + } + /// Identity is the base: everything maps to itself, in both directions, /// and the timeline's length never moved. #[test] diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index b83925e..870336e 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -4092,12 +4092,13 @@ impl Project { /// each member by its **own** span and on its **own** lane -- the members /// keep their offsets, so their spans may disagree, and the lanes may end /// up out of step with each other, which is inherent to a hand-built group - /// and one undo away. The caption members lift rather than ripple, for the - /// reason a caption's own delete always lifted: a subtitle lane never - /// slides, or the words after the cut would stop matching the ones before - /// it. One snapshot for the whole group, so one [`Project::undo`] puts it - /// back. `false` for a bad index, and for a lane that is not there. - /// Changes the mapping: the caller must reseek. + /// and one undo away. Every member -- clips and captions alike -- goes by + /// the same law: its own span out of its own lane, hole closed; a caption + /// in no group keeps the lift that leaves a gap, which is its own door's + /// law ([`delete_sub_in`](Project::delete_sub_in)). One snapshot for the + /// whole group, so one [`Project::undo`] puts it back. `false` for a bad + /// index, and for a lane that is not there. Changes the mapping: the + /// caller must reseek. pub fn delete_in(&mut self, lane: Lane, idx: usize) -> bool { let Some(members) = self.group_of(lane, idx) else { return false; @@ -4127,11 +4128,11 @@ impl Project { self.lift_sub(lane, idx) } - /// The one whole-group delete both doors land in: every media member's own - /// span cut out of its own lane and the hole closed there -- and nothing - /// else on another lane moves, which is what keeps one member's ripple from - /// dragging another member's lane out from under it -- and every caption - /// member lifted. One snapshot, one undo step. + /// The one whole-group delete both doors land in: every member's own span + /// cut out of its own lane and the hole closed there -- clips and + /// captions by the same law -- and nothing else on another lane moves, + /// which is what keeps one member's ripple from dragging another + /// member's lane out from under it. One snapshot, one undo step. fn delete_members(&mut self, members: &Members) -> bool { self.snapshot(); for &(l, i) in &members.clips { @@ -5680,11 +5681,11 @@ mod tests { assert_eq!(p.sub_lane(s1)[0].link, Some(take)); } - /// Deleting a member of a hand-built group takes the group: each media - /// member's own span out of its own lane, the captions lifted -- the lanes - /// may end up out of step, which is what one undo restores together. + /// Deleting a member of a hand-built group takes the group: every + /// member's own span out of its own lane, hole closed -- the lanes may + /// end up out of step, which is what one undo restores together. #[test] - fn deleting_a_grouped_clip_ripples_each_lane_alone_and_lifts_the_captions() { + fn deleting_a_grouped_clip_ripples_every_members_own_lane() { let caption = |start: u32, frames: u32| SubClip { start, frames, @@ -5714,7 +5715,7 @@ mod tests { // By the picture or by the caption, the same whole-group delete. assert!(p.delete_sub_in(s1, 0)); assert!(p.lane(Lane::V1).is_empty(), "the take's span left V1"); - assert!(p.sub_lane(s1).is_empty(), "the caption lifted"); + assert!(p.sub_lane(s1).is_empty(), "the caption's span left its lane"); assert_eq!(p.lane(v2)[0].start, 10, "a lane the group does not touch stays"); assert!(links_are_consistent(&p.lanes).is_ok()); assert!(p.undo(), "one step puts the group back"); diff --git a/crates/engine/src/sweep.rs b/crates/engine/src/sweep.rs index c9b471e..ef25da0 100644 --- a/crates/engine/src/sweep.rs +++ b/crates/engine/src/sweep.rs @@ -175,15 +175,17 @@ fn the_sweep_speed_keeps_the_group_and_the_playhead_scene() { for (what, speed) in [("2x", Speed::from_permille(2000)), ("half", Speed::from_permille(500))] { let (mut p, _s1) = project(shape); let pre = p.parts(); - // The map the op will have applied, asked BEFORE the write (the - // old ends are what it maps through). let at = 45u32; let mapped = p.speeded_playhead(Lane::V1, 0, speed, at); let before = p.span_at(Lane::V1, at).and_then(|s| s.from); - let held_frames = p.lane(Lane::V1)[0].frames(); + // The map the write itself is a shape of, built from the + // member's own old geometry -- NOT from the playhead's answer, + // which is what it will be checked against. + let held = p.lane(Lane::V1)[0]; + let new_len = (f64::from(held.len()) / speed.as_f64()).round() as u32; let map = TimelineMap::piece( - (p.lane(Lane::V1)[0].start, p.lane(Lane::V1)[0].start + held_frames), - (p.lane(Lane::V1)[0].start, mapped.unwrap_or(at).max(p.lane(Lane::V1)[0].start)), + (held.start, held.start + held.frames()), + (held.start, held.start + new_len), ); if let Err(why) = p.set_speed(Lane::V1, 0, speed) { // A stretch the neighbours leave no room for is refused in @@ -197,8 +199,15 @@ fn the_sweep_speed_keeps_the_group_and_the_playhead_scene() { continue; } laws(&p, &format!("{shape}/{what} after speed")); - // Survivor continuity: the clip's own new end is the map of its - // old one, and the playhead's scene survived. + // Survivor continuity through the map: the clip's old span maps + // onto exactly the span it landed in -- the piece's own ends, on + // the lane, not inferred back from anything the write produced. + let landed = p.lane(Lane::V1)[0]; + assert_eq!( + (landed.start, landed.start + landed.frames()), + (map.apply(held.start), map.apply(held.start + held.frames())), + "{shape}/{what}: the clip's new span is the map of its old one" + ); // The scene law to the frame a rate can hold: an odd source // frame at 2x never lands on an integer playhead, so the // tolerance is the one frame the map's own rounding admits. @@ -212,7 +221,6 @@ fn the_sweep_speed_keeps_the_group_and_the_playhead_scene() { apart <= 1, "{shape}/{what}: the scene under the playhead, {before:?} vs {after:?}" ); - let _ = map; // the piece is pinned by the span math above undo_round_trip(p, pre, &format!("{shape}/{what} speed")); } }