From 53131731b333cef4810df1d8b70111401b024498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Thu, 16 May 2013 22:23:25 +0000 Subject: [PATCH] Add fuzzy_eq implementation for everything. --- src/adaptors/rotmat.rs | 10 +++++++++ src/adaptors/transform.rs | 16 ++++++++++++++ src/dim1/mat1.rs | 10 +++++++++ src/dim1/vec1.rs | 10 +++++++++ src/dim2/mat2.rs | 22 +++++++++++++++++++ src/dim2/vec2.rs | 13 +++++++++++ src/dim3/mat3.rs | 45 ++++++++++++++++++++++++++++----------- src/dim3/vec3.rs | 18 ++++++++++++++++ src/ndim/nmat.rs | 12 ++++++++++- src/ndim/nvec.rs | 12 ++++++++++- 10 files changed, 154 insertions(+), 14 deletions(-) diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index bfe4a89b..a324a6e5 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -1,4 +1,5 @@ use core::num::{One, Zero}; // , Trigonometric}; +use std::cmp::FuzzyEq; use traits::workarounds::rlmul::{RMul, LMul}; use traits::workarounds::trigonometric::Trigonometric; use traits::dim::Dim; @@ -105,6 +106,15 @@ Transpose for Rotmat { self.submat.transpose() } } +impl> FuzzyEq for Rotmat +{ + fn fuzzy_eq(&self, other: &Rotmat) -> bool + { self.submat.fuzzy_eq(&other.submat) } + + fn fuzzy_eq_eps(&self, other: &Rotmat, epsilon: &T) -> bool + { self.submat.fuzzy_eq_eps(&other.submat, epsilon) } +} + impl ToStr for Rotmat { fn to_str(&self) -> ~str diff --git a/src/adaptors/transform.rs b/src/adaptors/transform.rs index bcd14256..9d1354cb 100644 --- a/src/adaptors/transform.rs +++ b/src/adaptors/transform.rs @@ -1,4 +1,5 @@ use core::num::{One, Zero}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; @@ -75,6 +76,21 @@ Inv for Transform } } +impl, V:FuzzyEq> FuzzyEq for Transform +{ + fn fuzzy_eq(&self, other: &Transform) -> bool + { + self.submat.fuzzy_eq(&other.submat) && + self.subtrans.fuzzy_eq(&other.subtrans) + } + + fn fuzzy_eq_eps(&self, other: &Transform, epsilon: &T) -> bool + { + self.submat.fuzzy_eq_eps(&other.submat, epsilon) && + self.subtrans.fuzzy_eq_eps(&other.subtrans, epsilon) + } +} + impl ToStr for Transform { fn to_str(&self) -> ~str diff --git a/src/dim1/mat1.rs b/src/dim1/mat1.rs index bf5584fc..9c4c98eb 100644 --- a/src/dim1/mat1.rs +++ b/src/dim1/mat1.rs @@ -1,4 +1,5 @@ use core::num::{One, Zero}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; @@ -83,6 +84,15 @@ impl Transpose for Mat1 { } } +impl> FuzzyEq for Mat1 +{ + fn fuzzy_eq(&self, other: &Mat1) -> bool + { self.m11.fuzzy_eq(&other.m11) } + + fn fuzzy_eq_eps(&self, other: &Mat1, epsilon: &T) -> bool + { self.m11.fuzzy_eq_eps(&other.m11, epsilon) } +} + impl ToStr for Mat1 { fn to_str(&self) -> ~str diff --git a/src/dim1/vec1.rs b/src/dim1/vec1.rs index 17f5b4e2..c8ad1be1 100644 --- a/src/dim1/vec1.rs +++ b/src/dim1/vec1.rs @@ -1,4 +1,5 @@ use core::num::{Zero, Algebraic}; +use std::cmp::FuzzyEq; use traits::dot::Dot; use traits::dim::Dim; @@ -57,6 +58,15 @@ impl Zero for Vec1 { self.x.is_zero() } } +impl> FuzzyEq for Vec1 +{ + fn fuzzy_eq(&self, other: &Vec1) -> bool + { self.x.fuzzy_eq(&other.x) } + + fn fuzzy_eq_eps(&self, other: &Vec1, epsilon: &T) -> bool + { self.x.fuzzy_eq_eps(&other.x, epsilon) } +} + impl ToStr for Vec1 { fn to_str(&self) -> ~str diff --git a/src/dim2/mat2.rs b/src/dim2/mat2.rs index ff682b5a..2f21a31d 100644 --- a/src/dim2/mat2.rs +++ b/src/dim2/mat2.rs @@ -1,4 +1,5 @@ use core::num::{One, Zero}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; @@ -122,6 +123,27 @@ impl Transpose for Mat2 { self.m21 <-> self.m12; } } +impl> FuzzyEq for Mat2 +{ + fn fuzzy_eq(&self, other: &Mat2) -> bool + { + self.m11.fuzzy_eq(&other.m11) && + self.m12.fuzzy_eq(&other.m12) && + + self.m21.fuzzy_eq(&other.m21) && + self.m22.fuzzy_eq(&other.m22) + } + + fn fuzzy_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool + { + self.m11.fuzzy_eq_eps(&other.m11, epsilon) && + self.m12.fuzzy_eq_eps(&other.m12, epsilon) && + + self.m21.fuzzy_eq_eps(&other.m21, epsilon) && + self.m22.fuzzy_eq_eps(&other.m22, epsilon) + } +} + impl ToStr for Mat2 { fn to_str(&self) -> ~str diff --git a/src/dim2/vec2.rs b/src/dim2/vec2.rs index b6a89ac1..00b2fbf6 100644 --- a/src/dim2/vec2.rs +++ b/src/dim2/vec2.rs @@ -1,4 +1,5 @@ use core::num::{Zero, Algebraic}; +use std::cmp::FuzzyEq; use traits::dot::Dot; use traits::dim::Dim; use traits::cross::Cross; @@ -68,6 +69,18 @@ impl Zero for Vec2 { self.x.is_zero() && self.y.is_zero() } } +impl> FuzzyEq for Vec2 +{ + fn fuzzy_eq(&self, other: &Vec2) -> bool + { self.x.fuzzy_eq(&other.x) && self.y.fuzzy_eq(&other.y) } + + fn fuzzy_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool + { + self.x.fuzzy_eq_eps(&other.x, epsilon) && + self.y.fuzzy_eq_eps(&other.y, epsilon) + } +} + impl ToStr for Vec2 { fn to_str(&self) -> ~str diff --git a/src/dim3/mat3.rs b/src/dim3/mat3.rs index 00fb0a80..357491f2 100644 --- a/src/dim3/mat3.rs +++ b/src/dim3/mat3.rs @@ -1,4 +1,5 @@ use core::num::{One, Zero}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; @@ -161,18 +162,38 @@ impl Transpose for Mat3 } } -// impl Rand for Mat3 -// { -// fn rand(rng: &mut R) -> Mat3 -// { -// Mat3 -// { -// m11: rng.next(), m12: rng.next(), m13: rng.next(), -// m21: rng.next(), m22: rng.next(), m23: rng.next(), -// m31: rng.next(), m32: rng.next(), m33: rng.next() -// } -// } -// } +impl> FuzzyEq for Mat3 +{ + fn fuzzy_eq(&self, other: &Mat3) -> bool + { + self.m11.fuzzy_eq(&other.m11) && + self.m12.fuzzy_eq(&other.m12) && + self.m13.fuzzy_eq(&other.m13) && + + self.m21.fuzzy_eq(&other.m21) && + self.m22.fuzzy_eq(&other.m22) && + self.m23.fuzzy_eq(&other.m23) && + + self.m31.fuzzy_eq(&other.m31) && + self.m32.fuzzy_eq(&other.m32) && + self.m33.fuzzy_eq(&other.m33) + } + + fn fuzzy_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool + { + self.m11.fuzzy_eq_eps(&other.m11, epsilon) && + self.m12.fuzzy_eq_eps(&other.m12, epsilon) && + self.m13.fuzzy_eq_eps(&other.m13, epsilon) && + + self.m21.fuzzy_eq_eps(&other.m21, epsilon) && + self.m22.fuzzy_eq_eps(&other.m22, epsilon) && + self.m23.fuzzy_eq_eps(&other.m23, epsilon) && + + self.m31.fuzzy_eq_eps(&other.m31, epsilon) && + self.m32.fuzzy_eq_eps(&other.m32, epsilon) && + self.m33.fuzzy_eq_eps(&other.m33, epsilon) + } +} impl ToStr for Mat3 { diff --git a/src/dim3/vec3.rs b/src/dim3/vec3.rs index 1797893d..edb18fdc 100644 --- a/src/dim3/vec3.rs +++ b/src/dim3/vec3.rs @@ -1,4 +1,5 @@ use core::num::{Zero, Algebraic}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::dot::Dot; use traits::cross::Cross; @@ -74,6 +75,23 @@ impl Zero for Vec3 { self.x.is_zero() && self.y.is_zero() && self.z.is_zero() } } +impl> FuzzyEq for Vec3 +{ + fn fuzzy_eq(&self, other: &Vec3) -> bool + { + self.x.fuzzy_eq(&other.x) && + self.y.fuzzy_eq(&other.y) && + self.z.fuzzy_eq(&other.z) + } + + fn fuzzy_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool + { + self.x.fuzzy_eq_eps(&other.x, epsilon) && + self.y.fuzzy_eq_eps(&other.y, epsilon) && + self.z.fuzzy_eq_eps(&other.z, epsilon) + } +} + impl ToStr for Vec3 { fn to_str(&self) -> ~str diff --git a/src/ndim/nmat.rs b/src/ndim/nmat.rs index 51b7317b..d7981c5e 100644 --- a/src/ndim/nmat.rs +++ b/src/ndim/nmat.rs @@ -1,5 +1,6 @@ use core::num::{One, Zero}; -use core::vec::{from_elem, swap, all}; +use core::vec::{from_elem, swap, all, all2}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; @@ -240,6 +241,15 @@ impl Transpose for NMat } } +impl> FuzzyEq for NMat +{ + fn fuzzy_eq(&self, other: &NMat) -> bool + { all2(self.mij, other.mij, |a, b| a.fuzzy_eq(b)) } + + fn fuzzy_eq_eps(&self, other: &NMat, epsilon: &T) -> bool + { all2(self.mij, other.mij, |a, b| a.fuzzy_eq_eps(b, epsilon)) } +} + impl ToStr for NMat { fn to_str(&self) -> ~str diff --git a/src/ndim/nvec.rs b/src/ndim/nvec.rs index fc002a13..ee9403b1 100644 --- a/src/ndim/nvec.rs +++ b/src/ndim/nvec.rs @@ -1,5 +1,6 @@ -use core::vec::{map_zip, from_elem, map, all}; +use core::vec::{map_zip, from_elem, map, all, all2}; use core::num::{Zero, Algebraic}; +use std::cmp::FuzzyEq; use traits::dim::Dim; use traits::dot::Dot; @@ -83,6 +84,15 @@ impl Zero for NVec } } +impl> FuzzyEq for NVec +{ + fn fuzzy_eq(&self, other: &NVec) -> bool + { all2(self.at, other.at, |a, b| a.fuzzy_eq(b)) } + + fn fuzzy_eq_eps(&self, other: &NVec, epsilon: &T) -> bool + { all2(self.at, other.at, |a, b| a.fuzzy_eq_eps(b, epsilon)) } +} + impl ToStr for NVec { fn to_str(&self) -> ~str