gistools/util/interpolation/
mod.rs1pub mod average;
3pub mod idw;
5pub mod lanczos;
7pub 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#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
22#[serde(rename_all = "lowercase")]
23pub enum InterpolationMethod {
24 Average,
26 Nearest,
28 IDW,
30 #[default]
32 Lanczos,
33}
34
35pub type InterpolationFunction<P, R, V> =
37 fn(point: &P, ref_data: &[R], get_value: GetInterpolateValue<R, V>) -> V;
38
39pub 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
70pub 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
92pub type GetInterpolateValue<R, V> = fn(point: &R) -> V;
94
95pub fn default_get_interpolate_current_value<T: GetZ>(point: &T) -> f64 {
97 point.z().unwrap_or_default()
98}
99
100pub 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
108pub type VectorPointRGBA = VectorPoint<RGBA>;