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;
pub trait Interpolate: Sized {
#[must_use]
fn interpolate(&self, to: &Self, t: f64) -> Self;
}
impl Interpolate for f64 {
fn interpolate(&self, to: &Self, t: f64) -> Self {
*self * (1.0 - t) + *to * t
}
}
impl Interpolate for f32 {
fn interpolate(&self, to: &Self, t: f64) -> Self {
(f64::from(*self) * (1.0 - t) + f64::from(*to) * t) as f32
}
}
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)
}
}
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))
}
}
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),
)
}
}
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)
}
}
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()
}
}
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)
}
#[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() {
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));
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() {
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);
}
}