From 1fdd8979afa464d1a13d0bfe926e0d9aaab0ee1a Mon Sep 17 00:00:00 2001 From: Dimitri 'phaazon' Sabadie Date: Sat, 30 Jul 2016 14:08:55 +0200 Subject: [PATCH] Fixed Zero and One for Quaternion. --- src/structs/quaternion.rs | 17 ++++++++++++++++- tests/quat.rs | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/structs/quaternion.rs b/src/structs/quaternion.rs index 504066b6..aa127402 100644 --- a/src/structs/quaternion.rs +++ b/src/structs/quaternion.rs @@ -596,6 +596,22 @@ impl Quaternion { } } +impl Zero for Quaternion where T: Zero { + fn zero() -> Self { + Quaternion::new(::zero(), ::zero(), ::zero(), ::zero()) + } + + fn is_zero(&self) -> bool { + self.w.is_zero() && self.i.is_zero() && self.j.is_zero() && self.k.is_zero() + } +} + +impl One for Quaternion where T: Copy + One + Zero + Sub + Add { + fn one() -> Self { + Quaternion::new(T::one(), T::zero(), T::zero(), T::zero()) + } +} + pord_impl!(Quaternion, w, i, j, k); vec_axis_impl!(Quaternion, w, i, j, k); vec_cast_impl!(Quaternion, w, i, j, k); @@ -613,7 +629,6 @@ scalar_sub_impl!(Quaternion, w, i, j, k); scalar_mul_impl!(Quaternion, w, i, j, k); scalar_div_impl!(Quaternion, w, i, j, k); neg_impl!(Quaternion, w, i, j, k); -zero_one_impl!(Quaternion, w, i, j, k); approx_eq_impl!(Quaternion, w, i, j, k); from_iterator_impl!(Quaternion, iterator, iterator, iterator, iterator); bounded_impl!(Quaternion, w, i, j, k); diff --git a/tests/quat.rs b/tests/quat.rs index cfe2557a..1b0185eb 100644 --- a/tests/quat.rs +++ b/tests/quat.rs @@ -96,3 +96,15 @@ fn test_quaternion_exp_zero_is_one() { let q = Quaternion::new(0., 0., 0., 0.); assert!(na::approx_eq(&q.exp(), &one())) } + +#[test] +fn test_quaternion_neutral() { + for _ in 0 .. 10000 { + let q1: Quaternion = random(); + let qi: Quaternion = one(); + let q2 = q1 * qi; + let q3 = qi * q1; + + assert!(na::approx_eq(&q1, &q2) && na::approx_eq(&q2, &q3)) + } +}