From 491a82ec2808c7ba985d68b405c894076c6b090a Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 20 Nov 2012 15:35:06 +1000 Subject: [PATCH] Implement DefaultEq for matrix, vector and quaternion types --- src/funs/test/test_boolv.rs | 20 ++-- src/mat.rs | 179 ++++++++++++++++++------------------ src/quat.rs | 28 +++--- src/vec.rs | 70 +++++++------- 4 files changed, 149 insertions(+), 148 deletions(-) diff --git a/src/funs/test/test_boolv.rs b/src/funs/test/test_boolv.rs index dce9ede..fcafb70 100644 --- a/src/funs/test/test_boolv.rs +++ b/src/funs/test/test_boolv.rs @@ -9,15 +9,15 @@ fn test_boolv2() { assert tf.any() == true; assert tf.all() == false; - assert tf.not().exact_eq(&Vec2::new(false, true)); + assert tf.not() == Vec2::new(false, true); assert ff.any() == false; assert ff.all() == false; - assert ff.not().exact_eq(&Vec2::new(true, true)); + assert ff.not() == Vec2::new(true, true); assert tt.any() == true; assert tt.all() == true; - assert tt.not().exact_eq(&Vec2::new(false, false)); + assert tt.not() == Vec2::new(false, false); } #[test] @@ -28,15 +28,15 @@ fn test_boolv3() { assert tft.any() == true; assert tft.all() == false; - assert tft.not().exact_eq(&Vec3::new(false, true, false)); + assert tft.not() == Vec3::new(false, true, false); assert fff.any() == false; assert fff.all() == false; - assert fff.not().exact_eq(&Vec3::new(true, true, true)); + assert fff.not() == Vec3::new(true, true, true); assert ttt.any() == true; assert ttt.all() == true; - assert ttt.not().exact_eq(&Vec3::new(false, false, false)); + assert ttt.not() == Vec3::new(false, false, false); } #[test] @@ -47,15 +47,15 @@ fn test_boolv4() { assert tftf.any() == true; assert tftf.all() == false; - assert tftf.not().exact_eq(&Vec4::new(false, true, false, true)); + assert tftf.not() == Vec4::new(false, true, false, true); assert ffff.any() == false; assert ffff.all() == false; - assert ffff.not().exact_eq(&Vec4::new(true, true, true, true)); + assert ffff.not() == Vec4::new(true, true, true, true); assert tttt.any() == true; assert tttt.all() == true; - assert tttt.not().exact_eq(&Vec4::new(false, false, false, false)); + assert tttt.not() == Vec4::new(false, false, false, false); } #[test] @@ -66,5 +66,5 @@ fn test_boolv_fns() { assert any(&tf) == true; assert all(&ftf) == false; - assert not(&tftf).exact_eq(&Vec4::new(false, true, false, true)); + assert not(&tftf) == Vec4::new(false, true, false, true); } \ No newline at end of file diff --git a/src/mat.rs b/src/mat.rs index df08ceb..6a3d9f6 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -10,11 +10,12 @@ use funs::common::*; use funs::exp::*; use math::*; use num::cast::*; +use num::default_eq::DefaultEq; use quat::{Quat, ToQuat}; use vec::{Vec2, Vec3, Vec4}; -pub trait Matrix: Dimensional, Eq { +pub trait Matrix: Dimensional, Eq, DefaultEq { pure fn rows() -> uint; pure fn cols() -> uint; pure fn is_col_major() -> bool; @@ -177,7 +178,7 @@ pub impl Mat2: NumericMatrix, Vec2> { } } -pub impl Mat2: NumericMatrix_NxN> { +pub impl Mat2: NumericMatrix_NxN> { #[inline(always)] pure fn mul_m(other: &Mat2) -> Mat2 { Mat2::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), @@ -192,7 +193,7 @@ pub impl Mat2: NumericMatrix_NxN> { pure fn invert() -> Option> { let _0 = cast(0); let d = self.det(); - if d.fuzzy_eq(&_0) { + if d.default_eq(&_0) { None } else { Some(Mat2::new(self[1][1]/d, -self[0][1]/d, @@ -208,31 +209,31 @@ pub impl Mat2: NumericMatrix_NxN> { #[inline(always)] pure fn is_identity() -> bool { - self.fuzzy_eq(&Mat2::identity()) + self.default_eq(&Mat2::identity()) } #[inline(always)] pure fn is_symmetric() -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[1][0].fuzzy_eq(&self[0][1]) + self[0][1].default_eq(&self[1][0]) && + self[1][0].default_eq(&self[0][1]) } #[inline(always)] pure fn is_diagonal() -> bool { let _0 = cast(0); - self[0][1].fuzzy_eq(&_0) && - self[1][0].fuzzy_eq(&_0) + self[0][1].default_eq(&_0) && + self[1][0].default_eq(&_0) } #[inline(always)] pure fn is_rotated() -> bool { - !self.fuzzy_eq(&Mat2::identity()) + !self.default_eq(&Mat2::identity()) } #[inline(always)] pure fn is_invertible() -> bool { let _0 = cast(0); - !self.det().fuzzy_eq(&_0) + !self.det().default_eq(&_0) } } @@ -249,10 +250,10 @@ pub impl Mat2: Matrix2 { } // TODO: make work for T:Integer -pub impl Mat2: Eq { +pub impl Mat2: Eq { #[inline(always)] pure fn eq(other: &Mat2) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -261,14 +262,6 @@ pub impl Mat2: Eq { } } -impl Mat2: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Mat2) -> bool { - self[0].exact_eq(&other[0]) && - self[1].exact_eq(&other[1]) - } -} - pub impl Mat2: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Mat2) -> bool { @@ -277,6 +270,14 @@ pub impl Mat2: FuzzyEq { } } +pub impl Mat2: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Mat2) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) + } +} + @@ -415,7 +416,7 @@ pub impl Mat3: NumericMatrix, Vec3> { } } -pub impl Mat3: NumericMatrix_NxN> { +pub impl Mat3: NumericMatrix_NxN> { #[inline(always)] pure fn mul_m(other: &Mat3) -> Mat3 { Mat3::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), @@ -431,7 +432,7 @@ pub impl Mat3: NumericMatrix_NxN> { pure fn invert() -> Option> { let d = self.det(); let _0 = cast(0); - if d.fuzzy_eq(&_0) { + if d.default_eq(&_0) { None } else { Some(Mat3::from_cols(self[1].cross(&self[2]).div_t(d), @@ -450,43 +451,43 @@ pub impl Mat3: NumericMatrix_NxN> { #[inline(always)] pure fn is_identity() -> bool { - self.fuzzy_eq(&Mat3::identity()) + self.default_eq(&Mat3::identity()) } #[inline(always)] pure fn is_symmetric() -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[0][2].fuzzy_eq(&self[2][0]) && + self[0][1].default_eq(&self[1][0]) && + self[0][2].default_eq(&self[2][0]) && - self[1][0].fuzzy_eq(&self[0][1]) && - self[1][2].fuzzy_eq(&self[2][1]) && + self[1][0].default_eq(&self[0][1]) && + self[1][2].default_eq(&self[2][1]) && - self[2][0].fuzzy_eq(&self[0][2]) && - self[2][1].fuzzy_eq(&self[1][2]) + self[2][0].default_eq(&self[0][2]) && + self[2][1].default_eq(&self[1][2]) } #[inline(always)] pure fn is_diagonal() -> bool { let _0 = cast(0); - self[0][1].fuzzy_eq(&_0) && - self[0][2].fuzzy_eq(&_0) && + self[0][1].default_eq(&_0) && + self[0][2].default_eq(&_0) && - self[1][0].fuzzy_eq(&_0) && - self[1][2].fuzzy_eq(&_0) && + self[1][0].default_eq(&_0) && + self[1][2].default_eq(&_0) && - self[2][0].fuzzy_eq(&_0) && - self[2][1].fuzzy_eq(&_0) + self[2][0].default_eq(&_0) && + self[2][1].default_eq(&_0) } #[inline(always)] pure fn is_rotated() -> bool { - !self.fuzzy_eq(&Mat3::identity()) + !self.default_eq(&Mat3::identity()) } #[inline(always)] pure fn is_invertible() -> bool { let _0 = cast(0); - !self.det().fuzzy_eq(&_0) + !self.det().default_eq(&_0) } } @@ -541,10 +542,10 @@ pub impl Mat3: ToQuat { } // TODO: make work for T:Integer -pub impl Mat3: Eq { +pub impl Mat3: Eq { #[inline(always)] pure fn eq(other: &Mat3) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -553,15 +554,6 @@ pub impl Mat3: Eq { } } -pub impl Mat3: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Mat3) -> bool { - self[0].exact_eq(&other[0]) && - self[1].exact_eq(&other[1]) && - self[2].exact_eq(&other[2]) - } -} - pub impl Mat3: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Mat3) -> bool { @@ -571,6 +563,15 @@ pub impl Mat3: FuzzyEq { } } +pub impl Mat3: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Mat3) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) && + self[2].default_eq(&other[2]) + } +} + @@ -731,7 +732,7 @@ pub impl Mat4: NumericMatrix, Vec4> { } } -pub impl Mat4: NumericMatrix_NxN> { +pub impl Mat4: NumericMatrix_NxN> { #[inline(always)] pure fn mul_m(other: &Mat4) -> Mat4 { // Surprisingly when building with optimisation turned on this is actually @@ -761,7 +762,7 @@ pub impl Mat4: NumericMatrix_NxN Option> { let d = self.det(); let _0 = cast(0); - if d.fuzzy_eq(&_0) { + if d.default_eq(&_0) { None } else { @@ -824,57 +825,57 @@ pub impl Mat4: NumericMatrix_NxN bool { - self.fuzzy_eq(&Mat4::identity()) + self.default_eq(&Mat4::identity()) } #[inline(always)] pure fn is_symmetric() -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[0][2].fuzzy_eq(&self[2][0]) && - self[0][3].fuzzy_eq(&self[3][0]) && + self[0][1].default_eq(&self[1][0]) && + self[0][2].default_eq(&self[2][0]) && + self[0][3].default_eq(&self[3][0]) && - self[1][0].fuzzy_eq(&self[0][1]) && - self[1][2].fuzzy_eq(&self[2][1]) && - self[1][3].fuzzy_eq(&self[3][1]) && + self[1][0].default_eq(&self[0][1]) && + self[1][2].default_eq(&self[2][1]) && + self[1][3].default_eq(&self[3][1]) && - self[2][0].fuzzy_eq(&self[0][2]) && - self[2][1].fuzzy_eq(&self[1][2]) && - self[2][3].fuzzy_eq(&self[3][2]) && + self[2][0].default_eq(&self[0][2]) && + self[2][1].default_eq(&self[1][2]) && + self[2][3].default_eq(&self[3][2]) && - self[3][0].fuzzy_eq(&self[0][3]) && - self[3][1].fuzzy_eq(&self[1][3]) && - self[3][2].fuzzy_eq(&self[2][3]) + self[3][0].default_eq(&self[0][3]) && + self[3][1].default_eq(&self[1][3]) && + self[3][2].default_eq(&self[2][3]) } #[inline(always)] pure fn is_diagonal() -> bool { let _0 = cast(0); - self[0][1].fuzzy_eq(&_0) && - self[0][2].fuzzy_eq(&_0) && - self[0][3].fuzzy_eq(&_0) && + self[0][1].default_eq(&_0) && + self[0][2].default_eq(&_0) && + self[0][3].default_eq(&_0) && - self[1][0].fuzzy_eq(&_0) && - self[1][2].fuzzy_eq(&_0) && - self[1][3].fuzzy_eq(&_0) && + self[1][0].default_eq(&_0) && + self[1][2].default_eq(&_0) && + self[1][3].default_eq(&_0) && - self[2][0].fuzzy_eq(&_0) && - self[2][1].fuzzy_eq(&_0) && - self[2][3].fuzzy_eq(&_0) && + self[2][0].default_eq(&_0) && + self[2][1].default_eq(&_0) && + self[2][3].default_eq(&_0) && - self[3][0].fuzzy_eq(&_0) && - self[3][1].fuzzy_eq(&_0) && - self[3][2].fuzzy_eq(&_0) + self[3][0].default_eq(&_0) && + self[3][1].default_eq(&_0) && + self[3][2].default_eq(&_0) } #[inline(always)] pure fn is_rotated() -> bool { - !self.fuzzy_eq(&Mat4::identity()) + !self.default_eq(&Mat4::identity()) } #[inline(always)] pure fn is_invertible() -> bool { let _0 = cast(0); - !self.det().fuzzy_eq(&_0) + !self.det().default_eq(&_0) } } @@ -883,10 +884,10 @@ pub impl Mat4: Matrix4 { } // TODO: make work for T:Integer -pub impl Mat4: Eq { +pub impl Mat4: Eq { #[inline(always)] pure fn eq(other: &Mat4) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -895,16 +896,6 @@ pub impl Mat4: Eq { } } -pub impl Mat4: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Mat4) -> bool { - self[0].exact_eq(&other[0]) && - self[1].exact_eq(&other[1]) && - self[2].exact_eq(&other[2]) && - self[3].exact_eq(&other[3]) - } -} - pub impl Mat4: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Mat4) -> bool { @@ -914,3 +905,13 @@ pub impl Mat4: FuzzyEq { self[3].fuzzy_eq(&other[3]) } } + +pub impl Mat4: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Mat4) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) && + self[2].default_eq(&other[2]) && + self[3].default_eq(&other[3]) + } +} diff --git a/src/quat.rs b/src/quat.rs index 8931744..cd0f05a 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -12,13 +12,14 @@ use funs::common::*; use math::*; use mat::{Mat3, Mat4}; use num::cast::*; +use num::default_eq::DefaultEq; use vec::Vec3; // // Quaternion // -pub trait Quaternion: Dimensional, Eq, Neg { +pub trait Quaternion: Dimensional, Eq, DefaultEq, Neg { pure fn mul_t(value: T) -> self; pure fn div_t(value: T) -> self; @@ -266,11 +267,10 @@ pub impl Quat: Index { } } -// TODO: make work for T:Integer -pub impl Quat: Eq { +pub impl Quat: Eq { #[inline(always)] pure fn eq(other: &Quat) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -279,16 +279,6 @@ pub impl Quat: Eq { } } -pub impl Quat: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Quat) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] && - self[3] == other[3] - } -} - pub impl Quat: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Quat) -> bool { @@ -297,4 +287,14 @@ pub impl Quat: FuzzyEq { self[2].fuzzy_eq(&other[2]) && self[3].fuzzy_eq(&other[3]) } +} + +pub impl Quat: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Quat) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) && + self[2].default_eq(&other[2]) && + self[3].default_eq(&other[3]) + } } \ No newline at end of file diff --git a/src/vec.rs b/src/vec.rs index a9c7704..93fc10d 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -9,9 +9,10 @@ use dim::Dimensional; use funs::exp::Exp; use math::*; use num::cast::*; +use num::default_eq::DefaultEq; -pub trait Vector: Dimensional, Eq {} +pub trait Vector: Dimensional, Eq, DefaultEq {} pub trait NumericVector: Vector, Neg{ pure fn mul_t(value: T) -> self; @@ -187,10 +188,10 @@ pub impl Vec2: GeometricVector { } // TODO: make work for T:Integer -pub impl Vec2: Eq { +pub impl Vec2: Eq { #[inline(always)] pure fn eq(other: &Vec2) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -199,14 +200,6 @@ pub impl Vec2: Eq { } } -impl Vec2: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Vec2) -> bool { - self[0] == other[0] && - self[1] == other[1] - } -} - pub impl Vec2: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Vec2) -> bool { @@ -215,6 +208,14 @@ pub impl Vec2: FuzzyEq { } } +pub impl Vec2: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Vec2) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) + } +} + @@ -375,10 +376,10 @@ pub impl Vec3: GeometricVector { } // TODO: make work for T:Integer -pub impl Vec3: Eq { +pub impl Vec3: Eq { #[inline(always)] pure fn eq(other: &Vec3) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -387,15 +388,6 @@ pub impl Vec3: Eq { } } -pub impl Vec3: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Vec3) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] - } -} - pub impl Vec3: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Vec3) -> bool { @@ -405,6 +397,15 @@ pub impl Vec3: FuzzyEq { } } +pub impl Vec3: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Vec3) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) && + self[2].default_eq(&other[2]) + } +} + @@ -566,10 +567,10 @@ pub impl Vec4: GeometricVector { } } -pub impl Vec4: Eq { +pub impl Vec4: Eq { #[inline(always)] pure fn eq(other: &Vec4) -> bool { - self.fuzzy_eq(other) + self.default_eq(other) } #[inline(always)] @@ -578,17 +579,6 @@ pub impl Vec4: Eq { } } -// TODO: make work for T:Integer -pub impl Vec4: ExactEq { - #[inline(always)] - pure fn exact_eq(other: &Vec4) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] && - self[3] == other[3] - } -} - pub impl Vec4: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Vec4) -> bool { @@ -597,4 +587,14 @@ pub impl Vec4: FuzzyEq { self[2].fuzzy_eq(&other[2]) && self[3].fuzzy_eq(&other[3]) } +} + +pub impl Vec4: DefaultEq { + #[inline(always)] + pure fn default_eq(other: &Vec4) -> bool { + self[0].default_eq(&other[0]) && + self[1].default_eq(&other[1]) && + self[2].default_eq(&other[2]) && + self[3].default_eq(&other[3]) + } } \ No newline at end of file