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
//! Closed-form damped spring response and conservative tick-based settling time.

use super::Animation;
use crate::{Duration, Interpolate, TICKS_PER_SECOND, Time};

/// A finite physical spring between arbitrary interpolatable values.
///
/// Initial velocity is in endpoint distances per second. The default mass is one,
/// stiffness 100, damping 10 and initial velocity zero. Duration is the conservative
/// displacement-envelope settling time at threshold 0.001, rounded up to ticks.
/// Values at/before zero and at/after duration are exact stored endpoints.
///
/// Builders validate the complete candidate configuration immediately. Nonfinite
/// or nonpositive physical parameters, an unrepresentable response or settling
/// time, and invalid thresholds panic. Zero damping is rejected because this type
/// promises a finite settling duration. Use a signal for undamped oscillation.
///
/// ```
/// 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 = Spring::new(Point::ORIGIN, Point::new(100.0, 40.0))
///     .damping(20.0)
///     .settling_threshold(0.0001);
/// let end = movement.duration().unwrap();
/// assert_eq!(movement.sample(end), Point::new(100.0, 40.0));
/// assert_eq!(movement.sample((-1).seconds()), Point::ORIGIN);
/// ```
#[derive(Clone, Debug)]
pub struct Spring<Value> {
    from: Value,
    to: Value,
    parameters: Parameters,
    response: Response,
}

impl<Value> Spring<Value> {
    /// Creates a spring with the default physical parameters and settling threshold.
    pub fn new(from: Value, to: Value) -> Self {
        let parameters = Parameters::default();
        Self {
            from,
            to,
            parameters,
            response: Response::new(parameters),
        }
    }

    /// Sets finite positive mass and recomputes the finite response immediately.
    pub fn mass(mut self, mass: f64) -> Self {
        self.parameters.mass = mass;
        self.rebuild()
    }

    /// Sets finite positive stiffness and recomputes the response immediately.
    pub fn stiffness(mut self, stiffness: f64) -> Self {
        self.parameters.stiffness = stiffness;
        self.rebuild()
    }

    /// Sets finite strictly positive damping. Zero has no finite settling time.
    pub fn damping(mut self, damping: f64) -> Self {
        self.parameters.damping = damping;
        self.rebuild()
    }

    /// Sets finite normalized velocity towards the target; negative moves away.
    /// Units are endpoint distances per second, not pixels per second.
    pub fn initial_velocity(mut self, velocity: f64) -> Self {
        self.parameters.velocity = velocity;
        self.rebuild()
    }

    /// Sets a finite relative displacement-envelope threshold strictly between
    /// zero and one. This is not a pixel tolerance or a velocity threshold.
    pub fn settling_threshold(mut self, threshold: f64) -> Self {
        self.parameters.threshold = threshold;
        self.rebuild()
    }

    fn rebuild(mut self) -> Self {
        self.response = Response::new(self.parameters);
        self
    }
}

impl Default for Spring<f64> {
    fn default() -> Self {
        Self::new(0.0, 1.0)
    }
}

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

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

#[derive(Clone, Copy, Debug)]
struct Parameters {
    mass: f64,
    stiffness: f64,
    damping: f64,
    velocity: f64,
    threshold: f64,
}

impl Default for Parameters {
    fn default() -> Self {
        Self {
            mass: 1.0,
            stiffness: 100.0,
            damping: 10.0,
            velocity: 0.0,
            threshold: 0.001,
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum Regime {
    Under { oscillation: f64 },
    Critical,
    Over { separation: f64 },
}

#[derive(Clone, Copy, Debug)]
struct Response {
    regime: Regime,
    rate: f64,
    velocity: f64,
    duration: Duration,
}

impl Response {
    fn new(parameters: Parameters) -> Self {
        assert!(
            parameters.mass.is_finite() && parameters.mass > 0.0,
            "spring mass must be finite and positive"
        );
        assert!(
            parameters.stiffness.is_finite() && parameters.stiffness > 0.0,
            "spring stiffness must be finite and positive"
        );
        assert!(
            parameters.damping.is_finite() && parameters.damping > 0.0,
            "finite spring damping must be finite and positive"
        );
        assert!(
            parameters.velocity.is_finite(),
            "spring velocity must be finite"
        );
        assert!(
            parameters.threshold.is_finite()
                && parameters.threshold > 0.0
                && parameters.threshold < 1.0,
            "spring settling threshold must be finite and between zero and one"
        );
        let frequency = libm::sqrt(parameters.stiffness) / libm::sqrt(parameters.mass);
        let quotient = parameters.damping / parameters.mass;
        let decay = if quotient.is_finite() {
            quotient * 0.5
        } else {
            (parameters.damping * 0.5) / parameters.mass
        };
        assert!(
            frequency.is_finite() && frequency > 0.0 && decay.is_finite() && decay > 0.0,
            "spring derived rates must be finite and positive"
        );
        let (regime, rate) = if decay < frequency {
            let oscillation =
                frequency * libm::sqrt((frequency - decay) / frequency * (1.0 + decay / frequency));
            assert!(
                oscillation.is_finite() && oscillation > 0.0,
                "spring oscillation must be representable"
            );
            (Regime::Under { oscillation }, decay)
        } else if decay == frequency {
            (Regime::Critical, decay)
        } else {
            let root = decay * libm::sqrt((decay - frequency) / decay * (1.0 + frequency / decay));
            let separation = 2.0 * root;
            let slow = (frequency / decay) * frequency / (1.0 + root / decay);
            assert!(
                separation.is_finite() && separation > 0.0 && slow.is_finite() && slow > 0.0,
                "spring characteristic rates must be representable"
            );
            (Regime::Over { separation }, slow)
        };
        let velocity = parameters.velocity / rate;
        assert!(
            velocity.is_finite(),
            "spring normalized velocity must be representable"
        );
        let duration = settling_duration(rate, (1.0 - velocity).abs(), parameters.threshold);
        if let Regime::Under { oscillation } = regime {
            assert!(
                (oscillation * duration.as_seconds()).is_finite(),
                "spring phase must be representable"
            );
        }
        Self {
            regime,
            rate,
            velocity,
            duration,
        }
    }

    fn progress(self, seconds: f64) -> f64 {
        let elapsed = self.rate * seconds;
        if elapsed.is_infinite() {
            return 1.0;
        }
        let decay = libm::exp(-elapsed);
        let weighted_time = elapsed * decay;
        let base = critical_progress(elapsed, decay);
        let progress = match self.regime {
            Regime::Critical => base + self.velocity * weighted_time,
            Regime::Under { oscillation } => {
                let angle = oscillation * seconds;
                let (sinc, remainder) = sine_ratio(angle);
                let half_sine = libm::sin(angle * 0.5);
                base + weighted_time * remainder
                    + decay * (2.0 * half_sine * half_sine)
                    + self.velocity * weighted_time * sinc
            }
            Regime::Over { separation } => {
                let (ratio, remainder) = exponential_ratio(separation * seconds);
                base + weighted_time * remainder + self.velocity * weighted_time * ratio
            }
        };
        assert!(progress.is_finite(), "spring response must be finite");
        progress
    }
}

fn critical_progress(elapsed: f64, decay: f64) -> f64 {
    if elapsed < 0.001 {
        elapsed
            * elapsed
            * (0.5
                + elapsed
                    * (-1.0 / 3.0 + elapsed * (0.125 + elapsed * (-1.0 / 30.0 + elapsed / 144.0))))
    } else {
        -libm::expm1(-elapsed) - elapsed * decay
    }
}

fn sine_ratio(angle: f64) -> (f64, f64) {
    if angle.abs() < 0.001 {
        let square = angle * angle;
        let remainder = square * (1.0 / 6.0 + square * (-1.0 / 120.0 + square / 5_040.0));
        (1.0 - remainder, remainder)
    } else {
        let ratio = libm::sin(angle) / angle;
        (ratio, 1.0 - ratio)
    }
}

fn exponential_ratio(elapsed: f64) -> (f64, f64) {
    if elapsed.is_infinite() {
        (0.0, 1.0)
    } else if elapsed < 0.001 {
        let remainder = elapsed
            * (0.5
                + elapsed
                    * (-1.0 / 6.0
                        + elapsed * (1.0 / 24.0 + elapsed * (-1.0 / 120.0 + elapsed / 720.0))));
        (1.0 - remainder, remainder)
    } else {
        let ratio = -libm::expm1(-elapsed) / elapsed;
        (ratio, 1.0 - ratio)
    }
}

fn log_envelope(elapsed: f64, slope: f64) -> f64 {
    if elapsed == 0.0 {
        return 0.0;
    }
    if slope == 0.0 {
        return -elapsed;
    }
    let product_log = libm::log(slope) + libm::log(elapsed);
    let envelope_log = if product_log > 0.0 {
        product_log + libm::log1p(libm::exp(-product_log))
    } else {
        libm::log1p(libm::exp(product_log))
    };
    envelope_log - elapsed
}

fn settling_duration(rate: f64, slope: f64, threshold: f64) -> Duration {
    let target = libm::log(threshold);
    let mut lower = 0.0;
    let mut upper = 1.0;
    while log_envelope(upper, slope) > target {
        upper *= 2.0;
    }
    for _ in 0..80 {
        let middle = lower + (upper - lower) * 0.5;
        if middle == lower || middle == upper {
            break;
        }
        if log_envelope(middle, slope) > target {
            lower = middle;
        } else {
            upper = middle;
        }
    }
    let seconds = (upper * (1.0 + 32.0 * f64::EPSILON)) / rate;
    let ticks = libm::ceil(seconds * TICKS_PER_SECOND as f64).max(1.0);
    assert!(
        ticks.is_finite() && ticks < -(i64::MIN as f64),
        "spring settling duration exceeds the signed tick range"
    );
    Time::from_ticks(ticks as i64)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Spring, TimeLiteral};

    fn close(actual: f64, expected: f64) {
        assert!(actual.is_finite() && expected.is_finite());
        assert!(
            (actual - expected).abs() <= 2e-12 * expected.abs().max(1e-12),
            "{actual} != {expected}"
        );
    }

    #[test]
    fn all_damping_regimes_match_analytic_references_and_retain_first_tick_motion() {
        for damping in [10.0, 20.0, 40.0] {
            let response = Response::new(Parameters {
                damping,
                ..Parameters::default()
            });
            let decay = damping * 0.5;
            for seconds in [0.01, 0.1, 0.25, 0.5] {
                let expected = if damping < 20.0 {
                    let oscillation = libm::sqrt(100.0 - decay * decay);
                    1.0 - libm::exp(-decay * seconds)
                        * (libm::cos(oscillation * seconds)
                            + decay / oscillation * libm::sin(oscillation * seconds))
                } else if damping == 20.0 {
                    1.0 - libm::exp(-decay * seconds) * (1.0 + decay * seconds)
                } else {
                    let root = libm::sqrt(decay * decay - 100.0);
                    1.0 - libm::exp(-decay * seconds)
                        * (libm::cosh(root * seconds) + decay / root * libm::sinh(root * seconds))
                };
                close(response.progress(seconds), expected);
            }
            let tick = 1.0 / TICKS_PER_SECOND as f64;
            let expected = 50.0 * tick * tick;
            assert!((response.progress(tick) / expected - 1.0).abs() < 1e-6);
        }
    }

    #[test]
    fn settling_envelope_stays_below_threshold_across_regimes_and_large_velocities() {
        for damping in [0.1, 10.0, 20.0 - 1e-8, 20.0, 20.0 + 1e-8, 40.0, 1e8] {
            for velocity in [-4.0, 0.0, 20.0, 1e300] {
                let parameters = Parameters {
                    damping,
                    velocity,
                    ..Parameters::default()
                };
                let response = Response::new(parameters);
                assert!(response.duration > Time::ZERO);
                for factor in [1.0, 1.1, 2.0, 10.0] {
                    let seconds = response.duration.as_seconds() * factor;
                    let error = (1.0 - response.progress(seconds)).abs();
                    assert!(
                        error <= parameters.threshold * (1.0 + 1e-10),
                        "damping={damping}, velocity={velocity}, error={error}"
                    );
                    assert!(
                        log_envelope(response.rate * seconds, (1.0 - response.velocity).abs())
                            <= libm::log(parameters.threshold) + 1e-12
                    );
                }
            }
        }
    }

    #[test]
    fn initial_velocity_and_mass_scaling_have_physical_units() {
        let tick = Time::from_ticks(1);
        for velocity in [-3.0, 4.0] {
            let spring = Spring::new(0.0_f64, 1.0).initial_velocity(velocity);
            assert!((spring.sample(tick) / tick.as_seconds() - velocity).abs() < 1e-6);
        }
        let base = Spring::new(0.0_f64, 1.0);
        let scaled = Spring::new(0.0_f64, 1.0)
            .mass(2.0)
            .stiffness(200.0)
            .damping(20.0);
        for time in [100.milliseconds(), 300.milliseconds(), 500.milliseconds()] {
            close(base.sample(time), scaled.sample(time));
        }
        assert!((base.duration().unwrap().ticks() - scaled.duration().unwrap().ticks()).abs() <= 1);
    }

    #[test]
    fn near_critical_responses_and_durations_converge_without_coefficient_blowup() {
        let critical = Spring::new(0.0_f64, 1.0).damping(20.0);
        for damping in [20.0 - 1e-10, 20.0 + 1e-10] {
            let nearby = Spring::new(0.0_f64, 1.0).damping(damping);
            for time in [10.milliseconds(), 100.milliseconds(), 500.milliseconds()] {
                assert!((nearby.sample(time) - critical.sample(time)).abs() < 1e-11);
            }
            assert!(
                (nearby.duration().unwrap().as_seconds()
                    / critical.duration().unwrap().as_seconds()
                    - 1.0)
                    .abs()
                    < 1e-4
            );
        }
    }

    #[test]
    fn default_threshold_and_random_order_sampling_pin_exact_endpoints() {
        let spring = Spring::default();
        let end = spring.duration().unwrap();
        assert!(end > Time::ZERO);
        assert_eq!(spring.sample(Time::from_ticks(i64::MIN)), 0.0);
        assert_eq!(spring.sample(Time::ZERO), 0.0);
        assert_eq!(spring.sample(end), 1.0);
        assert_eq!(spring.sample(Time::from_ticks(i64::MAX)), 1.0);
        let expected = spring.sample(300.milliseconds());
        assert!(spring.sample(500.milliseconds()) > 1.0);
        for time in [
            end,
            Time::ZERO,
            300.milliseconds(),
            100.milliseconds(),
            300.milliseconds(),
        ] {
            assert_eq!(spring.sample(time), spring.clone().sample(time));
            if time == 300.milliseconds() {
                assert_eq!(spring.sample(time), expected);
            }
        }
        let tighter = spring.clone().settling_threshold(1e-6);
        assert!(tighter.duration().unwrap() > end);
        let smallest = spring.settling_threshold(f64::from_bits(1));
        let response = smallest.response;
        assert!(
            log_envelope(
                response.rate * response.duration.as_seconds(),
                (1.0 - response.velocity).abs()
            ) <= libm::log(f64::from_bits(1))
        );
    }

    #[test]
    fn invalid_physical_parameters_and_unrepresentable_durations_fail_while_building() {
        for value in [f64::NAN, f64::INFINITY, -1.0, 0.0] {
            assert!(std::panic::catch_unwind(|| Spring::default().mass(value)).is_err());
            assert!(std::panic::catch_unwind(|| Spring::default().stiffness(value)).is_err());
            assert!(std::panic::catch_unwind(|| Spring::default().damping(value)).is_err());
        }
        for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert!(
                std::panic::catch_unwind(|| Spring::default().initial_velocity(value)).is_err()
            );
        }
        for value in [0.0, 1.0, -1.0, f64::NAN, f64::INFINITY] {
            assert!(
                std::panic::catch_unwind(|| Spring::default().settling_threshold(value)).is_err()
            );
        }
        assert!(std::panic::catch_unwind(|| Spring::default().damping(1e-15)).is_err());
        assert!(std::panic::catch_unwind(|| Spring::default().stiffness(1e-30)).is_err());
        assert!(std::panic::catch_unwind(|| Spring::default().mass(f64::from_bits(1))).is_err());
    }
}