diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 4a4cc65..9173dd2 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -103,7 +103,7 @@ use crate::color::{ColorKey, MAX_COLOR_KEYS, evaluate_color}; use crate::transform::{ KeyEase, Keyed, MAX_TRANSFORM_KEYS, OpacityKey, TransformKey, TransformParams, ease_shape, evaluate as keyed_transform, evaluate_opacity, keys_after_still_head, keys_within, - sample, + with_cut_key, }; /// How far down a lane's own volume goes ([`Project::set_lane_gain_db`]). @@ -1228,21 +1228,12 @@ fn rekey_trimmed(c: &mut Clip, old: &Clip, edge: Edge, still: bool) { /// half keeps the keys before the cut and the right half the keys after it, /// and both get a key at `cut` holding the placement the whole animation gives /// there, so each half still moves exactly as the clip did over its own frames. -/// The cut key takes the easing of the segment it sits in. Empty in, empty out. +/// A cut inside an eased segment splits its ease between the two halves +/// ([`with_cut_key`]), so the motion keeps its exact shape. Empty in, empty out. pub fn split_transform_keys(keys: &[K], cut: u32) -> (Vec, Vec) { - let Some(at) = sample(keys, f64::from(cut)) else { - return (Vec::new(), Vec::new()); - }; - let ease = keys - .iter() - .rev() - .find(|k| k.at() <= cut) - .map_or(KeyEase::Linear, |k| k.ease()); - let boundary = at.placed(cut, ease); - let mut left: Vec = keys.iter().copied().filter(|k| k.at() < cut).collect(); - left.push(boundary); - let mut right = vec![boundary]; - right.extend(keys.iter().copied().filter(|k| k.at() > cut)); + let list = with_cut_key(keys, cut); + let left: Vec = list.iter().copied().filter(|k| k.at() <= cut).collect(); + let right: Vec = list.iter().copied().filter(|k| k.at() >= cut).collect(); (left, right) } diff --git a/crates/engine/src/transform.rs b/crates/engine/src/transform.rs index 6f54e2a..9bb9c39 100644 --- a/crates/engine/src/transform.rs +++ b/crates/engine/src/transform.rs @@ -275,36 +275,39 @@ pub fn ease_shape(ease: KeyEase, t: f32) -> f32 { } } -/// The height of a custom curve where its horizontal position is `t`. The -/// curve's parameter is found by a few Newton steps, with bisection as the -/// fallback when the slope is too flat for Newton. -fn bezier_height(b: Bezier, t: f32) -> f32 { +/// One coordinate of the cubic from `0` to `1` whose inner control points are +/// `p1` and `p2`, at curve parameter `s`. +fn cubic_coord(p1: f32, p2: f32, s: f32) -> f32 { + let u = 1.0 - s; + 3.0 * u * u * s * p1 + 3.0 * u * s * s * p2 + s * s * s +} + +/// The slope of [`cubic_coord`] with respect to `s`. +fn cubic_slope(p1: f32, p2: f32, s: f32) -> f32 { + let u = 1.0 - s; + 3.0 * u * u * p1 + 6.0 * u * s * (p2 - p1) + 3.0 * s * s * (1.0 - p2) +} + +/// The curve parameter `s` at which a cubic with horizontal inner controls +/// `x1` and `x2` is at horizontal position `t` (`0` at `t <= 0`, `1` at +/// `t >= 1`). A few Newton steps find it, with bisection as the fallback when +/// the slope is too flat for Newton. +fn curve_param(x1: f32, x2: f32, t: f32) -> f32 { if t <= 0.0 { return 0.0; } if t >= 1.0 { return 1.0; } - let [x1, y1, x2, y2] = b.points(); - // One coordinate of the cubic from 0 to 1 with control points `p1` and `p2`, at parameter `s`. - let coord = |p1: f32, p2: f32, s: f32| { - let u = 1.0 - s; - 3.0 * u * u * s * p1 + 3.0 * u * s * s * p2 + s * s * s - }; - // The slope of `coord` with respect to `s`. - let slope = |p1: f32, p2: f32, s: f32| { - let u = 1.0 - s; - 3.0 * u * u * p1 + 6.0 * u * s * (p2 - p1) + 3.0 * s * s * (1.0 - p2) - }; let mut s = t; let mut found = false; for _ in 0..8 { - let err = coord(x1, x2, s) - t; + let err = cubic_coord(x1, x2, s) - t; if err.abs() < 1e-6 { found = true; break; } - let d = slope(x1, x2, s); + let d = cubic_slope(x1, x2, s); if d.abs() < 1e-6 { break; } @@ -314,14 +317,80 @@ fn bezier_height(b: Bezier, t: f32) -> f32 { let (mut lo, mut hi) = (0.0f32, 1.0f32); for _ in 0..32 { s = 0.5 * (lo + hi); - if coord(x1, x2, s) < t { + if cubic_coord(x1, x2, s) < t { lo = s; } else { hi = s; } } } - coord(y1, y2, s) + s +} + +/// The height of a custom curve where its horizontal position is `t`. +fn bezier_height(b: Bezier, t: f32) -> f32 { + if t <= 0.0 { + return 0.0; + } + if t >= 1.0 { + return 1.0; + } + let [x1, y1, x2, y2] = b.points(); + cubic_coord(y1, y2, curve_param(x1, x2, t)) +} + +/// The eases of the two halves of a segment eased by `ease` when it is cut +/// at fraction `t` of its way. The left half runs from the segment's start to +/// the cut and the right half from the cut to its end, and each half moves +/// exactly as the whole segment did over its own frames. Linear and hold are +/// their own halves. Any other ease is split as the cubic bezier it is: de +/// Casteljau at the parameter where its horizontal position is `t`, then each +/// half normalised back to the unit square. Control points are rounded to +/// thousandths by [`Bezier::new`], so each half matches the original to about +/// `1e-3` of the segment's span. A half whose value does not change takes +/// [`KeyEase::Linear`], since every ease moves it the same way. A `t` at or +/// beyond an end leaves that empty half [`KeyEase::Linear`] too. +pub fn split_ease(ease: KeyEase, t: f32) -> (KeyEase, KeyEase) { + let [x1, y1, x2, y2] = match ease { + KeyEase::Linear | KeyEase::Hold => return (ease, ease), + KeyEase::Ease => [1.0 / 3.0, 0.0, 2.0 / 3.0, 1.0], + KeyEase::EaseIn => [1.0 / 3.0, 0.0, 2.0 / 3.0, 0.0], + KeyEase::EaseOut => [1.0 / 3.0, 1.0, 2.0 / 3.0, 1.0], + KeyEase::Bezier(b) => b.points(), + }; + if t <= 0.0 { + return (KeyEase::Linear, ease); + } + if t >= 1.0 { + return (ease, KeyEase::Linear); + } + // A point of the segment, de Casteljau style, between `a` and `b` at `u`. + let mix = |a: (f32, f32), b: (f32, f32), u: f32| (a.0 + (b.0 - a.0) * u, a.1 + (b.1 - a.1) * u); + let s = curve_param(x1, x2, t); + let q0 = mix((0.0, 0.0), (x1, y1), s); + let q1 = mix((x1, y1), (x2, y2), s); + let q2 = mix((x2, y2), (1.0, 1.0), s); + let r0 = mix(q0, q1, s); + let r1 = mix(q1, q2, s); + let yt = mix(r0, r1, s).1; + const FLAT: f32 = 1e-6; + let left = if yt <= FLAT { + KeyEase::Linear + } else { + let c = |p: (f32, f32)| (p.0 / t, p.1 / yt); + let (c1, c2) = (c(q0), c(r0)); + Bezier::new(c1.0.clamp(0.0, 1.0), c1.1.clamp(0.0, 1.0), c2.0.clamp(0.0, 1.0), c2.1.clamp(0.0, 1.0)) + .map_or(KeyEase::Linear, KeyEase::Bezier) + }; + let right = if 1.0 - yt <= FLAT { + KeyEase::Linear + } else { + let c = |p: (f32, f32)| ((p.0 - t) / (1.0 - t), (p.1 - yt) / (1.0 - yt)); + let (c1, c2) = (c(r1), c(q2)); + Bezier::new(c1.0.clamp(0.0, 1.0), c1.1.clamp(0.0, 1.0), c2.0.clamp(0.0, 1.0), c2.1.clamp(0.0, 1.0)) + .map_or(KeyEase::Linear, KeyEase::Bezier) + }; + (left, right) } /// One point of a clip's transform animation ([`crate::project::Clip::transform_keys`]): @@ -464,29 +533,50 @@ pub fn evaluate_opacity(base: f32, keys: &[OpacityKey], src: f64) -> f32 { /// there is a key at the window's edge holding the placement the whole /// animation gives there, so the visible part moves exactly as it did. An /// edge with nothing dropped gets no key: the animation already holds there. -/// Empty in, empty out. +/// An edge key cuts the eased segment it sits in, so the kept part of that +/// segment keeps its exact shape (see [`with_cut_key`]). Empty in, empty out. pub fn keys_within(keys: &[K], lo: u32, hi: u32) -> Vec { if keys.is_empty() || lo > hi { return Vec::new(); } - // The easing of the segment the edge sits in, as a split takes it (`split_transform_keys`). - let edge_key = |src: u32| { - sample(keys, f64::from(src)).expect("keyed, so a value").placed( - src, - keys.iter() - .rev() - .find(|k| k.at() <= src) - .map_or(KeyEase::Linear, |k| k.ease()), - ) - }; - let mut out = Vec::new(); + let mut list = keys.to_vec(); if keys.iter().any(|k| k.at() < lo) && !keys.iter().any(|k| k.at() == lo) { - out.push(edge_key(lo)); + list = with_cut_key(&list, lo); } - out.extend(keys.iter().copied().filter(|k| (lo..=hi).contains(&k.at()))); if hi > lo && keys.iter().any(|k| k.at() > hi) && !keys.iter().any(|k| k.at() == hi) { - out.push(edge_key(hi)); + list = with_cut_key(&list, hi); } + list.into_iter().filter(|k| (lo..=hi).contains(&k.at())).collect() +} + +/// `keys` with a key at source frame `cut` holding the value the animation +/// gives there, so a cut through the animation loses nothing. Cut inside an +/// eased segment, the key takes the right half of that segment's ease and the +/// earlier key takes the left half ([`split_ease`]), so each side keeps the +/// exact shape the whole segment had. Cut outside the segments, the key takes +/// the ease of the key before it, as a plain hold would. `keys` unchanged if a +/// key already sits at `cut`. Empty in, empty out. +pub fn with_cut_key(keys: &[K], cut: u32) -> Vec { + let Some(at) = sample(keys, f64::from(cut)) else { + return Vec::new(); + }; + let i = keys.partition_point(|k| k.at() < cut); + if keys.get(i).is_some_and(|k| k.at() == cut) { + return keys.to_vec(); + } + let mut out = keys.to_vec(); + let ease = if i > 0 && i < keys.len() { + let (a, b) = (keys[i - 1], keys[i]); + let t = ((f64::from(cut) - f64::from(a.at())) / f64::from(b.at() - a.at())) as f32; + let (left, right) = split_ease(a.ease(), t); + out[i - 1] = a.placed(a.at(), left); + right + } else if i > 0 { + keys[i - 1].ease() + } else { + KeyEase::Linear + }; + out.insert(i, at.placed(cut, ease)); out } @@ -904,11 +994,29 @@ mod tests { let keys = [ok(0, 0.0, KeyEase::EaseIn), ok(100, 1.0, KeyEase::Linear)]; let within = keys_within(&keys, 20, 80); assert_eq!(within.iter().map(|k| k.at).collect::>(), vec![20, 80]); - assert_eq!(within[0].ease, KeyEase::EaseIn, "an edge key keeps the segment's ease"); + assert!( + matches!(within[0].ease, KeyEase::Bezier(_)), + "an edge key takes the right half of the cut segment's ease" + ); let want = evaluate_opacity(1.0, &keys, 20.0); assert!((within[0].opacity - want).abs() < 1e-7); } + #[test] + fn a_cut_through_an_ease_in_keeps_the_motion() { + use crate::project::split_transform_keys; + let ok = |at, opacity, ease| OpacityKey { at, opacity, ease }; + // An ease-in from 0 to 100 over frames 0..100, cut at frame 40. + let keys = [ok(0, 0.0, KeyEase::EaseIn), ok(100, 100.0, KeyEase::Linear)]; + let (left, right) = split_transform_keys(&keys, 40); + for f in [20.0, 40.0, 70.0] { + let half = if f <= 40.0 { &left } else { &right }; + let got = sample(half, f).expect("keyed").opacity; + let want = sample(&keys, f).expect("keyed").opacity; + assert!((got - want).abs() < 0.5, "frame {f}: {got} against {want}"); + } + } + #[test] fn corner_offset_matches_the_documented_examples() { // 16:9 on 16:9 at scale 0.3: half-width 0.15, half-height 0.15. The 0.03