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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/// [`Easing`] with extra easings that may not be monotonic over easing parameter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AdvancedEasing {
Linear,
InConst,
OutConst,
InOutConst,
InSine,
OutSine,
InOutSine,
InQuad,
OutQuad,
InOutQuad,
InCubic,
OutCubic,
InOutCubic,
InQuart,
OutQuart,
InOutQuart,
InQuint,
OutQuint,
InOutQuint,
InExpo,
OutExpo,
InOutExpo,
InCirc,
OutCirc,
InOutCirc,
InBack,
OutBack,
InOutBack,
InElastic,
OutElastic,
InOutElastic,
InBounce,
OutBounce,
InOutBounce,
}
/// All easing variants whose evaluation result is monotonic over easing parameter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Easing {
Linear,
InConst,
OutConst,
InOutConst,
InSine,
OutSine,
InOutSine,
InQuad,
OutQuad,
InOutQuad,
InCubic,
OutCubic,
InOutCubic,
InQuart,
OutQuart,
InOutQuart,
InQuint,
OutQuint,
InOutQuint,
InExpo,
OutExpo,
InOutExpo,
}
impl AdvancedEasing {
/// An array of all [`AdvancedEasing`] variants.
pub const ALL: &[AdvancedEasing] = &[
AdvancedEasing::Linear,
AdvancedEasing::InConst,
AdvancedEasing::OutConst,
AdvancedEasing::InOutConst,
AdvancedEasing::InSine,
AdvancedEasing::OutSine,
AdvancedEasing::InOutSine,
AdvancedEasing::InQuad,
AdvancedEasing::OutQuad,
AdvancedEasing::InOutQuad,
AdvancedEasing::InCubic,
AdvancedEasing::OutCubic,
AdvancedEasing::InOutCubic,
AdvancedEasing::InQuart,
AdvancedEasing::OutQuart,
AdvancedEasing::InOutQuart,
AdvancedEasing::InQuint,
AdvancedEasing::OutQuint,
AdvancedEasing::InOutQuint,
AdvancedEasing::InExpo,
AdvancedEasing::OutExpo,
AdvancedEasing::InOutExpo,
AdvancedEasing::InCirc,
AdvancedEasing::OutCirc,
AdvancedEasing::InOutCirc,
AdvancedEasing::InBack,
AdvancedEasing::OutBack,
AdvancedEasing::InOutBack,
AdvancedEasing::InElastic,
AdvancedEasing::OutElastic,
AdvancedEasing::InOutElastic,
AdvancedEasing::InBounce,
AdvancedEasing::OutBounce,
AdvancedEasing::InOutBounce,
];
/// Ease between two values.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
///
/// # Returns
/// - If t <= 0: returns `from`;
/// - If t >= 1: returns `to`;
/// - Otherwise, returns an eased value inbetween `from` and `to` based on the easing variant.
///
/// # Finiteness
/// Given NaN input, it returns NaN immediately.
/// Given finite `from`, `to`, and `t`:
/// - For bounded easings (all variants except Back and Elastic), the output is always finite.
/// - For overshooting easings (Back and Elastic variants), the output may overflow if `|from|`
/// or `|to|` is within 10% of [`f64::MAX`]
#[inline]
pub fn ease<T, const N: usize>(self, from: T, to: T, t: T) -> T
where
T: Easable<N>,
{
super::eval::evaluate_easing(self, from, to, t)
}
/// The derivative at a given point.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
///
/// # Returns
/// - If 0 < t < 1: returns the derivative of [`Self::ease`] at the given `t` value;
/// - Otherwise, the derivative is 0.
///
/// # Remarks
/// The derivative is with respect to `t` which is normalized to [0, 1]. You may need to
/// scale it with the ease duration in order for the result to be consistent.
///
/// More specifically: if you're easing from A to B, with the total animation duration D
/// starting at S, then:
/// t = (current_time - S) / D
/// => dt = dtime / D
/// => dtime / dt = D
///
/// de/dtime = de/dt * dtime / dt = de/dt * D
/// Where `de/dtime` is the derivative with respect to real time, and `de/dt` is the derivative
/// with respect to normalized time (the result of this method).
///
/// # Finiteness
/// Overflow can only occur if `|from|` or `|to|` exceeds [`f64::MAX`] / max_derivative, where
/// max_derivative is the maximum derivative value of all variants given 0->1 input range
/// (which is about 80 for InOutQuint).
/// Otherwise, given finite `from`, `to`, and `t`, the output is always finite.
///
/// # Performance
/// All easings are evaluated analytically with a closed-form formula.
#[inline]
pub fn derivative<T, const N: usize>(self, from: T, to: T, t: T) -> T
where
T: Easable<N>,
{
super::derivative::evaluate_derivative(self, from, to, t)
}
/// Integrate the eased value at the range [0, t].
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
///
/// # Returns
/// - If t < 0, the value is from * t (which is negative);
/// - If t > 1, the value is from * t + (to - from) * integ[0,1] e(t)*dt + to * (t - 1);
/// - At t = 0, the value is 0.
/// - Otherwise it integrates [`Self::ease`] from 0 to t.
///
/// # Remarks
/// This integrates over `t` which is normalized to [0, 1]. You may need to
/// scale it with the ease duration in order for the result to be consistent.
///
/// More specifically: if you're easing from A to B, with the total animation duration D
/// starting at S, then:
/// t = (current_time - S) / D
/// => dt = dtime / D
/// => dtime = dt * D
///
/// integ e(t)dtime = integ e(time) * D * dt = D * integ e(t)dt
/// Where `integ e(t)dtime` is the integration over real time, and `integ e(t)dt` is the
/// integration over normalized time (the result of this method).
///
/// # Finiteness
/// Given finite `from`, `to`, and `t`:
/// - The output is always finite for `t ∈ [0, 1]`.
/// - For `t < 0`, the result is `from * t`, which is finite for finite input.
/// - For `t > 1`, the result grows linearly with `t` and may overflow for extremely large `|t|`
/// combined with large `|from|` or `|to|`.
///
/// # Performance
/// Circ, Back, Elastic and Bounce variants either have very complex closed form integration,
/// or doesn't have a closed form. It instead look up a LUT table of 10000 values and linearly
/// interpolate inbetween them.
///
/// The rest are evaluated analytically with a closed form formula.
#[inline]
#[cfg(any(test, feature = "lut_integration"))]
pub fn integrate<T, const N: usize>(self, from: T, to: T, t: T) -> T
where
T: Easable<N>,
{
super::integration::evaluate_advanced(self, from, to, t)
}
/// Integrate the reciprocal of the eased value at the range [0, t].
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
/// - `samples`: The number of samples for numerical integration.
///
/// # Returns
/// - `None` if the integrand has a pole (the denominator `from*(1-e(s)) +
/// to*e(s)` is zero for some `s ∈ [0, t]`). See [`Self::integrate_reciprocal`]'s
/// finiteness section for the exact conditions.
/// - If t < 0, the value is 1/from * t (which is negative);
/// - If t > 1, the value is integ[0,1] 1/(from + (to - from))e(t)*dt + 1/to * (t - 1);
/// - At t = 0, the value is 0.
/// - Otherwise it integrates the reciprocal of [`Self::ease`] from 0 to t.
///
/// # Remarks
/// This integrates over `t` which is normalized to [0, 1]. You may need to
/// scale it with the ease duration in order for the result to be consistent.
///
/// More specifically: if you're easing from A to B, with the total animation duration D
/// starting at S, then:
/// t = (current_time - S) / D
/// => dt = dtime / D
/// => dtime = dt * D
///
/// integ 1/(from + (to-from)e(t))dtime
/// = integ 1/(from + (to-from))e(t) * D * dt
/// = D * integ 1/(from + (to-from)e(t))dt
/// Where `integ (from + (to-from)e(t))dtime` is the integration over real time, and
/// `integ 1/(from + (to-from)e(t))dt` is the integration over normalized time(the result of
/// this method).
///
/// # Finiteness
/// Returns `None` (the integral diverges or input is invalid) if any of the
/// following hold:
/// - Any input (`from`, `to`, or `t`) is not finite.
/// - `from * to <= 0` (opposite signs or one is zero): by the intermediate
/// value theorem, the denominator crosses zero for some `t ∈ [0, 1]`.
/// - For overshooting easings (Back, Elastic) with `from * to > 0`: the
/// easing's value exits `[0, 1]`. If its actual range `[e_min, e_max]`
/// (see [`Self::easing_range`]) contains the critical value
/// `from / (from - to)`, the denominator hits zero.
///
/// Otherwise, given finite input, returns `Some(finite)`.
///
/// # Performance
/// Only Linear and Const variant have closed forms. All other easings are done by numerical
/// integration of `samples` number of values. As such, this is quite expensive.
#[inline]
pub fn integrate_reciprocal<T, const N: usize>(
self,
from: T,
to: T,
t: T,
samples: usize,
) -> Option<T>
where
T: Easable<N>,
{
super::integration_reciprocal::evaluate_integration_reciprocal(self, from, to, t, samples)
}
/// Whether the easing's value `e(t)` exits `[0, 1]` for some `t ∈ [0, 1]`.
///
/// Only Back and Elastic variants overshoot. All other easings stay within `[0, 1]`.
#[inline]
pub fn overshoots(self) -> bool {
super::integration_reciprocal::overshoots(self)
}
/// The lower and upper bound of this easing's value for `t ∈ [0, 1]`.
///
/// For non-overshooting easings (see [`Self::overshoots`]), this returns
/// exactly `(0.0, 1.0)`. For overshooting easings, the bounds are pre-computed constants.
///
/// Use this to check whether [`Self::integrate_reciprocal`] will have a
/// pole for given `from`/`to`:
/// ```text
/// let (e_min, e_max) = ease.easing_range();
/// let e_crit = from / (from - to);
/// if e_crit >= e_min && e_crit <= e_max {
/// // pole — integrate_reciprocal will return None
/// }
/// ```
#[inline]
pub fn easing_range(self) -> (f64, f64) {
super::integration_reciprocal::overshoot_bounds(self)
}
}
impl Easing {
/// An array of all [`Easing`] variants.
pub const ALL: &[Easing] = &[
Easing::Linear,
Easing::InConst,
Easing::OutConst,
Easing::InOutConst,
Easing::InSine,
Easing::OutSine,
Easing::InOutSine,
Easing::InQuad,
Easing::OutQuad,
Easing::InOutQuad,
Easing::InCubic,
Easing::OutCubic,
Easing::InOutCubic,
Easing::InQuart,
Easing::OutQuart,
Easing::InOutQuart,
Easing::InQuint,
Easing::OutQuint,
Easing::InOutQuint,
Easing::InExpo,
Easing::OutExpo,
Easing::InOutExpo,
];
/// Ease between two values.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
///
/// # Returns
/// - If t <= 0: returns `from`;
/// - If t >= 1: returns `to`;
/// - Otherwise, returns an eased value inbetween `from` and `to` based on the easing variant.
///
/// # Finiteness
/// Given finite `from`, `to`, and `t`, the output is always finite.
#[inline]
pub fn ease<T, const N: usize>(self, from: T, to: T, t: T) -> T
where
T: Easable<N>,
{
self.as_easing().ease(from, to, t)
}
/// The derivative at a given point.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
///
/// # Returns
/// - If 0 < t < 1: returns the derivative of [`Self::ease`] at the given `t` value;
/// - Otherwise, the derivative is 0.
///
/// # Remarks
/// The derivative is with respect to `t` which is normalized to [0, 1]. You may need to
/// scale it with the ease duration in order for the result to be consistent.
///
/// More specifically: if you're easing from A to B, with the total animation duration D
/// starting at S, then:
/// t = (current_time - S) / D
/// => dt = dtime / D
/// => dtime / dt = D
///
/// de/dtime = de/dt * dtime / dt = de/dt * D
/// Where `de/dtime` is the derivative with respect to real time, and `de/dt` is the derivative
/// with respect to normalized time (the result of this method).
///
/// # Finiteness
/// Overflow can only occur if `|from|` or `|to|` exceeds [`f64::MAX`] / max_derivative, where
/// max_derivative is the maximum derivative value of all variants given 0->1 input range
/// (which is about 80 for InOutQuint).
/// Otherwise, given finite `from`, `to`, and `t`, the output is always finite.
///
/// # Performance
/// All easings are evaluated analytically with a closed-form formula.
#[inline]
pub fn derivative<T, const N: usize>(self, from: T, to: T, t: T) -> T
where
T: Easable<N>,
{
self.as_easing().derivative(from, to, t)
}
/// Integrate the eased value at the range [0, t].
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
///
/// # Returns
/// - If t < 0, the value is from * t (which is negative);
/// - If t > 1, the value is from * t + (to - from) * integ[0,1] e(t)*dt + to * (t - 1);
/// - At t = 0, the value is 0.
/// - Otherwise it integrates [`Self::ease`] from 0 to t.
///
/// # Remarks
/// This integrates over `t` which is normalized to [0, 1]. You may need to
/// scale it with the ease duration in order for the result to be consistent.
///
/// More specifically: if you're easing from A to B, with the total animation duration D
/// starting at S, then:
/// t = (current_time - S) / D
/// => dt = dtime / D
/// => dtime = dt * D
///
/// integ e(t)dtime = integ e(time) * D * dt = D * integ e(t)dt
/// Where `integ e(t)dtime` is the integration over real time, and `integ e(t)dt` is the
/// integration over normalized time (the result of this method).
///
/// # Finiteness
/// Given finite `from`, `to`, and `t`:
/// - The output is always finite for `t ∈ [0, 1]`.
/// - For `t < 0`, the result is `from * t`, which is finite for finite input.
/// - For `t > 1`, the result grows linearly with `t` and may overflow for extremely large `|t|`
/// combined with large `|from|` or `|to|`.
///
/// Non-finite input returns NaN.
///
/// # Performance
/// Circ variants have very complex closed form integration, so it instead look up a LUT table
/// of 10000 values and linearly interpolate inbetween them.
///
/// The rest are evaluated analytically with a closed form formula.
#[inline]
pub fn integrate<T, const N: usize>(self, from: T, to: T, t: T) -> T
where
T: Easable<N>,
{
super::integration::evaluate_simple(self, from, to, t)
}
/// Computes a t ∈ [0, 1] whose [`Self::ease`] would evaluate to the given value.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `val`: The value to solve for.
///
/// # Returns
/// - If val < from, it returns 0;
/// - If val > to, it returns 1;
/// - Otherwise solves for t that satisfies the equality.
///
/// # Remarks
/// Const variants are handled specially, it lumps `val` into two value range:
/// - val <= from => Returns 0.0
/// - val > from => Returns the point at which [`Self::ease`] evaluates to `to`
///
/// # Finiteness
/// Given finite `from`, `to`, and `val`, the output is always finite and
/// lies in `[0, 1]`. The internal computation `(val - from) / (to - from)`
/// is guarded by a `from == to` early return.
///
/// # Performance
/// All variants are evaluated analytically with a closed form formula.
#[inline]
pub fn solve_ease<T, const N: usize>(self, from: T, to: T, val: T) -> T
where
T: Easable<N>,
{
super::solve::solve_easing_evaluation(self, from, to, val)
}
/// Computes a t ∈ [0, 1] whose [`Self::integrate`] would evaluate to the given value.
///
/// # Remarks
/// This method requires that from * to >= 0 (i.e no sign flip),
/// which guarantees that [`Self::integrate`] is monotonic and thus only has one unique solution.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `val`: The value to solve for.
///
/// # Returns
/// - `None` if `from * to < 0` (sign flip makes the integral non-monotonic,
/// so there is no unique solution) or if any input is not finite.
/// - Otherwise, `Some(t)` where `t ∈ [0, 1]` satisfies the equality. If `val`
/// is outside the achievable range `[F(0), F(1)]`, the result is clamped
/// to `0` or `1`.
///
/// # Finiteness
/// - Returns `None` if any input (`from`, `to`, or `val`) is not finite.
/// - Returns `None` if `from * to < 0` (sign flip).
/// - Otherwise returns `Some(t)` where `t` is always finite and in
/// `[0, 1]`. The bisection solver maintains a bracket `[lo, hi] ⊆ [0, 1]`
/// and returns `hi`, which is guaranteed finite regardless of any
/// intermediate overflow in the forward function evaluation.
///
/// # Performance
/// Only Linear and Const variants are evaluated analytically.
/// The rest are either too complex or don't have a closed form formula, in which case Newton
/// method + bisection is used to solve for `t`.
#[inline]
pub fn solve_integrate<T, const N: usize>(self, from: T, to: T, val: T) -> Option<T>
where
T: Easable<N>,
{
super::solve_integration::solve_integration_evaluation(self, from, to, val)
}
/// Integrate the reciprocal of the eased value at the range [0, t].
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `t`: The easing parameter.
/// - `samples`: The number of samples for numerical integration.
///
/// # Returns
/// - `None` if the integrand has a pole (the denominator `from*(1-e(s)) +
/// to*e(s)` is zero for some `s ∈ [0, t]`). See [`Self::integrate_reciprocal`]'s
/// finiteness section for the exact conditions.
/// - If t < 0, the value is 1/from * t (which is negative);
/// - If t > 1, the value is integ[0,1] 1/(from + (to - from))e(t)*dt + 1/to * (t - 1);
/// - At t = 0, the value is 0.
/// - Otherwise it integrates the reciprocal of [`Self::ease`] from 0 to t.
///
/// # Remarks
/// This integrates over `t` which is normalized to [0, 1]. You may need to
/// scale it with the ease duration in order for the result to be consistent.
///
/// More specifically: if you're easing from A to B, with the total animation duration D
/// starting at S, then:
/// t = (current_time - S) / D
/// => dt = dtime / D
/// => dtime = dt * D
///
/// integ 1/(from + (to-from)e(t))dtime
/// = integ 1/(from + (to-from))e(t) * D * dt
/// = D * integ 1/(from + (to-from)e(t))dt
/// Where `integ (from + (to-from)e(t))dtime` is the integration over real time, and
/// `integ 1/(from + (to-from)e(t))dt` is the integration over normalized time(the result of
/// this method).
///
/// # Finiteness
/// Returns `None` (the integral diverges or input is invalid) if any of the
/// following hold:
/// - Any input (`from`, `to`, or `t`) is not finite.
/// - `from * to <= 0` (opposite signs or one is zero).
///
/// Otherwise, output is always finite.
///
/// # Performance
/// Only Linear and Const variant have closed forms. All other easings are done by numerical
/// integration of `samples` number of values. As such, this is quite expensive.
#[inline]
pub fn integrate_reciprocal<T, const N: usize>(
self,
from: T,
to: T,
t: T,
samples: usize,
) -> Option<T>
where
T: Easable<N>,
{
self.as_easing().integrate_reciprocal(from, to, t, samples)
}
/// Computes a `t ∈ [0, 1]` whose [`Self::integrate_reciprocal`] would evaluate
/// to the given value.
///
/// # Remarks
/// This method requires that `from * to > 0` (i.e no sign flip and no zero
/// endpoint), which guarantees that [`Self::integrate_reciprocal`] is monotonic
/// and thus only has one unique solution.
///
/// # Parameter
/// - `from`: The lower value bound.
/// - `to`: The upper value bound.
/// - `val`: The value to solve for.
/// - `samples`: The number of panels on the fixed `[0, 1]` grid used by the
/// single-pass walk (see algorithm below).
///
/// # Returns
/// - `None` if `from * to <= 0` (sign flip or zero endpoint makes the integral
/// non-monotonic or divergent) or if any input is not finite.
/// - Otherwise, `Some(t)` where `t ∈ [0, 1]` satisfies the equality. If `val`
/// is outside the achievable range `[R(0), R(1)] = [0, R(1)]`, the result is
/// clamped to `0` or `1`.
///
/// # Algorithm & Accuracy
/// `Linear` and the `*Const` variants admit closed-form inverses and are
/// evaluated analytically. All other easings are solved by a **single forward
/// walk** over a fixed `[0, 1]` grid of `samples` Simpson panel-pairs,
/// accumulating the reciprocal integral and stopping as soon as the running
/// sum reaches `val`. This is one O(`samples`) pass (with early termination),
/// rather than a Newton/bisection loop that re-evaluates the whole integral
/// on every iteration.
///
/// The completed panel-pairs use Simpson's rule (4th-order), but the crossing
/// within the final partial pair uses linear interpolation (2nd-order), so the
/// overall error is O(1/`samples`²) — roughly `1e-6` at the default 1024
/// samples.
///
/// # Round-trip caveat
/// This walk integrates on a fixed `[0, 1]` grid, whereas
/// [`Self::integrate_reciprocal`] integrates on a grid compressed to `[0, t]`
/// (`h = t/samples`). The two quadratures therefore differ slightly, so
/// solving and then re-integrating does not round-trip bit-exactly: the
/// discrepancy is of the same order as the quadrature error (≈ `1e-6` at
/// 1024 samples). This is the accepted tradeoff for the ~24× speedup over an
/// exact Newton/bisection inversion.
#[inline]
pub fn solve_integrate_reciprocal<T, const N: usize>(
self,
from: T,
to: T,
val: T,
samples: usize,
) -> Option<T>
where
T: Easable<N>,
{
super::solve_integration_reciprocal::solve_integration_reciprocal_evaluation(
self, from, to, val, samples,
)
}
/// Whether the easing's value `e(t)` exits `[0, 1]` for some `t ∈ [0, 1]`.
///
/// [`Easing`] only includes bounded easings, so this always returns `false`.
/// See [`Easing::overshoots`] for details.
#[inline]
pub fn overshoots(self) -> bool {
false
}
/// The actual `(e_min, e_max)` range of this easing's value `e(t)` for
/// `t ∈ [0, 1]`.
///
/// [`Easing`] only includes bounded easings, so this always returns `(0.0, 1.0)`.
/// See [`Easing::easing_range`] for details.
#[inline]
pub fn easing_range(self) -> (f64, f64) {
(0.0, 1.0)
}
}
impl From<Easing> for AdvancedEasing {
fn from(value: Easing) -> Self {
value.as_easing()
}
}
pub trait Easable<const N: usize> {
fn components(self) -> [f64; N];
fn compose(components: [f64; N]) -> Self;
}
impl Easable<1> for f32 {
fn components(self) -> [f64; 1] {
[self as f64]
}
fn compose([c]: [f64; 1]) -> Self {
c as f32
}
}
impl Easable<1> for f64 {
fn components(self) -> [f64; 1] {
[self]
}
fn compose([c]: [f64; 1]) -> Self {
c
}
}
#[cfg(feature = "glam")]
mod glam_impls {
use super::Easable;
use glam::{DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec4};
impl Easable<2> for Vec2 {
fn components(self) -> [f64; 2] {
[self.x as f64, self.y as f64]
}
fn compose([x, y]: [f64; 2]) -> Self {
Self::new(x as f32, y as f32)
}
}
impl Easable<3> for Vec3 {
fn components(self) -> [f64; 3] {
[self.x as f64, self.y as f64, self.z as f64]
}
fn compose([x, y, z]: [f64; 3]) -> Self {
Self::new(x as f32, y as f32, z as f32)
}
}
impl Easable<4> for Vec4 {
fn components(self) -> [f64; 4] {
[self.x as f64, self.y as f64, self.z as f64, self.w as f64]
}
fn compose([x, y, z, w]: [f64; 4]) -> Self {
Self::new(x as f32, y as f32, z as f32, w as f32)
}
}
impl Easable<2> for IVec2 {
fn components(self) -> [f64; 2] {
[self.x as f64, self.y as f64]
}
fn compose([x, y]: [f64; 2]) -> Self {
Self::new(x as i32, y as i32)
}
}
impl Easable<3> for IVec3 {
fn components(self) -> [f64; 3] {
[self.x as f64, self.y as f64, self.z as f64]
}
fn compose([x, y, z]: [f64; 3]) -> Self {
Self::new(x as i32, y as i32, z as i32)
}
}
impl Easable<4> for IVec4 {
fn components(self) -> [f64; 4] {
[self.x as f64, self.y as f64, self.z as f64, self.w as f64]
}
fn compose([x, y, z, w]: [f64; 4]) -> Self {
Self::new(x as i32, y as i32, z as i32, w as i32)
}
}
impl Easable<2> for UVec2 {
fn components(self) -> [f64; 2] {
[self.x as f64, self.y as f64]
}
fn compose([x, y]: [f64; 2]) -> Self {
Self::new(x as u32, y as u32)
}
}
impl Easable<3> for UVec3 {
fn components(self) -> [f64; 3] {
[self.x as f64, self.y as f64, self.z as f64]
}
fn compose([x, y, z]: [f64; 3]) -> Self {
Self::new(x as u32, y as u32, z as u32)
}
}
impl Easable<4> for UVec4 {
fn components(self) -> [f64; 4] {
[self.x as f64, self.y as f64, self.z as f64, self.w as f64]
}
fn compose([x, y, z, w]: [f64; 4]) -> Self {
Self::new(x as u32, y as u32, z as u32, w as u32)
}
}
impl Easable<2> for DVec2 {
fn components(self) -> [f64; 2] {
[self.x, self.y]
}
fn compose([x, y]: [f64; 2]) -> Self {
Self::new(x, y)
}
}
impl Easable<3> for DVec3 {
fn components(self) -> [f64; 3] {
[self.x, self.y, self.z]
}
fn compose([x, y, z]: [f64; 3]) -> Self {
Self::new(x, y, z)
}
}
impl Easable<4> for DVec4 {
fn components(self) -> [f64; 4] {
[self.x, self.y, self.z, self.w]
}
fn compose([x, y, z, w]: [f64; 4]) -> Self {
Self::new(x, y, z, w)
}
}
}