Make Vector traits more specific, add BooleanVector trait

This commit is contained in:
Brendan Zabarauskas 2012-11-05 13:18:44 +10:00
parent 9491c4d93f
commit ae9112452a
2 changed files with 127 additions and 14 deletions

View file

@ -51,6 +51,22 @@ fn test_Vec2() {
assert c.abs() == Vec2::new( 2.0f, 1.0f);
assert c.min(&d) == Vec2::new(-2.0f, -1.0f);
assert c.max(&d) == Vec2::new( 1.0f, 0.0f);
let tf = Vec2::new(true, false);
let ff = Vec2::new(false, false);
let tt = Vec2::new(true, true);
assert tf.any() == true;
assert tf.all() == false;
assert tf.not().exact_eq(&Vec2::new(false, true));
assert ff.any() == false;
assert ff.all() == false;
assert ff.not().exact_eq(&Vec2::new(true, true));
assert tt.any() == true;
assert tt.all() == true;
assert tt.not().exact_eq(&Vec2::new(false, false));
}
#[test]
@ -106,6 +122,22 @@ fn test_Vec3() {
assert c.abs() == Vec3::new( 2.0f, 1.0f, 1.0f);
assert c.min(&d) == Vec3::new(-2.0f, -1.0f, 0.5f);
assert c.max(&d) == Vec3::new( 1.0f, 0.0f, 1.0f);
let tft = Vec3::new(true, false, true);
let fff = Vec3::new(false, false, false);
let ttt = Vec3::new(true, true, true);
assert tft.any() == true;
assert tft.all() == false;
assert tft.not().exact_eq(&Vec3::new(false, true, false));
assert fff.any() == false;
assert fff.all() == false;
assert fff.not().exact_eq(&Vec3::new(true, true, true));
assert ttt.any() == true;
assert ttt.all() == true;
assert ttt.not().exact_eq(&Vec3::new(false, false, false));
}
#[test]
@ -164,4 +196,20 @@ fn test_Vec4() {
assert c.abs() == Vec4::new( 2.0f, 1.0f, 1.0f, 2.0f);
assert c.min(&d) == Vec4::new(-2.0f, -1.0f, 0.5f, 1.0f);
assert c.max(&d) == Vec4::new( 1.0f, 0.0f, 1.0f, 2.0f);
let tftf = Vec4::new(true, false, true, false);
let ffff = Vec4::new(false, false, false, false);
let tttt = Vec4::new(true, true, true, true);
assert tftf.any() == true;
assert tftf.all() == false;
assert tftf.not().exact_eq(&Vec4::new(false, true, false, true));
assert ffff.any() == false;
assert ffff.all() == false;
assert ffff.not().exact_eq(&Vec4::new(true, true, true, true));
assert tttt.any() == true;
assert tttt.all() == true;
assert tttt.not().exact_eq(&Vec4::new(false, false, false, false));
}

View file

@ -30,12 +30,12 @@ type uvec2 = Vec2<u32>; /// a two-component unsigned integer vector
type uvec3 = Vec3<u32>; /// a three-component unsigned integer vector
type uvec4 = Vec4<u32>; /// a four-component unsigned integer vector
//
// N-dimensional Vector
//
pub trait Vector<T> {
pub trait Vector {
static pure fn dim() -> uint;
}
pub trait NumericVector<T> {
pure fn add_t(value: T) -> self;
pure fn sub_t(value: T) -> self;
pure fn mul_t(value: T) -> self;
@ -45,14 +45,25 @@ pub trait Vector<T> {
pure fn sub_v(other: &self) -> self;
pure fn dot(other: &self) -> T;
}
pub trait GeometricVector<T> {
pure fn magnitude2() -> T;
pure fn magnitude() -> T;
pure fn normalize() -> self;
pure fn lerp(other: &self, value: T) -> self;
}
pub trait BooleanVector {
pub fn any() -> bool;
pub fn all() -> bool;
pub fn not() -> self;
}
pub trait Vector2<T> {
}
pub trait Vector3<T> {
fn cross(other: &self) -> self;
}
@ -104,10 +115,12 @@ pub mod Vec2 {
}
}
pub impl<T:Copy Num Sqrt> Vec2<T>: Vector<T> {
pub impl<T> Vec2<T>: Vector {
#[inline(always)]
static pure fn dim() -> uint { 2 }
}
pub impl<T:Copy Num> Vec2<T>: NumericVector<T> {
#[inline(always)]
pure fn add_t(value: T) -> Vec2<T> {
Vec2::new(self[0] + value,
@ -149,7 +162,9 @@ pub impl<T:Copy Num Sqrt> Vec2<T>: Vector<T> {
self[0] * other[0] +
self[1] * other[1]
}
}
pub impl<T:Copy Num Sqrt> Vec2<T>: GeometricVector<T> {
#[inline(always)]
pure fn magnitude2() -> T {
self[0] * self[0] +
@ -174,6 +189,20 @@ pub impl<T:Copy Num Sqrt> Vec2<T>: Vector<T> {
}
}
pub impl Vec2<bool>: BooleanVector {
pub fn any() -> bool {
self[0] || self[1]
}
pub fn all() -> bool {
self[0] && self[1]
}
pub fn not() -> Vec2<bool> {
Vec2::new(!self[0], !self[1])
}
}
pub impl<T:Copy> Vec2<T>: Index<uint, T> {
#[inline(always)]
pure fn index(i: uint) -> T {
@ -314,10 +343,12 @@ pub impl<T:Copy Num> Vec3<T>: Vector3<T> {
}
}
pub impl<T:Copy Num Sqrt> Vec3<T>: Vector<T> {
pub impl<T> Vec3<T>: Vector {
#[inline(always)]
static pure fn dim() -> uint { 3 }
}
pub impl<T:Copy Num> Vec3<T>: NumericVector<T> {
#[inline(always)]
pure fn add_t(value: T) -> Vec3<T> {
Vec3::new(self[0] + value,
@ -366,7 +397,9 @@ pub impl<T:Copy Num Sqrt> Vec3<T>: Vector<T> {
self[1] * other[1] +
self[2] * other[2]
}
}
pub impl<T:Copy Num Sqrt> Vec3<T>: GeometricVector<T> {
#[inline(always)]
pure fn magnitude2() -> T {
self[0] * self[0] +
@ -392,6 +425,20 @@ pub impl<T:Copy Num Sqrt> Vec3<T>: Vector<T> {
}
}
pub impl Vec3<bool>: BooleanVector {
pub fn any() -> bool {
self[0] || self[1] || self[2]
}
pub fn all() -> bool {
self[0] && self[1] && self[2]
}
pub fn not() -> Vec3<bool> {
Vec3::new(!self[0], !self[1], !self[2])
}
}
pub impl<T:Copy> Vec3<T>: Index<uint, T> {
#[inline(always)]
pure fn index(i: uint) -> T {
@ -535,10 +582,12 @@ pub mod Vec4 {
}
}
pub impl<T:Copy Num Sqrt> Vec4<T>: Vector<T> {
pub impl<T> Vec4<T>: Vector {
#[inline(always)]
static pure fn dim() -> uint { 4 }
}
pub impl<T:Copy Num> Vec4<T>: NumericVector<T> {
#[inline(always)]
pure fn add_t(value: T) -> Vec4<T> {
Vec4::new(self[0] + value,
@ -594,7 +643,9 @@ pub impl<T:Copy Num Sqrt> Vec4<T>: Vector<T> {
self[2] * other[2] +
self[3] * other[3]
}
}
pub impl<T:Copy Num Sqrt> Vec4<T>: GeometricVector<T> {
#[inline(always)]
pure fn magnitude2() -> T {
self[0] * self[0] +
@ -621,6 +672,20 @@ pub impl<T:Copy Num Sqrt> Vec4<T>: Vector<T> {
}
}
pub impl Vec4<bool>: BooleanVector {
pub fn any() -> bool {
self[0] || self[1] || self[2] || self[3]
}
pub fn all() -> bool {
self[0] && self[1] && self[2] && self[3]
}
pub fn not() -> Vec4<bool> {
Vec4::new(!self[0], !self[1], !self[2], !self[3])
}
}
pub impl<T:Copy> Vec4<T>: Index<uint, T> {
#[inline(always)]
pure fn index(i: uint) -> T {