Implement operator traits

This commit is contained in:
Brendan Zabarauskas 2012-09-29 20:25:49 +10:00
parent bf144110b7
commit 0ba6f5a633
3 changed files with 212 additions and 143 deletions

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use cmp::Ord; use cmp::Eq;
use num::Num; use ops::Neg;
// use to_str::ToStr; // use to_str::ToStr;
use math::Sqrt; use math::Sqrt;
use quat::Quat; use quat::Quat;
@ -18,8 +18,6 @@ pub trait Matrix<T, V> {
pure fn row(&&i:uint) -> V; pure fn row(&&i:uint) -> V;
pure fn col(&&i:uint) -> V; pure fn col(&&i:uint) -> V;
pure fn neg() -> self;
pure fn mul_f(&&value:T) -> self; pure fn mul_f(&&value:T) -> self;
pure fn mul_v(&&other:V) -> V; pure fn mul_v(&&other:V) -> V;
pure fn add_m(&&other:self) -> self; pure fn add_m(&&other:self) -> self;
@ -30,8 +28,6 @@ pub trait Matrix<T, V> {
pure fn transpose() -> self; pure fn transpose() -> self;
pure fn exact_eq(&&other:self) -> bool; pure fn exact_eq(&&other:self) -> bool;
pure fn fuzzy_eq(&&other:self) -> bool;
pure fn eq(&&other:self) -> bool;
pure fn is_identity() -> bool; pure fn is_identity() -> bool;
pure fn is_symmetric() -> bool; pure fn is_symmetric() -> bool;
@ -118,11 +114,6 @@ pub impl Mat2: Matrix<float, Vec2> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Mat2 {
Mat2_v(-self[0], -self[1])
}
#[inline] #[inline]
pure fn mul_f(&&value:float) -> Mat2 { pure fn mul_f(&&value:float) -> Mat2 {
Mat2_v(self[0].mul_f(value), Mat2_v(self[0].mul_f(value),
@ -172,20 +163,9 @@ pub impl Mat2: Matrix<float, Vec2> {
self[1].exact_eq(other[1]) self[1].exact_eq(other[1])
} }
#[inline]
pure fn fuzzy_eq(&&other:Mat2) -> bool {
self[0].fuzzy_eq(other[0]) &&
self[1].fuzzy_eq(other[1])
}
#[inline]
pure fn eq(&&other:Mat2) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn is_identity() -> bool { pure fn is_identity() -> bool {
self.fuzzy_eq(mat2_identity) self.fuzzy_eq(&mat2_identity)
} }
#[inline] #[inline]
@ -202,7 +182,34 @@ pub impl Mat2: Matrix<float, Vec2> {
#[inline] #[inline]
pure fn is_rotated() -> bool { pure fn is_rotated() -> bool {
!self.fuzzy_eq(mat2_identity) !self.fuzzy_eq(&mat2_identity)
}
}
pub impl Mat2: Neg<Mat2> {
#[inline]
pure fn neg() -> Mat2 {
Mat2_v(-self[0], -self[1])
}
}
pub impl Mat2: Eq {
#[inline]
pure fn eq(other: &Mat2) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Mat2) -> bool {
!(self == *other)
}
}
pub impl Mat2: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Mat2) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1])
} }
} }
@ -273,11 +280,6 @@ pub impl Mat3: Matrix<float, Vec3> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Mat3 {
Mat3_v(-self[0], -self[1], -self[2])
}
#[inline] #[inline]
pure fn mul_f(&&value:float) -> Mat3 { pure fn mul_f(&&value:float) -> Mat3 {
Mat3_v(self[0].mul_f(value), Mat3_v(self[0].mul_f(value),
@ -339,21 +341,9 @@ pub impl Mat3: Matrix<float, Vec3> {
self[2].exact_eq(other[2]) self[2].exact_eq(other[2])
} }
#[inline]
pure fn fuzzy_eq(&&other:Mat3) -> bool {
self[0].fuzzy_eq(other[0]) &&
self[1].fuzzy_eq(other[1]) &&
self[2].fuzzy_eq(other[2])
}
#[inline]
pure fn eq(&&other:Mat3) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn is_identity() -> bool { pure fn is_identity() -> bool {
self.fuzzy_eq(mat3_identity) self.fuzzy_eq(&mat3_identity)
} }
#[inline] #[inline]
@ -382,7 +372,7 @@ pub impl Mat3: Matrix<float, Vec3> {
#[inline] #[inline]
pure fn is_rotated() -> bool { pure fn is_rotated() -> bool {
!self.fuzzy_eq(mat3_identity) !self.fuzzy_eq(&mat3_identity)
} }
} }
@ -443,6 +433,34 @@ pub impl Mat3: Matrix3<Vec3> {
} }
} }
pub impl Mat3: Neg<Mat3> {
#[inline]
pure fn neg() -> Mat3 {
Mat3_v(-self[0], -self[1], -self[2])
}
}
pub impl Mat3: Eq {
#[inline]
pure fn eq(other: &Mat3) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Mat3) -> bool {
!(self == *other)
}
}
pub impl Mat3: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Mat3) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2])
}
}
@ -515,11 +533,6 @@ pub impl Mat4: Matrix<float, Vec4> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Mat4 {
Mat4_v(-self[0], -self[1], -self[2], -self[3])
}
#[inline] #[inline]
pure fn mul_f(&&value:float) -> Mat4 { pure fn mul_f(&&value:float) -> Mat4 {
Mat4_v(self[0].mul_f(value), Mat4_v(self[0].mul_f(value),
@ -595,22 +608,9 @@ pub impl Mat4: Matrix<float, Vec4> {
self[3].exact_eq(other[3]) self[3].exact_eq(other[3])
} }
#[inline]
pure fn fuzzy_eq(&&other:Mat4) -> bool {
self[0].fuzzy_eq(other[0]) &&
self[1].fuzzy_eq(other[1]) &&
self[2].fuzzy_eq(other[2]) &&
self[3].fuzzy_eq(other[3])
}
#[inline]
pure fn eq(&&other:Mat4) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn is_identity() -> bool { pure fn is_identity() -> bool {
self.fuzzy_eq(mat4_identity) self.fuzzy_eq(&mat4_identity)
} }
#[inline] #[inline]
@ -653,7 +653,7 @@ pub impl Mat4: Matrix<float, Vec4> {
#[inline] #[inline]
pure fn is_rotated() -> bool { pure fn is_rotated() -> bool {
!self.fuzzy_eq(mat4_identity) !self.fuzzy_eq(&mat4_identity)
} }
} }
@ -676,4 +676,33 @@ pub impl Mat4: Matrix4<Vec3, Vec4> {
self[3][2] + vec.z(), self[3][2] + vec.z(),
self[3][3])) self[3][3]))
} }
}
pub impl Mat4: Neg<Mat4> {
#[inline]
pure fn neg() -> Mat4 {
Mat4_v(-self[0], -self[1], -self[2], -self[3])
}
}
pub impl Mat4: Eq {
#[inline]
pure fn eq(other: &Mat4) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Mat4) -> bool {
!(self == *other)
}
}
pub impl Mat4: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Mat4) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
} }

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use cmp::Ord; use cmp::Eq;
use num::Num; use ops::Neg;
use to_str::ToStr; use to_str::ToStr;
use math::Sqrt; use math::Sqrt;
use mat::{Mat3, Mat4}; use mat::{Mat3, Mat4};
@ -20,7 +20,7 @@ pub trait Quaternion<T> {
pure fn y() -> T; pure fn y() -> T;
pure fn z() -> T; pure fn z() -> T;
pure fn neg() -> self; // pure fn neg() -> self;
pure fn mul_f(&&value:T) -> self; pure fn mul_f(&&value:T) -> self;
pure fn div_f(&&value:T) -> self; pure fn div_f(&&value:T) -> self;
@ -32,8 +32,8 @@ pub trait Quaternion<T> {
pure fn mul_q(&&other:self) -> self; pure fn mul_q(&&other:self) -> self;
pure fn exact_eq(&&other:self) -> bool; pure fn exact_eq(&&other:self) -> bool;
pure fn fuzzy_eq(&&other:self) -> bool; // pure fn fuzzy_eq(&&other:self) -> bool;
pure fn eq(&&other:self) -> bool; // pure fn eq(&&other:self) -> bool;
pure fn conjugate() -> self; pure fn conjugate() -> self;
pure fn inverse() -> self; pure fn inverse() -> self;
@ -77,11 +77,6 @@ pub impl Quat: Quaternion<float> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Quat {
Quat(-self[0], -self[1], -self[2], -self[3])
}
#[inline] pure fn w() -> float { self.data[0] } #[inline] pure fn w() -> float { self.data[0] }
#[inline] pure fn x() -> float { self.data[1] } #[inline] pure fn x() -> float { self.data[1] }
#[inline] pure fn y() -> float { self.data[2] } #[inline] pure fn y() -> float { self.data[2] }
@ -135,19 +130,6 @@ pub impl Quat: Quaternion<float> {
self[3] == other[3] self[3] == other[3]
} }
#[inline]
pure fn fuzzy_eq(&&other: Quat) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
#[inline]
pure fn eq(&&other:Quat) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn conjugate() -> Quat { pure fn conjugate() -> Quat {
Quat(self.w(), -self.x(), -self.y(), -self.z()) Quat(self.w(), -self.x(), -self.y(), -self.z())
@ -200,6 +182,35 @@ pub impl Quat: Quaternion<float> {
} }
} }
pub impl Quat: Neg<Quat> {
#[inline]
pure fn neg() -> Quat {
Quat(-self[0], -self[1], -self[2], -self[3])
}
}
pub impl Quat: Eq {
#[inline]
pure fn eq(other: &Quat) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Quat) -> bool {
!(self == *other)
}
}
pub impl Quat: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Quat) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
}
// //
// Convert To String // Convert To String
// //

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use cmp::Ord; use cmp::Eq;
use num::Num; use ops::Neg;
use math::{Abs, min, max, Sqrt}; use math::{Abs, min, max, Sqrt};
use to_str::ToStr; use to_str::ToStr;
@ -12,8 +12,6 @@ pub trait Vector<T> {
pure fn index(&&index:uint) -> T; pure fn index(&&index:uint) -> T;
pure fn neg() -> self;
pure fn add_f(&&value:T) -> self; pure fn add_f(&&value:T) -> self;
pure fn sub_f(&&value:T) -> self; pure fn sub_f(&&value:T) -> self;
pure fn mul_f(&&value:T) -> self; pure fn mul_f(&&value:T) -> self;
@ -25,8 +23,6 @@ pub trait Vector<T> {
pure fn dot(&&other: self) -> T; pure fn dot(&&other: self) -> T;
pure fn exact_eq(&&other:self) -> bool; pure fn exact_eq(&&other:self) -> bool;
pure fn fuzzy_eq(&&other:self) -> bool;
pure fn eq(&&other:self) -> bool;
pure fn magnitude2() -> T; pure fn magnitude2() -> T;
pure fn magnitude() -> T; pure fn magnitude() -> T;
@ -134,11 +130,6 @@ pub impl Vec2: Vector<float> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Vec2 {
Vec2(-self[0], -self[1])
}
#[inline] #[inline]
pure fn add_f(&&value:float) -> Vec2 { pure fn add_f(&&value:float) -> Vec2 {
Vec2(self[0] + value, Vec2(self[0] + value,
@ -187,17 +178,6 @@ pub impl Vec2: Vector<float> {
self[1] == other[1] self[1] == other[1]
} }
#[inline]
pure fn fuzzy_eq(&&other: Vec2) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1])
}
#[inline]
pure fn eq(&&other:Vec2) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn magnitude2() -> float { pure fn magnitude2() -> float {
self[0] * self[0] + self[0] * self[0] +
@ -242,6 +222,33 @@ pub impl Vec2: Vector<float> {
#[inline] static pure fn identity() -> Vec2 { Vec2(1f, 1f) } #[inline] static pure fn identity() -> Vec2 { Vec2(1f, 1f) }
} }
pub impl Vec2: Neg<Vec2> {
#[inline]
pure fn neg() -> Vec2 {
Vec2(-self[0], -self[1])
}
}
pub impl Vec2: Eq {
#[inline]
pure fn eq(other: &Vec2) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Vec2) -> bool {
!(self == *other)
}
}
pub impl Vec2: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Vec2) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1])
}
}
pub impl Vec2: ToStr { pub impl Vec2: ToStr {
fn to_str() -> ~str { fn to_str() -> ~str {
fmt!("Vec2[ %f, %f ]", self[0], self[1]) fmt!("Vec2[ %f, %f ]", self[0], self[1])
@ -303,11 +310,6 @@ pub impl Vec3: Vector<float> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Vec3 {
Vec3(-self[0], -self[1], -self[2])
}
#[inline] #[inline]
pure fn add_f(&&value:float) -> Vec3 { pure fn add_f(&&value:float) -> Vec3 {
Vec3(self[0] + value, Vec3(self[0] + value,
@ -364,18 +366,6 @@ pub impl Vec3: Vector<float> {
self[2] == other[2] self[2] == other[2]
} }
#[inline]
pure fn fuzzy_eq(&&other: Vec3) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2])
}
#[inline]
pure fn eq(&&other:Vec3) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn magnitude2() -> float { pure fn magnitude2() -> float {
self[0] * self[0] + self[0] * self[0] +
@ -424,6 +414,34 @@ pub impl Vec3: Vector<float> {
#[inline] static pure fn identity() -> Vec3 { Vec3(1f, 1f, 1f) } #[inline] static pure fn identity() -> Vec3 { Vec3(1f, 1f, 1f) }
} }
pub impl Vec3: Neg<Vec3> {
#[inline]
pure fn neg() -> Vec3 {
Vec3(-self[0], -self[1], -self[2])
}
}
pub impl Vec3: Eq {
#[inline]
pure fn eq(other: &Vec3) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Vec3) -> bool {
!(self == *other)
}
}
pub impl Vec3: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Vec3) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2])
}
}
pub impl Vec3: ToStr { pub impl Vec3: ToStr {
fn to_str() -> ~str { fn to_str() -> ~str {
fmt!("Vec3[ %f, %f, %f ]", self[0], self[1], self[2]) fmt!("Vec3[ %f, %f, %f ]", self[0], self[1], self[2])
@ -481,11 +499,6 @@ pub impl Vec4: Vector<float> {
self.data[i] self.data[i]
} }
#[inline]
pure fn neg() -> Vec4 {
Vec4(-self[0], -self[1], -self[2], -self[3])
}
#[inline] #[inline]
pure fn add_f(&&value:float) -> Vec4 { pure fn add_f(&&value:float) -> Vec4 {
Vec4(self[0] + value, Vec4(self[0] + value,
@ -550,19 +563,6 @@ pub impl Vec4: Vector<float> {
self[3] == other[3] self[3] == other[3]
} }
#[inline]
pure fn fuzzy_eq(&&other: Vec4) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
#[inline]
pure fn eq(&&other:Vec4) -> bool {
self.fuzzy_eq(other)
}
#[inline] #[inline]
pure fn magnitude2() -> float { pure fn magnitude2() -> float {
self[0] * self[0] + self[0] * self[0] +
@ -615,6 +615,35 @@ pub impl Vec4: Vector<float> {
#[inline] static pure fn identity() -> Vec4 { Vec4(1f, 1f, 1f, 1f) } #[inline] static pure fn identity() -> Vec4 { Vec4(1f, 1f, 1f, 1f) }
} }
pub impl Vec4: Neg<Vec4> {
#[inline]
pure fn neg() -> Vec4 {
Vec4(-self[0], -self[1], -self[2], -self[3])
}
}
pub impl Vec4: Eq {
#[inline]
pure fn eq(other: &Vec4) -> bool {
self.fuzzy_eq(other)
}
#[inline]
pure fn ne(other: &Vec4) -> bool {
!(self == *other)
}
}
pub impl Vec4: FuzzyEq {
#[inline]
pure fn fuzzy_eq(other: &Vec4) -> bool {
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
}
pub impl Vec4: ToStr { pub impl Vec4: ToStr {
fn to_str() -> ~str { fn to_str() -> ~str {
fmt!("Vec4[ %f, %f, %f, %f ]", self[0], self[1], self[2], self[3]) fmt!("Vec4[ %f, %f, %f, %f ]", self[0], self[1], self[2], self[3])