motion-core 0.1.0

Backend-independent motion engine primitives
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Clock-free easing and value interpolation.

use std::{marker::PhantomData, sync::Arc};

use crate::{Duration, Point, Time, Transform, Vector2};

mod easing;
mod keyframes;
mod primitives;
mod spring;
#[cfg(test)]
mod tests;

pub use easing::Easing;
pub use keyframes::Keyframes;
pub use primitives::{
    Extrapolate, FollowPath, Noise, Oscillator, PathSample, Waveform, piecewise, remap, value_noise,
};
pub use spring::Spring;

const BEFORE_START: Time = Time::from_ticks(-1);

/// Values that can interpolate between endpoints. Progress may overshoot `0.0..=1.0`.
/// Implement this trait to sample custom values with [`Tween`] and [`Keyframes`].
/// Progress and geometry use double precision. f32 values retain their
/// own arithmetic; colors extrapolate Oklab and clamp only alpha.
pub trait Interpolate: Clone {
    /// Blends toward `target` with nominal progress zero at self and one at target.
    ///
    /// Custom implementations should be deterministic and handle overshoot
    /// intentionally. Built-in floats/pairs extrapolate; bounds preserve
    /// signed extrapolated dimensions. Colors extrapolate Oklab and clamp alpha.
    fn interpolate(&self, target: &Self, progress: f64) -> Self;
}

impl Interpolate for f32 {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        let progress = progress as f32;
        self * (1.0 - progress) + target * progress
    }
}

impl Interpolate for f64 {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        self * (1.0 - progress) + target * progress
    }
}

impl Interpolate for Point {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        Point::interpolate(self, target, progress)
    }
}

impl Interpolate for Vector2 {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        Vector2::interpolate(self, target, progress)
    }
}

impl Interpolate for Transform {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        Transform::interpolate(self, target, progress)
    }
}

impl<Value: Interpolate, const COUNT: usize> Interpolate for [Value; COUNT] {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        std::array::from_fn(|index| self[index].interpolate(&target[index], progress))
    }
}

impl Interpolate for crate::Color {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        crate::Color::interpolate(self, target, progress)
    }
}

impl Interpolate for crate::Size {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        Self::new(
            self.width.interpolate(&target.width, progress),
            self.height.interpolate(&target.height, progress),
        )
    }
}

impl Interpolate for crate::Rect {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        Self::new(
            self.left.interpolate(&target.left, progress),
            self.top.interpolate(&target.top, progress),
            self.right.interpolate(&target.right, progress),
            self.bottom.interpolate(&target.bottom, progress),
        )
    }
}

impl<First: Interpolate, Second: Interpolate> Interpolate for (First, Second) {
    fn interpolate(&self, target: &Self, progress: f64) -> Self {
        (
            self.0.interpolate(&target.0, progress),
            self.1.interpolate(&target.1, progress),
        )
    }
}

/// A thread-safe, randomly addressable animation of values.
///
/// Finite implementations hold their endpoint values outside `[0, duration]`.
/// Infinite implementations may use negative time. Sampling must not depend on
/// earlier calls. A reported duration must be nonnegative and stable.
pub trait Animation<Value>: Send + Sync {
    /// Samples without advancing a clock or changing playback state.
    fn sample(&self, time: Time) -> Value;

    /// Returns the finite duration, or `None` for an unbounded signal.
    fn duration(&self) -> Option<Duration> {
        None
    }

    /// Transforms values without changing timing.
    fn map<Output, Function>(self, function: Function) -> Map<Self, Function, Value>
    where
        Self: Sized,
        Function: Fn(Value) -> Output + Send + Sync,
    {
        Map {
            source: self,
            function,
            input: PhantomData,
        }
    }

    /// Samples in parallel; the duration is infinite if either input is infinite.
    fn zip<OtherValue, Other>(self, other: Other) -> Zip<Self, Other>
    where
        Self: Sized,
        Other: Animation<OtherValue>,
    {
        Zip {
            first: self,
            second: other,
        }
    }

    /// Delays a finite animation, holding its initial value before the offset.
    /// Infinite signals shift their signed time instead. Negative offsets and
    /// finite duration overflow panic at construction.
    fn delay(self, offset: Duration) -> Delay<Self>
    where
        Self: Sized,
    {
        assert!(offset >= Time::ZERO, "animation delay must be nonnegative");
        let duration = checked_duration(self.duration()).map(|duration| duration + offset);
        Delay {
            source: self,
            offset,
            duration,
        }
    }

    /// Changes playback speed by a finite, strictly positive factor.
    /// Durations round to the nearest tick; a collapsed finite animation becomes
    /// instantaneous. Invalid factors and unrepresentable durations panic.
    fn speed(self, factor: f64) -> Speed<Self>
    where
        Self: Sized,
    {
        assert!(
            factor.is_finite() && factor > 0.0,
            "animation speed must be finite and positive"
        );
        let source_duration = checked_duration(self.duration());
        let duration = source_duration.map(|duration| duration / factor);
        Speed {
            source: self,
            factor,
            source_duration,
            duration,
        }
    }

    /// Runs two finite animations in sequence. The second owns their shared
    /// boundary. Infinite sources or duration overflow panic at construction.
    fn then<Next: Animation<Value>>(self, next: Next) -> Then<Self, Next>
    where
        Self: Sized,
    {
        let first_duration = finite_duration(self.duration(), "then");
        let next_duration = finite_duration(next.duration(), "then");
        Then {
            first: self,
            next,
            first_duration,
            duration: first_duration + next_duration,
        }
    }

    /// Reverses a finite animation. Infinite sources panic at construction.
    fn reverse(self) -> Reverse<Self>
    where
        Self: Sized,
    {
        let duration = finite_duration(self.duration(), "reverse");
        Reverse {
            source: self,
            duration,
        }
    }

    /// Plays a finite animation `count` times. Intermediate boundaries restart;
    /// the final boundary holds the target. Zero repeats hold the initial value.
    /// Infinite sources or duration overflow panic at construction.
    fn repeat(self, count: u32) -> Repeat<Self>
    where
        Self: Sized,
    {
        let source_duration = finite_duration(self.duration(), "repeat");
        let duration = cycle_duration(source_duration, count);
        Repeat {
            source: self,
            count,
            source_duration,
            duration,
        }
    }

    /// Plays a finite animation forward then backward, ending at its initial
    /// value. Infinite sources or duration overflow panic at construction.
    fn ping_pong(self) -> PingPong<Self>
    where
        Self: Sized,
    {
        let source_duration = finite_duration(self.duration(), "ping_pong");
        let duration = cycle_duration(source_duration, 2);
        PingPong {
            source: self,
            source_duration,
            duration,
        }
    }

    /// Shares a type-erased animation, retaining sampling and combinators.
    fn boxed(self) -> Arc<dyn Animation<Value>>
    where
        Self: Sized + 'static,
    {
        Arc::new(self)
    }
}

impl<Value, Function> Animation<Value> for Function
where
    Function: Fn(Time) -> Value + Send + Sync,
{
    fn sample(&self, time: Time) -> Value {
        self(time)
    }
}

impl<Value> Animation<Value> for Arc<dyn Animation<Value> + '_> {
    fn sample(&self, time: Time) -> Value {
        self.as_ref().sample(time)
    }

    fn duration(&self) -> Option<Duration> {
        self.as_ref().duration()
    }
}

/// An unbounded constant signal.
#[derive(Clone, Debug)]
pub struct Constant<Value> {
    value: Value,
}

impl<Value> Constant<Value> {
    /// Stores the value returned at every signed time.
    pub const fn new(value: Value) -> Self {
        Self { value }
    }
}

impl<Value: Clone + Send + Sync> Animation<Value> for Constant<Value> {
    fn sample(&self, _time: Time) -> Value {
        self.value.clone()
    }
}

/// A finite endpoint-to-endpoint interpolation with double-precision progress.
///
/// ```
/// use motion_core::prelude::*;
/// use motion_core::{
///     DisplayList, RasterLimits, ResourcePool, ValidatedDisplayList, MAX_PATH_VERBS,
///     MAX_STROKE_VERBS, MAX_SVG_PATH_BYTES, sample_offsets,
/// };
/// let movement = Tween::new(Point::ORIGIN, Point::new(100.0, 40.0), 2.seconds());
/// assert_eq!(movement.sample(1.seconds()), Point::new(50.0, 20.0));
/// assert_eq!(movement.sample((-1).seconds()), Point::ORIGIN);
/// ```
#[derive(Clone, Debug)]
pub struct Tween<Value> {
    from: Value,
    to: Value,
    duration: Duration,
    easing: Easing,
}

impl<Value> Tween<Value> {
    /// Creates a linear tween. A negative duration panics at construction.
    /// A zero duration holds `from` before zero and `to` at/after zero.
    pub fn new(from: Value, to: Value, duration: Duration) -> Self {
        Self::try_new(from, to, duration).unwrap_or_else(|error| panic!("{}", error.message))
    }

    /// Creates a tween from dynamic input without panicking for negative duration.
    pub fn try_new(
        from: Value,
        to: Value,
        duration: Duration,
    ) -> Result<Self, crate::ValidationError> {
        if duration < Time::ZERO {
            return Err(crate::ValidationError::invalid(
                "tween",
                "duration",
                "tween duration must be nonnegative",
            ));
        }
        Ok(Self {
            from,
            to,
            duration,
            easing: Easing::Linear,
        })
    }

    /// Selects a f64 curve.
    /// Invalid curve parameters panic during construction.
    pub fn easing(mut self, easing: Easing) -> Self {
        self.easing = easing.validated();
        self
    }

    /// Selects a curve after recoverable validation of its built-in parameters.
    pub fn try_easing(mut self, easing: Easing) -> Result<Self, crate::ValidationError> {
        easing.try_validate()?;
        self.easing = easing;
        Ok(self)
    }
}

impl<Value: Interpolate + Send + Sync> Animation<Value> for Tween<Value> {
    fn sample(&self, time: Time) -> Value {
        if time < Time::ZERO {
            return self.from.clone();
        }
        if time >= self.duration {
            return self.to.clone();
        }
        if time == Time::ZERO {
            return self.from.clone();
        }
        let progress = self.easing.apply(time / self.duration);
        self.from.interpolate(&self.to, progress)
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.duration)
    }
}

/// Value mapping produced by [`Animation::map`].
#[derive(Clone, Debug)]
pub struct Map<Source, Function, Input> {
    source: Source,
    function: Function,
    input: PhantomData<fn(Input)>,
}

impl<Input, Output, Source, Function> Animation<Output> for Map<Source, Function, Input>
where
    Source: Animation<Input>,
    Function: Fn(Input) -> Output + Send + Sync,
{
    fn sample(&self, time: Time) -> Output {
        (self.function)(self.source.sample(time))
    }

    fn duration(&self) -> Option<Duration> {
        self.source.duration()
    }
}

/// Parallel sampling produced by [`Animation::zip`].
#[derive(Clone, Debug)]
pub struct Zip<First, Second> {
    first: First,
    second: Second,
}

impl<FirstValue, SecondValue, First, Second> Animation<(FirstValue, SecondValue)>
    for Zip<First, Second>
where
    First: Animation<FirstValue>,
    Second: Animation<SecondValue>,
{
    fn sample(&self, time: Time) -> (FirstValue, SecondValue) {
        (self.first.sample(time), self.second.sample(time))
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.first.duration()?.max(self.second.duration()?))
    }
}

fn checked_duration(duration: Option<Duration>) -> Option<Duration> {
    if let Some(duration) = duration {
        assert!(
            duration >= Time::ZERO,
            "animation duration must be nonnegative"
        );
    }
    duration
}

fn finite_duration(duration: Option<Duration>, operation: &str) -> Duration {
    checked_duration(duration).unwrap_or_else(|| panic!("{operation} requires a finite animation"))
}

fn cycle_duration(duration: Duration, count: u32) -> Duration {
    Time::from_ticks(
        duration
            .ticks()
            .checked_mul(i64::from(count))
            .expect("animation duration overflow"),
    )
}

/// A delayed animation produced by [`Animation::delay`].
#[derive(Clone, Debug)]
pub struct Delay<Source> {
    source: Source,
    offset: Duration,
    duration: Option<Duration>,
}

impl<Value, Source: Animation<Value>> Animation<Value> for Delay<Source> {
    fn sample(&self, time: Time) -> Value {
        let local = match self.duration {
            Some(_) if time < self.offset => BEFORE_START,
            Some(duration) if time >= duration => duration - self.offset,
            _ => time - self.offset,
        };
        self.source.sample(local)
    }

    fn duration(&self) -> Option<Duration> {
        self.duration
    }
}

/// A time-scaled animation produced by [`Animation::speed`].
#[derive(Clone, Debug)]
pub struct Speed<Source> {
    source: Source,
    factor: f64,
    source_duration: Option<Duration>,
    duration: Option<Duration>,
}

impl<Value, Source: Animation<Value>> Animation<Value> for Speed<Source> {
    fn sample(&self, time: Time) -> Value {
        let local = match (self.duration, self.source_duration) {
            (Some(_), _) if time < Time::ZERO => BEFORE_START,
            (Some(duration), Some(source_duration)) if time >= duration => source_duration,
            (Some(_), Some(_)) if self.factor == 1.0 => time,
            (Some(_), Some(source_duration)) => Time::from_ticks(
                (time.ticks() as f64 * self.factor)
                    .round()
                    .clamp(0.0, source_duration.ticks() as f64) as i64,
            ),
            _ => time * self.factor,
        };
        self.source.sample(local)
    }

    fn duration(&self) -> Option<Duration> {
        self.duration
    }
}

/// A finite sequence produced by [`Animation::then`].
#[derive(Clone, Debug)]
pub struct Then<First, Next> {
    first: First,
    next: Next,
    first_duration: Duration,
    duration: Duration,
}

impl<Value, First: Animation<Value>, Next: Animation<Value>> Animation<Value>
    for Then<First, Next>
{
    fn sample(&self, time: Time) -> Value {
        if time < Time::ZERO {
            self.first.sample(BEFORE_START)
        } else if time >= self.duration {
            self.next.sample(self.duration - self.first_duration)
        } else if time < self.first_duration {
            self.first.sample(time)
        } else {
            self.next.sample(time - self.first_duration)
        }
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.duration)
    }
}

/// A reversed finite animation produced by [`Animation::reverse`].
#[derive(Clone, Debug)]
pub struct Reverse<Source> {
    source: Source,
    duration: Duration,
}

impl<Value, Source: Animation<Value>> Animation<Value> for Reverse<Source> {
    fn sample(&self, time: Time) -> Value {
        let local = if time < Time::ZERO {
            self.duration
        } else if time >= self.duration {
            BEFORE_START
        } else {
            self.duration - time
        };
        self.source.sample(local)
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.duration)
    }
}

/// A finite repetition produced by [`Animation::repeat`].
#[derive(Clone, Debug)]
pub struct Repeat<Source> {
    source: Source,
    count: u32,
    source_duration: Duration,
    duration: Duration,
}

impl<Value, Source: Animation<Value>> Animation<Value> for Repeat<Source> {
    fn sample(&self, time: Time) -> Value {
        let local = if self.count == 0 || time < Time::ZERO {
            BEFORE_START
        } else if time >= self.duration {
            self.source_duration
        } else {
            Time::from_ticks(time.ticks() % self.source_duration.ticks())
        };
        self.source.sample(local)
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.duration)
    }
}

/// A finite forward/backward cycle produced by [`Animation::ping_pong`].
#[derive(Clone, Debug)]
pub struct PingPong<Source> {
    source: Source,
    source_duration: Duration,
    duration: Duration,
}

impl<Value, Source: Animation<Value>> Animation<Value> for PingPong<Source> {
    fn sample(&self, time: Time) -> Value {
        let local = if time < Time::ZERO || time >= self.duration {
            BEFORE_START
        } else if time <= self.source_duration {
            time
        } else {
            self.duration - time
        };
        self.source.sample(local)
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.duration)
    }
}

#[cfg(test)]
mod interpolation_tests {
    use super::*;

    #[test]
    fn interpolation_preserves_double_precision_progress() {
        #[derive(Clone)]
        struct CustomValue(f64);

        impl Interpolate for CustomValue {
            fn interpolate(&self, _target: &Self, progress: f64) -> Self {
                Self(progress)
            }
        }

        fn blend<Value: Interpolate>(from: Value, to: Value, progress: f64) -> Value {
            from.interpolate(&to, progress)
        }

        let progress = 0.500_000_000_1;
        assert_ne!(progress, f64::from(progress as f32));
        assert_eq!(blend(0.0_f64, 1.0, progress), progress);
        assert_eq!(
            blend(CustomValue(0.0), CustomValue(1.0), progress).0,
            progress
        );
        let point = blend(Point::ORIGIN, Point::new(1.0, 2.0), progress);
        assert_eq!(point.x, progress);
        assert_eq!(point.y, 2.0 * progress);
        assert_eq!(
            blend(Vector2::ZERO, Vector2::new(1.0, 0.0), progress).x,
            progress
        );
        let transform = blend(
            Transform::IDENTITY,
            Transform::translate(1.0, 0.0),
            progress,
        );
        assert_eq!(transform.apply(Point::ORIGIN).x, progress);
        let array = blend([0.0_f64; 2], [1.0_f64; 2], progress);
        assert_eq!(array, [progress; 2]);
        assert_eq!(
            blend((0.0_f64, 0.0_f64), (1.0, 2.0), progress),
            (progress, 2.0 * progress)
        );
    }
}