Trait Curve

Source
pub trait Curve<T> {
    // Required methods
    fn domain(&self) -> Interval;
    fn sample_unchecked(&self, t: f32) -> T;

    // Provided methods
    fn sample(&self, t: f32) -> Option<T> { ... }
    fn sample_clamped(&self, t: f32) -> T { ... }
}
Expand description

A trait for a type that can represent values of type T parametrized over a fixed interval.

Typical examples of this are actual geometric curves where T: VectorSpace, but other kinds of output data can be represented as well. See the module-level documentation for details.

Required Methods§

Source

fn domain(&self) -> Interval

The interval over which this curve is parametrized.

This is the range of values of t where we can sample the curve and receive valid output.

Source

fn sample_unchecked(&self, t: f32) -> T

Sample a point on this curve at the parameter value t, extracting the associated value. This is the unchecked version of sampling, which should only be used if the sample time t is already known to lie within the curve’s domain.

Values sampled from outside of a curve’s domain are generally considered invalid; data which is nonsensical or otherwise useless may be returned in such a circumstance, and extrapolation beyond a curve’s domain should not be relied upon.

Provided Methods§

Source

fn sample(&self, t: f32) -> Option<T>

Sample a point on this curve at the parameter value t, returning None if the point is outside of the curve’s domain.

Examples found in repository?
examples/animation/easing_functions.rs (line 174)
129fn display_curves(
130    mut gizmos: Gizmos,
131    ease_functions: Query<(&EaseFunctionPlot, &Transform, &Children)>,
132    mut transforms: Query<&mut Transform, Without<EaseFunctionPlot>>,
133    mut ui_text: Single<&mut Text>,
134    time: Res<Time>,
135) {
136    let samples = 100;
137    let duration = 2.5;
138    let time_margin = 0.5;
139
140    let now = ((time.elapsed_secs() % (duration + time_margin * 2.0) - time_margin) / duration)
141        .clamp(0.0, 1.0);
142
143    ui_text.0 = format!("Progress: {:.2}", now);
144
145    for (EaseFunctionPlot(function, color), transform, children) in &ease_functions {
146        let center = transform.translation.xy();
147        let half_size = PLOT_SIZE / 2.0;
148
149        // Draw a box around the curve
150        gizmos.linestrip_2d(
151            [
152                center + half_size,
153                center + half_size * Vec2::new(-1., 1.),
154                center + half_size * Vec2::new(-1., -1.),
155                center + half_size * Vec2::new(1., -1.),
156                center + half_size,
157            ],
158            color.darker(0.4),
159        );
160
161        // Draw the curve
162        let f = EasingCurve::new(0.0, 1.0, *function);
163        let drawn_curve = f
164            .by_ref()
165            .graph()
166            .map(|(x, y)| center - half_size + Vec2::new(x, y) * PLOT_SIZE);
167        gizmos.curve_2d(
168            &drawn_curve,
169            drawn_curve.domain().spaced_points(samples).unwrap(),
170            *color,
171        );
172
173        // Show progress along the curve for the current time
174        let y = f.sample(now).unwrap() * PLOT_SIZE.y;
175        transforms.get_mut(children[0]).unwrap().translation.y = -half_size.y + y;
176        transforms.get_mut(children[1]).unwrap().translation =
177            -half_size.extend(0.0) + Vec3::new(now * PLOT_SIZE.x, y, 0.0);
178
179        // Show horizontal bar at y value
180        gizmos.linestrip_2d(
181            [
182                center - half_size + Vec2::Y * y,
183                center - half_size + Vec2::new(PLOT_SIZE.x, y),
184            ],
185            color.darker(0.2),
186        );
187    }
188}
Source

fn sample_clamped(&self, t: f32) -> T

Sample a point on this curve at the parameter value t, clamping t to lie inside the domain of the curve.

Implementors§

Source§

impl Curve<f32> for EaseFunction

Source§

impl Curve<Quat> for CubicRotationCurve

Source§

impl<P> Curve<P> for CubicCurve<P>
where P: VectorSpace,

Source§

impl<P> Curve<P> for CubicSegment<P>
where P: VectorSpace,

Source§

impl<P> Curve<P> for RationalCurve<P>
where P: VectorSpace,

Source§

impl<P> Curve<P> for RationalSegment<P>
where P: VectorSpace,

Source§

impl<S, T, C, D> Curve<(S, T)> for ZipCurve<S, T, C, D>
where C: Curve<S>, D: Curve<T>,

Source§

impl<S, T, C, F> Curve<T> for MapCurve<S, T, C, F>
where C: Curve<S>, F: Fn(S) -> T,

Source§

impl<T> Curve<T> for SteppedKeyframeCurve<T>
where T: Clone,

Source§

impl<T> Curve<T> for ColorCurve<T>
where T: Mix + Clone,

Source§

impl<T> Curve<T> for AnimatableKeyframeCurve<T>
where T: Animatable + Clone,

Source§

impl<T> Curve<T> for ConstantCurve<T>
where T: Clone,

Source§

impl<T> Curve<T> for EasingCurve<T>
where T: Ease + Clone,

Source§

impl<T> Curve<T> for SampleAutoCurve<T>

Source§

impl<T> Curve<T> for UnevenSampleAutoCurve<T>

Source§

impl<T, C> Curve<(f32, T)> for GraphCurve<T, C>
where C: Curve<T>,

Source§

impl<T, C> Curve<WithDerivative<T>> for SampleDerivativeWrapper<C>
where T: HasTangent, C: SampleDerivative<T>,

Source§

impl<T, C> Curve<WithTwoDerivatives<T>> for SampleTwoDerivativesWrapper<C>

Source§

impl<T, C> Curve<T> for ForeverCurve<T, C>
where C: Curve<T>,

Source§

impl<T, C> Curve<T> for LinearReparamCurve<T, C>
where C: Curve<T>,

Source§

impl<T, C> Curve<T> for PingPongCurve<T, C>
where C: Curve<T>,

Source§

impl<T, C> Curve<T> for RepeatCurve<T, C>
where C: Curve<T>,

Source§

impl<T, C> Curve<T> for ReverseCurve<T, C>
where C: Curve<T>,

Source§

impl<T, C, D> Curve<T> for ChainCurve<T, C, D>
where C: Curve<T>, D: Curve<T>,

Source§

impl<T, C, D> Curve<T> for ContinuationCurve<T, C, D>
where T: VectorSpace, C: Curve<T>, D: Curve<T>,

Source§

impl<T, C, D> Curve<T> for CurveReparamCurve<T, C, D>
where C: Curve<T>, D: Curve<f32>,

Source§

impl<T, C, D> Curve<T> for D
where C: Curve<T> + ?Sized, D: Deref<Target = C>,

Source§

impl<T, C, F> Curve<T> for ReparamCurve<T, C, F>
where C: Curve<T>, F: Fn(f32) -> f32,

Source§

impl<T, F> Curve<T> for FunctionCurve<T, F>
where F: Fn(f32) -> T,

Source§

impl<T, I> Curve<T> for SampleCurve<T, I>
where T: Clone, I: Fn(&T, &T, f32) -> T,

Source§

impl<T, I> Curve<T> for UnevenSampleCurve<T, I>
where T: Clone, I: Fn(&T, &T, f32) -> T,

Source§

impl<V> Curve<V> for CubicKeyframeCurve<V>
where V: VectorSpace,