lineic

Struct InterpolationBucket

Source
pub struct InterpolationBucket<const N: usize, S: Numeric, T: Numeric> { /* private fields */ }
Expand description

A value set for interpolation.
Interpolates between 2 sets of values based on a range.

For interpolating between more than 2 data sets, see crate::LinearInterpolator.

§Example

use lineic::InterpolationBucket;

const RED: [u8; 3] = [0xB8, 0x1D, 0x13];
const GRN: [u8; 3] = [0x00, 0x84, 0x50];

let bucket = InterpolationBucket::new(0.0..=100.0, RED, GRN);

// Interpolate between RED and GRN at 50% of the range
let interpolated = bucket.interpolate(50.0);

Implementations§

Source§

impl<const N: usize, S: Numeric, T: Numeric> InterpolationBucket<N, S, T>

Source

pub fn new( range: impl Into<ReversibleRange<S>>, values_lo: [T; N], values_hi: [T; N], ) -> Self

Create a new interpolation bucket.

  • range is the range of values that this bucket interpolates between.
  • values_lo is the set of values to interpolate from.
  • values_hi is the set of values to interpolate to.

This will enable the bucket to smoothly interpolate from lo to hi for T values in the range.
Values < range min will be clamped to lo.
Values > range max will be clamped to hi.

Examples found in repository?
examples/start_here.rs (line 9)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    // The simplest possible use of the library is mapping one range to another
    // Here we can map values in the range 0.0..=10.0 to the range 30.0..=35.0
    let interpolator = F32InterpolationBucket::new(0.0..=10.0, [30.0], [35.0]);
    assert_eq!(interpolator.interpolate(5.0), [32.5]);

    // The target does not have to be a single value - here we interpolate across a pair of RGB values
    // The result is a smooth gradient from red to green for values in the range 0.0..=10.0
    let interpolator =
        F32InterpolationBucket::new(0.0..=10.0, [255.0, 0.0, 0.0], [0.0, 255.0, 0.0]);
    assert_eq!(interpolator.interpolate(5.0), [127.5, 127.5, 0.0]);

    // The library can also interpolate smoothly across multiple buckets
    // This example forms a sort of traffic light sequence, interpolating between red, yellow, and green
    // The range is reversed here to demonstrate that the library can handle that
    let interpolator = F32LinearInterpolator::new(
        10.0..=0.0,
        &[[0.0, 255.0, 0.0], [255.0, 255.0, 0.0], [255.0, 0.0, 0.0]],
    );
    assert_eq!(interpolator.interpolate(5.0), [255.0, 255.0, 0.0]);
    assert_eq!(interpolator.interpolate(0.0), [255.0, 0.0, 0.0]);

    // The types for the range and values do not need to the same
    // Here a f64 range is used to interpolate between u8 values
    let interpolator: LinearInterpolator<'_, 3, f64, u8> =
        LinearInterpolator::new(0.0..=10.0, &[[0, 255, 0], [255, 255, 0], [255, 0, 0]]);
    assert_eq!(interpolator.interpolate(5.0), [127, 127, 0]);
    assert_eq!(interpolator.interpolate(0.0), [0, 255, 0]);
}
Source

pub const fn new_const( range: (S, S), values_lo: [T; N], values_hi: [T; N], ) -> Self

Create a new interpolation bucket.

  • range is the range of values that this bucket interpolates between.
  • values_lo is the set of values to interpolate from.
  • values_hi is the set of values to interpolate to.

This will enable the bucket to smoothly interpolate from lo to hi for T values in the range.
Values < range min will be clamped to lo.
Values > range max will be clamped to hi.

Source

pub fn range(&self) -> &ReversibleRange<S>

Get the range of values that this bucket interpolates between.

Source

pub fn start(&self) -> S

Get the start value of the range.

Source

pub fn end(&self) -> S

Get the end value of the range.

Source

pub fn values_lo(&self) -> &[T; N]

Get the set of values to interpolate from.

Source

pub fn values_hi(&self) -> &[T; N]

Get the set of values to interpolate to.

Source

pub fn interpolate(&self, s: S) -> [T; N]

Interpolate between the 2 value sets of this bucket at the given t value. This will return a new set of values that are interpolated between values_lo and values_hi based on t’s position in the bucket’s range.

Examples found in repository?
examples/start_here.rs (line 10)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    // The simplest possible use of the library is mapping one range to another
    // Here we can map values in the range 0.0..=10.0 to the range 30.0..=35.0
    let interpolator = F32InterpolationBucket::new(0.0..=10.0, [30.0], [35.0]);
    assert_eq!(interpolator.interpolate(5.0), [32.5]);

    // The target does not have to be a single value - here we interpolate across a pair of RGB values
    // The result is a smooth gradient from red to green for values in the range 0.0..=10.0
    let interpolator =
        F32InterpolationBucket::new(0.0..=10.0, [255.0, 0.0, 0.0], [0.0, 255.0, 0.0]);
    assert_eq!(interpolator.interpolate(5.0), [127.5, 127.5, 0.0]);

    // The library can also interpolate smoothly across multiple buckets
    // This example forms a sort of traffic light sequence, interpolating between red, yellow, and green
    // The range is reversed here to demonstrate that the library can handle that
    let interpolator = F32LinearInterpolator::new(
        10.0..=0.0,
        &[[0.0, 255.0, 0.0], [255.0, 255.0, 0.0], [255.0, 0.0, 0.0]],
    );
    assert_eq!(interpolator.interpolate(5.0), [255.0, 255.0, 0.0]);
    assert_eq!(interpolator.interpolate(0.0), [255.0, 0.0, 0.0]);

    // The types for the range and values do not need to the same
    // Here a f64 range is used to interpolate between u8 values
    let interpolator: LinearInterpolator<'_, 3, f64, u8> =
        LinearInterpolator::new(0.0..=10.0, &[[0, 255, 0], [255, 255, 0], [255, 0, 0]]);
    assert_eq!(interpolator.interpolate(5.0), [127, 127, 0]);
    assert_eq!(interpolator.interpolate(0.0), [0, 255, 0]);
}
Source

pub fn reverse_interpolate(&self, input: &[T; N]) -> Option<S>

Attempt to retrieve the value within the bucket’s range that would produce the given set of values.

Trait Implementations§

Source§

impl<const N: usize, S: Clone + Numeric, T: Clone + Numeric> Clone for InterpolationBucket<N, S, T>

Source§

fn clone(&self) -> InterpolationBucket<N, S, T>

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<const N: usize, S: Debug + Numeric, T: Debug + Numeric> Debug for InterpolationBucket<N, S, T>

Source§

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

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

impl<const N: usize, S: Hash + Numeric, T: Hash + Numeric> Hash for InterpolationBucket<N, S, T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize, S: PartialEq + Numeric, T: PartialEq + Numeric> PartialEq for InterpolationBucket<N, S, T>

Source§

fn eq(&self, other: &InterpolationBucket<N, S, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, S: Eq + Numeric, T: Eq + Numeric> Eq for InterpolationBucket<N, S, T>

Source§

impl<const N: usize, S: Numeric, T: Numeric> StructuralPartialEq for InterpolationBucket<N, S, T>

Auto Trait Implementations§

§

impl<const N: usize, S, T> Freeze for InterpolationBucket<N, S, T>
where S: Freeze, T: Freeze,

§

impl<const N: usize, S, T> RefUnwindSafe for InterpolationBucket<N, S, T>

§

impl<const N: usize, S, T> Send for InterpolationBucket<N, S, T>
where S: Send, T: Send,

§

impl<const N: usize, S, T> Sync for InterpolationBucket<N, S, T>
where S: Sync, T: Sync,

§

impl<const N: usize, S, T> Unpin for InterpolationBucket<N, S, T>
where S: Unpin, T: Unpin,

§

impl<const N: usize, S, T> UnwindSafe for InterpolationBucket<N, S, T>
where S: UnwindSafe, T: 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 u8)

🔬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.