pub struct LinearDirector<K, E, F, W> { /* private fields */ }
Expand description

Builder for linear interpolation.

This struct helps create linear interpolations. The differene between this struct and LinearBuilder is that this struct may have other fallible methods and not only the build() method.

Before building, one has to give information for:

  • The elements the interpolation should use. Methods like elements() and elements_with_weights() exist for that cause.
  • The knots the interpolation uses. This can be seen as the spacing between those elements. Either by giving them directly with knots() or by using equidistant knots with equidistant().
let linear = LinearDirector::new()
                .elements([1.0,5.0,100.0])?
                .equidistant::<f64>()
                .normalized()
                .build();
let results = [1.0,3.0,5.0,52.5,100.0];
for (value,result) in linear.take(5).zip(results.iter().copied()){
    assert_f64_near!(value, result);
}

Sometimes the spacing between elements should not also be linear, such creating a quasi-linear interpolation. To achieve this, one may use the easing() function. A working example of this can be seen in plateaus.rs.

Linear equidistant constant interpolations are often wanted to define some specific curve (like a specific gradient). To create such interpolation, the builder pattern can not be used yet. Instead one should create a linear interpolation directly with the equidistant_unchecked() constructor.

Implementations§

source§

impl LinearDirector<Unknown, Unknown, Identity, Unknown>

source

pub const fn new() -> Self

Create a new linear interpolation builder.

source§

impl<F> LinearDirector<Unknown, Unknown, F, Unknown>

source

pub fn elements<E>( self, elements: E ) -> Result<LinearDirector<Unknown, E, F, WithoutWeight>, TooFewElements>where E: DiscreteGenerator,

Set the elements of the linear interpolation.

Errors

Returns TooFewElements if not at least 2 elements are given.

source

pub fn elements_with_weights<G>( self, gen: G ) -> Result<LinearDirector<Unknown, Weights<G>, F, WithWeight>, TooFewElements>where G: DiscreteGenerator, G::Output: IntoWeight, <G::Output as IntoWeight>::Element: Mul<<G::Output as IntoWeight>::Weight, Output = <G::Output as IntoWeight>::Element>, <G::Output as IntoWeight>::Weight: Zero + Copy,

Set the elements and their weights for this interpolation.

Weights of Zero can achieve unwanted results as their corresponding elements are considered to be at infinity. In this case the interpolation may generate NaN, infinite or even panic as elements are divided by Zero.

If you want to work with points at infinity, you may want to use homogeneous data itself without this wrapping mechanism.

Examples
let linear = Linear::builder()
                .elements_with_weights([(1.0,1.0),(2.0,4.0),(3.0,0.0)])
                .equidistant::<f64>()
                .normalized()
                .build()?;
let results = [1.0,1.8,2.0,2.75,f64::INFINITY];
for (value,result) in linear.take(5).zip(results.iter().copied()){
    assert_f64_near!(value, result);
}
Errors

Returns TooFewElements if not at least 2 elements are given.

source§

impl<E, F, W> LinearDirector<Unknown, E, F, W>

source

pub fn knots<K>( self, knots: K ) -> Result<LinearDirector<Sorted<K>, E, F, W>, LinearError>where E: DiscreteGenerator, K: DiscreteGenerator, K::Output: PartialOrd,

Set the knots of the interpolation.

The amount of knots must be equal to the amount of elements.

Performance

If you have equidistant knots, near equidistant knots are you do not really care about knots, consider using equidistant() instead.

Errors

Returns KnotElementInequality if the number of knots is not equal to the number of elements. Returns NotSorted if the knots are not sorted such that they are increasing.

source

pub fn equidistant<R>(self) -> LinearDirector<Type<R>, E, F, W>

Build an interpolation with equidistant knots.

This method takes R as a generic parameter. R has to be the type you want the knots to be. Often this is just f32 or f64.

After this call, you also have to call either of

which all define the domain of the interpolation and the spacing of the knots.

Performance

This may drastically increase performance, as one does not have to use binary search to find the relevant knots in an interpolation.

source§

impl<R, E, F, W> LinearDirector<Type<R>, E, F, W>where E: DiscreteGenerator, R: Real + FromPrimitive,

source

pub fn domain(self, start: R, end: R) -> LinearDirector<Equidistant<R>, E, F, W>

Set the domain of the interpolation.

source

pub fn normalized(self) -> LinearDirector<Equidistant<R>, E, F, W>

Set the domain of the interpolation to be [0.0,1.0].

source

pub fn distance( self, start: R, step: R ) -> LinearDirector<Equidistant<R>, E, F, W>

Set the domain of the interpolation by defining the distance between the knots

source§

impl<K, E, F, W> LinearDirector<K, E, F, W>where K: SortedGenerator,

source

pub fn easing<FF>(self, easing: FF) -> LinearDirector<K, E, FF, W>

Sets an easing function.

This allows quasi-linear interpolations. Before merging two elements together with a factor, the factor is send to the given function before and the output is the new factor.

Examples

See the plateau example for more information.

source§

impl<K, E, F> LinearDirector<K, E, F, WithoutWeight>where E: DiscreteGenerator, K: SortedGenerator, E::Output: Merge<K::Output>, K::Output: Real,

source

pub fn build(self) -> Linear<K, E, F>

Build a linear interpolation.

source§

impl<K, G, F> LinearDirector<K, Weights<G>, F, WithWeight>where K: SortedGenerator, K::Output: Real + Copy, G: DiscreteGenerator, G::Output: IntoWeight, <Weights<G> as Generator<usize>>::Output: Merge<K::Output>,

source

pub fn build(self) -> Weighted<Linear<K, Weights<G>, F>>

Build a weighted linear interpolation.

Trait Implementations§

source§

impl<K: Clone, E: Clone, F: Clone, W: Clone> Clone for LinearDirector<K, E, F, W>

source§

fn clone(&self) -> LinearDirector<K, E, F, W>

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<K: Debug, E: Debug, F: Debug, W: Debug> Debug for LinearDirector<K, E, F, W>

source§

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

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

impl Default for LinearDirector<Unknown, Unknown, Identity, Unknown>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, E, F, W> RefUnwindSafe for LinearDirector<K, E, F, W>where E: RefUnwindSafe, F: RefUnwindSafe, K: RefUnwindSafe, W: RefUnwindSafe,

§

impl<K, E, F, W> !Send for LinearDirector<K, E, F, W>

§

impl<K, E, F, W> !Sync for LinearDirector<K, E, F, W>

§

impl<K, E, F, W> Unpin for LinearDirector<K, E, F, W>where E: Unpin, F: Unpin, K: Unpin,

§

impl<K, E, F, W> UnwindSafe for LinearDirector<K, E, F, W>where E: UnwindSafe, F: UnwindSafe, K: UnwindSafe, W: RefUnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.