Struct enterpolation::linear::LinearDirector
source · 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()
andelements_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 withequidistant()
.
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>
impl LinearDirector<Unknown, Unknown, Identity, Unknown>
source§impl<F> LinearDirector<Unknown, Unknown, F, Unknown>
impl<F> LinearDirector<Unknown, Unknown, F, Unknown>
sourcepub fn elements<E>(
self,
elements: E
) -> Result<LinearDirector<Unknown, E, F, WithoutWeight>, TooFewElements>where
E: DiscreteGenerator,
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.
sourcepub 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,
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>
impl<E, F, W> LinearDirector<Unknown, E, F, W>
sourcepub fn knots<K>(
self,
knots: K
) -> Result<LinearDirector<Sorted<K>, E, F, W>, LinearError>where
E: DiscreteGenerator,
K: DiscreteGenerator,
K::Output: PartialOrd,
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.
sourcepub fn equidistant<R>(self) -> LinearDirector<Type<R>, E, F, W>
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,
impl<R, E, F, W> LinearDirector<Type<R>, E, F, W>where E: DiscreteGenerator, R: Real + FromPrimitive,
sourcepub fn domain(self, start: R, end: R) -> LinearDirector<Equidistant<R>, E, F, W>
pub fn domain(self, start: R, end: R) -> LinearDirector<Equidistant<R>, E, F, W>
Set the domain of the interpolation.
sourcepub fn normalized(self) -> LinearDirector<Equidistant<R>, E, F, W>
pub fn normalized(self) -> LinearDirector<Equidistant<R>, E, F, W>
Set the domain of the interpolation to be [0.0,1.0].
sourcepub fn distance(
self,
start: R,
step: R
) -> LinearDirector<Equidistant<R>, E, F, W>
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,
impl<K, E, F, W> LinearDirector<K, E, F, W>where K: SortedGenerator,
sourcepub fn easing<FF>(self, easing: FF) -> LinearDirector<K, E, FF, W>
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,
impl<K, E, F> LinearDirector<K, E, F, WithoutWeight>where E: DiscreteGenerator, K: SortedGenerator, E::Output: Merge<K::Output>, K::Output: Real,
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>,
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>,
Trait Implementations§
source§impl<K: Clone, E: Clone, F: Clone, W: Clone> Clone for LinearDirector<K, E, F, W>
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>
fn clone(&self) -> LinearDirector<K, E, F, W>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more