Struct scribl_curves::Lerp[][src]

pub struct Lerp { /* fields omitted */ }

Specifies interpolations between two sets of times.

This struct maintains two lists of "key-frame" times, which it uses for mapping times from one scale to another. The two lists of key-frames must have the same length; the ith key-frame of one list is mapped to the ith key-frame of the other list, and times that fall in between key-frames are mapped using linear interpolation. There are various different ways that you can handle times that lie outside the range of key-frames, as illustrated in the examples below.

Examples


let t = |x| Time::from_micros(x);

// Create an "identity" lerp that just maps the interval [100, 200] to itself.
let mut lerp = Lerp::identity(t(100), t(200));

// Now map time 150 to time 180. The interval [100, 150] will be stretched to cover
// [100, 180], and the interval [150, 200] will be squished to [180, 200].
lerp.add_lerp(t(150), t(180));

// The midpoint between 100 and 150 will be mapped to the midpoint between 100 and 180.
assert_eq!(lerp.lerp(t(125)), Some(t(140)));
// The midpoint between 150 and 200 will be mapped to the midpoint between 180 and 200.
assert_eq!(lerp.lerp(t(175)), Some(t(190)));

// Adding a lerp is like taking the *composition* of the two mappings. So taking the previous
// lerp and then sending 140 to 150 means that the original 125 will get sent to 150.
lerp.add_lerp(t(140), t(150));
assert_eq!(lerp.lerp(t(125)), Some(t(150)));
assert_eq!(lerp.lerp(t(120)), Some(t(140)));

// When we map a time to something outside the current output range, something special happens:
// all of the output times will get "squished" to the new time. Currently, the interval
// [125, 200] is getting mapped to [150, 200]; after this line, that whole interval will get
// mapped to 250.
lerp.add_lerp(t(150), t(250));
assert_eq!(lerp.lerp(t(125)), Some(t(250)));
assert_eq!(lerp.lerp(t(199)), Some(t(250)));

// There are three different ways that elements outside the domain can be handled.
// The `lerp` method returns `None` if asked to map something outside its domain.
assert_eq!(lerp.lerp(t(201)), None);
// The `lerp_clamped` method effectively rounds its input to the nearest end of the domain.
assert_eq!(lerp.lerp_clamped(t(201)), t(250));
// The `lerp_extended` method extends the map past the original domain by linear interpolation.
assert_eq!(lerp.lerp_extended(t(201)), t(251));

Implementations

impl Lerp[src]

pub fn identity(start: Time, end: Time) -> Lerp[src]

Creates a new Lerp representing the identity mapping on the interval [start, end].

That is, start will be mapped to start, end will be mapped to end, and everything in between will also be mapped to itself. Times outside the interval [start, end] will be treated differently depending on which of the lerping functions you call.

Examples

let t = |x| Time::from_micros(x);
assert_eq!(Lerp::identity(t(0), t(10)).lerp(t(5)), Some(t(5)));
assert_eq!(Lerp::identity(t(0), t(10)).lerp(t(10)), Some(t(10)));

pub fn first(&self) -> Time[src]

The first time in the range of the mapping.

Examples

let t = |x| Time::from_micros(x);
assert_eq!(
    Lerp::identity(t(10), t(20))
        .with_new_lerp(t(10), t(9))
        .first(),
    t(9)
);

pub fn last(&self) -> Time[src]

The last time in the range of the mapping.

Examples

let t = |x| Time::from_micros(x);
assert_eq!(
    Lerp::identity(t(10), t(20))
        .with_new_lerp(t(20), t(25))
        .last(),
    t(25)
);

pub fn times(&self) -> &[Time][src]

All of the key frames in the range of the mapping.

Examples

let t = |x| Time::from_micros(x);
assert_eq!(
    Lerp::identity(t(10), t(20))
        .with_new_lerp(t(15), t(18))
        .times(),
    &[t(10), t(18), t(20)]
);

pub fn shifted(&self, shift: TimeDiff) -> Lerp[src]

Returns a clone of this Lerp, with all times shifted by shift.

pub fn lerp(&self, t: Time) -> Option<Time>[src]

pub fn lerp_clamped(&self, t: Time) -> Time[src]

pub fn lerp_extended(&self, t: Time) -> Time[src]

pub fn unlerp(&self, t: Time) -> Option<Time>[src]

pub fn unlerp_clamped(&self, t: Time) -> Time[src]

pub fn unlerp_extended(&self, t: Time) -> Time[src]

pub fn add_lerp(&mut self, time_from: Time, time_to: Time)[src]

pub fn with_new_lerp(&self, time_from: Time, time_to: Time) -> Lerp[src]

Trait Implementations

impl Clone for Lerp[src]

impl Debug for Lerp[src]

impl<'de> Deserialize<'de> for Lerp[src]

impl From<Lerp> for Lerp[src]

impl PartialEq<Lerp> for Lerp[src]

impl Serialize for Lerp[src]

impl StructuralPartialEq for Lerp[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> AnyEq for T where
    T: PartialEq<T> + Any

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> RoundFrom<T> for T

impl<T, U> RoundInto<U> for T where
    U: RoundFrom<T>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.