perf(math): added inline attribute to easing functions

This commit is contained in:
lisk77 2025-07-29 15:44:37 +02:00
parent cd58c0ee06
commit 05764965b3

View file

@ -1,128 +1,230 @@
use std::f32::consts::PI; use std::f32::consts::PI;
#[inline(always)]
pub fn ease_in_sine(x: f32) -> f32 { pub fn ease_in_sine(x: f32) -> f32 {
1.0 - ((x * PI) / 2.0).cos() 1.0 - ((x * PI) / 2.0).cos()
} }
#[inline(always)]
pub fn ease_out_sine(x: f32) -> f32 { pub fn ease_out_sine(x: f32) -> f32 {
((x * PI) / 2.0).sin() ((x * PI) / 2.0).sin()
} }
#[inline(always)]
pub fn ease_in_out_sine(x: f32) -> f32 { pub fn ease_in_out_sine(x: f32) -> f32 {
-((PI * x).cos() - 1.0) / 2.0 -((PI * x).cos() - 1.0) / 2.0
} }
#[inline(always)]
pub fn ease_in_quad(x: f32) -> f32 { pub fn ease_in_quad(x: f32) -> f32 {
x * x x * x
} }
#[inline(always)]
pub fn ease_out_quad(x: f32) -> f32 { pub fn ease_out_quad(x: f32) -> f32 {
1.0 - (1.0 - x) * (1.0 - x) 1.0 - (1.0 - x) * (1.0 - x)
} }
#[inline(always)]
pub fn ease_in_out_quad(x: f32) -> f32 { pub fn ease_in_out_quad(x: f32) -> f32 {
if x < 0.5 { 2.0 * x * x } else { 1.0 - (-2.0 * x + 2.0).powi(2) / 2.0 } if x < 0.5 {
2.0 * x * x
} else {
1.0 - (-2.0 * x + 2.0).powi(2) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_cubic(x: f32) -> f32 { pub fn ease_in_cubic(x: f32) -> f32 {
x * x * x x * x * x
} }
#[inline(always)]
pub fn ease_out_cubic(x: f32) -> f32 { pub fn ease_out_cubic(x: f32) -> f32 {
1.0 - (1.0 - x).powi(3) 1.0 - (1.0 - x).powi(3)
} }
#[inline(always)]
pub fn ease_in_out_cubic(x: f32) -> f32 { pub fn ease_in_out_cubic(x: f32) -> f32 {
if x < 0.5 { 4.0 * x * x * x } else { 1.0 - (-2.0 * x + 2.0).powi(3) / 2.0 } if x < 0.5 {
4.0 * x * x * x
} else {
1.0 - (-2.0 * x + 2.0).powi(3) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_quart(x: f32) -> f32 { pub fn ease_in_quart(x: f32) -> f32 {
x * x * x * x x * x * x * x
} }
#[inline(always)]
pub fn ease_out_quart(x: f32) -> f32 { pub fn ease_out_quart(x: f32) -> f32 {
1.0 - (1.0 - x).powi(4) 1.0 - (1.0 - x).powi(4)
} }
#[inline(always)]
pub fn ease_in_out_quart(x: f32) -> f32 { pub fn ease_in_out_quart(x: f32) -> f32 {
if x < 0.5 { 8.0 * x * x * x * x } else { 1.0 - (-2.0 * x + 2.0).powi(4) / 2.0 } if x < 0.5 {
8.0 * x * x * x * x
} else {
1.0 - (-2.0 * x + 2.0).powi(4) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_quint(x: f32) -> f32 { pub fn ease_in_quint(x: f32) -> f32 {
x * x * x * x * x x * x * x * x * x
} }
#[inline(always)]
pub fn ease_out_quint(x: f32) -> f32 { pub fn ease_out_quint(x: f32) -> f32 {
1.0 - (1.0 - x).powi(5) 1.0 - (1.0 - x).powi(5)
} }
#[inline(always)]
pub fn ease_in_out_quint(x: f32) -> f32 { pub fn ease_in_out_quint(x: f32) -> f32 {
if x < 0.5 { 16.0 * x * x * x * x * x } else { 1.0 - (-2.0 * x + 2.0).powi(5) / 2.0 } if x < 0.5 {
16.0 * x * x * x * x * x
} else {
1.0 - (-2.0 * x + 2.0).powi(5) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_expo(x: f32) -> f32 { pub fn ease_in_expo(x: f32) -> f32 {
if x == 0.0 { 0.0 } else { 2.0_f32.powf(10.0 * x - 10.0) } if x == 0.0 {
0.0
} else {
2.0_f32.powf(10.0 * x - 10.0)
}
} }
#[inline(always)]
pub fn ease_out_expo(x: f32) -> f32 { pub fn ease_out_expo(x: f32) -> f32 {
if x == 1.0 { 1.0 } else { 1.0 - 2.0_f32.powf(-10.0 * x) } if x == 1.0 {
1.0
} else {
1.0 - 2.0_f32.powf(-10.0 * x)
}
} }
#[inline(always)]
pub fn ease_in_out_expo(x: f32) -> f32 { pub fn ease_in_out_expo(x: f32) -> f32 {
if x == 0.0 { 0.0 } else if x == 1.0 { 1.0 } else if x < 0.5 { 2.0_f32.powf(20.0 * x - 10.0) / 2.0 } else { (2.0 - 2.0_f32.powf(-20.0 * x + 10.0)) / 2.0 } if x == 0.0 {
0.0
} else if x == 1.0 {
1.0
} else if x < 0.5 {
2.0_f32.powf(20.0 * x - 10.0) / 2.0
} else {
(2.0 - 2.0_f32.powf(-20.0 * x + 10.0)) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_circ(x: f32) -> f32 { pub fn ease_in_circ(x: f32) -> f32 {
1.0 - (1.0 - x * x).sqrt() 1.0 - (1.0 - x * x).sqrt()
} }
#[inline(always)]
pub fn ease_out_circ(x: f32) -> f32 { pub fn ease_out_circ(x: f32) -> f32 {
(1.0 - (x - 1.0).powi(2)).sqrt() (1.0 - (x - 1.0).powi(2)).sqrt()
} }
#[inline(always)]
pub fn ease_in_out_circ(x: f32) -> f32 { pub fn ease_in_out_circ(x: f32) -> f32 {
if x < 0.5 { (1.0 - (1.0 - 2.0 * x).powi(2)).sqrt() / 2.0 } else { ((1.0 - (-2.0 * x + 2.0).powi(2)).sqrt() + 1.0) / 2.0 } if x < 0.5 {
(1.0 - (1.0 - 2.0 * x).powi(2)).sqrt() / 2.0
} else {
((1.0 - (-2.0 * x + 2.0).powi(2)).sqrt() + 1.0) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_back(x: f32) -> f32 { pub fn ease_in_back(x: f32) -> f32 {
2.70158 * x * x * x - 1.70158 * x * x 2.70158 * x * x * x - 1.70158 * x * x
} }
#[inline(always)]
pub fn ease_out_back(x: f32) -> f32 { pub fn ease_out_back(x: f32) -> f32 {
1.0 + 2.70158 * (x - 1.0).powi(3) + 1.70158 * (x - 1.0).powi(2) 1.0 + 2.70158 * (x - 1.0).powi(3) + 1.70158 * (x - 1.0).powi(2)
} }
#[inline(always)]
pub fn ease_in_out_back(x: f32) -> f32 { pub fn ease_in_out_back(x: f32) -> f32 {
let c1 = 1.70158; let c1 = 1.70158;
let c2 = c1 * 1.525; let c2 = c1 * 1.525;
if x < 0.5 { (2.0 * x).powi(2) * ((c2 + 1.0) * 2.0 * x - c2) / 2.0 } else { ((2.0 * x - 2.0).powi(2) * ((c2 + 1.0) * (2.0 * x - 2.0) + c2) + 2.0) / 2.0 } if x < 0.5 {
(2.0 * x).powi(2) * ((c2 + 1.0) * 2.0 * x - c2) / 2.0
} else {
((2.0 * x - 2.0).powi(2) * ((c2 + 1.0) * (2.0 * x - 2.0) + c2) + 2.0) / 2.0
}
} }
#[inline(always)]
pub fn ease_in_elastic(x: f32) -> f32 { pub fn ease_in_elastic(x: f32) -> f32 {
let c4 = (2.0 * PI) / 3.0; let c4 = (2.0 * PI) / 3.0;
if x == 0.0 { 0.0 } else if x == 1.0 { 1.0 } else { -2.0_f32.powf(10.0 * x - 10.0) * ((x * 10.0 - 10.75) * c4).sin() } if x == 0.0 {
0.0
} else if x == 1.0 {
1.0
} else {
-2.0_f32.powf(10.0 * x - 10.0) * ((x * 10.0 - 10.75) * c4).sin()
}
} }
#[inline(always)]
pub fn ease_out_elastic(x: f32) -> f32 { pub fn ease_out_elastic(x: f32) -> f32 {
let c4 = (2.0 * PI) / 3.0; let c4 = (2.0 * PI) / 3.0;
if x == 0.0 { 0.0 } else if x == 1.0 { 1.0 } else { 2.0_f32.powf(-10.0 * x) * ((x * 10.0 - 0.75) * c4).sin() + 1.0 } if x == 0.0 {
0.0
} else if x == 1.0 {
1.0
} else {
2.0_f32.powf(-10.0 * x) * ((x * 10.0 - 0.75) * c4).sin() + 1.0
}
} }
#[inline(always)]
pub fn ease_in_out_elastic(x: f32) -> f32 { pub fn ease_in_out_elastic(x: f32) -> f32 {
let c5 = (2.0 * PI) / 4.5; let c5 = (2.0 * PI) / 4.5;
if x == 0.0 { 0.0 } else if x == 1.0 { 1.0 } else if x < 0.5 { -(2.0_f32.powf(20.0 * x - 10.0) * ((20.0 * x - 11.125) * c5).sin()) / 2.0 } else { (2.0_f32.powf(-20.0 * x + 10.0) * ((20.0 * x - 11.125) * c5).sin()) / 2.0 + 1.0 } if x == 0.0 {
0.0
} else if x == 1.0 {
1.0
} else if x < 0.5 {
-(2.0_f32.powf(20.0 * x - 10.0) * ((20.0 * x - 11.125) * c5).sin()) / 2.0
} else {
(2.0_f32.powf(-20.0 * x + 10.0) * ((20.0 * x - 11.125) * c5).sin()) / 2.0 + 1.0
}
} }
#[inline(always)]
pub fn ease_in_bounce(x: f32) -> f32 { pub fn ease_in_bounce(x: f32) -> f32 {
1.0 - ease_out_bounce(1.0 - x) 1.0 - ease_out_bounce(1.0 - x)
} }
#[inline(always)]
pub fn ease_out_bounce(x: f32) -> f32 { pub fn ease_out_bounce(x: f32) -> f32 {
let n1 = 7.5625; let n1 = 7.5625;
let d1 = 2.75; let d1 = 2.75;
if x < 1.0 / d1 { n1 * x * x } else if x < 2.0 / d1 { n1 * (x - 1.5 / d1) * (x - 1.5 / d1) + 0.75 } else if x < 2.5 / d1 { n1 * (x - 2.25 / d1) * (x - 2.25 / d1) + 0.9375 } else { n1 * (x - 2.625 / d1) * (x - 2.625 / d1) + 0.984375 } if x < 1.0 / d1 {
n1 * x * x
} else if x < 2.0 / d1 {
n1 * (x - 1.5 / d1) * (x - 1.5 / d1) + 0.75
} else if x < 2.5 / d1 {
n1 * (x - 2.25 / d1) * (x - 2.25 / d1) + 0.9375
} else {
n1 * (x - 2.625 / d1) * (x - 2.625 / d1) + 0.984375
}
} }
#[inline(always)]
pub fn ease_in_out_bounce(x: f32) -> f32 { pub fn ease_in_out_bounce(x: f32) -> f32 {
if x < 0.5 { (1.0 - ease_out_bounce(1.0 - 2.0 * x)) / 2.0 } else { (1.0 + ease_out_bounce(2.0 * x - 1.0)) / 2.0 } if x < 0.5 {
(1.0 - ease_out_bounce(1.0 - 2.0 * x)) / 2.0
} else {
(1.0 + ease_out_bounce(2.0 * x - 1.0)) / 2.0
}
} }