Skip to main content

mln_util/
interpolate.rs

1// C++ `util::interpolate(a, b, t)` has a `double t` and a `float t`
2// overload; `Interpolate::interpolate` is the first, `interpolate_f32` the
3// second. Specializations that only define one of the two get the other
4// through C++'s implicit conversion of `t`, and so do the impls here.
5
6use geometry::Point;
7
8use crate::color::Color;
9use crate::padding::Padding;
10use crate::range::Range;
11
12pub trait Interpolate: Sized {
13    #[must_use]
14    fn interpolate(&self, to: &Self, t: f64) -> Self;
15
16    /// Interpolates with a float `t`. Defaults to promoting `t`, which is
17    /// what C++ does for specializations without a float overload.
18    #[must_use]
19    fn interpolate_f32(&self, to: &Self, t: f32) -> Self {
20        self.interpolate(to, f64::from(t))
21    }
22}
23
24/// The primary template: `a * (1.0 - t) + b * t`.
25impl Interpolate for f64 {
26    fn interpolate(&self, to: &Self, t: f64) -> Self {
27        *self * (1.0 - t) + *to * t
28    }
29}
30
31impl Interpolate for f32 {
32    fn interpolate(&self, to: &Self, t: f64) -> Self {
33        // static_cast<float>(a * (1.0 - t) + b * t)
34        (f64::from(*self) * (1.0 - t) + f64::from(*to) * t) as f32
35    }
36
37    fn interpolate_f32(&self, to: &Self, t: f32) -> Self {
38        *self * (1.0f32 - t) + *to * t
39    }
40}
41
42/// The primary template on `mapbox::geometry::point<double>` (point
43/// arithmetic: `a * (1.0 - t) + b * t` per coordinate).
44impl Interpolate for Point<f64> {
45    fn interpolate(&self, to: &Self, t: f64) -> Self {
46        Point::new(self.x * (1.0 - t) + to.x * t, self.y * (1.0 - t) + to.y * t)
47    }
48}
49
50/// Interpolates in float: a double `t` is narrowed to float first.
51impl<const N: usize> Interpolate for [f32; N] {
52    fn interpolate(&self, to: &Self, t: f64) -> Self {
53        self.interpolate_f32(to, t as f32)
54    }
55
56    fn interpolate_f32(&self, to: &Self, t: f32) -> Self {
57        std::array::from_fn(|i| self[i].interpolate_f32(&to[i], t))
58    }
59}
60
61impl<const N: usize> Interpolate for [f64; N] {
62    fn interpolate(&self, to: &Self, t: f64) -> Self {
63        std::array::from_fn(|i| self[i].interpolate(&to[i], t))
64    }
65}
66
67/// Channel-wise lerp of the *premultiplied* channels
68/// (this is what `ColorSpace::RGB` means).
69impl Interpolate for Color {
70    fn interpolate(&self, to: &Self, t: f64) -> Self {
71        Color::new(
72            self.r.interpolate(&to.r, t),
73            self.g.interpolate(&to.g, t),
74            self.b.interpolate(&to.b, t),
75            self.a.interpolate(&to.a, t),
76        )
77    }
78
79    fn interpolate_f32(&self, to: &Self, t: f32) -> Self {
80        Color::new(
81            self.r.interpolate_f32(&to.r, t),
82            self.g.interpolate_f32(&to.g, t),
83            self.b.interpolate_f32(&to.b, t),
84            self.a.interpolate_f32(&to.a, t),
85        )
86    }
87}
88
89impl Interpolate for Padding {
90    fn interpolate(&self, to: &Self, t: f64) -> Self {
91        Padding::new(
92            self.top.interpolate(&to.top, t),
93            self.right.interpolate(&to.right, t),
94            self.bottom.interpolate(&to.bottom, t),
95            self.left.interpolate(&to.left, t),
96        )
97    }
98
99    fn interpolate_f32(&self, to: &Self, t: f32) -> Self {
100        Padding::new(
101            self.top.interpolate_f32(&to.top, t),
102            self.right.interpolate_f32(&to.right, t),
103            self.bottom.interpolate_f32(&to.bottom, t),
104            self.left.interpolate_f32(&to.left, t),
105        )
106    }
107}
108
109// `Uninterpolated` specializations: the result is `a`.
110
111impl Interpolate for bool {
112    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
113        *self
114    }
115}
116
117impl Interpolate for String {
118    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
119        self.clone()
120    }
121}
122
123/// Vectors don't interpolate: the result is `self`. (Vectors of expression
124/// values interpolate in `mln_style`'s expressions.)
125impl<T: Clone> Interpolate for Vec<T> {
126    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
127        self.clone()
128    }
129}
130
131pub fn interpolate<T: Interpolate>(from: &T, to: &T, t: f64) -> T {
132    from.interpolate(to, t)
133}
134
135#[must_use]
136pub fn interpolation_factor(base: f32, range: Range<f32>, z: f32) -> f32 {
137    let zoom_diff = range.max - range.min;
138    let zoom_progress = z - range.min;
139    if zoom_diff == 0.0 {
140        0.0
141    } else if base == 1.0f32 {
142        zoom_progress / zoom_diff
143    } else {
144        ((f64::from(base).powf(f64::from(zoom_progress)) - 1.0) / (f64::from(base).powf(f64::from(zoom_diff)) - 1.0))
145            as f32
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn float_overloads() {
155        // 0.1f32 * (1 - 0.3) + 0.7 * 0.3 differs between the overloads.
156        let a = 0.1f32;
157        let b = 0.7f32;
158        assert_eq!(a.interpolate_f32(&b, 0.3), a * (1.0 - 0.3f32) + b * 0.3f32);
159        assert_eq!(a.interpolate(&b, 0.3), (f64::from(a) * 0.7 + f64::from(b) * 0.3) as f32);
160        let arr = [a, b];
161        assert_eq!(arr.interpolate(&[b, a], 0.3), arr.interpolate_f32(&[b, a], 0.3f32));
162    }
163
164    #[test]
165    fn factor() {
166        assert_eq!(interpolation_factor(1.0, Range::new(0.0, 10.0), 5.0), 0.5);
167        assert_eq!(interpolation_factor(2.0, Range::new(3.0, 3.0), 5.0), 0.0);
168        assert_eq!(
169            interpolation_factor(2.0, Range::new(0.0, 2.0), 1.0),
170            ((2.0f64.powf(1.0) - 1.0) / (2.0f64.powf(2.0) - 1.0)) as f32
171        );
172    }
173}