Skip to main content

ternary_contours/interpolation/
alpha.rs

1/// Normalized endpoint coefficients for one directed cubic interval.
2///
3/// The interval value is
4/// `y0*(1-t) + y1*t + (1-t)*t*(alpha0 + alpha1*t)`, with `t=0` at
5/// the first endpoint and `t=1` at the second.
6#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct AlphaInterval {
8    pub alpha0: f64,
9    pub alpha1: f64,
10}
11
12impl AlphaInterval {
13    pub const fn new(alpha0: f64, alpha1: f64) -> Self {
14        Self { alpha0, alpha1 }
15    }
16
17    /// Evaluate the normalized directed interval.
18    pub fn value(self, y0: f64, y1: f64, t: f64) -> f64 {
19        y0 * (1.0 - t) + y1 * t + (1.0 - t) * t * (self.alpha0 + self.alpha1 * t)
20    }
21
22    /// Reverse the directed interval without changing its geometric curve.
23    ///
24    /// `(alpha0, alpha1) -> (alpha0 + alpha1, -alpha1)`.
25    pub const fn reversed(self) -> Self {
26        Self {
27            alpha0: self.alpha0 + self.alpha1,
28            alpha1: -self.alpha1,
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    fn close(left: f64, right: f64) {
38        assert!((left - right).abs() < 1.0e-11, "{left} != {right}");
39    }
40
41    #[test]
42    fn asymmetric_polynomial_proves_alpha1_multiplies_t() {
43        let interval = AlphaInterval::new(2.5, -4.0);
44        for t in [0.0, 0.1, 0.25, 0.5, 0.8, 1.0] {
45            let expected = 1.25 * (1.0 - t) - 0.75 * t + (1.0 - t) * t * (2.5 - 4.0 * t);
46            close(interval.value(1.25, -0.75, t), expected);
47            if t > 0.0 && t < 1.0 && (t - 0.5_f64).abs() > 1.0e-12 {
48                let wrong = 1.25 * (1.0 - t) - 0.75 * t + (1.0 - t) * t * (2.5 - 4.0 * (1.0 - t));
49                assert!((expected - wrong).abs() > 1.0e-4);
50            }
51        }
52        assert_eq!(interval.value(1.25, -0.75, 0.0), 1.25);
53        assert_eq!(interval.value(1.25, -0.75, 1.0), -0.75);
54    }
55
56    #[test]
57    fn reversal_identity_holds() {
58        let interval = AlphaInterval::new(2.5, -4.0);
59        let reversed = interval.reversed();
60        assert_eq!(reversed, AlphaInterval::new(-1.5, 4.0));
61        for t in [0.0, 0.1, 0.25, 0.5, 0.8, 1.0] {
62            close(
63                interval.value(1.25, -0.75, t),
64                reversed.value(-0.75, 1.25, 1.0 - t),
65            );
66        }
67    }
68
69    #[cfg(feature = "cubic-alpha")]
70    #[test]
71    fn direct_coefficient_round_trip_on_non_unit_interval() {
72        let coefficients = [0.375, -1.25, 2.75, -0.5];
73        let x0 = 2.0;
74        let x1 = 5.0;
75        let h = x1 - x0;
76        let eval = |dx: f64| {
77            ((coefficients[0] * dx + coefficients[1]) * dx + coefficients[2]) * dx + coefficients[3]
78        };
79        let y0 = eval(0.0);
80        let y1 = eval(h);
81        let alpha = spline1d::cubic_coeffs_to_alpha(coefficients, h);
82        let interval = AlphaInterval::new(alpha[0], alpha[1]);
83        let roundtrip =
84            spline1d::alpha_to_cubic_coeffs(x0, y0, x1, y1, interval.alpha0, interval.alpha1);
85        for (actual, expected) in roundtrip.into_iter().zip(coefficients) {
86            close(actual, expected);
87        }
88        for t in [0.0, 0.13, 0.37, 0.71, 1.0] {
89            close(interval.value(y0, y1, t), eval(t * h));
90        }
91    }
92}