splines::spline

Struct 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>

Source§

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

The type of the elements being iterated over.
Source§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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.