rust_rocket/interpolation.rs
1//! This module contains anything related to interpolation.
2
3/// The `Interpolation` Type.
4/// This represents the various forms of interpolation that can be performed.
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
7#[derive(Debug, Copy, Clone)]
8pub enum Interpolation {
9 /// `0`
10 Step = 0,
11 /// `t`
12 Linear = 1,
13 /// `t * t * (3 - 2 * t)`
14 Smooth = 2,
15 /// `t.powi(2)`
16 Ramp = 3,
17}
18
19impl From<u8> for Interpolation {
20 fn from(raw: u8) -> Interpolation {
21 match raw {
22 0 => Interpolation::Step,
23 1 => Interpolation::Linear,
24 2 => Interpolation::Smooth,
25 3 => Interpolation::Ramp,
26 _ => Interpolation::Step,
27 }
28 }
29}
30
31impl Interpolation {
32 /// This performs the interpolation.
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// # use rust_rocket::interpolation::Interpolation;
38 /// assert_eq!(Interpolation::Linear.interpolate(0.5), 0.5);
39 /// ```
40 ///
41 /// ```
42 /// # use rust_rocket::interpolation::Interpolation;
43 /// assert_eq!(Interpolation::Step.interpolate(0.5), 0.);
44 /// ```
45 pub fn interpolate(&self, t: f32) -> f32 {
46 match *self {
47 Interpolation::Step => 0.0,
48 Interpolation::Linear => t,
49 Interpolation::Smooth => t * t * (3.0 - 2.0 * t),
50 Interpolation::Ramp => t.powi(2),
51 }
52 }
53}