alpha2-eval 0.3.1

Pure alpha2 timeline evaluation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use crate::EvaluatedVisual;
use alpha2_model::{
    Animated, AnimatedPoint, EffectValue, GradientStop, Keyframe, Paint, Path, PointKeyframe, Vec2,
    VisualProperties,
};
use alpha2_time::MediaTime;

/// Evaluates visual properties at a timestamp: a clip's at timeline time,
/// a transform node's at composition time. `aspect` is the width over the
/// height of the space the position is normalized in (the canvas), in which
/// a position moving along a spatial curve is measured.
///
/// An overshooting timing curve can carry a value past its keyframes. Opacity
/// is clamped to its domain, `0..=1`; position, scale, and rotation have no
/// bounds and keep the overshoot, as far as a finite `f32` goes.
#[must_use]
pub fn sample_visual(
    properties: &VisualProperties,
    time: MediaTime,
    aspect: f64,
) -> EvaluatedVisual {
    EvaluatedVisual {
        position: sample_point(&properties.position, time, aspect),
        anchor: properties.anchor,
        scale: evaluate_vec2(&properties.scale, time),
        rotation_degrees: evaluate_scalar(&properties.rotation_degrees, time),
        opacity: evaluate_scalar(&properties.opacity, time).clamp(0.0, 1.0),
    }
}

fn evaluate_vec2(animation: &Animated<Vec2>, time: MediaTime) -> Vec2 {
    match animation {
        Animated::Constant { value } => *value,
        Animated::Keyframes { keyframes } => match segment(keyframes, time) {
            Segment::Between(left, right, amount) => Vec2::new(
                interpolate_f32(left.value.x, right.value.x, amount),
                interpolate_f32(left.value.y, right.value.y, amount),
            ),
            Segment::At(keyframe) => keyframe.value,
            Segment::Empty => Vec2::default(),
        },
    }
}

/// Samples one animated two-dimensional value with the project's keyframe
/// semantics; see [`sample_scalar`]. A position that may follow spatial
/// curves is sampled with [`sample_point`].
#[must_use]
pub fn sample_vec2(animation: &Animated<Vec2>, time: MediaTime) -> Vec2 {
    evaluate_vec2(animation, time)
}

/// Samples one animated scalar with the project's keyframe semantics.
///
/// Values outside the keyframe sequence clamp to its first or last value, and a
/// [`alpha2_model::Interpolation::Hold`] segment keeps its left value until the
/// next keyframe. Values are interpolated numerically, so a rotation from 0 to
/// 720 degrees turns twice. Audio mixing calls this at control points fixed
/// by the project sample, and on every sample around a keyframe, so that
/// automation does not depend on the host block size.
#[must_use]
pub fn sample_scalar(animation: &Animated<f32>, time: MediaTime) -> f32 {
    evaluate_scalar(animation, time)
}

fn evaluate_scalar(animation: &Animated<f32>, time: MediaTime) -> f32 {
    match animation {
        Animated::Constant { value } => *value,
        Animated::Keyframes { keyframes } => match segment(keyframes, time) {
            Segment::Between(left, right, amount) => {
                interpolate_f32(left.value, right.value, amount)
            }
            Segment::At(keyframe) => keyframe.value,
            Segment::Empty => 0.0,
        },
    }
}

/// Samples a position that may move along spatial curves.
///
/// Between two keyframes whose facing tangents (the first's `out_tangent`,
/// the second's `in_tangent`) are both zero, the value moves in a straight
/// line exactly as an `Animated<Vec2>` does. Otherwise it travels along the
/// cubic Bezier through them, measured in a space `aspect` wide per unit
/// high, at the speed the timing curve sets: the curve's eased amount is the
/// fraction of its length travelled. An amount past either end continues
/// straight on along the curve's tangent there.
#[must_use]
pub fn sample_point(animation: &AnimatedPoint, time: MediaTime, aspect: f64) -> Vec2 {
    match animation {
        AnimatedPoint::Constant { value } => *value,
        AnimatedPoint::Keyframes { keyframes } => match point_segment(keyframes, time) {
            PointSegment::At(value) => value,
            PointSegment::Between(left, right, amount) => {
                if left.out_tangent.is_zero() && right.in_tangent.is_zero() {
                    return Vec2::new(
                        interpolate_f32(left.value.x, right.value.x, amount),
                        interpolate_f32(left.value.y, right.value.y, amount),
                    );
                }
                let aspect = if aspect.is_finite() && aspect > 0.0 {
                    aspect
                } else {
                    1.0
                };
                let point = |value: Vec2| {
                    alpha2_geometry::kurbo::Point::new(
                        f64::from(value.x) * aspect,
                        f64::from(value.y),
                    )
                };
                let vector = |value: Vec2| {
                    alpha2_geometry::kurbo::Vec2::new(
                        f64::from(value.x) * aspect,
                        f64::from(value.y),
                    )
                };
                let at = alpha2_geometry::spatial::point_along(
                    point(left.value),
                    vector(left.out_tangent),
                    vector(right.in_tangent),
                    point(right.value),
                    amount,
                );
                Vec2::new(finite_f32(at.x / aspect), finite_f32(at.y))
            }
        },
    }
}

/// Where a timestamp falls in a position's keyframes.
enum PointSegment<'a> {
    At(Vec2),
    Between(&'a PointKeyframe, &'a PointKeyframe, f64),
}

fn point_segment(keyframes: &[PointKeyframe], time: MediaTime) -> PointSegment<'_> {
    let next = keyframes.partition_point(|keyframe| keyframe.time <= time);
    if next == 0 {
        return PointSegment::At(
            keyframes
                .first()
                .map_or_else(Vec2::default, |key| key.value),
        );
    }
    let left = &keyframes[next - 1];
    let Some(right) = keyframes.get(next) else {
        return PointSegment::At(left.value);
    };
    if time == left.time {
        return PointSegment::At(left.value);
    }
    let progress = interpolation_amount(left.time, right.time, time);
    match left.interpolation.amount(progress) {
        Some(amount) => PointSegment::Between(left, right, amount),
        None => PointSegment::At(left.value),
    }
}

/// `value` as the nearest finite `f32`; NaN becomes zero.
#[allow(clippy::cast_possible_truncation)]
fn finite_f32(value: f64) -> f32 {
    if value.is_nan() {
        0.0
    } else {
        value.clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32
    }
}

/// Samples an animated path: every point and tangent moves along a straight
/// line between keyframes, in `f64`. Validation keeps the keyframes alike in
/// structure; were they not, the left keyframe's path is held.
#[must_use]
pub fn sample_path(animation: &Animated<Path>, time: MediaTime) -> alpha2_geometry::EvaluatedPath {
    use alpha2_geometry::kurbo::{Point, Vec2 as Vector};
    use alpha2_geometry::{EvaluatedPath, EvaluatedSubpath, EvaluatedVertex};
    match animation {
        Animated::Constant { value } => EvaluatedPath::from(value),
        Animated::Keyframes { keyframes } => match segment(keyframes, time) {
            Segment::At(keyframe) => EvaluatedPath::from(&keyframe.value),
            Segment::Empty => EvaluatedPath::default(),
            Segment::Between(left, right, amount) => {
                let structure = |path: &Path| {
                    path.subpaths
                        .iter()
                        .map(|subpath| (subpath.closed, subpath.vertices.len()))
                        .collect::<Vec<_>>()
                };
                if structure(&left.value) != structure(&right.value) {
                    return EvaluatedPath::from(&left.value);
                }
                let lerp = |a: f32, b: f32| f64::from(a) + (f64::from(b) - f64::from(a)) * amount;
                let point = |a: Vec2, b: Vec2| Point::new(lerp(a.x, b.x), lerp(a.y, b.y));
                let vector = |a: Vec2, b: Vec2| Vector::new(lerp(a.x, b.x), lerp(a.y, b.y));
                EvaluatedPath {
                    subpaths: left
                        .value
                        .subpaths
                        .iter()
                        .zip(&right.value.subpaths)
                        .map(|(from, to)| EvaluatedSubpath {
                            closed: from.closed,
                            vertices: from
                                .vertices
                                .iter()
                                .zip(&to.vertices)
                                .map(|(a, b)| EvaluatedVertex {
                                    point: point(a.point, b.point),
                                    in_tangent: vector(a.in_tangent, b.in_tangent),
                                    out_tangent: vector(a.out_tangent, b.out_tangent),
                                })
                                .collect(),
                        })
                        .collect(),
                }
            }
        },
    }
}

/// Samples an animated paint. Colors interpolate premultiplied, as effect
/// colors do, and gradient points, radii, angles, and stop offsets
/// numerically; offsets are then kept in order within `0..=1` and a radius
/// positive, as an overshooting curve could carry them past. Validation keeps
/// the keyframes alike in kind, stops, and spread; were they not, the left
/// keyframe's paint is held.
#[must_use]
pub fn sample_paint(animation: &Animated<Paint>, time: MediaTime) -> Paint {
    match animation {
        Animated::Constant { value } => value.clone(),
        Animated::Keyframes { keyframes } => match segment(keyframes, time) {
            Segment::At(keyframe) => keyframe.value.clone(),
            Segment::Empty => Paint::Solid {
                color: alpha2_model::Rgba::new(0.0, 0.0, 0.0, 0.0),
            },
            Segment::Between(left, right, amount) => {
                interpolate_paint(&left.value, &right.value, amount)
                    .unwrap_or_else(|| left.value.clone())
            }
        },
    }
}

fn interpolate_paint(from: &Paint, to: &Paint, amount: f64) -> Option<Paint> {
    let lerp = |a: f32, b: f32| interpolate_f32(a, b, amount);
    let point = |a: Vec2, b: Vec2| Vec2::new(lerp(a.x, b.x), lerp(a.y, b.y));
    let color = |a: alpha2_model::Rgba, b: alpha2_model::Rgba| {
        let [red, green, blue, alpha] = interpolate_color(
            [a.red, a.green, a.blue, a.alpha],
            [b.red, b.green, b.blue, b.alpha],
            amount,
        );
        alpha2_model::Rgba::new(red, green, blue, alpha)
    };
    let stops = |a: &[GradientStop], b: &[GradientStop]| -> Option<Vec<GradientStop>> {
        if a.len() != b.len() {
            return None;
        }
        let mut previous = 0.0_f32;
        Some(
            a.iter()
                .zip(b)
                .map(|(a, b)| {
                    let offset = lerp(a.offset, b.offset).clamp(previous, 1.0);
                    previous = offset;
                    GradientStop {
                        offset,
                        color: color(a.color, b.color),
                    }
                })
                .collect(),
        )
    };
    Some(match (from, to) {
        (Paint::Solid { color: a }, Paint::Solid { color: b }) => Paint::Solid {
            color: color(*a, *b),
        },
        (
            Paint::LinearGradient {
                start: s0,
                end: e0,
                stops: a,
                spread,
            },
            Paint::LinearGradient {
                start: s1,
                end: e1,
                stops: b,
                spread: other,
            },
        ) if spread == other => Paint::LinearGradient {
            start: point(*s0, *s1),
            end: point(*e0, *e1),
            stops: stops(a, b)?,
            spread: *spread,
        },
        (
            Paint::RadialGradient {
                center: c0,
                radius: r0,
                stops: a,
                spread,
            },
            Paint::RadialGradient {
                center: c1,
                radius: r1,
                stops: b,
                spread: other,
            },
        ) if spread == other => Paint::RadialGradient {
            center: point(*c0, *c1),
            radius: lerp(*r0, *r1).max(f32::MIN_POSITIVE),
            stops: stops(a, b)?,
            spread: *spread,
        },
        (
            Paint::ConicGradient {
                center: c0,
                angle_degrees: a0,
                stops: a,
            },
            Paint::ConicGradient {
                center: c1,
                angle_degrees: a1,
                stops: b,
            },
        ) => Paint::ConicGradient {
            center: point(*c0, *c1),
            angle_degrees: lerp(*a0, *a1),
            stops: stops(a, b)?,
        },
        _ => return None,
    })
}

/// One effect parameter's value at a timeline timestamp.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct EffectParamValue {
    /// The value at this timestamp.
    pub value: EffectValue,
    /// Whether `value` lies between two keyframes rather than being an
    /// authored value. A timing curve may carry an interpolated value past
    /// the parameter's range, and a renderer clamps it there; an authored
    /// value outside the range is an error.
    pub interpolated: bool,
}

/// Samples one effect parameter with the project's keyframe semantics.
///
/// `Float` and `Vec2` interpolate component-wise. A `Color` (straight,
/// sRGB-encoded) interpolates premultiplied, as CSS colors do, so a fade from
/// transparent to red does not pass through a dark fringe; it is clamped to
/// `0..=1` after an overshooting curve. `Enum` and `Bool` keyframes hold
/// (validation rejects any other interpolation for them).
#[must_use]
pub fn sample_effect_param(animation: &Animated<EffectValue>, time: MediaTime) -> EffectParamValue {
    let authored = |value: EffectValue| EffectParamValue {
        value,
        interpolated: false,
    };
    match animation {
        Animated::Constant { value } => authored(*value),
        Animated::Keyframes { keyframes } => match segment(keyframes, time) {
            Segment::Between(left, right, amount) => {
                interpolate_value(left.value, right.value, amount).map_or_else(
                    || authored(left.value),
                    |value| EffectParamValue {
                        value,
                        interpolated: true,
                    },
                )
            }
            Segment::At(keyframe) => authored(keyframe.value),
            Segment::Empty => authored(EffectValue::Float(0.0)),
        },
    }
}

fn interpolate_value(start: EffectValue, end: EffectValue, amount: f64) -> Option<EffectValue> {
    let lerp = |a: f32, b: f32| interpolate_f32(a, b, amount);
    match (start, end) {
        (EffectValue::Float(a), EffectValue::Float(b)) => Some(EffectValue::Float(lerp(a, b))),
        (EffectValue::Vec2(a), EffectValue::Vec2(b)) => {
            Some(EffectValue::Vec2([lerp(a[0], b[0]), lerp(a[1], b[1])]))
        }
        (EffectValue::Vec4(a), EffectValue::Vec4(b)) => {
            Some(EffectValue::Vec4(std::array::from_fn(|index| {
                lerp(a[index], b[index])
            })))
        }
        (EffectValue::Color(a), EffectValue::Color(b)) => {
            Some(EffectValue::Color(interpolate_color(a, b, amount)))
        }
        _ => None,
    }
}

/// Interpolates two straight colors premultiplied and returns the result
/// straight, clamped to `0..=1`.
#[allow(clippy::cast_possible_truncation)]
fn interpolate_color(start: [f32; 4], end: [f32; 4], amount: f64) -> [f32; 4] {
    let (start_alpha, end_alpha) = (f64::from(start[3]), f64::from(end[3]));
    let alpha = (start_alpha + (end_alpha - start_alpha) * amount).clamp(0.0, 1.0);
    let mut color = [0.0; 4];
    for index in 0..3 {
        let from = f64::from(start[index]) * start_alpha;
        let to = f64::from(end[index]) * end_alpha;
        let premultiplied = from + (to - from) * amount;
        // Fully transparent has no color; keep the straight interpolation so the value stays
        // continuous.
        let straight = if alpha > 0.0 {
            premultiplied / alpha
        } else {
            f64::from(start[index]) + (f64::from(end[index]) - f64::from(start[index])) * amount
        };
        color[index] = straight.clamp(0.0, 1.0) as f32;
    }
    color[3] = alpha as f32;
    color
}

/// Where a timestamp falls in a keyframe sequence.
enum Segment<'a, T> {
    /// Before the first, after the last, or inside a held segment.
    At(&'a Keyframe<T>),
    /// Inside an interpolated segment, this fraction of the way to the right.
    Between(&'a Keyframe<T>, &'a Keyframe<T>, f64),
    /// No keyframes; validation rejects this.
    Empty,
}

fn segment<T>(keyframes: &[Keyframe<T>], time: MediaTime) -> Segment<'_, T> {
    let next = keyframes.partition_point(|keyframe| keyframe.time <= time);
    if next == 0 {
        return keyframes.first().map_or(Segment::Empty, Segment::At);
    }
    let left = &keyframes[next - 1];
    let Some(right) = keyframes.get(next) else {
        return Segment::At(left);
    };
    // On a keyframe the value is the authored one, whatever the curve.
    if time == left.time {
        return Segment::At(left);
    }
    let progress = interpolation_amount(left.time, right.time, time);
    match left.interpolation.amount(progress) {
        Some(amount) => Segment::Between(left, right, amount),
        None => Segment::At(left),
    }
}

#[allow(clippy::cast_precision_loss)]
fn interpolation_amount(start: MediaTime, end: MediaTime, time: MediaTime) -> f64 {
    (i128::from(time.ticks()) - i128::from(start.ticks())) as f64
        / (i128::from(end.ticks()) - i128::from(start.ticks())) as f64
}

/// The value `amount` of the way from `start` to `end`, held to the finite
/// `f32` values: an overshooting curve can carry it past what an `f32` holds.
#[allow(clippy::cast_possible_truncation)]
fn interpolate_f32(start: f32, end: f32, amount: f64) -> f32 {
    (f64::from(start) + (f64::from(end) - f64::from(start)) * amount)
        .clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32
}