Mint flavour

This commit is contained in:
Dzmitry Malyshau 2017-06-06 18:07:38 -04:00
parent 7a21125c55
commit d72fc08826
10 changed files with 126 additions and 5 deletions

View file

@ -9,7 +9,7 @@ cache: cargo
env: env:
- CARGO_FEATURES="" - CARGO_FEATURES=""
- CARGO_FEATURES="serde" - CARGO_FEATURES="mint serde"
matrix: matrix:
include: include:

View file

@ -6,21 +6,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [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 ## [v0.14.1] - 2017-05-02
### Fixed ### 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 some type projections for angles
## [v0.14.0] - 2017-04-26 ## [v0.14.0] - 2017-04-26
## Changed ## Changed
- Constrained `VectorSpace`, `Rotation`, and `Angle` by `iter::Sum` - Constrain `VectorSpace`, `Rotation`, and `Angle` by `iter::Sum`
- Constrained `SquareMatrix` by `iter::Product` - Constrain `SquareMatrix` by `iter::Product`
## [v0.13.1] - 2017-04-22 ## [v0.13.1] - 2017-04-22

View file

@ -21,6 +21,7 @@ unstable = []
[dependencies] [dependencies]
approx = "0.1" approx = "0.1"
mint = { version = "0.4.1", optional = true }
num-traits = "0.1" num-traits = "0.1"
rand = "0.3" rand = "0.3"
serde = { version = "1.0", features = ["serde_derive"], optional = true } serde = { version = "1.0", features = ["serde_derive"], optional = true }

View file

@ -21,6 +21,8 @@ use structure::*;
use angle::Rad; use angle::Rad;
use approx::ApproxEq; use approx::ApproxEq;
use quaternion::Quaternion; use quaternion::Quaternion;
#[cfg(feature = "mint")]
use mint;
use num::BaseFloat; use num::BaseFloat;
/// A set of [Euler angles] representing a rotation in three-dimensional space. /// A set of [Euler angles] representing a rotation in three-dimensional space.
@ -179,3 +181,24 @@ impl<A: Angle + Rand> Rand for Euler<A> {
Euler { x: rng.gen(), y: rng.gen(), z: rng.gen() } Euler { x: rng.gen(), y: rng.gen(), z: rng.gen() }
} }
} }
#[cfg(feature = "mint")]
type MintEuler<S> = mint::EulerAngles<S, mint::IntraXYZ>;
#[cfg(feature = "mint")]
impl<S, A: Angle + From<S>> From<MintEuler<S>> for Euler<A> {
fn from(mint: MintEuler<S>) -> Self {
Euler {
x: mint.a.into(),
y: mint.b.into(),
z: mint.c.into(),
}
}
}
#[cfg(feature = "mint")]
impl<S: Clone, A: Angle + Into<S>> Into<MintEuler<S>> for Euler<A> {
fn into(self) -> MintEuler<S> {
MintEuler::from([self.x.into(), self.y.into(), self.z.into()])
}
}

View file

@ -54,6 +54,10 @@
#[macro_use] #[macro_use]
extern crate approx; extern crate approx;
#[cfg(feature = "mint")]
pub extern crate mint;
pub extern crate num_traits; pub extern crate num_traits;
extern crate rand; extern crate rand;

View file

@ -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<S: Clone> Into<mint::$Mint<S>> for $ArrayN<S> {
#[inline]
fn into(self) -> mint::$Mint<S> {
mint::$Mint::from([$(self.$field),+])
}
}
impl<S> From<mint::$Mint<S>> for $ArrayN<S> {
#[inline]
fn from(v: mint::$Mint<S>) -> Self {
$ArrayN { $( $field: v.$field, )+ }
}
}
}
}

View file

@ -32,6 +32,9 @@ use quaternion::Quaternion;
use transform::{Transform, Transform2, Transform3}; use transform::{Transform, Transform2, Transform3};
use vector::{Vector2, Vector3, Vector4}; use vector::{Vector2, Vector3, Vector4};
#[cfg(feature = "mint")]
use mint;
/// A 2 x 2, column major matrix /// A 2 x 2, column major matrix
/// ///
/// This type is marked as `#[repr(C)]`. /// This type is marked as `#[repr(C)]`.
@ -1268,6 +1271,33 @@ fixed_array_conversions!(Matrix2<S> { x:0, y:1 }, 2);
fixed_array_conversions!(Matrix3<S> { x:0, y:1, z:2 }, 3); fixed_array_conversions!(Matrix3<S> { x:0, y:1, z:2 }, 3);
fixed_array_conversions!(Matrix4<S> { x:0, y:1, z:2, w:3 }, 4); fixed_array_conversions!(Matrix4<S> { x:0, y:1, z:2, w:3 }, 4);
#[cfg(feature = "mint")]
macro_rules! mint_conversions {
($MatrixN:ident { $($field:ident),+ }, $MintN:ident) => {
impl<S: Clone> Into<mint::$MintN<S>> for $MatrixN<S> {
#[inline]
fn into(self) -> mint::$MintN<S> {
mint::$MintN { $($field: self.$field.into()),+ }
}
}
impl<S> From<mint::$MintN<S>> for $MatrixN<S> {
#[inline]
fn from(m: mint::$MintN<S>) -> 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<S: BaseFloat> From<Matrix2<S>> for Matrix3<S> { impl<S: BaseFloat> From<Matrix2<S>> for Matrix3<S> {
/// Clone the elements of a 2-dimensional matrix into the top-left corner /// Clone the elements of a 2-dimensional matrix into the top-left corner
/// of a 3-dimensional identity matrix. /// of a 3-dimensional identity matrix.

View file

@ -28,6 +28,9 @@ use approx::ApproxEq;
use num::{BaseNum, BaseFloat}; use num::{BaseNum, BaseFloat};
use vector::{Vector1, Vector2, Vector3, Vector4}; use vector::{Vector1, Vector2, Vector3, Vector4};
#[cfg(feature = "mint")]
use mint;
/// A point in 1-dimensional space. /// A point in 1-dimensional space.
/// ///
/// This type is marked as `#[repr(C)]`. /// This type is marked as `#[repr(C)]`.
@ -264,6 +267,11 @@ impl_tuple_conversions!(Point1<S> { x }, (S,));
impl_tuple_conversions!(Point2<S> { x, y }, (S, S)); impl_tuple_conversions!(Point2<S> { x, y }, (S, S));
impl_tuple_conversions!(Point3<S> { x, y, z }, (S, S, S)); impl_tuple_conversions!(Point3<S> { 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<S: fmt::Debug> fmt::Debug for Point1<S> { impl<S: fmt::Debug> fmt::Debug for Point1<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Point1 ")); try!(write!(f, "Point1 "));

View file

@ -34,6 +34,9 @@ use vector::Vector3;
#[cfg(feature = "simd")] #[cfg(feature = "simd")]
use simd::f32x4 as Simdf32x4; use simd::f32x4 as Simdf32x4;
#[cfg(feature = "mint")]
use mint;
/// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector
/// form. /// form.
/// ///
@ -808,6 +811,27 @@ impl<S: BaseFloat + Rand> Rand for Quaternion<S> {
} }
} }
#[cfg(feature = "mint")]
impl<S> From<mint::Quaternion<S>> for Quaternion<S> {
fn from(q: mint::Quaternion<S>) -> Self {
Quaternion {
s: q.s,
v: q.v.into(),
}
}
}
#[cfg(feature = "mint")]
impl<S: Clone> Into<mint::Quaternion<S>> for Quaternion<S> {
fn into(self) -> mint::Quaternion<S> {
mint::Quaternion {
s: self.s,
v: self.v.into(),
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use quaternion::*; use quaternion::*;

View file

@ -33,6 +33,9 @@ use simd::i32x4 as Simdi32x4;
#[cfg(feature = "simd")] #[cfg(feature = "simd")]
use simd::u32x4 as Simdu32x4; use simd::u32x4 as Simdu32x4;
#[cfg(feature = "mint")]
use mint;
/// A 1-dimensional vector. /// A 1-dimensional vector.
/// ///
/// This type is marked as `#[repr(C)]`. /// This type is marked as `#[repr(C)]`.
@ -1119,6 +1122,13 @@ impl MulAssign<u32> for Vector4<u32> {
} }
} }
#[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)] #[cfg(test)]
mod tests { mod tests {