eazy_core/easing/polynomial/
cubic.rs

1//! # The Cubic Curve.
2//!
3//! An algebric curve of degree three.
4//!
5//! #### formula.
6//!
7//! `p^3`
8
9use wide::f32x8;
10
11use crate::easing::Curve;
12
13/// ### The [`InCubic`] Easing Function.
14///
15/// #### examples.
16///
17/// ```
18/// use eazy::Curve;
19/// use eazy::polynomial::cubic::InCubic;
20///
21/// let p = InCubic.y(1.0);
22/// ```
23/// `f(t) = 2.0^(10.0 * (t - 1.0))`
24#[derive(Debug)]
25pub struct InCubic;
26
27impl Curve for InCubic {
28  #[inline(always)]
29  fn y(&self, p: f32) -> f32 {
30    p * p * p
31  }
32}
33
34#[test]
35fn test_in_cubic() {
36  let p = InCubic.y(1.0);
37
38  assert_eq!(p, 1.0);
39}
40
41/// ### The [`OutCubic`] Easing Function.
42///
43/// #### examples.
44///
45/// ```
46/// use eazy::Curve;
47/// use eazy::polynomial::cubic::OutCubic;
48///
49/// let p = OutCubic.y(1.0);
50/// ```
51#[derive(Debug)]
52pub struct OutCubic;
53
54impl Curve for OutCubic {
55  #[inline(always)]
56  fn y(&self, p: f32) -> f32 {
57    let m = p - 1.0;
58
59    1.0 + m * m * m
60  }
61}
62
63#[test]
64fn test_out_cubic() {
65  let p = OutCubic.y(1.0);
66
67  assert_eq!(p, 1.0);
68}
69
70/// ### The [`InOutCubic`] Easing Function.
71///
72/// #### examples.
73///
74/// ```
75/// use eazy::Curve;
76/// use eazy::polynomial::cubic::InOutCubic;
77///
78/// let p = InOutCubic.y(1.0);
79/// ```
80#[derive(Debug)]
81pub struct InOutCubic;
82
83impl Curve for InOutCubic {
84  #[inline(always)]
85  fn y(&self, p: f32) -> f32 {
86    let m = p - 1.0;
87    let t = p * 2.0;
88
89    if t < 1.0 {
90      p * t * t
91    } else {
92      1.0 + m * m * m * 4.0
93    }
94  }
95}
96
97use wide::CmpLt;
98
99pub fn in_out_cubic_simd(p: f32x8) -> f32x8 {
100  let one = f32x8::splat(1.0);
101  let two = f32x8::splat(2.0);
102  let four = f32x8::splat(4.0);
103
104  let m = p - one;
105  let t = p * two;
106
107  let t_branch = p * t * t;
108  let m_branch = one + m * m * m * four;
109
110  let mask = t.simd_lt(one);
111
112  mask.blend(t_branch, m_branch)
113}
114
115#[test]
116fn test_in_out_cubic() {
117  let p = InOutCubic.y(1.0);
118
119  assert_eq!(p, 1.0);
120}