From d72fc08826b66710b9231f196cccdcd41ae3e1c6 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 6 Jun 2017 18:07:38 -0400 Subject: [PATCH] Mint flavour --- .travis.yml | 2 +- CHANGELOG.md | 9 +++++---- Cargo.toml | 1 + src/euler.rs | 23 +++++++++++++++++++++++ src/lib.rs | 4 ++++ src/macros.rs | 20 ++++++++++++++++++++ src/matrix.rs | 30 ++++++++++++++++++++++++++++++ src/point.rs | 8 ++++++++ src/quaternion.rs | 24 ++++++++++++++++++++++++ src/vector.rs | 10 ++++++++++ 10 files changed, 126 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 99d2910..ab88eda 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ cache: cargo env: - CARGO_FEATURES="" - - CARGO_FEATURES="serde" + - CARGO_FEATURES="mint serde" matrix: include: diff --git a/CHANGELOG.md b/CHANGELOG.md index 45bc5e6..0d89ea9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,21 +6,22 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] -- Refactored `simd` and `serde` dependencies to match feature names +- Refactor `simd` and `serde` dependencies to match feature names +- Integrate `mint` conversions behind a feature ## [v0.14.1] - 2017-05-02 ### Fixed -- Added a workaround for rust-lang/rust#41478, and in the process cleaned up +- Add a workaround for rust-lang/rust#41478, and in the process cleaned up some type projections for angles ## [v0.14.0] - 2017-04-26 ## Changed -- Constrained `VectorSpace`, `Rotation`, and `Angle` by `iter::Sum` -- Constrained `SquareMatrix` by `iter::Product` +- Constrain `VectorSpace`, `Rotation`, and `Angle` by `iter::Sum` +- Constrain `SquareMatrix` by `iter::Product` ## [v0.13.1] - 2017-04-22 diff --git a/Cargo.toml b/Cargo.toml index 9ae5550..bf51087 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ unstable = [] [dependencies] approx = "0.1" +mint = { version = "0.4.1", optional = true } num-traits = "0.1" rand = "0.3" serde = { version = "1.0", features = ["serde_derive"], optional = true } diff --git a/src/euler.rs b/src/euler.rs index 5fd83e2..4ba855d 100644 --- a/src/euler.rs +++ b/src/euler.rs @@ -21,6 +21,8 @@ use structure::*; use angle::Rad; use approx::ApproxEq; use quaternion::Quaternion; +#[cfg(feature = "mint")] +use mint; use num::BaseFloat; /// A set of [Euler angles] representing a rotation in three-dimensional space. @@ -179,3 +181,24 @@ impl Rand for Euler { Euler { x: rng.gen(), y: rng.gen(), z: rng.gen() } } } + +#[cfg(feature = "mint")] +type MintEuler = mint::EulerAngles; + +#[cfg(feature = "mint")] +impl> From> for Euler { + fn from(mint: MintEuler) -> Self { + Euler { + x: mint.a.into(), + y: mint.b.into(), + z: mint.c.into(), + } + } +} + +#[cfg(feature = "mint")] +impl> Into> for Euler { + fn into(self) -> MintEuler { + MintEuler::from([self.x.into(), self.y.into(), self.z.into()]) + } +} diff --git a/src/lib.rs b/src/lib.rs index a9b1b0d..d68585e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,10 @@ #[macro_use] extern crate approx; + +#[cfg(feature = "mint")] +pub extern crate mint; + pub extern crate num_traits; extern crate rand; diff --git a/src/macros.rs b/src/macros.rs index 10e39d3..768c4fd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -456,3 +456,23 @@ macro_rules! impl_operator_simd { } }; } + +/// Generate `mint` types conversion implementations +#[cfg(feature = "mint")] +macro_rules! impl_mint_conversions { + ($ArrayN:ident { $($field:ident),+ }, $Mint:ident) => { + impl Into> for $ArrayN { + #[inline] + fn into(self) -> mint::$Mint { + mint::$Mint::from([$(self.$field),+]) + } + } + + impl From> for $ArrayN { + #[inline] + fn from(v: mint::$Mint) -> Self { + $ArrayN { $( $field: v.$field, )+ } + } + } + } +} diff --git a/src/matrix.rs b/src/matrix.rs index 713edca..176a954 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -32,6 +32,9 @@ use quaternion::Quaternion; use transform::{Transform, Transform2, Transform3}; use vector::{Vector2, Vector3, Vector4}; +#[cfg(feature = "mint")] +use mint; + /// A 2 x 2, column major matrix /// /// This type is marked as `#[repr(C)]`. @@ -1268,6 +1271,33 @@ fixed_array_conversions!(Matrix2 { x:0, y:1 }, 2); fixed_array_conversions!(Matrix3 { x:0, y:1, z:2 }, 3); fixed_array_conversions!(Matrix4 { x:0, y:1, z:2, w:3 }, 4); +#[cfg(feature = "mint")] +macro_rules! mint_conversions { + ($MatrixN:ident { $($field:ident),+ }, $MintN:ident) => { + impl Into> for $MatrixN { + #[inline] + fn into(self) -> mint::$MintN { + mint::$MintN { $($field: self.$field.into()),+ } + } + } + + impl From> for $MatrixN { + #[inline] + fn from(m: mint::$MintN) -> Self { + $MatrixN { $($field: m.$field.into()),+ } + } + } + + } +} + +#[cfg(feature = "mint")] +mint_conversions!(Matrix2 { x, y }, ColumnMatrix2); +#[cfg(feature = "mint")] +mint_conversions!(Matrix3 { x, y, z }, ColumnMatrix3); +#[cfg(feature = "mint")] +mint_conversions!(Matrix4 { x, y, z, w }, ColumnMatrix4); + impl From> for Matrix3 { /// Clone the elements of a 2-dimensional matrix into the top-left corner /// of a 3-dimensional identity matrix. diff --git a/src/point.rs b/src/point.rs index 8b08d59..c263f5c 100644 --- a/src/point.rs +++ b/src/point.rs @@ -28,6 +28,9 @@ use approx::ApproxEq; use num::{BaseNum, BaseFloat}; use vector::{Vector1, Vector2, Vector3, Vector4}; +#[cfg(feature = "mint")] +use mint; + /// A point in 1-dimensional space. /// /// This type is marked as `#[repr(C)]`. @@ -264,6 +267,11 @@ impl_tuple_conversions!(Point1 { x }, (S,)); impl_tuple_conversions!(Point2 { x, y }, (S, S)); impl_tuple_conversions!(Point3 { x, y, z }, (S, S, S)); +#[cfg(feature = "mint")] +impl_mint_conversions!(Point2 { x, y }, Point2); +#[cfg(feature = "mint")] +impl_mint_conversions!(Point3 { x, y, z }, Point3); + impl fmt::Debug for Point1 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(write!(f, "Point1 ")); diff --git a/src/quaternion.rs b/src/quaternion.rs index 599756d..2c27e72 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -34,6 +34,9 @@ use vector::Vector3; #[cfg(feature = "simd")] use simd::f32x4 as Simdf32x4; +#[cfg(feature = "mint")] +use mint; + /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. /// @@ -808,6 +811,27 @@ impl Rand for Quaternion { } } +#[cfg(feature = "mint")] +impl From> for Quaternion { + fn from(q: mint::Quaternion) -> Self { + Quaternion { + s: q.s, + v: q.v.into(), + } + } +} + +#[cfg(feature = "mint")] +impl Into> for Quaternion { + fn into(self) -> mint::Quaternion { + mint::Quaternion { + s: self.s, + v: self.v.into(), + } + } +} + + #[cfg(test)] mod tests { use quaternion::*; diff --git a/src/vector.rs b/src/vector.rs index 412fc43..7b70a72 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -33,6 +33,9 @@ use simd::i32x4 as Simdi32x4; #[cfg(feature = "simd")] use simd::u32x4 as Simdu32x4; +#[cfg(feature = "mint")] +use mint; + /// A 1-dimensional vector. /// /// This type is marked as `#[repr(C)]`. @@ -1119,6 +1122,13 @@ impl MulAssign for Vector4 { } } +#[cfg(feature = "mint")] +impl_mint_conversions!(Vector2 { x, y }, Vector2); +#[cfg(feature = "mint")] +impl_mint_conversions!(Vector3 { x, y, z }, Vector3); +#[cfg(feature = "mint")] +impl_mint_conversions!(Vector4 { x, y, z, w }, Vector4); + #[cfg(test)] mod tests {