From ebf6a9529dca213fcd6a125443ff057446412813 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 8 May 2013 01:00:06 +1000 Subject: [PATCH] Use the new ApproxEq trait in core --- src/mat.rs | 189 +++++++++++++++++++++++++--------------------- src/projection.rs | 8 +- src/quat.rs | 27 ++++--- src/vec.rs | 89 +++++++++++++--------- 4 files changed, 173 insertions(+), 140 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index 3afb7df..9e38a53 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -1,6 +1,6 @@ +use core::cmp::ApproxEq; use core::num::Zero::zero; use core::num::One::one; -use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use vec::*; use quat::Quat; @@ -123,7 +123,7 @@ pub trait BaseMat: Index + Eq + Neg { * The transposed matrix */ fn transpose(&self) -> Self; - + /** * # Return value * @@ -313,7 +313,7 @@ pub trait BaseMat4: BaseMat { #[deriving(Eq)] pub struct Mat2 { x: Vec2, y: Vec2 } -impl> BaseMat> for Mat2 { +impl BaseMat> for Mat2 { #[inline(always)] fn col(&self, i: uint) -> Vec2 { self[i] } @@ -424,7 +424,7 @@ impl> BaseMat> for Mat2 { #[inline(always)] fn inverse(&self) -> Option> { let d = self.determinant(); - if d.fuzzy_eq(&zero()) { + if d.approx_eq(&zero()) { None } else { Some(BaseMat2::new( self[1][1]/d, -self[0][1]/d, @@ -437,7 +437,7 @@ impl> BaseMat> for Mat2 { BaseMat2::new(self[0][0], self[1][0], self[0][1], self[1][1]) } - + #[inline(always)] fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { match i { @@ -507,29 +507,29 @@ impl> BaseMat> for Mat2 { #[inline(always)] fn is_identity(&self) -> bool { - self.fuzzy_eq(&BaseMat::identity()) + self.approx_eq(&BaseMat::identity()) } #[inline(always)] fn is_diagonal(&self) -> bool { - self[0][1].fuzzy_eq(&zero()) && - self[1][0].fuzzy_eq(&zero()) + self[0][1].approx_eq(&zero()) && + self[1][0].approx_eq(&zero()) } #[inline(always)] fn is_rotated(&self) -> bool { - !self.fuzzy_eq(&BaseMat::identity()) + !self.approx_eq(&BaseMat::identity()) } #[inline(always)] fn is_symmetric(&self) -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[1][0].fuzzy_eq(&self[0][1]) + self[0][1].approx_eq(&self[1][0]) && + self[1][0].approx_eq(&self[0][1]) } #[inline(always)] fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&zero()) + !self.determinant().approx_eq(&zero()) } #[inline(always)] @@ -538,7 +538,7 @@ impl> BaseMat> for Mat2 { } } -impl> BaseMat2> for Mat2 { +impl BaseMat2> for Mat2 { /** * Construct a 2 x 2 matrix * @@ -645,23 +645,28 @@ impl Index> for Mat2 { } } -impl> Neg> for Mat2 { +impl Neg> for Mat2 { #[inline(always)] fn neg(&self) -> Mat2 { BaseMat2::from_cols(-self[0], -self[1]) } } -impl> FuzzyEq for Mat2 { +impl> ApproxEq for Mat2 { #[inline(always)] - fn fuzzy_eq(&self, other: &Mat2) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) + fn approx_eq(&self, other: &Mat2) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) } } @@ -681,7 +686,7 @@ impl> FuzzyEq for Mat2 { #[deriving(Eq)] pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } -impl> BaseMat> for Mat3 { +impl BaseMat> for Mat3 { #[inline(always)] fn col(&self, i: uint) -> Vec3 { self[i] } @@ -815,7 +820,7 @@ impl> BaseMat> for Mat3 { // #[inline(always)] fn inverse(&self) -> Option> { let d = self.determinant(); - if d.fuzzy_eq(&zero()) { + if d.approx_eq(&zero()) { None } else { let m: Mat3 = BaseMat3::from_cols(self[1].cross(&self[2]).div_t(d), @@ -831,7 +836,7 @@ impl> BaseMat> for Mat3 { self[0][1], self[1][1], self[2][1], self[0][2], self[1][2], self[2][2]) } - + #[inline(always)] fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { match i { @@ -912,41 +917,41 @@ impl> BaseMat> for Mat3 { #[inline(always)] fn is_identity(&self) -> bool { - self.fuzzy_eq(&BaseMat::identity()) + self.approx_eq(&BaseMat::identity()) } #[inline(always)] fn is_diagonal(&self) -> bool { - self[0][1].fuzzy_eq(&zero()) && - self[0][2].fuzzy_eq(&zero()) && + self[0][1].approx_eq(&zero()) && + self[0][2].approx_eq(&zero()) && - self[1][0].fuzzy_eq(&zero()) && - self[1][2].fuzzy_eq(&zero()) && + self[1][0].approx_eq(&zero()) && + self[1][2].approx_eq(&zero()) && - self[2][0].fuzzy_eq(&zero()) && - self[2][1].fuzzy_eq(&zero()) + self[2][0].approx_eq(&zero()) && + self[2][1].approx_eq(&zero()) } #[inline(always)] fn is_rotated(&self) -> bool { - !self.fuzzy_eq(&BaseMat::identity()) + !self.approx_eq(&BaseMat::identity()) } #[inline(always)] fn is_symmetric(&self) -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[0][2].fuzzy_eq(&self[2][0]) && + self[0][1].approx_eq(&self[1][0]) && + self[0][2].approx_eq(&self[2][0]) && - self[1][0].fuzzy_eq(&self[0][1]) && - self[1][2].fuzzy_eq(&self[2][1]) && + self[1][0].approx_eq(&self[0][1]) && + self[1][2].approx_eq(&self[2][1]) && - self[2][0].fuzzy_eq(&self[0][2]) && - self[2][1].fuzzy_eq(&self[1][2]) + self[2][0].approx_eq(&self[0][2]) && + self[2][1].approx_eq(&self[1][2]) } #[inline(always)] fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&zero()) + !self.determinant().approx_eq(&zero()) } #[inline(always)] @@ -955,7 +960,7 @@ impl> BaseMat> for Mat3 { } } -impl> BaseMat3> for Mat3 { +impl BaseMat3> for Mat3 { /** * Construct a 3 x 3 matrix * @@ -1187,24 +1192,29 @@ impl Index> for Mat3 { } } -impl> Neg> for Mat3 { +impl Neg> for Mat3 { #[inline(always)] fn neg(&self) -> Mat3 { BaseMat3::from_cols(-self[0], -self[1], -self[2]) } } -impl> FuzzyEq for Mat3 { +impl> ApproxEq for Mat3 { #[inline(always)] - fn fuzzy_eq(&self, other: &Mat3) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) && - self[2].fuzzy_eq_eps(&other[2], epsilon) + fn approx_eq(&self, other: &Mat3) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) && + self[2].approx_eq_eps(&other[2], epsilon) } } @@ -1225,7 +1235,7 @@ impl> FuzzyEq for Mat3 { #[deriving(Eq)] pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } -impl> BaseMat> for Mat4 { +impl BaseMat> for Mat4 { #[inline(always)] fn col(&self, i: uint) -> Vec4 { self[i] } @@ -1397,7 +1407,7 @@ impl> BaseMat> for Mat4 { fn inverse(&self) -> Option> { let d = self.determinant(); - if d.fuzzy_eq(&zero()) { + if d.approx_eq(&zero()) { None } else { @@ -1446,7 +1456,7 @@ impl> BaseMat> for Mat4 { self[0][2], self[1][2], self[2][2], self[3][2], self[0][3], self[1][3], self[2][3], self[3][3]) } - + #[inline(always)] fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { match i { @@ -1539,55 +1549,55 @@ impl> BaseMat> for Mat4 { #[inline(always)] fn is_identity(&self) -> bool { - self.fuzzy_eq(&BaseMat::identity()) + self.approx_eq(&BaseMat::identity()) } #[inline(always)] fn is_diagonal(&self) -> bool { - self[0][1].fuzzy_eq(&zero()) && - self[0][2].fuzzy_eq(&zero()) && - self[0][3].fuzzy_eq(&zero()) && + self[0][1].approx_eq(&zero()) && + self[0][2].approx_eq(&zero()) && + self[0][3].approx_eq(&zero()) && - self[1][0].fuzzy_eq(&zero()) && - self[1][2].fuzzy_eq(&zero()) && - self[1][3].fuzzy_eq(&zero()) && + self[1][0].approx_eq(&zero()) && + self[1][2].approx_eq(&zero()) && + self[1][3].approx_eq(&zero()) && - self[2][0].fuzzy_eq(&zero()) && - self[2][1].fuzzy_eq(&zero()) && - self[2][3].fuzzy_eq(&zero()) && + self[2][0].approx_eq(&zero()) && + self[2][1].approx_eq(&zero()) && + self[2][3].approx_eq(&zero()) && - self[3][0].fuzzy_eq(&zero()) && - self[3][1].fuzzy_eq(&zero()) && - self[3][2].fuzzy_eq(&zero()) + self[3][0].approx_eq(&zero()) && + self[3][1].approx_eq(&zero()) && + self[3][2].approx_eq(&zero()) } #[inline(always)] fn is_rotated(&self) -> bool { - !self.fuzzy_eq(&BaseMat::identity()) + !self.approx_eq(&BaseMat::identity()) } #[inline(always)] fn is_symmetric(&self) -> 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].approx_eq(&self[1][0]) && + self[0][2].approx_eq(&self[2][0]) && + self[0][3].approx_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].approx_eq(&self[0][1]) && + self[1][2].approx_eq(&self[2][1]) && + self[1][3].approx_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].approx_eq(&self[0][2]) && + self[2][1].approx_eq(&self[1][2]) && + self[2][3].approx_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].approx_eq(&self[0][3]) && + self[3][1].approx_eq(&self[1][3]) && + self[3][2].approx_eq(&self[2][3]) } #[inline(always)] fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&zero()) + !self.determinant().approx_eq(&zero()) } #[inline(always)] @@ -1596,7 +1606,7 @@ impl> BaseMat> for Mat4 { } } -impl> BaseMat4> for Mat4 { +impl BaseMat4> for Mat4 { /** * Construct a 4 x 4 matrix * @@ -1660,7 +1670,7 @@ impl> BaseMat4> for Mat4 { } } -impl> Neg> for Mat4 { +impl Neg> for Mat4 { #[inline(always)] fn neg(&self) -> Mat4 { BaseMat4::from_cols(-self[0], -self[1], -self[2], -self[3]) @@ -1674,17 +1684,22 @@ impl Index> for Mat4 { } } -impl> FuzzyEq for Mat4 { +impl> ApproxEq for Mat4 { #[inline(always)] - fn fuzzy_eq(&self, other: &Mat4) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) && - self[2].fuzzy_eq_eps(&other[2], epsilon) && - self[3].fuzzy_eq_eps(&other[3], epsilon) + fn approx_eq(&self, other: &Mat4) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) && + self[2].approx_eq_eps(&other[2], epsilon) && + self[3].approx_eq_eps(&other[3], epsilon) } } diff --git a/src/projection.rs b/src/projection.rs index 3c10efa..329ba9a 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -1,5 +1,3 @@ -use std::cmp::FuzzyEq; - use mat::{Mat4, BaseMat4}; use num::NumAssign; @@ -13,7 +11,7 @@ use num::NumAssign; * can be found [here](http://www.opengl.org/wiki/GluPerspective_code). */ #[inline(always)] -pub fn perspective>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { +pub fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { let _2: T = num::cast(2); let ymax = near * (fovy / _2).to_radians().tan(); @@ -29,7 +27,7 @@ pub fn perspective>(fovy: T, aspectRatio * (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. */ #[inline(always)] -pub fn frustum>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { +pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { let _0: T = num::cast(0); let _1: T = num::cast(1); let _2: T = num::cast(2); @@ -67,7 +65,7 @@ pub fn frustum>(left: T, right: T, botto * (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function. */ #[inline(always)] -pub fn ortho>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { +pub fn ortho(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { let _0: T = num::cast(0); let _1: T = num::cast(1); let _2: T = num::cast(2); diff --git a/src/quat.rs b/src/quat.rs index 107b7b8..9500460 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -7,9 +7,9 @@ * Sir William Hamilton */ +use core::cmp::ApproxEq; use core::num::Zero::zero; use core::num::One::one; -use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use mat::{Mat3, BaseMat3}; use vec::{Vec3, BaseVec3, AffineVec, NumVec, NumVec3}; @@ -31,7 +31,7 @@ use num::NumAssign; #[deriving(Eq)] pub struct Quat { s: T, v: Vec3 } -pub impl> Quat { +pub impl Quat { /** * Construct the quaternion from one scalar component and three * imaginary components @@ -377,24 +377,29 @@ impl Index for Quat { } } -impl> Neg> for Quat { +impl Neg> for Quat { #[inline(always)] fn neg(&self) -> Quat { Quat::new(-self[0], -self[1], -self[2], -self[3]) } } -impl> FuzzyEq for Quat { +impl> ApproxEq for Quat { #[inline(always)] - fn fuzzy_eq(&self, other: &Quat) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Quat, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) && - self[2].fuzzy_eq_eps(&other[2], epsilon) && - self[3].fuzzy_eq_eps(&other[3], epsilon) + fn approx_eq(&self, other: &Quat) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Quat, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) && + self[2].approx_eq_eps(&other[2], epsilon) && + self[3].approx_eq_eps(&other[3], epsilon) } } diff --git a/src/vec.rs b/src/vec.rs index 93339c8..a17e8a1 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1,6 +1,6 @@ +use core::cmp::ApproxEq; use core::num::Zero::zero; use core::num::One::one; -use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use num::NumAssign; @@ -24,7 +24,7 @@ pub trait BaseVec: Index + Eq { * A pointer to the first component of the vector */ fn to_ptr(&self) -> *T; - + /** * Get a mutable reference to the component at `i` */ @@ -126,7 +126,7 @@ pub trait NumVec: BaseVec + Neg { * The dot product of the vector and `other` */ fn dot(&self, other: &Self) -> T; - + /** * Negate the vector */ @@ -192,7 +192,7 @@ pub trait NumVec3: NumVec { * The cross product of the vector and `other` */ fn cross(&self, other: &Self) -> Self; - + /** * Set to the cross product of the vector and `other` */ @@ -286,7 +286,7 @@ pub trait AffineVec: NumVec { * The intoperlated vector */ fn lerp(&self, other: &Self, amount: T) -> Self; - + /** * Normalize the vector */ @@ -500,7 +500,7 @@ macro_rules! zip_assign( ($a:ident[] $method:ident $b:ident[] ..2) => ({ $a.index_mut(0).$method(&$b[0]); $a.index_mut(1).$method(&$b[1]); }); ($a:ident[] $method:ident $b:ident[] ..3) => ({ zip_assign!($a[] $method $b[] ..2); $a.index_mut(2).$method(&$b[2]); }); ($a:ident[] $method:ident $b:ident[] ..4) => ({ zip_assign!($a[] $method $b[] ..3); $a.index_mut(3).$method(&$b[3]); }); - + ($a:ident[] $method:ident $b:ident ..2) => ({ $a.index_mut(0).$method(&$b); $a.index_mut(1).$method(&$b); }); ($a:ident[] $method:ident $b:ident ..3) => ({ zip_assign!($a[] $method $b ..2); $a.index_mut(2).$method(&$b); }); ($a:ident[] $method:ident $b:ident ..4) => ({ zip_assign!($a[] $method $b ..3); $a.index_mut(3).$method(&$b); }); @@ -532,7 +532,7 @@ impl BaseVec for Vec2 { fn to_ptr(&self) -> *T { unsafe { cast::transmute(self) } } - + #[inline(always)] fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { match i { @@ -614,7 +614,7 @@ impl NumVec for Vec2 { self[0] * other[0] + self[1] * other[1] } - + #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -self[0]; @@ -723,7 +723,7 @@ impl AffineVec for Vec2 { fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { self.add_v(&other.sub_v(self).mul_t(amount)) } - + #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); @@ -742,16 +742,21 @@ impl AffineVec for Vec2 { } } -impl> FuzzyEq for Vec2 { +impl> ApproxEq for Vec2 { #[inline(always)] - fn fuzzy_eq(&self, other: &Vec2) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) + fn approx_eq(&self, other: &Vec2) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) } } @@ -833,7 +838,7 @@ impl BaseVec for Vec3 { fn to_ptr(&self) -> *T { unsafe { cast::transmute(self) } } - + #[inline(always)] fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { match i { @@ -918,7 +923,7 @@ impl NumVec for Vec3 { self[1] * other[1] + self[2] * other[2] } - + #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -self[0]; @@ -986,7 +991,7 @@ impl NumVec3 for Vec3 { (self[2] * other[0]) - (self[0] * other[2]), (self[0] * other[1]) - (self[1] * other[0])) } - + #[inline(always)] fn cross_self(&mut self, other: &Vec3) { *self = self.cross(other); @@ -1040,7 +1045,7 @@ impl AffineVec for Vec3 { fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { self.add_v(&other.sub_v(self).mul_t(amount)) } - + #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); @@ -1059,17 +1064,22 @@ impl AffineVec for Vec3 { } } -impl> FuzzyEq for Vec3 { +impl> ApproxEq for Vec3 { #[inline(always)] - fn fuzzy_eq(&self, other: &Vec3) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) && - self[2].fuzzy_eq_eps(&other[2], epsilon) + fn approx_eq(&self, other: &Vec3) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) && + self[2].approx_eq_eps(&other[2], epsilon) } } @@ -1152,7 +1162,7 @@ impl BaseVec for Vec4 { fn to_ptr(&self) -> *T { unsafe { cast::transmute(self) } } - + #[inline(always)] fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { match i { @@ -1240,7 +1250,7 @@ impl NumVec for Vec4 { self[2] * other[2] + self[3] * other[3] } - + #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -self[0]; @@ -1349,7 +1359,7 @@ impl AffineVec for Vec4 { fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { self.add_v(&other.sub_v(self).mul_t(amount)) } - + #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); @@ -1368,18 +1378,23 @@ impl AffineVec for Vec4 { } } -impl> FuzzyEq for Vec4 { +impl> ApproxEq for Vec4 { #[inline(always)] - fn fuzzy_eq(&self, other: &Vec4) -> bool { - self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) + fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() } #[inline(always)] - fn fuzzy_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { - self[0].fuzzy_eq_eps(&other[0], epsilon) && - self[1].fuzzy_eq_eps(&other[1], epsilon) && - self[2].fuzzy_eq_eps(&other[2], epsilon) && - self[3].fuzzy_eq_eps(&other[3], epsilon) + fn approx_eq(&self, other: &Vec4) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline(always)] + fn approx_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { + self[0].approx_eq_eps(&other[0], epsilon) && + self[1].approx_eq_eps(&other[1], epsilon) && + self[2].approx_eq_eps(&other[2], epsilon) && + self[3].approx_eq_eps(&other[3], epsilon) } }