pub const DEFAULT_EPS: f32 = 1e-5;
pub fn fast_negexp(x: f32) -> f32 {
1.0 / (1.0 + x + 0.48 * x * x + 0.235 * x * x * x)
}
pub fn halflife_to_damping(halflife: f32, eps: f32) -> f32 {
(4.0 * 0.69314718056) / (halflife + eps)
}
pub fn damping_to_halflife(damping: f32, eps: f32) -> f32 {
(4.0 * 0.69314718056) / (damping + eps)
}
pub fn frequency_to_stiffness(frequency: f32) -> f32 {
squaref((2.0 * std::f64::consts::PI * f64::from(frequency)) as f32)
}
pub fn stiffness_to_frequency(stiffness: f32) -> f32 {
(f64::from(stiffness.sqrt()) / (2.0 * std::f64::consts::PI)) as f32
}
pub fn critical_halflife(frequency: f32) -> f32 {
damping_to_halflife(
(frequency_to_stiffness(frequency) * 4.0).sqrt(),
DEFAULT_EPS,
)
}
pub fn critical_frequency(halflife: f32) -> f32 {
stiffness_to_frequency(squaref(halflife_to_damping(halflife, DEFAULT_EPS)) / 4.0)
}
pub fn damping_ratio_to_stiffness(ratio: f32, damping: f32) -> f32 {
squaref(damping / (ratio * 2.0))
}
pub fn damping_ratio_to_damping(ratio: f32, stiffness: f32) -> f32 {
ratio * 2.0 * stiffness.sqrt()
}
pub fn halflife_to_lag(halflife: f32) -> f32 {
halflife / 0.69314718056
}
pub fn lag_to_halflife(lag: f32) -> f32 {
lag * 0.69314718056
}
pub(crate) fn squaref(x: f32) -> f32 {
x * x
}
pub(crate) fn lerp(x: f32, y: f32, a: f32) -> f32 {
(1.0 - a) * x + a * y
}
pub(crate) fn fast_atan(x: f32) -> f32 {
let z = x.abs();
let w = if z > 1.0 { 1.0 / z } else { z };
let y = ((std::f64::consts::PI / 4.0) * f64::from(w)
- f64::from(w * (w - 1.0) * (0.2447 + 0.0663 * w))) as f32;
(if z > 1.0 {
(std::f64::consts::PI / 2.0 - f64::from(y)) as f32
} else {
y
})
.copysign(x)
}