From 729a8a1f4b7da305e7bd15aca642bb4d96438d17 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 10:11:35 -0400 Subject: [PATCH 1/9] successfully implemented Interpolate for all Point types. Vectors require an implementation for Matrix, which is proving trickier --- Cargo.toml | 2 +- src/lib.rs | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 956a0cf..ff3517d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ impl-cgmath = ["cgmath"] impl-nalgebra = ["nalgebra"] [dependencies.nalgebra] -version = "0.16" +version = "0.14" optional = true [dependencies.cgmath] diff --git a/src/lib.rs b/src/lib.rs index 138cfc8..e7cb84b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,10 +83,13 @@ //! - **[cgmath](https://crates.io/crates/cgmath) implementors.** //! + Adds some usefull implementations of `Interpolate` for some cgmath types. //! + Enable with the `"impl-cgmath"` feature. -//! - **Standard library / no stdandard library.** +//! - **[nalgebra](https://crates.io/crates/nalgebra) implementors.** +//! + Adds some usefull implementations of `Interpolate` for some nalgebra types. +//! + Enable with the `"impl-nalgebra"` feature. +//! - **Standard library / no standard library.** //! + It’s possible to compile against the standard library or go on your own without it. //! + Compiling with the standard library is enabled by default. -//! + Use `defaut-features = []` in your `Cargo.toml` to disable. +//! + Use `default-features = []` in your `Cargo.toml` to disable. //! + Enable explicitly with the `"std"` feataure. #![cfg_attr(not(feature = "std"), no_std)] @@ -105,7 +108,11 @@ #[cfg(feature = "impl-cgmath")] use cgmath::{InnerSpace, Quaternion, Vector2, Vector3, Vector4}; -#[cfg(feature = "impl-nalgebra")] use nalgera::{Point, Vector}; +#[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 = "std")] use std::cmp::Ordering; #[cfg(feature = "std")] use std::f32::consts; @@ -390,16 +397,32 @@ impl Interpolate for Quaternion { } #[cfg(feature = "impl-nalgebra")] -impl Interpolate for Point { +impl Interpolate for na::Point + where DefaultAllocator : Allocator, + >::Buffer: Copy, + N : Interpolate, +{ fn lerp(a: Self, b: Self, t: f32) -> Self { - a.lerp(b, t) + 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) } } #[cfg(feature = "impl-nalgebra")] -impl Interpolate for Vector { - fn lerp(a: Self, b: Self, t: f32) -> Self { - a.lerp(b, t) +impl Interpolate for na::Matrix>::Buffer> + where N : Interpolate, + N : Scalar, + R : DimName, + C : DimName, + ::Value : Mul<::Value>, + ::Value : Mul<::Value>, + <::Value as Mul<::Value>>::Output : generic_array::ArrayLengh +{ + fn lerp(a: Self, b: Self, t: f32) -> Self + { + let lerp = |c1 : N, c2 : N| Interpolate::lerp(c1, c2, t); + Matrix::zip_map(&a, &b, lerp) } } From b795267632e92cd7b8fdd0907202add60bd85294 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 10:31:08 -0400 Subject: [PATCH 2/9] matrix implementation compiles with new type variable bounds and dependance on generic_array --- Cargo.toml | 6 +++++- src/lib.rs | 14 ++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff3517d..60d8e1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,12 +22,16 @@ default = ["std", "impl-cgmath"] serialization = ["serde", "serde_derive"] std = [] impl-cgmath = ["cgmath"] -impl-nalgebra = ["nalgebra"] +impl-nalgebra = ["nalgebra", "generic-array"] [dependencies.nalgebra] version = "0.14" optional = true +[dependencies.generic-array] +version = "^0.8" +optional = true + [dependencies.cgmath] version = "0.16" optional = true diff --git a/src/lib.rs b/src/lib.rs index e7cb84b..f904e0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,7 @@ #[cfg(feature = "impl-cgmath")] extern crate cgmath; #[cfg(feature = "impl-nalgebra")] extern crate nalgebra; +#[cfg(feature = "impl-nalgebra")] extern crate generic_array; #[cfg(feature = "serialization")] extern crate serde; #[cfg(feature = "serialization")] #[macro_use] extern crate serde_derive; @@ -113,6 +114,7 @@ #[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; @@ -406,22 +408,22 @@ impl Interpolate for na::Point 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) + } } #[cfg(feature = "impl-nalgebra")] -impl Interpolate for na::Matrix>::Buffer> - where N : Interpolate, - N : Scalar, - R : DimName, +impl Interpolate for na::Matrix>::Buffer> + where R : DimName, C : DimName, ::Value : Mul<::Value>, ::Value : Mul<::Value>, - <::Value as Mul<::Value>>::Output : generic_array::ArrayLengh + <::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 : N, c2 : N| Interpolate::lerp(c1, c2, t); + let lerp = |c1 : f32, c2 : f32| Interpolate::lerp(c1, c2, t); Matrix::zip_map(&a, &b, lerp) } } From 15e0efa9c07ed101cef1b125c5d3d43a3523fd44 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 11:05:54 -0400 Subject: [PATCH 3/9] 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); +} + From 24cd0d7fca8d81ea63c20ee3a39446579cbadfe1 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 10:47:21 -0400 Subject: [PATCH 4/9] bumped version numbers in examples for splines dependancy --- examples/01-hello-world/Cargo.toml | 4 ++-- examples/02-serialization/Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/01-hello-world/Cargo.toml b/examples/01-hello-world/Cargo.toml index d11070f..9dc610a 100644 --- a/examples/01-hello-world/Cargo.toml +++ b/examples/01-hello-world/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "hello-world" -version = "0.1.0" +version = "0.2.0" authors = ["Dimitri Sabadie "] [dependencies] -splines = "0.1" +splines = "0.2" diff --git a/examples/02-serialization/Cargo.toml b/examples/02-serialization/Cargo.toml index 0c3e61a..6a39ab8 100644 --- a/examples/02-serialization/Cargo.toml +++ b/examples/02-serialization/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "serialization" -version = "0.1.0" +version = "0.2.0" authors = ["Dimitri Sabadie "] [dependencies] serde_json = "1" [dependencies.splines] -version = "0.1" +version = "0.2" features = ["serialization"] From 0dcfe4841538eb9768cbdabe78676050b766a2b1 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 10:37:08 -0400 Subject: [PATCH 5/9] minor spelling corrections --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b2d3485..a4849ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,11 +83,11 @@ //! - **[cgmath](https://crates.io/crates/cgmath) implementors.** //! + Adds some usefull implementations of `Interpolate` for some cgmath types. //! + Enable with the `"impl-cgmath"` feature. -//! - **Standard library / no stdandard library.** +//! - **Standard library / no standard library.** //! + It’s possible to compile against the standard library or go on your own without it. //! + Compiling with the standard library is enabled by default. -//! + Use `defaut-features = []` in your `Cargo.toml` to disable. -//! + Enable explicitly with the `"std"` feataure. +//! + Use `default-features = []` in your `Cargo.toml` to disable. +//! + Enable explicitly with the `"std"` feature. #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), feature(alloc))] From 882b9e7b34a7afb2a0d5a8d816c0d890f5689509 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 10:38:59 -0400 Subject: [PATCH 6/9] minor corrections in README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0c6e895..284da63 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ This crate has features! Here’s a comprehensive list of what you can enable: - **[cgmath](https://crates.io/crates/cgmath) implementors** + Adds some usefull implementations of `Interpolate` for some cgmath types. + Enable with the `"impl-cgmath"` feature. - - **Standard library / no stdandard library.** + - **Standard library / no standard library.** + It’s possible to compile against the standard library or go on your own without it. + Compiling with the standard library is enabled by default. - + Use `defaut-features = []` in your `Cargo.toml` to disable. - + Enable explicitly with the `"std"` feataure. + + Use `default-features = []` in your `Cargo.toml` to disable. + + Enable explicitly with the `"std"` feature. From 766066d9edbafc6d4f932ad2e5c36917ea14ab4e Mon Sep 17 00:00:00 2001 From: Dimitri Sabadie Date: Sun, 30 Sep 2018 21:38:49 +0200 Subject: [PATCH 7/9] 0.2.2. --- CHANGELOG.md | 9 ++++++++- Cargo.toml | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2222cb2..dcf8784 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ -# 0.2.1 +## 0.2.2 + +> Sun 30th September 2018 + + - Bump version numbers (`splines-0.2`) in examples. + - Fix several typos in the documentation. + +## 0.2.1 > Thu 20th September 2018 diff --git a/Cargo.toml b/Cargo.toml index 42cb008..a6061d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "splines" -version = "0.2.1" +version = "0.2.2" license = "BSD-3-Clause" authors = ["Dimitri Sabadie "] description = "Spline interpolation made easy" From 4ca1c1bbb1016ec07b7de110b319d1ee14a01c41 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 21:15:10 -0400 Subject: [PATCH 8/9] replaced matrix implementation with simpler vector instances for each of Vector1 through Vector6 --- src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1559605..efc07c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -409,18 +409,56 @@ impl Interpolate for na::Point } #[cfg(feature = "impl-nalgebra")] -impl Interpolate for na::Matrix>::Buffer> - where R : DimName, - C : DimName, - ::Value : Mul<::Value>, - ::Value : Mul<::Value>, - <::Value as Mul<::Value>>::Output : generic_array::ArrayLength, - <<::Value as Mul<::Value>>::Output as generic_array::ArrayLength>::ArrayType : Copy, +impl Interpolate for na::Vector1 { 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) + na::Matrix::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) + } +} + +#[cfg(feature = "impl-nalgebra")] +impl Interpolate for na::Vector2 +{ + fn lerp(a: Self, b: Self, t: f32) -> Self + { + na::Matrix::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) + } +} + +#[cfg(feature = "impl-nalgebra")] +impl Interpolate for na::Vector3 +{ + fn lerp(a: Self, b: Self, t: f32) -> Self + { + na::Matrix::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) + } +} + +#[cfg(feature = "impl-nalgebra")] +impl Interpolate for na::Vector4 +{ + fn lerp(a: Self, b: Self, t: f32) -> Self + { + na::Matrix::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) + } +} + +#[cfg(feature = "impl-nalgebra")] +impl Interpolate for na::Vector5 +{ + fn lerp(a: Self, b: Self, t: f32) -> Self + { + na::Matrix::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) + } +} + +#[cfg(feature = "impl-nalgebra")] +impl Interpolate for na::Vector6 +{ + fn lerp(a: Self, b: Self, t: f32) -> Self + { + na::Matrix::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) } } From fb05c123ea6f1482e8d759bf35b3d0671bc36798 Mon Sep 17 00:00:00 2001 From: nsmryan Date: Sun, 30 Sep 2018 21:17:36 -0400 Subject: [PATCH 9/9] removed dependance on generic_arrays which was only needed for the matrix implementation of Interpolate, which was removed --- Cargo.toml | 6 +----- src/lib.rs | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60d8e1a..ff3517d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,16 +22,12 @@ default = ["std", "impl-cgmath"] serialization = ["serde", "serde_derive"] std = [] impl-cgmath = ["cgmath"] -impl-nalgebra = ["nalgebra", "generic-array"] +impl-nalgebra = ["nalgebra"] [dependencies.nalgebra] version = "0.14" optional = true -[dependencies.generic-array] -version = "^0.8" -optional = true - [dependencies.cgmath] version = "0.16" optional = true diff --git a/src/lib.rs b/src/lib.rs index efc07c6..ab215c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,6 @@ #[cfg(feature = "impl-cgmath")] extern crate cgmath; #[cfg(feature = "impl-nalgebra")] extern crate nalgebra; -#[cfg(feature = "impl-nalgebra")] extern crate generic_array; #[cfg(feature = "serialization")] extern crate serde; #[cfg(feature = "serialization")] #[macro_use] extern crate serde_derive;