From 46711a4b2b34fb839e8a48623e128160cd0ff756 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 11:05:54 -0400 Subject: [PATCH] added basic tests for nalgebra, and cleaned up some unused imports --- src/lib.rs | 24 ++++++++++-------------- tests/mod.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f904e0c..1559605 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,11 +110,8 @@ #[cfg(feature = "impl-cgmath")] use cgmath::{InnerSpace, Quaternion, Vector2, Vector3, Vector4}; #[cfg(feature = "impl-nalgebra")] use nalgebra as na; -#[cfg(feature = "impl-nalgebra")] use nalgebra::Matrix; #[cfg(feature = "impl-nalgebra")] use nalgebra::core::allocator::Allocator; #[cfg(feature = "impl-nalgebra")] use nalgebra::core::{DimName, DefaultAllocator, Scalar}; -#[cfg(feature = "impl-nalgebra")] use nalgebra::storage::Storage; -#[cfg(feature = "impl-nalgebra")] use generic_array::ArrayLength; #[cfg(feature = "std")] use std::cmp::Ordering; #[cfg(feature = "std")] use std::f32::consts; @@ -404,12 +401,11 @@ impl Interpolate for na::Point >::Buffer: Copy, N : Interpolate, { - fn lerp(a: Self, b: Self, t: f32) -> Self { - let lerp = |c1 : N, c2 : N| { Interpolate::lerp(c1, c2, t) }; - let coords = Matrix::zip_map(&a.coords, &b.coords, lerp); - na::Point::from_coordinates(coords) - - } + fn lerp(a: Self, b: Self, t: f32) -> Self { + let lerp = |c1 : N, c2 : N| { Interpolate::lerp(c1, c2, t) }; + let coords = na::Matrix::zip_map(&a.coords, &b.coords, lerp); + na::Point::from_coordinates(coords) + } } #[cfg(feature = "impl-nalgebra")] @@ -421,11 +417,11 @@ impl Interpolate for na::Matrix::Value as Mul<::Value>>::Output : generic_array::ArrayLength, <<::Value as Mul<::Value>>::Output as generic_array::ArrayLength>::ArrayType : Copy, { - fn lerp(a: Self, b: Self, t: f32) -> Self - { - let lerp = |c1 : f32, c2 : f32| Interpolate::lerp(c1, c2, t); - Matrix::zip_map(&a, &b, lerp) - } + fn lerp(a: Self, b: Self, t: f32) -> Self + { + let lerp = |c1 : f32, c2 : f32| Interpolate::lerp(c1, c2, t); + na::Matrix::zip_map(&a, &b, lerp) + } } // Default implementation of Interpolate::cubic_hermit. diff --git a/tests/mod.rs b/tests/mod.rs index b09e50d..39061ea 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,6 +1,9 @@ extern crate splines; +#[cfg(feature = "impl-nalgebra")] extern crate nalgebra; + +use splines::{Interpolation, Key, Spline, Interpolate}; +#[cfg(feature = "impl-nalgebra")] use nalgebra as na; -use splines::{Interpolation, Key, Spline}; #[test] fn step_interpolation_0() { @@ -128,3 +131,28 @@ fn several_interpolations_several_keys() { assert_eq!(spline.sample(10.), Some(2.)); assert_eq!(spline.clamped_sample(11.), 4.); } + +#[test] +#[cfg(feature = "impl-nalgebra")] +fn nalgebra_point_interpolation() { + let start : na::Point2 = na::Point2::new(0.0, 0.0); + let mid : na::Point2 = na::Point2::new(0.5, 0.5); + let end : na::Point2 = na::Point2::new(1.0, 1.0); + + assert_eq!(Interpolate::lerp(start, end, 0.0), start); + assert_eq!(Interpolate::lerp(start, end, 1.0), end); + assert_eq!(Interpolate::lerp(start, end, 0.5), mid); +} + +#[test] +#[cfg(feature = "impl-nalgebra")] +fn nalgebra_vector_interpolation() { + let start : na::Vector2 = na::Vector2::new(0.0, 0.0); + let mid : na::Vector2 = na::Vector2::new(0.5, 0.5); + let end : na::Vector2 = na::Vector2::new(1.0, 1.0); + + assert_eq!(Interpolate::lerp(start, end, 0.0), start); + assert_eq!(Interpolate::lerp(start, end, 1.0), end); + assert_eq!(Interpolate::lerp(start, end, 0.5), mid); +} +