Struct splines::spline::Spline

source ·
pub struct Spline<T, V>(/* private fields */);
Expand description

Spline curve used to provide interpolation between control points (keys).

Splines are made out of control points (Key). When creating a Spline with Spline::from_vec or Spline::from_iter, the keys don’t have to be sorted (they are sorted automatically by the sampling value).

You can sample from a spline with several functions:

  • Spline::sample: allows you to sample from a spline. If not enough keys are available for the required interpolation mode, you get None.
  • Spline::clamped_sample: behaves like Spline::sample but will return either the first or last key if out of bound; it will return None if not enough key.

Implementations§

source§

impl<T, V> Spline<T, V>

source

pub fn from_vec(keys: Vec<Key<T, V>>) -> Self
where T: PartialOrd,

Create a new spline out of keys. The keys don’t have to be sorted even though it’s recommended to provide ascending sorted ones (for performance purposes).

source

pub fn from_iter<I>(iter: I) -> Self
where I: Iterator<Item = Key<T, V>>, T: PartialOrd,

Create a new spline by consuming an Iterater<Item = Key<T>>. They keys don’t have to be sorted.

§Note on iterators

It’s valid to use any iterator that implements Iterator<Item = Key<T>>. However, you should use Spline::from_vec if you are passing a Vec.

source

pub fn keys(&self) -> &[Key<T, V>]

Retrieve the keys of a spline.

source

pub fn len(&self) -> usize

Number of keys.

source

pub fn is_empty(&self) -> bool

Check whether the spline has no key.

source

pub fn sample_with_key( &self, t: T ) -> Option<(V, &Key<T, V>, Option<&Key<T, V>>)>
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd, V: Interpolate<T>,

Sample a spline at a given time, returning the interpolated value along with its associated key.

The current implementation, based on immutability, cannot perform in constant time. This means that sampling’s processing complexity is currently O(log n). It’s possible to achieve O(1) performance by using a slightly different spline type. If you are interested by this feature, an implementation for a dedicated type is foreseen yet not started yet.

§Return

None if you try to sample a value at a time that has no key associated with. That can also happen if you try to sample between two keys with a specific interpolation mode that makes the sampling impossible. For instance, Interpolation::CatmullRom requires four keys. If you’re near the beginning of the spline or its end, ensure you have enough keys around to make the sampling.

source

pub fn sample(&self, t: T) -> Option<V>
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd, V: Interpolate<T>,

Sample a spline at a given time.

source

pub fn clamped_sample_with_key( &self, t: T ) -> Option<(V, &Key<T, V>, Option<&Key<T, V>>)>
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd, V: Interpolate<T>,

Sample a spline at a given time with clamping, returning the interpolated value along with its associated key.

§Return

If you sample before the first key or after the last one, return the first key or the last one, respectively. Otherwise, behave the same way as Spline::sample.

§Error

This function returns None if you have no key.

source

pub fn clamped_sample(&self, t: T) -> Option<V>
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd, V: Interpolate<T>,

Sample a spline at a given time with clamping.

source

pub fn add(&mut self, key: Key<T, V>)
where T: PartialOrd,

Add a key into the spline.

source

pub fn remove(&mut self, index: usize) -> Option<Key<T, V>>

Remove a key from the spline.

source

pub fn replace<F>(&mut self, index: usize, f: F) -> Option<Key<T, V>>
where F: FnOnce(&Key<T, V>) -> Key<T, V>, T: PartialOrd,

Update a key and return the key already present.

The key is updated — if present — with the provided function.

§Notes

That function makes sense only if you want to change the interpolator (i.e. Key::t) of your key. If you just want to change the interpolation mode or the carried value, consider using the Spline::get_mut method instead as it will be way faster.

source

pub fn get(&self, index: usize) -> Option<&Key<T, V>>

Get a key at a given index.

source

pub fn get_mut(&mut self, index: usize) -> Option<KeyMut<'_, T, V>>

Mutably get a key at a given index.

Trait Implementations§

source§

impl<T: Clone, V: Clone> Clone for Spline<T, V>

source§

fn clone(&self) -> Spline<T, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug, V: Debug> Debug for Spline<T, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, T, V> IntoIterator for &'a Spline<T, V>

§

type Item = &'a Key<T, V>

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T, V> Freeze for Spline<T, V>

§

impl<T, V> RefUnwindSafe for Spline<T, V>

§

impl<T, V> Send for Spline<T, V>
where T: Send, V: Send,

§

impl<T, V> Sync for Spline<T, V>
where T: Sync, V: Sync,

§

impl<T, V> Unpin for Spline<T, V>
where T: Unpin, V: Unpin,

§

impl<T, V> UnwindSafe for Spline<T, V>
where T: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.