use crate::foundation::{Lattice, Result};
use ndarray::Array2;
pub trait Gridder {
fn grid(&self, coords: &[[f64; 3]], lattice: &Lattice) -> Result<Array2<f64>>;
}
impl Gridder for super::GridMethod {
fn grid(&self, coords: &[[f64; 3]], lattice: &Lattice) -> Result<Array2<f64>> {
super::grid(coords, lattice, *self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::gridding::GridMethod;
#[test]
fn gridmethod_trait_matches_free_function() {
let lattice = Lattice::regular(0.0, 0.0, 1.0, 1.0, 5, 5);
let coords = [[0.0, 0.0, 1.0], [4.0, 4.0, 9.0], [2.0, 2.0, 5.0]];
for method in [
GridMethod::Nearest,
GridMethod::InverseDistance,
GridMethod::MinimumCurvature,
] {
let via_trait = Gridder::grid(&method, &coords, &lattice).unwrap();
let via_free = crate::gridding::grid(&coords, &lattice, method).unwrap();
assert_eq!(via_trait, via_free, "{method:?}");
}
}
#[test]
fn usable_as_trait_object() {
let lattice = Lattice::regular(0.0, 0.0, 1.0, 1.0, 4, 4);
let coords = [[0.0, 0.0, 1.0], [3.0, 3.0, 4.0]];
let g: Box<dyn Gridder> = Box::new(GridMethod::InverseDistance);
let out = g.grid(&coords, &lattice).unwrap();
assert_eq!(out.dim(), (4, 4));
}
}