From b0127ab5dcf506493bbece3771f37354dd89e898 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 25 Jul 2017 11:49:27 -0400 Subject: [PATCH] Mint quaternions --- src/geometry/quaternion_conversion.rs | 45 +++++++++++++++++++++++++++ tests/core/conversion.rs | 17 ++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/geometry/quaternion_conversion.rs b/src/geometry/quaternion_conversion.rs index 7ac90eed..9c264180 100644 --- a/src/geometry/quaternion_conversion.rs +++ b/src/geometry/quaternion_conversion.rs @@ -5,6 +5,8 @@ use alga::linear::Rotation as AlgaRotation; use core::{Vector4, Matrix4}; use core::dimension::U3; +#[cfg(feature = "mint")] +use core::storage::Storage; use geometry::{Quaternion, UnitQuaternion, Rotation, Isometry, Similarity, Transform, SuperTCategoryOf, TAffine, Translation, Rotation3, Point3}; @@ -21,6 +23,9 @@ use geometry::{Quaternion, UnitQuaternion, Rotation, Isometry, Similarity, * UnitQuaternion -> Transform * UnitQuaternion -> Matrix (homogeneous) * + * mint::Quaternion <-> Quaternion + * UnitQuaternion -> mint::Quaternion + * * NOTE: * UnitQuaternion -> Quaternion is already provided by: Unit -> T */ @@ -166,3 +171,43 @@ impl> SubsetOf> for UnitQuaterni Self::from_rotation_matrix(&rot) } } + +#[cfg(feature = "mint")] +impl From> for QuaternionBase +where + N: Real, + S: OwnedStorage, + S::Alloc: OwnedAllocator, +{ + fn from(q: mint::Quaternion) -> Self { + QuaternionBase::new(q.s, q.v.x, q.v.y, q.v.z) + } +} + +#[cfg(feature = "mint")] +impl> Into> for QuaternionBase { + fn into(self) -> mint::Quaternion { + mint::Quaternion { + v: mint::Vector3 { + x: self[0], + y: self[1], + z: self[2], + }, + s: self[3], + } + } +} + +#[cfg(feature = "mint")] +impl> Into> for UnitQuaternionBase { + fn into(self) -> mint::Quaternion { + mint::Quaternion { + v: mint::Vector3 { + x: self[0], + y: self[1], + z: self[2], + }, + s: self[3], + } + } +} diff --git a/tests/core/conversion.rs b/tests/core/conversion.rs index 84420569..ca6d23cf 100644 --- a/tests/core/conversion.rs +++ b/tests/core/conversion.rs @@ -11,6 +11,8 @@ use na::{ Matrix6x2, Matrix6x3, Matrix6x4, Matrix6x5, Point3, Translation3, Isometry3, Similarity3, Affine3, Projective3, Transform3, Rotation3, UnitQuaternion}; +#[cfg(feature = "mint")] +use na::Quaternion; #[cfg(feature = "arbitrary")] @@ -283,3 +285,18 @@ mint_vector_conversion!( mint_vector_conversion_3, Vector3, 3; mint_vector_conversion_4, Vector4, 4; ); + + +#[test] +fn mint_quaternion_conversions() { + let q = Quaternion::new(0.1f64, 0.2, 0.3, 0.4); + let mq: mint::Quaternion = q.into(); + let q2 = Quaternion::from(mq); + + assert_eq!(mq.v.x, q[0]); + assert_eq!(mq.v.y, q[1]); + assert_eq!(mq.v.z, q[2]); + assert_eq!(mq.s, q[3]); + + assert_eq!(q, q2); +}