Remove the linear limiter as not smooth enough

This commit is contained in:
numzero 2024-06-29 00:39:11 +03:00
parent 88bfae9608
commit 382ce16822

View File

@ -5,16 +5,6 @@ pub trait Limiter {
fn derivative(&self, x: f32) -> f32; fn derivative(&self, x: f32) -> f32;
} }
pub struct LinearLimiter {
pub min: f32,
pub max: f32,
}
impl Limiter for LinearLimiter {
fn value(&self, x: f32) -> f32 { (self.min, self.max).inverse_lerp(x.abs()).clamp(0.0, 1.0) }
fn derivative(&self, x: f32) -> f32 { if x.abs() > self.min && x.abs() < self.max { x.signum() / (self.max - self.min) } else { 0.0 } }
}
pub struct SmoothstepLimiter { pub struct SmoothstepLimiter {
pub min: f32, pub min: f32,
pub max: f32, pub max: f32,
@ -100,21 +90,12 @@ mod test {
assert_abs_diff_eq!(testee.value(-x), 1., epsilon = ε); assert_abs_diff_eq!(testee.value(-x), 1., epsilon = ε);
} }
for x in itertools_num::linspace(-mul * max, mul * max, 100) { for x in itertools_num::linspace(-mul * max, mul * max, 100) {
// Currently, the derivative is discontinuous at ±min and ±max... lets just skip these for now.
if x.abs().abs_diff_eq(&min, δ) || x.abs().abs_diff_eq(&max, δ) {
continue;
}
let df_num = (testee.value(x + δ) - testee.value(x - δ)) / (2. * δ); let df_num = (testee.value(x + δ) - testee.value(x - δ)) / (2. * δ);
let df_expl = testee.derivative(x); let df_expl = testee.derivative(x);
assert!(abs_diff_eq!(df_expl, df_num, epsilon = ε), "At x={x}, df/dx:\nnumerical: {df_num}\nexplicit: {df_expl}\n"); assert!(abs_diff_eq!(df_expl, df_num, epsilon = ε), "At x={x}, df/dx:\nnumerical: {df_num}\nexplicit: {df_expl}\n");
} }
} }
#[test]
fn test_linear_limiter() {
test_limiter(LinearLimiter { min: 20.0, max: 30.0 }, 20.0, 30.0, 1.0 / 8.0);
}
#[test] #[test]
fn test_smoothstep_limiter() { fn test_smoothstep_limiter() {
test_limiter(SmoothstepLimiter { min: 20.0, max: 30.0 }, 20.0, 30.0, 1.0 / 32.0); test_limiter(SmoothstepLimiter { min: 20.0, max: 30.0 }, 20.0, 30.0, 1.0 / 32.0);