Update FuzzyEq impls

This commit is contained in:
Brendan Zabarauskas 2013-02-10 09:42:06 +11:00
parent 238e034e07
commit ff185b373c
9 changed files with 168 additions and 52 deletions

View file

@ -5,8 +5,9 @@ use core::sys::size_of;
use core::util::swap; use core::util::swap;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
use vec::{ use vec::{
@ -42,7 +43,7 @@ use mat::{
#[deriving_eq] #[deriving_eq]
pub struct Mat2<T> { x: Vec2<T>, y: Vec2<T> } pub struct Mat2<T> { x: Vec2<T>, y: Vec2<T> }
pub impl<T:Copy Float> Mat2<T>: Matrix<T, Vec2<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat2<T>: Matrix<T, Vec2<T>> {
#[inline(always)] #[inline(always)]
pure fn col(&self, i: uint) -> Vec2<T> { self[i] } pure fn col(&self, i: uint) -> Vec2<T> { self[i] }
@ -204,7 +205,7 @@ pub impl<T:Copy Float> Mat2<T>: Matrix<T, Vec2<T>> {
} }
} }
pub impl<T:Copy Float> Mat2<T>: MutableMatrix<T, Vec2<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat2<T>: MutableMatrix<T, Vec2<T>> {
#[inline(always)] #[inline(always)]
fn col_mut(&mut self, i: uint) -> &self/mut Vec2<T> { fn col_mut(&mut self, i: uint) -> &self/mut Vec2<T> {
match i { match i {
@ -274,7 +275,7 @@ pub impl<T:Copy Float> Mat2<T>: MutableMatrix<T, Vec2<T>> {
} }
} }
pub impl<T:Copy Float> Mat2<T>: Matrix2<T, Vec2<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat2<T>: Matrix2<T, Vec2<T>> {
/** /**
* Construct a 2 x 2 matrix * Construct a 2 x 2 matrix
* *
@ -385,18 +386,23 @@ pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
} }
} }
pub impl<T:Copy Float> Mat2<T>: Neg<Mat2<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat2<T>: Neg<Mat2<T>> {
#[inline(always)] #[inline(always)]
pure fn neg(&self) -> Mat2<T> { pure fn neg(&self) -> Mat2<T> {
Matrix2::from_cols(-self[0], -self[1]) Matrix2::from_cols(-self[0], -self[1])
} }
} }
pub impl<T:Copy Float> Mat2<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Mat2<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Mat2<T>) -> bool { pure fn fuzzy_eq(&self, other: &Mat2<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) }
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Mat2<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
self[1].fuzzy_eq_eps(&other[1], epsilon)
} }
} }

View file

@ -5,7 +5,7 @@ use core::util::swap;
use core::sys::size_of; use core::sys::size_of;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number; use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
@ -43,7 +43,7 @@ use mat::{
#[deriving_eq] #[deriving_eq]
pub struct Mat3<T> { x: Vec3<T>, y: Vec3<T>, z: Vec3<T> } pub struct Mat3<T> { x: Vec3<T>, y: Vec3<T>, z: Vec3<T> }
pub impl<T:Copy Float> Mat3<T>: Matrix<T, Vec3<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat3<T>: Matrix<T, Vec3<T>> {
#[inline(always)] #[inline(always)]
pure fn col(&self, i: uint) -> Vec3<T> { self[i] } pure fn col(&self, i: uint) -> Vec3<T> { self[i] }
@ -243,7 +243,7 @@ pub impl<T:Copy Float> Mat3<T>: Matrix<T, Vec3<T>> {
} }
} }
pub impl<T:Copy Float> Mat3<T>: Matrix3<T, Vec3<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat3<T>: Matrix3<T, Vec3<T>> {
/** /**
* Construct a 3 x 3 matrix * Construct a 3 x 3 matrix
* *
@ -470,7 +470,7 @@ pub impl<T:Copy Float> Mat3<T>: Matrix3<T, Vec3<T>> {
} }
} }
pub impl<T:Copy Float> Mat3<T>: MutableMatrix<T, Vec3<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat3<T>: MutableMatrix<T, Vec3<T>> {
#[inline(always)] #[inline(always)]
fn col_mut(&mut self, i: uint) -> &self/mut Vec3<T> { fn col_mut(&mut self, i: uint) -> &self/mut Vec3<T> {
match i { match i {
@ -561,19 +561,24 @@ pub impl<T:Copy> Mat3<T>: Index<uint, Vec3<T>> {
} }
} }
pub impl<T:Copy Float> Mat3<T>: Neg<Mat3<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat3<T>: Neg<Mat3<T>> {
#[inline(always)] #[inline(always)]
pure fn neg(&self) -> Mat3<T> { pure fn neg(&self) -> Mat3<T> {
Matrix3::from_cols(-self[0], -self[1], -self[2]) Matrix3::from_cols(-self[0], -self[1], -self[2])
} }
} }
pub impl<T:Copy Float> Mat3<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Mat3<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Mat3<T>) -> bool { pure fn fuzzy_eq(&self, other: &Mat3<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) && }
self[2].fuzzy_eq(&other[2])
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Mat3<T>, 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)
} }
} }

View file

@ -5,8 +5,9 @@ use core::util::swap;
use core::sys::size_of; use core::sys::size_of;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
use vec::{ use vec::{
@ -41,7 +42,7 @@ use mat::{
#[deriving_eq] #[deriving_eq]
pub struct Mat4<T> { x: Vec4<T>, y: Vec4<T>, z: Vec4<T>, w: Vec4<T> } pub struct Mat4<T> { x: Vec4<T>, y: Vec4<T>, z: Vec4<T>, w: Vec4<T> }
pub impl<T:Copy Float> Mat4<T>: Matrix<T, Vec4<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat4<T>: Matrix<T, Vec4<T>> {
#[inline(always)] #[inline(always)]
pure fn col(&self, i: uint) -> Vec4<T> { self[i] } pure fn col(&self, i: uint) -> Vec4<T> { self[i] }
@ -328,7 +329,7 @@ pub impl<T:Copy Float> Mat4<T>: Matrix<T, Vec4<T>> {
} }
} }
pub impl<T:Copy Float> Mat4<T>: Matrix4<T, Vec4<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat4<T>: Matrix4<T, Vec4<T>> {
/** /**
* Construct a 4 x 4 matrix * Construct a 4 x 4 matrix
* *
@ -395,7 +396,7 @@ pub impl<T:Copy Float> Mat4<T>: Matrix4<T, Vec4<T>> {
} }
} }
pub impl<T:Copy Float> Mat4<T>: MutableMatrix<T, Vec4<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat4<T>: MutableMatrix<T, Vec4<T>> {
#[inline(always)] #[inline(always)]
fn col_mut(&mut self, i: uint) -> &self/mut Vec4<T> { fn col_mut(&mut self, i: uint) -> &self/mut Vec4<T> {
match i { match i {
@ -488,7 +489,7 @@ pub impl<T:Copy Float> Mat4<T>: MutableMatrix<T, Vec4<T>> {
} }
} }
pub impl<T:Copy Float> Mat4<T>: Neg<Mat4<T>> { pub impl<T:Copy Float FuzzyEq<T>> Mat4<T>: Neg<Mat4<T>> {
#[inline(always)] #[inline(always)]
pure fn neg(&self) -> Mat4<T> { pure fn neg(&self) -> Mat4<T> {
Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3]) Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3])
@ -505,13 +506,18 @@ pub impl<T:Copy> Mat4<T>: Index<uint, Vec4<T>> {
} }
} }
pub impl<T:Copy Float> Mat4<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Mat4<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Mat4<T>) -> bool { pure fn fuzzy_eq(&self, other: &Mat4<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) && }
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3]) #[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Mat4<T>, 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)
} }
} }

View file

@ -1,6 +1,8 @@
use numeric::*; use numeric::*;
use numeric::number::Number; use numeric::number::Number;
use std::cmp::FuzzyEq;
use mat::{Mat4, Matrix4}; use mat::{Mat4, Matrix4};
/** /**
@ -10,7 +12,7 @@ use mat::{Mat4, Matrix4};
* can be found [here](http://www.opengl.org/wiki/GluPerspective_code). * can be found [here](http://www.opengl.org/wiki/GluPerspective_code).
*/ */
#[inline(always)] #[inline(always)]
pub pure fn perspective<T:Copy Float>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> { pub pure fn perspective<T:Copy Float FuzzyEq<T>>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> {
let ymax = near * tan(radians(fovy)); let ymax = near * tan(radians(fovy));
let xmax = ymax * aspectRatio; let xmax = ymax * aspectRatio;
@ -24,7 +26,7 @@ pub pure fn perspective<T:Copy Float>(fovy: T, aspectRatio: T, near: T, far: T)
* (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. * (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
*/ */
#[inline(always)] #[inline(always)]
pub pure fn frustum<T:Copy Float>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> { pub pure fn frustum<T:Copy Float FuzzyEq<T>>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
let _0: T = Number::from(0); let _0: T = Number::from(0);
let _1: T = Number::from(1); let _1: T = Number::from(1);
let _2: T = Number::from(2); let _2: T = Number::from(2);

View file

@ -13,7 +13,7 @@ use core::ptr::to_unsafe_ptr;
use core::sys::size_of; use core::sys::size_of;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number; use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
@ -45,7 +45,7 @@ use vec::{
#[deriving_eq] #[deriving_eq]
pub struct Quat<T> { s: T, v: Vec3<T> } pub struct Quat<T> { s: T, v: Vec3<T> }
pub impl<T:Copy Float> Quat<T> { pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
/** /**
* Construct the quaternion from one scalar component and three * Construct the quaternion from one scalar component and three
* imaginary components * imaginary components
@ -397,20 +397,25 @@ pub impl<T:Copy> Quat<T>: Index<uint, T> {
} }
} }
pub impl<T:Copy Float> Quat<T>: Neg<Quat<T>> { pub impl<T:Copy Float FuzzyEq<T>> Quat<T>: Neg<Quat<T>> {
#[inline(always)] #[inline(always)]
pure fn neg(&self) -> Quat<T> { pure fn neg(&self) -> Quat<T> {
Quat::new(-self[0], -self[1], -self[2], -self[3]) Quat::new(-self[0], -self[1], -self[2], -self[3])
} }
} }
pub impl<T:Copy FuzzyEq> Quat<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Quat<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Quat<T>) -> bool { pure fn fuzzy_eq(&self, other: &Quat<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) && }
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3]) #[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Quat<T>, 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)
} }
} }

View file

@ -407,4 +407,78 @@ pub trait BooleanVector: Vector<bool> {
* the component-wise logical complement * the component-wise logical complement
*/ */
pure fn not(&self) -> Self; pure fn not(&self) -> Self;
}
pub trait TrigVec<T>: Vector<T> {
pure fn radians(&self) -> Self;
pure fn degrees(&self) -> Self;
// Triganometric functions
pure fn sin(&self) -> Self;
pure fn cos(&self) -> Self;
pure fn tan(&self) -> Self;
// Inverse triganometric functions
pure fn asin(&self) -> Self;
pure fn acos(&self) -> Self;
pure fn atan(&self) -> Self;
pure fn atan2(&self, other: Self) -> Self;
// Hyperbolic triganometric functions
pure fn sinh(&self) -> Self;
pure fn cosh(&self) -> Self;
pure fn tanh(&self) -> Self;
// pure fn asinh() -> Self;
// pure fn acosh() -> Self;
// pure fn atanh() -> Self;
}
pub trait ExpVec<T>: Vector<T> {
// Exponential functions
pure fn pow_t(&self, n: Self) -> Self;
pure fn pow_v(&self, n: T) -> Self;
pure fn exp(&self) -> Self;
pure fn exp2(&self) -> Self;
pure fn ln(&self) -> Self;
pure fn ln2(&self) -> Self;
pure fn sqrt(&self) -> Self;
pure fn inv_sqrt(&self) -> Self;
}
pub trait ApproxVec<T>: Vector<T> {
// Whole-number approximation functions
pure fn floor(&self) -> Self;
pure fn trunc(&self) -> Self;
pure fn round(&self) -> Self;
// pure fn round_even(&self) -> Self;
pure fn ceil(&self) -> Self;
pure fn fract(&self) -> Self;
}
pub trait SignedVec<T,BV>: Vector<T> {
pure fn is_positive(&self) -> BV;
pure fn is_negative(&self) -> BV;
pure fn is_nonpositive(&self) -> BV;
pure fn is_nonnegative(&self) -> BV;
pure fn abs(&self) -> Self;
pure fn sign(&self) -> Self;
pure fn copysign(&self, other: Self) -> Self;
}
pub trait ExtentVec<T>: Vector<T> {
pure fn min_v(&self, other: &Self) -> Self;
pure fn max_v(&self, other: &Self) -> Self;
pure fn clamp_v(&self, mn: &Self, mx: &Self) -> Self;
pure fn min_t(&self, other: T) -> Self;
pure fn max_t(&self, other: T) -> Self;
pure fn clamp_t(&self, mn: T, mx: T) -> Self;
}
pub trait MixVec<T>: Vector<T> {
// Functions for blending numbers together
pure fn mix(&self, other: Self, value: Self) -> Self;
pure fn smooth_step(&self, edge0: Self, edge1: Self) -> Self;
pure fn step(&self, edge: Self) -> Self;
} }

View file

@ -5,8 +5,9 @@ use core::sys::size_of;
use core::util::swap; use core::util::swap;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
use vec::{ use vec::{
@ -284,11 +285,16 @@ pub impl<T:Copy Float> Vec2<T>: MutableEuclideanVector<&self/T> {
} }
} }
pub impl<T:Copy FuzzyEq Eq> Vec2<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Vec2<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Vec2<T>) -> bool { pure fn fuzzy_eq(&self, other: &Vec2<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) }
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
self[1].fuzzy_eq_eps(&other[1], epsilon)
} }
} }

View file

@ -5,8 +5,9 @@ use core::sys::size_of;
use core::util::swap; use core::util::swap;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
use vec::{ use vec::{
@ -316,12 +317,17 @@ pub impl<T:Copy Float> Vec3<T>: MutableEuclideanVector<&self/T> {
} }
} }
pub impl<T:Copy FuzzyEq Eq> Vec3<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Vec3<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Vec3<T>) -> bool { pure fn fuzzy_eq(&self, other: &Vec3<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) && }
self[2].fuzzy_eq(&other[2])
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Vec3<T>, 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)
} }
} }

View file

@ -5,8 +5,9 @@ use core::sys::size_of;
use core::util::swap; use core::util::swap;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq; use std::cmp::{FuzzyEq, FUZZY_EPSILON};
use numeric::*; use numeric::*;
use numeric::number::Number;
use numeric::number::Number::{zero,one}; use numeric::number::Number::{zero,one};
use vec::{ use vec::{
@ -314,13 +315,18 @@ pub impl<T:Copy Float> Vec4<T>: MutableEuclideanVector<&self/T> {
} }
} }
pub impl<T:Copy FuzzyEq Eq> Vec4<T>: FuzzyEq { pub impl<T:Copy Float FuzzyEq<T>> Vec4<T>: FuzzyEq<T> {
#[inline(always)] #[inline(always)]
pure fn fuzzy_eq(&self, other: &Vec4<T>) -> bool { pure fn fuzzy_eq(&self, other: &Vec4<T>) -> bool {
self[0].fuzzy_eq(&other[0]) && self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
self[1].fuzzy_eq(&other[1]) && }
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3]) #[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Vec4<T>, 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)
} }
} }