Fixed Zero and One for Quaternion.

This commit is contained in:
Dimitri 'phaazon' Sabadie 2016-07-30 14:08:55 +02:00
parent f9c1a2fd98
commit 1fdd8979af
2 changed files with 28 additions and 1 deletions

View File

@ -596,6 +596,22 @@ impl Quaternion<f32> {
} }
} }
impl<T> Zero for Quaternion<T> 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<T> One for Quaternion<T> where T: Copy + One + Zero + Sub<T, Output = T> + Add<T, Output = T> {
fn one() -> Self {
Quaternion::new(T::one(), T::zero(), T::zero(), T::zero())
}
}
pord_impl!(Quaternion, w, i, j, k); pord_impl!(Quaternion, w, i, j, k);
vec_axis_impl!(Quaternion, w, i, j, k); vec_axis_impl!(Quaternion, w, i, j, k);
vec_cast_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_mul_impl!(Quaternion, w, i, j, k);
scalar_div_impl!(Quaternion, w, i, j, k); scalar_div_impl!(Quaternion, w, i, j, k);
neg_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); approx_eq_impl!(Quaternion, w, i, j, k);
from_iterator_impl!(Quaternion, iterator, iterator, iterator, iterator); from_iterator_impl!(Quaternion, iterator, iterator, iterator, iterator);
bounded_impl!(Quaternion, w, i, j, k); bounded_impl!(Quaternion, w, i, j, k);

View File

@ -96,3 +96,15 @@ fn test_quaternion_exp_zero_is_one() {
let q = Quaternion::new(0., 0., 0., 0.); let q = Quaternion::new(0., 0., 0., 0.);
assert!(na::approx_eq(&q.exp(), &one())) assert!(na::approx_eq(&q.exp(), &one()))
} }
#[test]
fn test_quaternion_neutral() {
for _ in 0 .. 10000 {
let q1: Quaternion<f32> = random();
let qi: Quaternion<f32> = one();
let q2 = q1 * qi;
let q3 = qi * q1;
assert!(na::approx_eq(&q1, &q2) && na::approx_eq(&q2, &q3))
}
}