mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use geometry::Point;

use crate::color::{Color, ColorSpace};
use crate::constants::{DEGREES_MAX, LONGITUDE_MAX};
use crate::geo::{LatLng, WrapMode};
use crate::padding::Padding;
use crate::range::Range;

/// A value that blends toward another: `t` is 0 at `self` and 1 at `to`.
pub trait Interpolate: Sized {
    #[must_use]
    fn interpolate(&self, to: &Self, t: f64) -> Self;
}

/// `a * (1.0 - t) + b * t`.
impl Interpolate for f64 {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        *self * (1.0 - t) + *to * t
    }
}

/// In `f64`, rounded to `f32` once.
impl Interpolate for f32 {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        (f64::from(*self) * (1.0 - t) + f64::from(*to) * t) as f32
    }
}

/// `a * (1.0 - t) + b * t` per coordinate.
impl Interpolate for Point<f64> {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        Point::new(self.x * (1.0 - t) + to.x * t, self.y * (1.0 - t) + to.y * t)
    }
}

/// Interpolates each element.
impl<T: Interpolate, const N: usize> Interpolate for [T; N] {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        std::array::from_fn(|i| self[i].interpolate(&to[i], t))
    }
}

/// Interpolates in RGB: the straight channels blend.
impl Interpolate for Color {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        Color::interpolate(self, to, t, ColorSpace::Rgb)
    }
}

impl Interpolate for Padding {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        Padding::new(
            self.top.interpolate(&to.top, t),
            self.right.interpolate(&to.right, t),
            self.bottom.interpolate(&to.bottom, t),
            self.left.interpolate(&to.left, t),
        )
    }
}

/// Interpolates the latitude, and the longitude the shorter way around, which
/// crosses the antimeridian when that's shorter. The result's longitude is
/// wrapped into -180 to 180.
impl Interpolate for LatLng {
    fn interpolate(&self, to: &Self, t: f64) -> Self {
        let from = self.longitude();
        let delta = (to.longitude() - from + LONGITUDE_MAX).rem_euclid(DEGREES_MAX) - LONGITUDE_MAX;
        let latitude = self.latitude().interpolate(&to.latitude(), t);
        LatLng::saturating(latitude, from + delta * t, WrapMode::Wrapped)
    }
}

// Types that don't interpolate: the result is `self`.

impl Interpolate for bool {
    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
        *self
    }
}

impl Interpolate for String {
    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
        self.clone()
    }
}

/// Vectors don't interpolate: the result is `self`. (Vectors of expression
/// values interpolate in `mln_style`'s expressions.)
impl<T: Clone> Interpolate for Vec<T> {
    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
        self.clone()
    }
}

pub fn interpolate<T: Interpolate>(from: &T, to: &T, t: f64) -> T {
    from.interpolate(to, t)
}

/// How far `z` is through `range`, 0 at its min and 1 at its max, on an
/// exponential curve of `base` (linear for 1). A range of one value gives 0.
#[must_use]
pub fn interpolation_factor(base: f64, range: Range<f64>, z: f64) -> f64 {
    let zoom_diff = range.max - range.min;
    let zoom_progress = z - range.min;
    if zoom_diff == 0.0 {
        0.0
    } else if base == 1.0 {
        zoom_progress / zoom_diff
    } else {
        (base.powf(zoom_progress) - 1.0) / (base.powf(zoom_diff) - 1.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn f32_interpolates_in_f64() {
        let a = 0.1f32;
        let b = 0.7f32;
        let expected = (f64::from(a) * 0.7 + f64::from(b) * 0.3) as f32;
        assert_eq!(a.interpolate(&b, 0.3), expected);
        assert_eq!([a, b].interpolate(&[b, a], 0.3)[0], expected);
    }

    fn lat_lng(lat: f64, lon: f64) -> LatLng {
        LatLng::new(lat, lon).unwrap()
    }

    #[test]
    fn lat_lng_interpolates_each_coordinate() {
        assert_eq!(lat_lng(10.0, 20.0).interpolate(&lat_lng(30.0, 60.0), 0.25), lat_lng(15.0, 30.0));
        assert_eq!(lat_lng(10.0, 20.0).interpolate(&lat_lng(30.0, 60.0), 0.0), lat_lng(10.0, 20.0));
        assert_eq!(lat_lng(10.0, 20.0).interpolate(&lat_lng(30.0, 60.0), 1.0), lat_lng(30.0, 60.0));
    }

    #[test]
    fn lat_lng_crosses_the_antimeridian_when_shorter() {
        // East across it, and west.
        assert_eq!(lat_lng(0.0, 170.0).interpolate(&lat_lng(0.0, -170.0), 0.25), lat_lng(0.0, 175.0));
        assert_eq!(lat_lng(0.0, 170.0).interpolate(&lat_lng(0.0, -170.0), 0.75), lat_lng(0.0, -175.0));
        assert_eq!(lat_lng(0.0, -170.0).interpolate(&lat_lng(0.0, 170.0), 0.75), lat_lng(0.0, 175.0));
        // Not across it when the way through 0 is shorter.
        assert_eq!(lat_lng(0.0, -80.0).interpolate(&lat_lng(0.0, 80.0), 0.5), lat_lng(0.0, 0.0));
    }

    #[test]
    fn lat_lng_wraps_the_result() {
        // An unwrapped endpoint takes the same path as its wrapped twin.
        assert_eq!(lat_lng(0.0, 530.0).interpolate(&lat_lng(0.0, -170.0), 0.25), lat_lng(0.0, 175.0));
        assert_eq!(lat_lng(0.0, 10.0).interpolate(&lat_lng(0.0, 380.0), 0.5), lat_lng(0.0, 15.0));
    }

    #[test]
    fn factor() {
        assert_eq!(interpolation_factor(1.0, Range::new(0.0, 10.0), 5.0), 0.5);
        assert_eq!(interpolation_factor(2.0, Range::new(3.0, 3.0), 5.0), 0.0);
        assert_eq!(interpolation_factor(2.0, Range::new(0.0, 2.0), 1.0), 1.0 / 3.0);
    }
}