Path: blob/main/crates/bevy_math/src/cubic_splines/curve_impls.rs
6596 views
use super::{CubicSegment, RationalSegment};1use crate::common_traits::{VectorSpace, WithDerivative, WithTwoDerivatives};2use crate::curve::{3derivatives::{SampleDerivative, SampleTwoDerivatives},4Curve, Interval,5};67#[cfg(feature = "alloc")]8use super::{CubicCurve, RationalCurve};910// -- CubicSegment1112impl<P: VectorSpace<Scalar = f32>> Curve<P> for CubicSegment<P> {13#[inline]14fn domain(&self) -> Interval {15Interval::UNIT16}1718#[inline]19fn sample_unchecked(&self, t: f32) -> P {20self.position(t)21}22}2324impl<P: VectorSpace<Scalar = f32>> SampleDerivative<P> for CubicSegment<P> {25#[inline]26fn sample_with_derivative_unchecked(&self, t: f32) -> WithDerivative<P> {27WithDerivative {28value: self.position(t),29derivative: self.velocity(t),30}31}32}3334impl<P: VectorSpace<Scalar = f32>> SampleTwoDerivatives<P> for CubicSegment<P> {35#[inline]36fn sample_with_two_derivatives_unchecked(&self, t: f32) -> WithTwoDerivatives<P> {37WithTwoDerivatives {38value: self.position(t),39derivative: self.velocity(t),40second_derivative: self.acceleration(t),41}42}43}4445// -- CubicCurve4647#[cfg(feature = "alloc")]48impl<P: VectorSpace<Scalar = f32>> Curve<P> for CubicCurve<P> {49#[inline]50fn domain(&self) -> Interval {51// The non-emptiness invariant guarantees that this succeeds.52Interval::new(0.0, self.segments.len() as f32)53.expect("CubicCurve is invalid because it has no segments")54}5556#[inline]57fn sample_unchecked(&self, t: f32) -> P {58self.position(t)59}60}6162#[cfg(feature = "alloc")]63impl<P: VectorSpace<Scalar = f32>> SampleDerivative<P> for CubicCurve<P> {64#[inline]65fn sample_with_derivative_unchecked(&self, t: f32) -> WithDerivative<P> {66WithDerivative {67value: self.position(t),68derivative: self.velocity(t),69}70}71}7273#[cfg(feature = "alloc")]74impl<P: VectorSpace<Scalar = f32>> SampleTwoDerivatives<P> for CubicCurve<P> {75#[inline]76fn sample_with_two_derivatives_unchecked(&self, t: f32) -> WithTwoDerivatives<P> {77WithTwoDerivatives {78value: self.position(t),79derivative: self.velocity(t),80second_derivative: self.acceleration(t),81}82}83}8485// -- RationalSegment8687impl<P: VectorSpace<Scalar = f32>> Curve<P> for RationalSegment<P> {88#[inline]89fn domain(&self) -> Interval {90Interval::UNIT91}9293#[inline]94fn sample_unchecked(&self, t: f32) -> P {95self.position(t)96}97}9899impl<P: VectorSpace<Scalar = f32>> SampleDerivative<P> for RationalSegment<P> {100#[inline]101fn sample_with_derivative_unchecked(&self, t: f32) -> WithDerivative<P> {102WithDerivative {103value: self.position(t),104derivative: self.velocity(t),105}106}107}108109impl<P: VectorSpace<Scalar = f32>> SampleTwoDerivatives<P> for RationalSegment<P> {110#[inline]111fn sample_with_two_derivatives_unchecked(&self, t: f32) -> WithTwoDerivatives<P> {112WithTwoDerivatives {113value: self.position(t),114derivative: self.velocity(t),115second_derivative: self.acceleration(t),116}117}118}119120// -- RationalCurve121122#[cfg(feature = "alloc")]123impl<P: VectorSpace<Scalar = f32>> Curve<P> for RationalCurve<P> {124#[inline]125fn domain(&self) -> Interval {126// The non-emptiness invariant guarantees the success of this.127Interval::new(0.0, self.length())128.expect("RationalCurve is invalid because it has zero length")129}130131#[inline]132fn sample_unchecked(&self, t: f32) -> P {133self.position(t)134}135}136137#[cfg(feature = "alloc")]138impl<P: VectorSpace<Scalar = f32>> SampleDerivative<P> for RationalCurve<P> {139#[inline]140fn sample_with_derivative_unchecked(&self, t: f32) -> WithDerivative<P> {141WithDerivative {142value: self.position(t),143derivative: self.velocity(t),144}145}146}147148#[cfg(feature = "alloc")]149impl<P: VectorSpace<Scalar = f32>> SampleTwoDerivatives<P> for RationalCurve<P> {150#[inline]151fn sample_with_two_derivatives_unchecked(&self, t: f32) -> WithTwoDerivatives<P> {152WithTwoDerivatives {153value: self.position(t),154derivative: self.velocity(t),155second_derivative: self.acceleration(t),156}157}158}159160161