125125/// - [`strategy::RightNearest`]
126126/// - The extrapolation setting enum: [`Extrapolate`]
127127pub mod prelude {
128- pub use crate :: interpolator:: { Interp0D , Interp1D , Interp2D , Interp3D , InterpND } ;
128+ pub use crate :: interpolator:: {
129+ Extrapolate , Interp0D , Interp1D , Interp2D , Interp3D , InterpND , Interpolator ,
130+ } ;
129131 pub use crate :: strategy;
130- pub use crate :: Extrapolate ;
131- pub use crate :: Interpolator ;
132132}
133133
134134pub mod error;
@@ -137,6 +137,7 @@ pub mod strategy;
137137pub mod interpolator;
138138pub use interpolator:: data;
139139pub ( crate ) use interpolator:: data:: * ;
140+ pub ( crate ) use interpolator:: * ;
140141
141142pub ( crate ) use error:: * ;
142143pub ( crate ) use strategy:: traits:: * ;
@@ -169,103 +170,6 @@ macro_rules! assert_approx_eq {
169170#[ cfg( test) ]
170171pub ( crate ) use assert_approx_eq;
171172
172- /// An interpolator of data type `T`
173- ///
174- /// This trait is dyn-compatible, meaning you can use:
175- /// `Box<dyn Interpolator<_>>`
176- /// and swap the contained interpolator at runtime.
177- pub trait Interpolator < T > : DynClone {
178- /// Interpolator dimensionality.
179- fn ndim ( & self ) -> usize ;
180- /// Validate interpolator data.
181- fn validate ( & mut self ) -> Result < ( ) , ValidateError > ;
182- /// Interpolate at supplied point.
183- fn interpolate ( & self , point : & [ T ] ) -> Result < T , InterpolateError > ;
184- }
185-
186- clone_trait_object ! ( <T > Interpolator <T >) ;
187-
188- impl < T > Interpolator < T > for Box < dyn Interpolator < T > > {
189- fn ndim ( & self ) -> usize {
190- ( * * self ) . ndim ( )
191- }
192- fn validate ( & mut self ) -> Result < ( ) , ValidateError > {
193- ( * * self ) . validate ( )
194- }
195- fn interpolate ( & self , point : & [ T ] ) -> Result < T , InterpolateError > {
196- ( * * self ) . interpolate ( point)
197- }
198- }
199-
200- /// Extrapolation strategy
201- ///
202- /// Controls what happens if supplied interpolant point
203- /// is outside the bounds of the interpolation grid.
204- #[ derive( Clone , Copy , Debug , PartialEq , Default ) ]
205- #[ cfg_attr( feature = "serde" , derive( Deserialize , Serialize ) ) ]
206- pub enum Extrapolate < T > {
207- /// Evaluate beyond the limits of the interpolation grid.
208- Enable ,
209- /// If point is beyond grid limits, return this value instead.
210- Fill ( T ) ,
211- /// Restrict interpolant point to the limits of the interpolation grid, using [`num_traits::clamp`].
212- Clamp ,
213- /// Wrap around to other end of periodic data.
214- /// Does NOT check that first and last values are equal.
215- Wrap ,
216- /// Return an error when interpolant point is beyond the limits of the interpolation grid.
217- #[ default]
218- Error ,
219- }
220-
221- macro_rules! extrapolate_impl {
222- ( $InterpType: ident, $Strategy: ident) => {
223- impl <D , S > $InterpType<D , S >
224- where
225- D : Data + RawDataClone + Clone ,
226- D :: Elem : PartialEq + Debug ,
227- S : $Strategy<D > + Clone ,
228- {
229- /// Set [`Extrapolate`] variant, checking validity.
230- pub fn set_extrapolate(
231- & mut self ,
232- extrapolate: Extrapolate <D :: Elem >,
233- ) -> Result <( ) , ValidateError > {
234- self . check_extrapolate( & extrapolate) ?;
235- self . extrapolate = extrapolate;
236- Ok ( ( ) )
237- }
238-
239- pub fn check_extrapolate(
240- & self ,
241- extrapolate: & Extrapolate <D :: Elem >,
242- ) -> Result <( ) , ValidateError > {
243- // Check applicability of strategy and extrapolate setting
244- if matches!( extrapolate, Extrapolate :: Enable ) && !self . strategy. allow_extrapolate( )
245- {
246- return Err ( ValidateError :: ExtrapolateSelection ( format!(
247- "{:?}" ,
248- self . extrapolate
249- ) ) ) ;
250- }
251- // If using Extrapolate::Enable,
252- // check that each grid dimension has at least two elements
253- if matches!( self . extrapolate, Extrapolate :: Enable ) {
254- for ( i, g) in self . data. grid. iter( ) . enumerate( ) {
255- if g. len( ) < 2 {
256- return Err ( ValidateError :: Other ( format!(
257- "at least 2 data points are required for extrapolation: dim {i}" ,
258- ) ) ) ;
259- }
260- }
261- }
262- Ok ( ( ) )
263- }
264- }
265- } ;
266- }
267- pub ( crate ) use extrapolate_impl;
268-
269173/// Wrap value around data bounds.
270174/// Assumes `min` < `max`.
271175pub ( crate ) fn wrap < T : Num + Euclid + Copy > ( input : T , min : T , max : T ) -> T {
@@ -274,7 +178,7 @@ pub(crate) fn wrap<T: Num + Euclid + Copy>(input: T, min: T, max: T) -> T {
274178
275179#[ cfg( test) ]
276180mod tests {
277- use crate :: wrap;
181+ use super :: wrap;
278182
279183 #[ test]
280184 fn test ( ) {
0 commit comments