From ae9112452a203d28dbc4c6fa9e04195be268af97 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 5 Nov 2012 13:18:44 +1000 Subject: [PATCH] Make Vector traits more specific, add BooleanVector trait --- src/test/test_vector.rs | 48 +++++++++++++++++++++ src/vector.rs | 93 ++++++++++++++++++++++++++++++++++------- 2 files changed, 127 insertions(+), 14 deletions(-) diff --git a/src/test/test_vector.rs b/src/test/test_vector.rs index 10eaef5..2b371b5 100644 --- a/src/test/test_vector.rs +++ b/src/test/test_vector.rs @@ -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)); } \ No newline at end of file diff --git a/src/vector.rs b/src/vector.rs index f4d8bb7..0d6c3c8 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -30,12 +30,12 @@ type uvec2 = Vec2; /// a two-component unsigned integer vector type uvec3 = Vec3; /// a three-component unsigned integer vector type uvec4 = Vec4; /// a four-component unsigned integer vector -// -// N-dimensional Vector -// -pub trait Vector { + +pub trait Vector { static pure fn dim() -> uint; - +} + +pub trait NumericVector { 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 { pure fn sub_v(other: &self) -> self; pure fn dot(other: &self) -> T; - +} + +pub trait GeometricVector { 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 { + +} + pub trait Vector3 { fn cross(other: &self) -> self; } @@ -104,10 +115,12 @@ pub mod Vec2 { } } -pub impl Vec2: Vector { +pub impl Vec2: Vector { #[inline(always)] static pure fn dim() -> uint { 2 } +} +pub impl Vec2: NumericVector { #[inline(always)] pure fn add_t(value: T) -> Vec2 { Vec2::new(self[0] + value, @@ -149,7 +162,9 @@ pub impl Vec2: Vector { self[0] * other[0] + self[1] * other[1] } +} +pub impl Vec2: GeometricVector { #[inline(always)] pure fn magnitude2() -> T { self[0] * self[0] + @@ -174,6 +189,20 @@ pub impl Vec2: Vector { } } +pub impl Vec2: BooleanVector { + pub fn any() -> bool { + self[0] || self[1] + } + + pub fn all() -> bool { + self[0] && self[1] + } + + pub fn not() -> Vec2 { + Vec2::new(!self[0], !self[1]) + } +} + pub impl Vec2: Index { #[inline(always)] pure fn index(i: uint) -> T { @@ -314,10 +343,12 @@ pub impl Vec3: Vector3 { } } -pub impl Vec3: Vector { +pub impl Vec3: Vector { #[inline(always)] static pure fn dim() -> uint { 3 } - +} + +pub impl Vec3: NumericVector { #[inline(always)] pure fn add_t(value: T) -> Vec3 { Vec3::new(self[0] + value, @@ -366,7 +397,9 @@ pub impl Vec3: Vector { self[1] * other[1] + self[2] * other[2] } - +} + +pub impl Vec3: GeometricVector { #[inline(always)] pure fn magnitude2() -> T { self[0] * self[0] + @@ -392,6 +425,20 @@ pub impl Vec3: Vector { } } +pub impl Vec3: 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 { + Vec3::new(!self[0], !self[1], !self[2]) + } +} + pub impl Vec3: Index { #[inline(always)] pure fn index(i: uint) -> T { @@ -535,10 +582,12 @@ pub mod Vec4 { } } -pub impl Vec4: Vector { +pub impl Vec4: Vector { #[inline(always)] static pure fn dim() -> uint { 4 } - +} + +pub impl Vec4: NumericVector { #[inline(always)] pure fn add_t(value: T) -> Vec4 { Vec4::new(self[0] + value, @@ -594,7 +643,9 @@ pub impl Vec4: Vector { self[2] * other[2] + self[3] * other[3] } - +} + +pub impl Vec4: GeometricVector { #[inline(always)] pure fn magnitude2() -> T { self[0] * self[0] + @@ -621,6 +672,20 @@ pub impl Vec4: Vector { } } +pub impl Vec4: 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 { + Vec4::new(!self[0], !self[1], !self[2], !self[3]) + } +} + pub impl Vec4: Index { #[inline(always)] pure fn index(i: uint) -> T {