#[allow(dead_code)]
pub trait ExtFloor {
fn floor(self) -> f32;
}
impl ExtFloor for f32 {
fn floor(self) -> f32 {
let mut xi = self as i32;
if self < 0.0 && self != xi as f32 {
xi -= 1;
}
xi as f32
}
}
pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
let t = ((x - edge0) / (edge1 - edge0)).clamp(0.0, 1.0);
t * t * (3.0 - 2.0 * t)
}
#[cfg(test)]
mod tests {
#[test]
fn test_ext_floor_behavior() {
assert_eq!(1.9f32.floor(), 1.0);
assert_eq!((-1.1f32).floor(), -2.0);
assert_eq!(0.0f32.floor(), 0.0);
assert_eq!((-0.999f32).floor(), -1.0);
}
#[test]
fn test_smoothstep_behavior() {
assert_eq!(super::smoothstep(0.0, 1.0, -1.0), 0.0);
assert_eq!(super::smoothstep(0.0, 1.0, 0.0), 0.0);
assert_eq!(super::smoothstep(0.0, 1.0, 0.5), 0.5);
assert_eq!(super::smoothstep(0.0, 1.0, 1.0), 1.0);
assert_eq!(super::smoothstep(0.0, 1.0, 2.0), 1.0);
}
}