bevy_math/curve/
cores.rs

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
806
807
808
809
810
811
812
813
814
//! Core data structures to be used internally in Curve implementations, encapsulating storage
//! and access patterns for reuse.
//!
//! The `Core` types here expose their fields publicly so that it is easier to manipulate and
//! extend them, but in doing so, you must maintain the invariants of those fields yourself. The
//! provided methods all maintain the invariants, so this is only a concern if you manually mutate
//! the fields.

use super::interval::Interval;
use core::fmt::Debug;
use derive_more::derive::{Display, Error};
use itertools::Itertools;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;

/// This type expresses the relationship of a value to a fixed collection of values. It is a kind
/// of summary used intermediately by sampling operations.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum InterpolationDatum<T> {
    /// This value lies exactly on a value in the family.
    Exact(T),

    /// This value is off the left tail of the family; the inner value is the family's leftmost.
    LeftTail(T),

    /// This value is off the right tail of the family; the inner value is the family's rightmost.
    RightTail(T),

    /// This value lies on the interior, in between two points, with a third parameter expressing
    /// the interpolation factor between the two.
    Between(T, T, f32),
}

impl<T> InterpolationDatum<T> {
    /// Map all values using a given function `f`, leaving the interpolation parameters in any
    /// [`Between`] variants unchanged.
    ///
    /// [`Between`]: `InterpolationDatum::Between`
    #[must_use]
    pub fn map<S>(self, f: impl Fn(T) -> S) -> InterpolationDatum<S> {
        match self {
            InterpolationDatum::Exact(v) => InterpolationDatum::Exact(f(v)),
            InterpolationDatum::LeftTail(v) => InterpolationDatum::LeftTail(f(v)),
            InterpolationDatum::RightTail(v) => InterpolationDatum::RightTail(f(v)),
            InterpolationDatum::Between(u, v, s) => InterpolationDatum::Between(f(u), f(v), s),
        }
    }
}

/// The data core of a curve derived from evenly-spaced samples. The intention is to use this
/// in addition to explicit or inferred interpolation information in user-space in order to
/// implement curves using [`domain`] and [`sample_with`].
///
/// The internals are made transparent to give curve authors freedom, but [the provided constructor]
/// enforces the required invariants, and the methods maintain those invariants.
///
/// [the provided constructor]: EvenCore::new
/// [`domain`]: EvenCore::domain
/// [`sample_with`]: EvenCore::sample_with
///
/// # Example
/// ```rust
/// # use bevy_math::curve::*;
/// # use bevy_math::curve::cores::*;
/// // Let's make a curve that interpolates evenly spaced samples using either linear interpolation
/// // or step "interpolation" — i.e. just using the most recent sample as the source of truth.
/// enum InterpolationMode {
///     Linear,
///     Step,
/// }
///
/// // Linear interpolation mode is driven by a trait.
/// trait LinearInterpolate {
///     fn lerp(&self, other: &Self, t: f32) -> Self;
/// }
///
/// // Step interpolation just uses an explicit function.
/// fn step<T: Clone>(first: &T, second: &T, t: f32) -> T {
///     if t >= 1.0 {
///         second.clone()
///     } else {
///         first.clone()
///     }
/// }
///
/// // Omitted: Implementing `LinearInterpolate` on relevant types; e.g. `f32`, `Vec3`, and so on.
///
/// // The curve itself uses `EvenCore` to hold the evenly-spaced samples, and the `sample_with`
/// // function will do all the work of interpolating once given a function to do it with.
/// struct MyCurve<T> {
///     core: EvenCore<T>,
///     interpolation_mode: InterpolationMode,
/// }
///
/// impl<T> Curve<T> for MyCurve<T>
/// where
///     T: LinearInterpolate + Clone,
/// {
///     fn domain(&self) -> Interval {
///         self.core.domain()
///     }
///     
///     fn sample_unchecked(&self, t: f32) -> T {
///         // To sample this curve, check the interpolation mode and dispatch accordingly.
///         match self.interpolation_mode {
///             InterpolationMode::Linear => self.core.sample_with(t, <T as LinearInterpolate>::lerp),
///             InterpolationMode::Step => self.core.sample_with(t, step),
///         }
///     }
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct EvenCore<T> {
    /// The domain over which the samples are taken, which corresponds to the domain of the curve
    /// formed by interpolating them.
    ///
    /// # Invariants
    /// This must always be a bounded interval; i.e. its endpoints must be finite.
    pub domain: Interval,

    /// The samples that are interpolated to extract values.
    ///
    /// # Invariants
    /// This must always have a length of at least 2.
    pub samples: Vec<T>,
}

/// An error indicating that an [`EvenCore`] could not be constructed.
#[derive(Debug, Error, Display)]
#[display("Could not construct an EvenCore")]
pub enum EvenCoreError {
    /// Not enough samples were provided.
    #[display("Need at least two samples to create an EvenCore, but {samples} were provided")]
    NotEnoughSamples {
        /// The number of samples that were provided.
        samples: usize,
    },

    /// Unbounded domains are not compatible with `EvenCore`.
    #[display("Cannot create a EvenCore over an unbounded domain")]
    UnboundedDomain,
}

impl<T> EvenCore<T> {
    /// Create a new [`EvenCore`] from the specified `domain` and `samples`. The samples are
    /// regarded to be evenly spaced within the given domain interval, so that the outermost
    /// samples form the boundary of that interval. An error is returned if there are not at
    /// least 2 samples or if the given domain is unbounded.
    #[inline]
    pub fn new(
        domain: Interval,
        samples: impl IntoIterator<Item = T>,
    ) -> Result<Self, EvenCoreError> {
        let samples: Vec<T> = samples.into_iter().collect();
        if samples.len() < 2 {
            return Err(EvenCoreError::NotEnoughSamples {
                samples: samples.len(),
            });
        }
        if !domain.is_bounded() {
            return Err(EvenCoreError::UnboundedDomain);
        }

        Ok(EvenCore { domain, samples })
    }

    /// The domain of the curve derived from this core.
    #[inline]
    pub const fn domain(&self) -> Interval {
        self.domain
    }

    /// Obtain a value from the held samples using the given `interpolation` to interpolate
    /// between adjacent samples.
    ///
    /// The interpolation takes two values by reference together with a scalar parameter and
    /// produces an owned value. The expectation is that `interpolation(&x, &y, 0.0)` and
    /// `interpolation(&x, &y, 1.0)` are equivalent to `x` and `y` respectively.
    #[inline]
    pub fn sample_with<I>(&self, t: f32, interpolation: I) -> T
    where
        T: Clone,
        I: Fn(&T, &T, f32) -> T,
    {
        match even_interp(self.domain, self.samples.len(), t) {
            InterpolationDatum::Exact(idx)
            | InterpolationDatum::LeftTail(idx)
            | InterpolationDatum::RightTail(idx) => self.samples[idx].clone(),
            InterpolationDatum::Between(lower_idx, upper_idx, s) => {
                interpolation(&self.samples[lower_idx], &self.samples[upper_idx], s)
            }
        }
    }

    /// Given a time `t`, obtain a [`InterpolationDatum`] which governs how interpolation might recover
    /// a sample at time `t`. For example, when a [`Between`] value is returned, its contents can
    /// be used to interpolate between the two contained values with the given parameter. The other
    /// variants give additional context about where the value is relative to the family of samples.
    ///
    /// [`Between`]: `InterpolationDatum::Between`
    pub fn sample_interp(&self, t: f32) -> InterpolationDatum<&T> {
        even_interp(self.domain, self.samples.len(), t).map(|idx| &self.samples[idx])
    }

    /// Like [`sample_interp`], but the returned values include the sample times. This can be
    /// useful when sample interpolation is not scale-invariant.
    ///
    /// [`sample_interp`]: EvenCore::sample_interp
    pub fn sample_interp_timed(&self, t: f32) -> InterpolationDatum<(f32, &T)> {
        let segment_len = self.domain.length() / (self.samples.len() - 1) as f32;
        even_interp(self.domain, self.samples.len(), t).map(|idx| {
            (
                self.domain.start() + segment_len * idx as f32,
                &self.samples[idx],
            )
        })
    }
}

/// Given a domain and a number of samples taken over that interval, return an [`InterpolationDatum`]
/// that governs how samples are extracted relative to the stored data.
///
/// `domain` must be a bounded interval (i.e. `domain.is_bounded() == true`).
///
/// `samples` must be at least 2.
///
/// This function will never panic, but it may return invalid indices if its assumptions are violated.
pub fn even_interp(domain: Interval, samples: usize, t: f32) -> InterpolationDatum<usize> {
    let subdivs = samples - 1;
    let step = domain.length() / subdivs as f32;
    let t_shifted = t - domain.start();
    let steps_taken = t_shifted / step;

    if steps_taken <= 0.0 {
        // To the left side of all the samples.
        InterpolationDatum::LeftTail(0)
    } else if steps_taken >= subdivs as f32 {
        // To the right side of all the samples
        InterpolationDatum::RightTail(samples - 1)
    } else {
        let lower_index = steps_taken.floor() as usize;
        // This upper index is always valid because `steps_taken` is a finite value
        // strictly less than `samples - 1`, so its floor is at most `samples - 2`
        let upper_index = lower_index + 1;
        let s = steps_taken.fract();
        InterpolationDatum::Between(lower_index, upper_index, s)
    }
}

/// The data core of a curve defined by unevenly-spaced samples or keyframes. The intention is to
/// use this in concert with implicitly or explicitly-defined interpolation in user-space in
/// order to implement the curve interface using [`domain`] and [`sample_with`].
///
/// The internals are made transparent to give curve authors freedom, but [the provided constructor]
/// enforces the required invariants, and the methods maintain those invariants.
///
/// # Example
/// ```rust
/// # use bevy_math::curve::*;
/// # use bevy_math::curve::cores::*;
/// // Let's make a curve formed by interpolating rotations.
/// // We'll support two common modes of interpolation:
/// // - Normalized linear: First do linear interpolation, then normalize to get a valid rotation.
/// // - Spherical linear: Interpolate through valid rotations with constant angular velocity.
/// enum InterpolationMode {
///     NormalizedLinear,
///     SphericalLinear,
/// }
///
/// // Our interpolation modes will be driven by traits.
/// trait NormalizedLinearInterpolate {
///     fn nlerp(&self, other: &Self, t: f32) -> Self;
/// }
///
/// trait SphericalLinearInterpolate {
///     fn slerp(&self, other: &Self, t: f32) -> Self;
/// }
///
/// // Omitted: These traits would be implemented for `Rot2`, `Quat`, and other rotation representations.
///
/// // The curve itself just needs to use the curve core for keyframes, `UnevenCore`, which handles
/// // everything except for the explicit interpolation used.
/// struct RotationCurve<T> {
///     core: UnevenCore<T>,
///     interpolation_mode: InterpolationMode,
/// }
///
/// impl<T> Curve<T> for RotationCurve<T>
/// where
///     T: NormalizedLinearInterpolate + SphericalLinearInterpolate + Clone,
/// {
///     fn domain(&self) -> Interval {
///         self.core.domain()
///     }
///     
///     fn sample_unchecked(&self, t: f32) -> T {
///         // To sample the curve, we just look at the interpolation mode and
///         // dispatch accordingly.
///         match self.interpolation_mode {
///             InterpolationMode::NormalizedLinear =>
///                 self.core.sample_with(t, <T as NormalizedLinearInterpolate>::nlerp),
///             InterpolationMode::SphericalLinear =>
///                 self.core.sample_with(t, <T as SphericalLinearInterpolate>::slerp),
///         }
///     }
/// }
/// ```
///
/// [`domain`]: UnevenCore::domain
/// [`sample_with`]: UnevenCore::sample_with
/// [the provided constructor]: UnevenCore::new
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct UnevenCore<T> {
    /// The times for the samples of this curve.
    ///
    /// # Invariants
    /// This must always have a length of at least 2, be sorted, and have no
    /// duplicated or non-finite times.
    pub times: Vec<f32>,

    /// The samples corresponding to the times for this curve.
    ///
    /// # Invariants
    /// This must always have the same length as `times`.
    pub samples: Vec<T>,
}

/// An error indicating that an [`UnevenCore`] could not be constructed.
#[derive(Debug, Error, Display)]
#[display("Could not construct an UnevenCore")]
pub enum UnevenCoreError {
    /// Not enough samples were provided.
    #[display(
        "Need at least two unique samples to create an UnevenCore, but {samples} were provided"
    )]
    NotEnoughSamples {
        /// The number of samples that were provided.
        samples: usize,
    },
}

impl<T> UnevenCore<T> {
    /// Create a new [`UnevenCore`]. The given samples are filtered to finite times and
    /// sorted internally; if there are not at least 2 valid timed samples, an error will be
    /// returned.
    pub fn new(timed_samples: impl IntoIterator<Item = (f32, T)>) -> Result<Self, UnevenCoreError> {
        // Filter out non-finite sample times first so they don't interfere with sorting/deduplication.
        let mut timed_samples = timed_samples
            .into_iter()
            .filter(|(t, _)| t.is_finite())
            .collect_vec();
        timed_samples
            // Using `total_cmp` is fine because no NANs remain and because deduplication uses
            // `PartialEq` anyway (so -0.0 and 0.0 will be considered equal later regardless).
            .sort_by(|(t0, _), (t1, _)| t0.total_cmp(t1));
        timed_samples.dedup_by_key(|(t, _)| *t);

        if timed_samples.len() < 2 {
            return Err(UnevenCoreError::NotEnoughSamples {
                samples: timed_samples.len(),
            });
        }

        let (times, samples): (Vec<f32>, Vec<T>) = timed_samples.into_iter().unzip();
        Ok(UnevenCore { times, samples })
    }

    /// The domain of the curve derived from this core.
    ///
    /// # Panics
    /// This method may panic if the type's invariants aren't satisfied.
    #[inline]
    pub fn domain(&self) -> Interval {
        let start = self.times.first().unwrap();
        let end = self.times.last().unwrap();
        Interval::new(*start, *end).unwrap()
    }

    /// Obtain a value from the held samples using the given `interpolation` to interpolate
    /// between adjacent samples.
    ///
    /// The interpolation takes two values by reference together with a scalar parameter and
    /// produces an owned value. The expectation is that `interpolation(&x, &y, 0.0)` and
    /// `interpolation(&x, &y, 1.0)` are equivalent to `x` and `y` respectively.
    #[inline]
    pub fn sample_with<I>(&self, t: f32, interpolation: I) -> T
    where
        T: Clone,
        I: Fn(&T, &T, f32) -> T,
    {
        match uneven_interp(&self.times, t) {
            InterpolationDatum::Exact(idx)
            | InterpolationDatum::LeftTail(idx)
            | InterpolationDatum::RightTail(idx) => self.samples[idx].clone(),
            InterpolationDatum::Between(lower_idx, upper_idx, s) => {
                interpolation(&self.samples[lower_idx], &self.samples[upper_idx], s)
            }
        }
    }

    /// Given a time `t`, obtain a [`InterpolationDatum`] which governs how interpolation might recover
    /// a sample at time `t`. For example, when a [`Between`] value is returned, its contents can
    /// be used to interpolate between the two contained values with the given parameter. The other
    /// variants give additional context about where the value is relative to the family of samples.
    ///
    /// [`Between`]: `InterpolationDatum::Between`
    pub fn sample_interp(&self, t: f32) -> InterpolationDatum<&T> {
        uneven_interp(&self.times, t).map(|idx| &self.samples[idx])
    }

    /// Like [`sample_interp`], but the returned values include the sample times. This can be
    /// useful when sample interpolation is not scale-invariant.
    ///
    /// [`sample_interp`]: UnevenCore::sample_interp
    pub fn sample_interp_timed(&self, t: f32) -> InterpolationDatum<(f32, &T)> {
        uneven_interp(&self.times, t).map(|idx| (self.times[idx], &self.samples[idx]))
    }

    /// This core, but with the sample times moved by the map `f`.
    /// In principle, when `f` is monotone, this is equivalent to [`Curve::reparametrize`],
    /// but the function inputs to each are inverses of one another.
    ///
    /// The samples are re-sorted by time after mapping and deduplicated by output time, so
    /// the function `f` should generally be injective over the set of sample times, otherwise
    /// data will be deleted.
    ///
    /// [`Curve::reparametrize`]: crate::curve::Curve::reparametrize
    #[must_use]
    pub fn map_sample_times(mut self, f: impl Fn(f32) -> f32) -> UnevenCore<T> {
        let mut timed_samples = self
            .times
            .into_iter()
            .map(f)
            .zip(self.samples)
            .collect_vec();
        timed_samples.sort_by(|(t1, _), (t2, _)| t1.total_cmp(t2));
        timed_samples.dedup_by_key(|(t, _)| *t);
        (self.times, self.samples) = timed_samples.into_iter().unzip();
        self
    }
}

/// The data core of a curve using uneven samples (i.e. keyframes), where each sample time
/// yields some fixed number of values — the [sampling width]. This may serve as storage for
/// curves that yield vectors or iterators, and in some cases, it may be useful for cache locality
/// if the sample type can effectively be encoded as a fixed-length slice of values.
///
/// [sampling width]: ChunkedUnevenCore::width
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct ChunkedUnevenCore<T> {
    /// The times, one for each sample.
    ///
    /// # Invariants
    /// This must always have a length of at least 2, be sorted, and have no duplicated or
    /// non-finite times.
    pub times: Vec<f32>,

    /// The values that are used in sampling. Each width-worth of these correspond to a single sample.
    ///
    /// # Invariants
    /// The length of this vector must always be some fixed integer multiple of that of `times`.
    pub values: Vec<T>,
}

/// An error that indicates that a [`ChunkedUnevenCore`] could not be formed.
#[derive(Debug, Error, Display)]
#[display("Could not create a ChunkedUnevenCore")]
pub enum ChunkedUnevenCoreError {
    /// The width of a `ChunkedUnevenCore` cannot be zero.
    #[display("Chunk width must be at least 1")]
    ZeroWidth,

    /// At least two sample times are necessary to interpolate in `ChunkedUnevenCore`.
    #[display(
        "Need at least two unique samples to create a ChunkedUnevenCore, but {samples} were provided"
    )]
    NotEnoughSamples {
        /// The number of samples that were provided.
        samples: usize,
    },

    /// The length of the value buffer is supposed to be the `width` times the number of samples.
    #[display("Expected {expected} total values based on width, but {actual} were provided")]
    MismatchedLengths {
        /// The expected length of the value buffer.
        expected: usize,
        /// The actual length of the value buffer.
        actual: usize,
    },

    /// Tried to infer the width, but the ratio of lengths wasn't an integer, so no such length exists.
    #[display("The length of the list of values ({values_len}) was not divisible by that of the list of times ({times_len})")]
    NonDivisibleLengths {
        /// The length of the value buffer.
        values_len: usize,
        /// The length of the time buffer.
        times_len: usize,
    },
}

impl<T> ChunkedUnevenCore<T> {
    /// Create a new [`ChunkedUnevenCore`]. The given `times` are sorted, filtered to finite times,
    /// and deduplicated. See the [type-level documentation] for more information about this type.
    ///
    /// Produces an error in any of the following circumstances:
    /// - `width` is zero.
    /// - `times` has less than `2` unique valid entries.
    /// - `values` has the incorrect length relative to `times`.
    ///
    /// [type-level documentation]: ChunkedUnevenCore
    pub fn new(
        times: impl IntoIterator<Item = f32>,
        values: impl IntoIterator<Item = T>,
        width: usize,
    ) -> Result<Self, ChunkedUnevenCoreError> {
        let times = times.into_iter().collect_vec();
        let values = values.into_iter().collect_vec();

        if width == 0 {
            return Err(ChunkedUnevenCoreError::ZeroWidth);
        }

        let times = filter_sort_dedup_times(times);

        if times.len() < 2 {
            return Err(ChunkedUnevenCoreError::NotEnoughSamples {
                samples: times.len(),
            });
        }

        if values.len() != times.len() * width {
            return Err(ChunkedUnevenCoreError::MismatchedLengths {
                expected: times.len() * width,
                actual: values.len(),
            });
        }

        Ok(Self { times, values })
    }

    /// Create a new [`ChunkedUnevenCore`], inferring the width from the sizes of the inputs.
    /// The given `times` are sorted, filtered to finite times, and deduplicated. See the
    /// [type-level documentation] for more information about this type. Prefer using [`new`]
    /// if possible, since that constructor has richer error checking.
    ///
    /// Produces an error in any of the following circumstances:
    /// - `values` has length zero.
    /// - `times` has less than `2` unique valid entries.
    /// - The length of `values` is not divisible by that of `times` (once sorted, filtered,
    ///   and deduplicated).
    ///
    /// The [width] is implicitly taken to be the length of `values` divided by that of `times`
    /// (once sorted, filtered, and deduplicated).
    ///
    /// [type-level documentation]: ChunkedUnevenCore
    /// [`new`]: ChunkedUnevenCore::new
    /// [width]: ChunkedUnevenCore::width
    pub fn new_width_inferred(
        times: impl IntoIterator<Item = f32>,
        values: impl IntoIterator<Item = T>,
    ) -> Result<Self, ChunkedUnevenCoreError> {
        let times = times.into_iter().collect_vec();
        let values = values.into_iter().collect_vec();

        let times = filter_sort_dedup_times(times);

        if times.len() < 2 {
            return Err(ChunkedUnevenCoreError::NotEnoughSamples {
                samples: times.len(),
            });
        }

        if values.len() % times.len() != 0 {
            return Err(ChunkedUnevenCoreError::NonDivisibleLengths {
                values_len: values.len(),
                times_len: times.len(),
            });
        }

        if values.is_empty() {
            return Err(ChunkedUnevenCoreError::ZeroWidth);
        }

        Ok(Self { times, values })
    }

    /// The domain of the curve derived from this core.
    ///
    /// # Panics
    /// This may panic if this type's invariants aren't met.
    #[inline]
    pub fn domain(&self) -> Interval {
        let start = self.times.first().unwrap();
        let end = self.times.last().unwrap();
        Interval::new(*start, *end).unwrap()
    }

    /// The sample width: the number of values that are contained in each sample.
    #[inline]
    pub fn width(&self) -> usize {
        self.values.len() / self.times.len()
    }

    /// Given a time `t`, obtain a [`InterpolationDatum`] which governs how interpolation might recover
    /// a sample at time `t`. For example, when a [`Between`] value is returned, its contents can
    /// be used to interpolate between the two contained values with the given parameter. The other
    /// variants give additional context about where the value is relative to the family of samples.
    ///
    /// [`Between`]: `InterpolationDatum::Between`
    #[inline]
    pub fn sample_interp(&self, t: f32) -> InterpolationDatum<&[T]> {
        uneven_interp(&self.times, t).map(|idx| self.time_index_to_slice(idx))
    }

    /// Like [`sample_interp`], but the returned values include the sample times. This can be
    /// useful when sample interpolation is not scale-invariant.
    ///
    /// [`sample_interp`]: ChunkedUnevenCore::sample_interp
    pub fn sample_interp_timed(&self, t: f32) -> InterpolationDatum<(f32, &[T])> {
        uneven_interp(&self.times, t).map(|idx| (self.times[idx], self.time_index_to_slice(idx)))
    }

    /// Given an index in [times], returns the slice of [values] that correspond to the sample at
    /// that time.
    ///
    /// [times]: ChunkedUnevenCore::times
    /// [values]: ChunkedUnevenCore::values
    #[inline]
    fn time_index_to_slice(&self, idx: usize) -> &[T] {
        let width = self.width();
        let lower_idx = width * idx;
        let upper_idx = lower_idx + width;
        &self.values[lower_idx..upper_idx]
    }
}

/// Sort the given times, deduplicate them, and filter them to only finite times.
fn filter_sort_dedup_times(times: impl IntoIterator<Item = f32>) -> Vec<f32> {
    // Filter before sorting/deduplication so that NAN doesn't interfere with them.
    let mut times = times.into_iter().filter(|t| t.is_finite()).collect_vec();
    times.sort_by(f32::total_cmp);
    times.dedup();
    times
}

/// Given a list of `times` and a target value, get the interpolation relationship for the
/// target value in terms of the indices of the starting list. In a sense, this encapsulates the
/// heart of uneven/keyframe sampling.
///
/// `times` is assumed to be sorted, deduplicated, and consisting only of finite values. It is also
/// assumed to contain at least two values.
///
/// # Panics
/// This function will panic if `times` contains NAN.
pub fn uneven_interp(times: &[f32], t: f32) -> InterpolationDatum<usize> {
    match times.binary_search_by(|pt| pt.partial_cmp(&t).unwrap()) {
        Ok(index) => InterpolationDatum::Exact(index),
        Err(index) => {
            if index == 0 {
                // This is before the first keyframe.
                InterpolationDatum::LeftTail(0)
            } else if index >= times.len() {
                // This is after the last keyframe.
                InterpolationDatum::RightTail(times.len() - 1)
            } else {
                // This is actually in the middle somewhere.
                let t_lower = times[index - 1];
                let t_upper = times[index];
                let s = (t - t_lower) / (t_upper - t_lower);
                InterpolationDatum::Between(index - 1, index, s)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{ChunkedUnevenCore, EvenCore, UnevenCore};
    use crate::curve::{cores::InterpolationDatum, interval};
    use approx::{assert_abs_diff_eq, AbsDiffEq};

    fn approx_between<T>(datum: InterpolationDatum<T>, start: T, end: T, p: f32) -> bool
    where
        T: PartialEq,
    {
        if let InterpolationDatum::Between(m_start, m_end, m_p) = datum {
            m_start == start && m_end == end && m_p.abs_diff_eq(&p, 1e-6)
        } else {
            false
        }
    }

    fn is_left_tail<T>(datum: InterpolationDatum<T>) -> bool {
        matches!(datum, InterpolationDatum::LeftTail(_))
    }

    fn is_right_tail<T>(datum: InterpolationDatum<T>) -> bool {
        matches!(datum, InterpolationDatum::RightTail(_))
    }

    fn is_exact<T>(datum: InterpolationDatum<T>, target: T) -> bool
    where
        T: PartialEq,
    {
        if let InterpolationDatum::Exact(v) = datum {
            v == target
        } else {
            false
        }
    }

    #[test]
    fn even_sample_interp() {
        let even_core = EvenCore::<f32>::new(
            interval(0.0, 1.0).unwrap(),
            // 11 entries -> 10 segments
            vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
        )
        .expect("Failed to construct test core");

        let datum = even_core.sample_interp(-1.0);
        assert!(is_left_tail(datum));
        let datum = even_core.sample_interp(0.0);
        assert!(is_left_tail(datum));
        let datum = even_core.sample_interp(1.0);
        assert!(is_right_tail(datum));
        let datum = even_core.sample_interp(2.0);
        assert!(is_right_tail(datum));

        let datum = even_core.sample_interp(0.05);
        let InterpolationDatum::Between(0.0, 1.0, p) = datum else {
            panic!("Sample did not lie in the correct subinterval")
        };
        assert_abs_diff_eq!(p, 0.5);

        let datum = even_core.sample_interp(0.05);
        assert!(approx_between(datum, &0.0, &1.0, 0.5));
        let datum = even_core.sample_interp(0.33);
        assert!(approx_between(datum, &3.0, &4.0, 0.3));
        let datum = even_core.sample_interp(0.78);
        assert!(approx_between(datum, &7.0, &8.0, 0.8));

        let datum = even_core.sample_interp(0.5);
        assert!(approx_between(datum, &4.0, &5.0, 1.0) || approx_between(datum, &5.0, &6.0, 0.0));
        let datum = even_core.sample_interp(0.7);
        assert!(approx_between(datum, &6.0, &7.0, 1.0) || approx_between(datum, &7.0, &8.0, 0.0));
    }

    #[test]
    fn uneven_sample_interp() {
        let uneven_core = UnevenCore::<f32>::new(vec![
            (0.0, 0.0),
            (1.0, 3.0),
            (2.0, 9.0),
            (4.0, 10.0),
            (8.0, -5.0),
        ])
        .expect("Failed to construct test core");

        let datum = uneven_core.sample_interp(-1.0);
        assert!(is_left_tail(datum));
        let datum = uneven_core.sample_interp(0.0);
        assert!(is_exact(datum, &0.0));
        let datum = uneven_core.sample_interp(8.0);
        assert!(is_exact(datum, &(-5.0)));
        let datum = uneven_core.sample_interp(9.0);
        assert!(is_right_tail(datum));

        let datum = uneven_core.sample_interp(0.5);
        assert!(approx_between(datum, &0.0, &3.0, 0.5));
        let datum = uneven_core.sample_interp(2.5);
        assert!(approx_between(datum, &9.0, &10.0, 0.25));
        let datum = uneven_core.sample_interp(7.0);
        assert!(approx_between(datum, &10.0, &(-5.0), 0.75));

        let datum = uneven_core.sample_interp(2.0);
        assert!(is_exact(datum, &9.0));
        let datum = uneven_core.sample_interp(4.0);
        assert!(is_exact(datum, &10.0));
    }

    #[test]
    fn chunked_uneven_sample_interp() {
        let core =
            ChunkedUnevenCore::new(vec![0.0, 2.0, 8.0], vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0], 2)
                .expect("Failed to construct test core");

        let datum = core.sample_interp(-1.0);
        assert!(is_left_tail(datum));
        let datum = core.sample_interp(0.0);
        assert!(is_exact(datum, &[0.0, 1.0]));
        let datum = core.sample_interp(8.0);
        assert!(is_exact(datum, &[4.0, 5.0]));
        let datum = core.sample_interp(10.0);
        assert!(is_right_tail(datum));

        let datum = core.sample_interp(1.0);
        assert!(approx_between(datum, &[0.0, 1.0], &[2.0, 3.0], 0.5));
        let datum = core.sample_interp(3.0);
        assert!(approx_between(datum, &[2.0, 3.0], &[4.0, 5.0], 1.0 / 6.0));

        let datum = core.sample_interp(2.0);
        assert!(is_exact(datum, &[2.0, 3.0]));
    }
}