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>
impl<const N: usize, S: Numeric, T: Numeric> InterpolationBucket<N, S, T>
Sourcepub fn new(
range: impl Into<ReversibleRange<S>>,
values_lo: [T; N],
values_hi: [T; N],
) -> Self
pub fn new( range: impl Into<ReversibleRange<S>>, values_lo: [T; N], values_hi: [T; N], ) -> Self
Create a new interpolation bucket.
rangeis the range of values that this bucket interpolates between.values_lois the set of values to interpolate from.values_hiis 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?
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]);
}Sourcepub const fn new_const(
range: (S, S),
values_lo: [T; N],
values_hi: [T; N],
) -> Self
pub const fn new_const( range: (S, S), values_lo: [T; N], values_hi: [T; N], ) -> Self
Create a new interpolation bucket.
rangeis the range of values that this bucket interpolates between.values_lois the set of values to interpolate from.values_hiis 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.
Sourcepub fn range(&self) -> &ReversibleRange<S>
pub fn range(&self) -> &ReversibleRange<S>
Get the range of values that this bucket interpolates between.
Sourcepub fn interpolate(&self, s: S) -> [T; N]
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?
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]);
}Sourcepub fn reverse_interpolate(&self, input: &[T; N]) -> Option<S>
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>
impl<const N: usize, S: Clone + Numeric, T: Clone + Numeric> Clone for InterpolationBucket<N, S, T>
Source§fn clone(&self) -> InterpolationBucket<N, S, T>
fn clone(&self) -> InterpolationBucket<N, S, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const N: usize, S: Debug + Numeric, T: Debug + Numeric> Debug for InterpolationBucket<N, S, T>
impl<const N: usize, S: Debug + Numeric, T: Debug + Numeric> Debug for InterpolationBucket<N, S, T>
Source§impl<const N: usize, S: Hash + Numeric, T: Hash + Numeric> Hash for InterpolationBucket<N, S, T>
impl<const N: usize, S: Hash + Numeric, T: Hash + Numeric> Hash for InterpolationBucket<N, S, T>
Source§impl<const N: usize, S: PartialEq + Numeric, T: PartialEq + Numeric> PartialEq for InterpolationBucket<N, S, T>
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
fn eq(&self, other: &InterpolationBucket<N, S, T>) -> bool
self and other values to be equal, and is used by ==.