use naturalneighbor::{Interpolator, Point}; use rand::Rng; /// check the result of `interpolate` is same as the result of `query_weights` #[test] fn query_weights() { let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed([0; 32]); let n = 10000; let bound = 1000.0; let points = (0..n) .map(|_| Point { x: rng.gen::() * bound, y: rng.gen::() * bound, }) .collect::>(); let values = (0..n).map(|_| rng.gen::()).collect::>(); let interpolator = Interpolator::new(&points); let test_points = (0..n) .map(|_| Point { x: rng.gen::() * bound, y: rng.gen::() * bound, }) .collect::>(); for i in 0..100 { let value1 = interpolator .interpolate( &values, Point { x: test_points[i].x, y: test_points[i].y, }, ) .unwrap_or_else(|e| { panic!( "Failed to interpolate {:?} with error {:?}", test_points[i], e ) }); let queried_weights = interpolator .query_weights(Point { x: test_points[i].x, y: test_points[i].y, }) .unwrap_or_else(|e| { panic!( "Failed to interpolate {:?} with error {:?}", test_points[i], e ) }); if let Some(weights) = queried_weights { let value2 = weights.iter().map(|(i, w)| values[*i] * w).sum::(); assert!(value1.is_some()); assert!((value1.unwrap() - value2).abs() < 1e-6); } else { assert!(value1.is_none()); } } }