ndarray_interp/interp2d/strategies/mod.rs
1use std::{fmt::Debug, ops::Sub};
2
3use ndarray::{ArrayBase, ArrayViewMut, Data, Dimension, Ix1, RemoveAxis};
4use num_traits::{Num, NumCast};
5
6use crate::{BuilderError, InterpolateError};
7
8use super::Interp2D;
9
10mod bilinear;
11
12pub use bilinear::Bilinear;
13
14pub trait Interp2DStrategyBuilder<Sd, Sx, Sy, D>
15where
16 Sd: Data,
17 Sd::Elem: Num + PartialOrd + NumCast + Copy + Debug + Sub + Send,
18 Sx: Data<Elem = Sd::Elem>,
19 Sy: Data<Elem = Sd::Elem>,
20 D: Dimension + RemoveAxis,
21 D::Smaller: RemoveAxis,
22{
23 const MINIMUM_DATA_LENGHT: usize;
24 type FinishedStrat: Interp2DStrategy<Sd, Sx, Sy, D>;
25
26 /// initialize the strategy by validating data and
27 /// possibly calculating coefficients
28 /// This method is called in [`Interp2DBuilder::build`](crate::interp2d::Interp2DBuilder::build)
29 ///
30 /// When this method is called by [`Interp2DBuilder`](crate::interp2d::Interp2DBuilder) the
31 /// following properties are guaranteed:
32 /// - x and y is strictly monotonically rising
33 /// - the lenght of x equals the lenght of the data Axis 0
34 /// - the lenght of y equals the lenght of the data Axis 1
35 /// - the lenght is at least `MINIMUM_DATA_LENGHT`
36 /// - Interpolation in x will happen along Axis 0
37 /// - Interpolation in y will happen along Axis 1
38 fn build(
39 self,
40 x: &ArrayBase<Sx, Ix1>,
41 y: &ArrayBase<Sy, Ix1>,
42 data: &ArrayBase<Sd, D>,
43 ) -> Result<Self::FinishedStrat, BuilderError>;
44}
45
46pub trait Interp2DStrategy<Sd, Sx, Sy, D>
47where
48 Sd: Data,
49 Sd::Elem: Num + PartialOrd + NumCast + Copy + Debug + Sub + Send,
50 Sx: Data<Elem = Sd::Elem>,
51 Sy: Data<Elem = Sd::Elem>,
52 D: Dimension + RemoveAxis,
53 D::Smaller: RemoveAxis,
54 Self: Sized,
55{
56 /// Interpolate the at position `(x, y)` into the target array.
57 /// This is used internally by [`Interp2D`].
58 ///
59 /// When called by [`Interp2D`] the following
60 /// properties are guaranteed:
61 /// - The shape of the target array matches the
62 /// shape of the data array (provided to the builder)
63 /// with the first two axes removed.
64 /// - x can be any valid `Sx::Elem`
65 /// - y cna be any valid `Sy::Elem`
66 fn interp_into(
67 &self,
68 interpolator: &Interp2D<Sd, Sx, Sy, D, Self>,
69 target: ArrayViewMut<'_, Sd::Elem, <D::Smaller as Dimension>::Smaller>,
70 x: Sx::Elem,
71 y: Sy::Elem,
72 ) -> Result<(), InterpolateError>;
73}