alpha2-eval 0.10.0

Pure alpha2 timeline evaluation.
Documentation
use super::{segment_tick, wide, wide_ticks};
use crate::curves::{AutoTangent, CurveError, WideKey, between_keys, split_at, tangents_of};
use alpha2_model::{CubicBezier, Interpolation, TimeKeyframe, TimeMap};
use alpha2_time::MediaTime;

impl WideKey for TimeKeyframe {
    fn time(&self) -> MediaTime {
        self.time
    }

    fn value(&self) -> f64 {
        wide_ticks(self.source_time)
    }

    fn interpolation(&self) -> &Interpolation {
        &self.interpolation
    }

    fn set_interpolation(&mut self, interpolation: Interpolation) {
        self.interpolation = interpolation;
    }

    #[expect(
        clippy::cast_possible_truncation,
        reason = "a float to integer cast saturates"
    )]
    fn keyed(time: MediaTime, value: f64, interpolation: Interpolation) -> Self {
        Self {
            time,
            source_time: MediaTime(value as i64),
            interpolation,
        }
    }

    fn stored(value: f64) -> f64 {
        libm::floor(value)
    }

    fn shown(left: &Self, right: &Self, tick: i64) -> f64 {
        if tick >= right.time.ticks() {
            return right.value();
        }
        let elapsed = (i128::from(tick) - i128::from(left.time.ticks())).max(0);
        segment_tick(left, right, elapsed).map_or_else(|| left.value(), wide)
    }

    fn keeps_timing(curve: CubicBezier, start: f64, end: f64) -> bool {
        curve
            .turns()
            .into_iter()
            .flatten()
            .all(|turn| turn <= start || turn >= end)
    }
}

/// Adds a keyframe at `time`, inside the keyed range, to a time curve. The segment it falls in
/// is cut as [`crate::curves::insert_key`] cuts a number's, and the new keyframe holds the
/// source tick the curve shows at `time`. Between the keyframes the curve then names source
/// times within one tick of those it named, since a keyframe holds a whole tick; handles are
/// `f32` offsets, whole ticks up to 2^24 of them. A `time` with a keyframe changes nothing.
///
/// A `cubic_bezier` segment that turns back inside a part gives that part as a `value_bezier`.
///
/// # Errors
///
/// [`CurveError::NotKeyed`] for a map that is not a curve, [`CurveError::OutsideKeys`] for a
/// time before the first keyframe or after the last, whatever the curve does there, or
/// [`CurveError::TooManyKeys`].
pub fn insert_time_key(curve: &mut TimeMap, time: MediaTime) -> Result<(), CurveError> {
    let TimeMap::Curve { keyframes, .. } = curve else {
        return Err(CurveError::NotKeyed);
    };
    if keyframes
        .binary_search_by_key(&time, |key| key.time)
        .is_ok()
    {
        return Ok(());
    }
    let (index, _) = between_keys(keyframes.iter().map(|key| key.time), time)?;
    split_at(keyframes, index, time)
}

/// Writes value-curve handles, in ticks of source time, at the chosen keyframes of a time
/// curve (every keyframe when `keys` is `None`) from their neighbours, as
/// [`crate::curves::auto_tangents`] does for a number. [`AutoTangent::Monotone`] keeps a run
/// of rising keyframes rising and a falling run falling. Other time maps are left alone.
pub fn time_auto_tangents(curve: &mut TimeMap, mode: AutoTangent, keys: Option<&[MediaTime]>) {
    if let TimeMap::Curve { keyframes, .. } = curve {
        tangents_of(keyframes, mode, keys);
    }
}

#[cfg(test)]
#[path = "edits_tests.rs"]
mod tests;