bevy_math/curve/
iterable.rs

1//! Iterable curves, which sample in the form of an iterator in order to support `Vec`-like
2//! output whose length cannot be known statically.
3
4use super::{ConstantCurve, Interval};
5
6/// A curve which provides samples in the form of [`Iterator`]s.
7///
8/// This is an abstraction that provides an interface for curves which look like `Curve<Vec<T>>`
9/// but side-stepping issues with allocation on sampling. This happens when the size of an output
10/// array cannot be known statically.
11pub trait IterableCurve<T> {
12    /// The interval over which this curve is parametrized.
13    fn domain(&self) -> Interval;
14
15    /// Sample a point on this curve at the parameter value `t`, producing an iterator over values.
16    /// This is the unchecked version of sampling, which should only be used if the sample time `t`
17    /// is already known to lie within the curve's domain.
18    ///
19    /// Values sampled from outside of a curve's domain are generally considered invalid; data which
20    /// is nonsensical or otherwise useless may be returned in such a circumstance, and extrapolation
21    /// beyond a curve's domain should not be relied upon.
22    fn sample_iter_unchecked(&self, t: f32) -> impl Iterator<Item = T>;
23
24    /// Sample this curve at a specified time `t`, producing an iterator over sampled values.
25    /// The parameter `t` is clamped to the domain of the curve.
26    fn sample_iter_clamped(&self, t: f32) -> impl Iterator<Item = T> {
27        let t_clamped = self.domain().clamp(t);
28        self.sample_iter_unchecked(t_clamped)
29    }
30
31    /// Sample this curve at a specified time `t`, producing an iterator over sampled values.
32    /// If the parameter `t` does not lie in the curve's domain, `None` is returned.
33    fn sample_iter(&self, t: f32) -> Option<impl Iterator<Item = T>> {
34        if self.domain().contains(t) {
35            Some(self.sample_iter_unchecked(t))
36        } else {
37            None
38        }
39    }
40}
41
42impl<T> IterableCurve<T> for ConstantCurve<Vec<T>>
43where
44    T: Clone,
45{
46    fn domain(&self) -> Interval {
47        self.domain
48    }
49
50    fn sample_iter_unchecked(&self, _t: f32) -> impl Iterator<Item = T> {
51        self.value.iter().cloned()
52    }
53}