From 6f0b49aa0c834b52ea804299edf7acf5280f25cb Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 30 Oct 2012 12:45:18 +1000 Subject: [PATCH] Move exact_eq to trait --- src/math.rs | 4 ++++ src/matrix.rs | 52 +++++++++++++++++++++++++---------------------- src/quaternion.rs | 22 ++++++++++---------- src/vector.rs | 52 +++++++++++++++++++++++++---------------------- 4 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/math.rs b/src/math.rs index fd20a05..b5b6b5a 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,6 +1,10 @@ use cmp::Ord; use num::{Num, from_int}; +pub trait ExactEq { + pure fn exact_eq(other: &self) -> bool; +} + // // Min // diff --git a/src/matrix.rs b/src/matrix.rs index c16d780..f3cdece 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,6 +1,6 @@ use std::cmp::FuzzyEq; use cmp::Eq; -use math::Sqrt; +use math::{ExactEq, Sqrt}; use quaternion::Quat; use vector::{Vec2, Vec3, Vec4}; @@ -24,8 +24,6 @@ pub trait Matrix { // pure fn invert(other: &self) -> self; pure fn transpose() -> self; - pure fn exact_eq(other: &self) -> bool; - pure fn is_identity() -> bool; pure fn is_symmetric() -> bool; pure fn is_diagonal() -> bool; @@ -156,12 +154,6 @@ pub impl Mat2: Matrix { self[0][1], self[1][1]) } - #[inline] - pure fn exact_eq(other: &Mat2) -> bool { - self[0].exact_eq(&other[0]) && - self[1].exact_eq(&other[1]) - } - #[inline] pure fn is_identity() -> bool { self.fuzzy_eq(&Mat2::identity) @@ -211,6 +203,14 @@ pub impl Mat2: Eq { } } +impl Mat2: ExactEq { + #[inline] + pure fn exact_eq(other: &Mat2) -> bool { + self[0].exact_eq(&other[0]) && + self[1].exact_eq(&other[1]) + } +} + pub impl Mat2: FuzzyEq { #[inline] pure fn fuzzy_eq(other: &Mat2) -> bool { @@ -337,13 +337,6 @@ pub impl Mat3: Matrix { self[0][2], self[1][2], self[2][2]) } - #[inline] - 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]) - } - #[inline] pure fn is_identity() -> bool { self.fuzzy_eq(&Mat3::identity) @@ -462,6 +455,15 @@ pub impl Mat3: Eq { } } +impl Mat3: ExactEq { + #[inline] + 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] pure fn fuzzy_eq(other: &Mat3) -> bool { @@ -607,14 +609,6 @@ pub impl Mat4: Matrix { self[0][3], self[1][3], self[2][3], self[3][3]) } - #[inline] - 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]) - } - #[inline] pure fn is_identity() -> bool { self.fuzzy_eq(&Mat4::identity) @@ -711,6 +705,16 @@ pub impl Mat4: Eq { } } +impl Mat4: ExactEq { + #[inline] + 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] pure fn fuzzy_eq(other: &Mat4) -> bool { diff --git a/src/quaternion.rs b/src/quaternion.rs index 09bfa66..21e9ec6 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -3,7 +3,7 @@ use vec::raw::buf_as_slice; use ptr::to_unsafe_ptr; use cmp::Eq; use std::cmp::FuzzyEq; -use math::*; +use math::{ExactEq, Sqrt}; use matrix::{Mat3, Mat4}; use vector::Vec3; @@ -22,8 +22,6 @@ pub trait Quaternion { pure fn sub_q(other: &self) -> self; pure fn mul_q(other: &self) -> self; - pure fn exact_eq(other: &self) -> bool; - pure fn conjugate() -> self; pure fn inverse() -> self; pure fn magnitude2() -> T; @@ -103,14 +101,6 @@ pub impl Quat: Quaternion { self.w * other.z + self.z * other.w + self.x * other.y - self.y * other.x) } - #[inline] - pure fn exact_eq(other: &Quat) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] && - self[3] == other[3] - } - #[inline] pure fn conjugate() -> Quat { Quat(self.w, -self.x, -self.y, -self.z) @@ -193,6 +183,16 @@ pub impl Quat: Eq { } } +impl Quat: ExactEq { + #[inline] + 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] pure fn fuzzy_eq(other: &Quat) -> bool { diff --git a/src/vector.rs b/src/vector.rs index 83b6846..e0b6161 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -3,7 +3,7 @@ use vec::raw::buf_as_slice; use ptr::to_unsafe_ptr; use cmp::Eq; use std::cmp::FuzzyEq; -use math::*; +use math::{Abs, abs, ExactEq, max, min, Sqrt}; // // N-dimensional Vector @@ -21,8 +21,6 @@ pub trait Vector { pure fn dot(other: &self) -> T; - pure fn exact_eq(other:&self) -> bool; - pure fn magnitude2() -> T; pure fn magnitude() -> T; pure fn normalize() -> self; @@ -111,12 +109,6 @@ pub impl Vec2: Vector { self[1] * other[1] } - #[inline] - pure fn exact_eq(other: &Vec2) -> bool { - self[0] == other[0] && - self[1] == other[1] - } - #[inline] pure fn magnitude2() -> float { self[0] * self[0] + @@ -193,6 +185,14 @@ pub impl Vec2: Eq { } } +impl Vec2: ExactEq { + #[inline] + pure fn exact_eq(other: &Vec2) -> bool { + self[0] == other[0] && + self[1] == other[1] + } +} + pub impl Vec2: FuzzyEq { #[inline] pure fn fuzzy_eq(other: &Vec2) -> bool { @@ -295,13 +295,6 @@ pub impl Vec3: Vector { self[2] * other[2] } - #[inline] - pure fn exact_eq(other: &Vec3) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] - } - #[inline] pure fn magnitude2() -> float { self[0] * self[0] + @@ -381,6 +374,15 @@ pub impl Vec3: Eq { } } +impl Vec3: ExactEq { + #[inline] + pure fn exact_eq(other: &Vec3) -> bool { + self[0] == other[0] && + self[1] == other[1] && + self[2] == other[2] + } +} + pub impl Vec3: FuzzyEq { #[inline] pure fn fuzzy_eq(other: &Vec3) -> bool { @@ -483,14 +485,6 @@ pub impl Vec4: Vector { self[3] * other[3] } - #[inline] - pure fn exact_eq(other: &Vec4) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] && - self[3] == other[3] - } - #[inline] pure fn magnitude2() -> float { self[0] * self[0] + @@ -575,6 +569,16 @@ pub impl Vec4: Eq { } } +impl Vec4: ExactEq { + #[inline] + 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] pure fn fuzzy_eq(other: &Vec4) -> bool {