gistools/util/interpolation/
mod.rs

1/// Average Interpolation tools
2pub mod average;
3/// Inverse Distance Weighted Interpolation tools
4pub mod idw;
5/// Lanczos Interpolation tools
6pub mod lanczos;
7/// Nearest Interpolation tools
8pub mod nearest;
9
10use crate::parsers::RGBA;
11pub use average::*;
12use core::ops::{AddAssign, DivAssign, MulAssign};
13pub use idw::*;
14pub use lanczos::*;
15use libm::pow;
16pub use nearest::*;
17use s2json::{GetM, GetXY, GetZ, VectorPoint};
18use serde::{Deserialize, Serialize};
19
20/// Interpolation method
21#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
22#[serde(rename_all = "lowercase")]
23pub enum InterpolationMethod {
24    /// Average interpolation
25    Average,
26    /// Nearest interpolation
27    Nearest,
28    /// Inverse Distance Weighted interpolation
29    IDW,
30    /// Lanczos interpolation
31    #[default]
32    Lanczos,
33}
34
35/// Interpolation function To get the value of a point
36pub type InterpolationFunction<P, R, V> =
37    fn(point: &P, ref_data: &[R], get_value: GetInterpolateValue<R, V>) -> V;
38
39/// A trait for values that can be used in interpolation
40pub trait Interpolatable:
41    Default
42    + AddAssign<Self>
43    + AddAssign<f64>
44    + DivAssign<f64>
45    + DivAssign<Self>
46    + MulAssign<f64>
47    + PartialEq<f64>
48    + PartialEq<Self>
49    + Clone
50    + Copy
51where
52    Self: Sized,
53{
54}
55impl<T> Interpolatable for T where
56    T: Default
57        + AddAssign<T>
58        + AddAssign<f64>
59        + DivAssign<f64>
60        + DivAssign<T>
61        + MulAssign<f64>
62        + PartialEq<f64>
63        + PartialEq<T>
64        + Clone
65        + Copy
66        + Sized
67{
68}
69
70/// Get the interpolation function based on the method type
71/// Options are:
72/// - average
73/// - nearest
74/// - idw
75/// - lanczos (Best)
76pub fn get_interpolation<
77    M: Clone,
78    P: GetXY + GetZ,
79    R: GetM<M> + GetXY + GetZ,
80    V: Interpolatable,
81>(
82    method: InterpolationMethod,
83) -> InterpolationFunction<P, R, V> {
84    match method {
85        InterpolationMethod::Average => average_interpolation,
86        InterpolationMethod::Nearest => nearest_interpolation,
87        InterpolationMethod::IDW => idw_interpolation,
88        InterpolationMethod::Lanczos => lanczos_interpolation,
89    }
90}
91
92/// Function to get the value of a point
93pub type GetInterpolateValue<R, V> = fn(point: &R) -> V;
94
95/// Default function to get the value of a point
96pub fn default_get_interpolate_current_value<T: GetZ>(point: &T) -> f64 {
97    point.z().unwrap_or_default()
98}
99
100/// Get the distance between two points
101pub fn get_distance<A: GetXY + GetZ, B: GetXY + GetZ>(a: &A, b: &B) -> f64 {
102    let dx = a.x() - b.x();
103    let dy = a.y() - b.y();
104    let dz = a.z().unwrap_or_default() - b.z().unwrap_or_default();
105    pow(dx * dx + dy * dy + dz * dz, 0.5)
106}
107
108/// Vector Point with RGBA data
109pub type VectorPointRGBA = VectorPoint<RGBA>;