From 5eff2e499251d04b5350e109aed24b2f20679735 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 16 Dec 2012 15:19:38 +1000 Subject: [PATCH] Move relational vector traits to vec modules --- src/funs/relational.rs | 218 ----------------------------- src/funs/test/test_common.rs | 231 ------------------------------- src/funs/test/test_relational.rs | 72 ---------- src/test/test_vec.rs | 57 ++++++++ src/vec.rs | 21 +++ src/vec2.rs | 57 +++++++- src/vec3.rs | 63 ++++++++- src/vec4.rs | 69 ++++++++- 8 files changed, 264 insertions(+), 524 deletions(-) delete mode 100644 src/funs/relational.rs delete mode 100644 src/funs/test/test_common.rs delete mode 100644 src/funs/test/test_relational.rs diff --git a/src/funs/relational.rs b/src/funs/relational.rs deleted file mode 100644 index 64616bc..0000000 --- a/src/funs/relational.rs +++ /dev/null @@ -1,218 +0,0 @@ -/** - * Vector Relational Functions - * - * This module corresponds to Section 8.7 of the [GLSL 4.30.6 specification] - * (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf). - */ - -use core::cmp::{Eq, Ord}; -use vec::{Vector, Vec2, Vec3, Vec4}; - -pub trait RelVector { - pure fn less_than(&self, other: &self) -> BVec; - pure fn less_than_equal(&self, other: &self) -> BVec; - pure fn greater_than(&self, other: &self) -> BVec; - pure fn greater_than_equal(&self, other: &self) -> BVec; - pure fn equal(&self, other: &self) -> BVec; - pure fn not_equal(&self, other: &self) -> BVec; -} - -#[inline(always)] pub pure fn less_than , BV>(x: &T, y: &T) -> BV { x.less_than(y) } -#[inline(always)] pub pure fn less_than_equal , BV>(x: &T, y: &T) -> BV { x.less_than_equal(y) } -#[inline(always)] pub pure fn greater_than , BV>(x: &T, y: &T) -> BV { x.greater_than(y) } -#[inline(always)] pub pure fn greater_than_equal, BV>(x: &T, y: &T) -> BV { x.greater_than_equal(y) } -#[inline(always)] pub pure fn equal , BV>(x: &T, y: &T) -> BV { x.equal(y) } -#[inline(always)] pub pure fn not_equal , BV>(x: &T, y: &T) -> BV { x.not_equal(y) } - -pub impl Vec2: RelVector> { - #[inline(always)] - pure fn less_than(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] < other[0], - self[1] < other[1]) - } - - #[inline(always)] - pure fn less_than_equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] <= other[0], - self[1] <= other[1]) - } - - #[inline(always)] - pure fn greater_than(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] > other[0], - self[1] > other[1]) - } - - #[inline(always)] - pure fn greater_than_equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] >= other[0], - self[1] >= other[1]) - } - - #[inline(always)] - pure fn equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] == other[0], - self[1] == other[1]) - } - - #[inline(always)] - pure fn not_equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] != other[0], - self[1] != other[1]) - } -} - -pub impl Vec3: RelVector> { - #[inline(always)] - pure fn less_than(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] < other[0], - self[1] < other[1], - self[2] < other[2]) - } - - #[inline(always)] - pure fn less_than_equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] <= other[0], - self[1] <= other[1], - self[2] <= other[2]) - } - - #[inline(always)] - pure fn greater_than(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] > other[0], - self[1] > other[1], - self[2] > other[2]) - } - - #[inline(always)] - pure fn greater_than_equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] >= other[0], - self[1] >= other[1], - self[2] >= other[2]) - } - - #[inline(always)] - pure fn equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] == other[0], - self[1] == other[1], - self[2] == other[2]) - } - - #[inline(always)] - pure fn not_equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] != other[0], - self[1] != other[1], - self[2] != other[2]) - } -} - -pub impl Vec4: RelVector> { - #[inline(always)] - pure fn less_than(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] < other[0], - self[1] < other[1], - self[2] < other[2], - self[3] < other[3]) - } - - #[inline(always)] - pure fn less_than_equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] <= other[0], - self[1] <= other[1], - self[2] <= other[2], - self[3] <= other[3]) - } - - #[inline(always)] - pure fn greater_than(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] > other[0], - self[1] > other[1], - self[2] > other[2], - self[3] > other[3]) - } - - #[inline(always)] - pure fn greater_than_equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] >= other[0], - self[1] >= other[1], - self[2] >= other[2], - self[3] >= other[3]) - } - - #[inline(always)] - pure fn equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] == other[0], - self[1] == other[1], - self[2] == other[2], - self[3] == other[3]) - } - - #[inline(always)] - pure fn not_equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] != other[0], - self[1] != other[1], - self[2] != other[2], - self[3] != other[3]) - } -} - -pub trait BooleanVector: Vector { - pure fn any(&self) -> bool; - pure fn all(&self) -> bool; - pure fn not(&self) -> self; -} - -#[inline(always)] pub pure fn any(x: &T) -> bool { x.any() } -#[inline(always)] pub pure fn all(x: &T) -> bool { x.all() } -#[inline(always)] pub pure fn not(x: &T) -> T { x.not() } - -pub impl Vec2: BooleanVector { - #[inline(always)] - pure fn any(&self) -> bool { - self[0] || self[1] - } - - #[inline(always)] - pure fn all(&self) -> bool { - self[0] && self[1] - } - - #[inline(always)] - pure fn not(&self) -> Vec2 { - Vec2::new(!self[0], !self[1]) - } -} - -pub impl Vec3: BooleanVector { - #[inline(always)] - pure fn any(&self) -> bool { - self[0] || self[1] || self[2] - } - - #[inline(always)] - pure fn all(&self) -> bool { - self[0] && self[1] && self[2] - } - - #[inline(always)] - pure fn not(&self) -> Vec3 { - Vec3::new(!self[0], !self[1], !self[2]) - } -} - -pub impl Vec4: BooleanVector { - #[inline(always)] - pure fn any(&self) -> bool { - self[0] || self[1] || self[2] || self[3] - } - - #[inline(always)] - pure fn all(&self) -> bool { - self[0] && self[1] && self[2] && self[3] - } - - #[inline(always)] - pure fn not(&self) -> Vec4 { - Vec4::new(!self[0], !self[1], !self[2], !self[3]) - } -} \ No newline at end of file diff --git a/src/funs/test/test_common.rs b/src/funs/test/test_common.rs deleted file mode 100644 index d3f5e2e..0000000 --- a/src/funs/test/test_common.rs +++ /dev/null @@ -1,231 +0,0 @@ -use funs::common::*; -use angle::{Radians, Degrees}; -use vec::{Vec2, Vec3, Vec4}; - -#[test] -fn test_abs() { - assert 0.abs() == 0; - assert 2.abs() == 2; - assert (-2).abs() == 2; - assert abs(&0) == 0; - assert abs(&2) == 2; - assert abs(&-2) == 2; - - assert 0.0.abs() == 0.0; - assert 2.5.abs() == 2.5; - assert (-2.5).abs() == 2.5; - assert abs(&0.0) == 0.0; - assert abs(&2.5) == 2.5; - assert abs(&-2.5) == 2.5; - - assert Radians(0.0).abs() == Radians(0.0); - assert Radians(2.5).abs() == Radians(2.5); - assert (Radians(-2.5)).abs() == Radians(2.5); - assert abs(&Radians(0.0)) == Radians(0.0); - assert abs(&Radians(2.5)) == Radians(2.5); - assert abs(&Radians(-2.5)) == Radians(2.5); - - assert Degrees(0.0).abs() == Degrees(0.0); - assert Degrees(2.5).abs() == Degrees(2.5); - assert (Degrees(-2.5)).abs() == Degrees(2.5); - assert abs(&Degrees(0.0)) == Degrees(0.0); - assert abs(&Degrees(2.5)) == Degrees(2.5); - assert abs(&Degrees(-2.5)) == Degrees(2.5); -} - -#[test] -fn test_sign() { - assert 0.sign() == 0; - assert 2.sign() == 1; - assert (-2).sign() == -1; - assert sign(&0) == 0; - assert sign(&2) == 1; - assert sign(&-2) == -1; - - assert 0.0.sign() == 0.0; - assert 2.5.sign() == 1.0; - assert (-2.5).sign()== -1.0; - assert sign(&0.0) == 0.0; - assert sign(&2.5) == 1.0; - assert sign(&-2.5) == -1.0; - - assert Radians(0.0).sign() == Radians(0.0); - assert Radians(2.5).sign() == Radians(1.0); - assert (Radians(-2.5)).sign()== Radians(-1.0); - assert sign(&Radians(0.0)) == Radians(0.0); - assert sign(&Radians(2.5)) == Radians(1.0); - assert sign(&Radians(-2.5)) == Radians(-1.0); - - assert Degrees(0.0).sign() == Degrees(0.0); - assert Degrees(2.5).sign() == Degrees(1.0); - assert (Degrees(-2.5)).sign()== Degrees(-1.0); - assert sign(&Degrees(0.0)) == Degrees(0.0); - assert sign(&Degrees(2.5)) == Degrees(1.0); - assert sign(&Degrees(-2.5)) == Degrees(-1.0); -} - -#[test] -fn test_min() { - assert 1u.min(&2u) == 1u; - assert 1u8.min(&2u8) == 1u8; - assert 1u16.min(&2u16) == 1u16; - assert 1u32.min(&2u32) == 1u32; - assert 1u64.min(&2u64) == 1u64; - assert 1.min(&2) == 1; - assert 1i8.min(&2i8) == 1i8; - assert 1i16.min(&2i16) == 1i16; - assert 1i32.min(&2i32) == 1i32; - assert 1i64.min(&2i64) == 1i64; - assert 1f.min(&2f) == 1f; - assert 1f32.min(&2f32) == 1f32; - assert 1f64.min(&2f64) == 1f64; - - assert 2u.min(&1u) == 1u; - assert 2u8.min(&1u8) == 1u8; - assert 2u16.min(&1u16) == 1u16; - assert 2u32.min(&1u32) == 1u32; - assert 2u64.min(&1u64) == 1u64; - assert 2.min(&1) == 1; - assert 2i8.min(&1i8) == 1i8; - assert 2i16.min(&1i16) == 1i16; - assert 2i32.min(&1i32) == 1i32; - assert 2i64.min(&1i64) == 1i64; - assert 2f.min(&1f) == 1f; - assert 2f32.min(&1f32) == 1f32; - assert 2f64.min(&1f64) == 1f64; - - assert min(&1u, &2u) == 1u; - assert min(&1u8, &2u8) == 1u8; - assert min(&1u16, &2u16) == 1u16; - assert min(&1u32, &2u32) == 1u32; - assert min(&1u64, &2u64) == 1u64; - assert min(&1, &2) == 1; - assert min(&1i8, &2i8) == 1i8; - assert min(&1i16, &2i16) == 1i16; - assert min(&1i32, &2i32) == 1i32; - assert min(&1i64, &2i64) == 1i64; - assert min(&1f, &2f) == 1f; - assert min(&1f32, &2f32) == 1f32; - assert min(&1f64, &2f64) == 1f64; - - assert min(&2u, &1u) == 1u; - assert min(&2u8, &1u8) == 1u8; - assert min(&2u16, &1u16) == 1u16; - assert min(&2u32, &1u32) == 1u32; - assert min(&2u64, &1u64) == 1u64; - assert min(&2, &1) == 1; - assert min(&2i8, &1i8) == 1i8; - assert min(&2i16, &1i16) == 1i16; - assert min(&2i32, &1i32) == 1i32; - assert min(&2i64, &1i64) == 1i64; - assert min(&2f, &1f) == 1f; - assert min(&2f32, &1f32) == 1f32; - assert min(&2f64, &1f64) == 1f64; - - assert Radians(1.0).min(&Radians(2.0)) == Radians(1.0); - assert Radians(2.0).min(&Radians(1.0)) == Radians(1.0); - assert min(&Radians(1.0), &Radians(2.0)) == Radians(1.0); - assert min(&Radians(2.0), &Radians(1.0)) == Radians(1.0); - - assert Degrees(1.0).min(&Degrees(2.0)) == Degrees(1.0); - assert Degrees(2.0).min(&Degrees(1.0)) == Degrees(1.0); - assert min(&Degrees(1.0), &Degrees(2.0)) == Degrees(1.0); - assert min(&Degrees(2.0), &Degrees(1.0)) == Degrees(1.0); - - assert min(&Vec2::new(1, 2), &Vec2::new(2, 1)) == Vec2::new(1, 1); - assert min(&Vec3::new(1, 2, 3), &Vec3::new(3, 2, 1)) == Vec3::new(1, 2, 1); - assert min(&Vec4::new(1, 2, 3, 4), &Vec4::new(4, 3, 2, 1)) == Vec4::new(1, 2, 2, 1); - - assert min(&Vec2::new(2, 1), &Vec2::new(1, 2)) == Vec2::new(1, 1); - assert min(&Vec3::new(3, 2, 1), &Vec3::new(1, 2, 3)) == Vec3::new(1, 2, 1); - assert min(&Vec4::new(4, 3, 2, 1), &Vec4::new(1, 2, 3, 4)) == Vec4::new(1, 2, 2, 1); -} - -#[test] -fn test_max() { - assert 1u.max(&2u) == 2u; - assert 1u8.max(&2u8) == 2u8; - assert 1u16.max(&2u16) == 2u16; - assert 1u32.max(&2u32) == 2u32; - assert 1u64.max(&2u64) == 2u64; - assert 1.max(&2) == 2; - assert 1i8.max(&2i8) == 2i8; - assert 1i16.max(&2i16) == 2i16; - assert 1i32.max(&2i32) == 2i32; - assert 1i64.max(&2i64) == 2i64; - assert 1f.max(&2f) == 2f; - assert 1f32.max(&2f32) == 2f32; - assert 1f64.max(&2f64) == 2f64; - - assert 2u.max(&1u) == 2u; - assert 2u8.max(&1u8) == 2u8; - assert 2u16.max(&1u16) == 2u16; - assert 2u32.max(&1u32) == 2u32; - assert 2u64.max(&1u64) == 2u64; - assert 2.max(&1) == 2; - assert 2i8.max(&1i8) == 2i8; - assert 2i16.max(&1i16) == 2i16; - assert 2i32.max(&1i32) == 2i32; - assert 2i64.max(&1i64) == 2i64; - assert 2f.max(&1f) == 2f; - assert 2f32.max(&1f32) == 2f32; - assert 2f64.max(&1f64) == 2f64; - - - assert max(&1u, &2u) == 2u; - assert max(&1u8, &2u8) == 2u8; - assert max(&1u16, &2u16) == 2u16; - assert max(&1u32, &2u32) == 2u32; - assert max(&1u64, &2u64) == 2u64; - assert max(&1, &2) == 2; - assert max(&1i8, &2i8) == 2i8; - assert max(&1i16, &2i16) == 2i16; - assert max(&1i32, &2i32) == 2i32; - assert max(&1i64, &2i64) == 2i64; - assert max(&1f, &2f) == 2f; - assert max(&1f32, &2f32) == 2f32; - assert max(&1f64, &2f64) == 2f64; - - - assert max(&2u, &1u) == 2u; - assert max(&2u8, &1u8) == 2u8; - assert max(&2u16, &1u16) == 2u16; - assert max(&2u32, &1u32) == 2u32; - assert max(&2u64, &1u64) == 2u64; - assert max(&2, &1) == 2; - assert max(&2i8, &1i8) == 2i8; - assert max(&2i16, &1i16) == 2i16; - assert max(&2i32, &1i32) == 2i32; - assert max(&2i64, &1i64) == 2i64; - assert max(&2f, &1f) == 2f; - assert max(&2f32, &1f32) == 2f32; - assert max(&2f64, &1f64) == 2f64; - - assert Radians(1.0).max(&Radians(2.0)) == Radians(2.0); - assert Radians(2.0).max(&Radians(1.0)) == Radians(2.0); - assert max(&Radians(1.0), &Radians(2.0)) == Radians(2.0); - assert max(&Radians(2.0), &Radians(1.0)) == Radians(2.0); - - assert Degrees(1.0).max(&Degrees(2.0)) == Degrees(2.0); - assert Degrees(2.0).max(&Degrees(1.0)) == Degrees(2.0); - assert max(&Degrees(1.0), &Degrees(2.0)) == Degrees(2.0); - assert max(&Degrees(2.0), &Degrees(1.0)) == Degrees(2.0); - - assert max(&Vec2::new(1, 2), &Vec2::new(2, 1)) == Vec2::new(2, 2); - assert max(&Vec3::new(1, 2, 3), &Vec3::new(3, 2, 1)) == Vec3::new(3, 2, 3); - assert max(&Vec4::new(1, 2, 3, 4), &Vec4::new(4, 3, 2, 1)) == Vec4::new(4, 3, 3, 4); - - assert max(&Vec2::new(2, 1), &Vec2::new(1, 2)) == Vec2::new(2, 2); - assert max(&Vec3::new(3, 2, 1), &Vec3::new(1, 2, 3)) == Vec3::new(3, 2, 3); - assert max(&Vec4::new(4, 3, 2, 1), &Vec4::new(1, 2, 3, 4)) == Vec4::new(4, 3, 3, 4); -} - -#[test] -fn test_clamp() { - -} - -#[test] -fn test_clampv() { - -} \ No newline at end of file diff --git a/src/funs/test/test_relational.rs b/src/funs/test/test_relational.rs deleted file mode 100644 index b62ad00..0000000 --- a/src/funs/test/test_relational.rs +++ /dev/null @@ -1,72 +0,0 @@ -use funs::relational::*; -use vec::*; - -#[test] -fn test_boolv2() { - 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() == Vec2::new(false, true); - - assert ff.any() == false; - assert ff.all() == false; - assert ff.not() == Vec2::new(true, true); - - assert tt.any() == true; - assert tt.all() == true; - assert tt.not() == Vec2::new(false, false); -} - -#[test] -fn test_boolv3() { - 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() == Vec3::new(false, true, false); - - assert fff.any() == false; - assert fff.all() == false; - assert fff.not() == Vec3::new(true, true, true); - - assert ttt.any() == true; - assert ttt.all() == true; - assert ttt.not() == Vec3::new(false, false, false); -} - -#[test] -fn test_boolv4() { - 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() == Vec4::new(false, true, false, true); - - assert ffff.any() == false; - assert ffff.all() == false; - assert ffff.not() == Vec4::new(true, true, true, true); - - assert tttt.any() == true; - assert tttt.all() == true; - assert tttt.not() == Vec4::new(false, false, false, false); -} - -#[test] -fn test_boolv_fns() { - // let tf = Vec2::new(true, false); - // let ftf = Vec3::new(false, true, false); - // let tftf = Vec4::new(true, false, true, false); - - // FIXME: These tests won't compile! D: - - // assert any(&tf) == true; - // assert all(&ftf) == false; - // assert not(&tftf) == Vec4::new(false, true, false, true); -} \ No newline at end of file diff --git a/src/test/test_vec.rs b/src/test/test_vec.rs index c4729b6..d66ad77 100644 --- a/src/test/test_vec.rs +++ b/src/test/test_vec.rs @@ -110,6 +110,25 @@ fn test_Vec2_euclidean() { assert mut_c == c.lerp(&d, 0.75f); } +#[test] +fn test_Vec2_boolean() { + 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() == Vec2::new(false, true); + + assert ff.any() == false; + assert ff.all() == false; + assert ff.not() == Vec2::new(true, true); + + assert tt.any() == true; + assert tt.all() == true; + assert tt.not() == Vec2::new(false, false); +} + #[test] fn test_Vec3() { // assert Vec3::dim == 3; @@ -229,6 +248,25 @@ fn test_Vec3_euclidean() { assert mut_c == c.lerp(&d, 0.75f); } +#[test] +fn test_Vec3_boolean() { + 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() == Vec3::new(false, true, false); + + assert fff.any() == false; + assert fff.all() == false; + assert fff.not() == Vec3::new(true, true, true); + + assert ttt.any() == true; + assert ttt.all() == true; + assert ttt.not() == Vec3::new(false, false, false); +} + #[test] fn test_Vec4() { // assert Vec4::dim == 4; @@ -346,4 +384,23 @@ fn test_Vec4_euclidean() { let mut mut_c = c; mut_c.lerp_self(&d, &0.75f); assert mut_c == c.lerp(&d, 0.75f); +} + +#[test] +fn test_Vec4_boolean() { + 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() == Vec4::new(false, true, false, true); + + assert ffff.any() == false; + assert ffff.all() == false; + assert ffff.not() == Vec4::new(true, true, true, true); + + assert tttt.any() == true; + assert tttt.all() == true; + assert tttt.not() == Vec4::new(false, false, false, false); } \ No newline at end of file diff --git a/src/vec.rs b/src/vec.rs index e435671..5119c6d 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -304,4 +304,25 @@ pub trait MutableEuclideanVector: MutableNumericVector<&self/T> * Linearly intoperlate the vector towards `other` */ fn lerp_self(&mut self, other: &self, amount: T); +} + +/** + * Component-wise vector comparison methods + */ +pub trait RelationalVector: Vector { + pure fn less_than(&self, other: &self) -> BoolVec; + pure fn less_than_equal(&self, other: &self) -> BoolVec; + pure fn greater_than(&self, other: &self) -> BoolVec; + pure fn greater_than_equal(&self, other: &self) -> BoolVec; + pure fn equal(&self, other: &self) -> BoolVec; + pure fn not_equal(&self, other: &self) -> BoolVec; +} + +/** + * A vector with boolean components + */ +pub trait BooleanVector: Vector { + pure fn any(&self) -> bool; + pure fn all(&self) -> bool; + pure fn not(&self) -> self; } \ No newline at end of file diff --git a/src/vec2.rs b/src/vec2.rs index f65d676..9b750a9 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -1,5 +1,5 @@ use core::cast::transmute; -use core::cmp::Eq; +use core::cmp::{Eq, Ord}; use core::ptr::to_unsafe_ptr; use core::vec::raw::buf_as_slice; @@ -244,4 +244,59 @@ pub impl Vec2: FuzzyEq { self[0].fuzzy_eq(&other[0]) && self[1].fuzzy_eq(&other[1]) } +} + +pub impl Vec2: RelationalVector> { + #[inline(always)] + pure fn less_than(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] < other[0], + self[1] < other[1]) + } + + #[inline(always)] + pure fn less_than_equal(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] <= other[0], + self[1] <= other[1]) + } + + #[inline(always)] + pure fn greater_than(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] > other[0], + self[1] > other[1]) + } + + #[inline(always)] + pure fn greater_than_equal(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] >= other[0], + self[1] >= other[1]) + } + + #[inline(always)] + pure fn equal(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] == other[0], + self[1] == other[1]) + } + + #[inline(always)] + pure fn not_equal(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] != other[0], + self[1] != other[1]) + } +} + +pub impl Vec2: BooleanVector { + #[inline(always)] + pure fn any(&self) -> bool { + self[0] || self[1] + } + + #[inline(always)] + pure fn all(&self) -> bool { + self[0] && self[1] + } + + #[inline(always)] + pure fn not(&self) -> Vec2 { + Vec2::new(!self[0], !self[1]) + } } \ No newline at end of file diff --git a/src/vec3.rs b/src/vec3.rs index 9e18f52..3af93f6 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -1,5 +1,5 @@ use core::cast::transmute; -use core::cmp::Eq; +use core::cmp::{Eq, Ord}; use core::ptr::to_unsafe_ptr; use core::vec::raw::buf_as_slice; @@ -269,4 +269,65 @@ pub impl Vec3: FuzzyEq { self[1].fuzzy_eq(&other[1]) && self[2].fuzzy_eq(&other[2]) } +} + +pub impl Vec3: RelationalVector> { + #[inline(always)] + pure fn less_than(&self, other: &Vec3) -> Vec3 { + Vec3::new(self[0] < other[0], + self[1] < other[1], + self[2] < other[2]) + } + + #[inline(always)] + pure fn less_than_equal(&self, other: &Vec3) -> Vec3 { + Vec3::new(self[0] <= other[0], + self[1] <= other[1], + self[2] <= other[2]) + } + + #[inline(always)] + pure fn greater_than(&self, other: &Vec3) -> Vec3 { + Vec3::new(self[0] > other[0], + self[1] > other[1], + self[2] > other[2]) + } + + #[inline(always)] + pure fn greater_than_equal(&self, other: &Vec3) -> Vec3 { + Vec3::new(self[0] >= other[0], + self[1] >= other[1], + self[2] >= other[2]) + } + + #[inline(always)] + pure fn equal(&self, other: &Vec3) -> Vec3 { + Vec3::new(self[0] == other[0], + self[1] == other[1], + self[2] == other[2]) + } + + #[inline(always)] + pure fn not_equal(&self, other: &Vec3) -> Vec3 { + Vec3::new(self[0] != other[0], + self[1] != other[1], + self[2] != other[2]) + } +} + +pub impl Vec3: BooleanVector { + #[inline(always)] + pure fn any(&self) -> bool { + self[0] || self[1] || self[2] + } + + #[inline(always)] + pure fn all(&self) -> bool { + self[0] && self[1] && self[2] + } + + #[inline(always)] + pure fn not(&self) -> Vec3 { + Vec3::new(!self[0], !self[1], !self[2]) + } } \ No newline at end of file diff --git a/src/vec4.rs b/src/vec4.rs index af15953..e3c8e41 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -1,5 +1,5 @@ use core::cast::transmute; -use core::cmp::Eq; +use core::cmp::{Eq, Ord}; use core::ptr::to_unsafe_ptr; use core::vec::raw::buf_as_slice; @@ -269,4 +269,71 @@ pub impl Vec4: FuzzyEq { self[2].fuzzy_eq(&other[2]) && self[3].fuzzy_eq(&other[3]) } +} + +pub impl Vec4: RelationalVector> { + #[inline(always)] + pure fn less_than(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] < other[0], + self[1] < other[1], + self[2] < other[2], + self[3] < other[3]) + } + + #[inline(always)] + pure fn less_than_equal(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] <= other[0], + self[1] <= other[1], + self[2] <= other[2], + self[3] <= other[3]) + } + + #[inline(always)] + pure fn greater_than(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] > other[0], + self[1] > other[1], + self[2] > other[2], + self[3] > other[3]) + } + + #[inline(always)] + pure fn greater_than_equal(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] >= other[0], + self[1] >= other[1], + self[2] >= other[2], + self[3] >= other[3]) + } + + #[inline(always)] + pure fn equal(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] == other[0], + self[1] == other[1], + self[2] == other[2], + self[3] == other[3]) + } + + #[inline(always)] + pure fn not_equal(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] != other[0], + self[1] != other[1], + self[2] != other[2], + self[3] != other[3]) + } +} + +pub impl Vec4: BooleanVector { + #[inline(always)] + pure fn any(&self) -> bool { + self[0] || self[1] || self[2] || self[3] + } + + #[inline(always)] + pure fn all(&self) -> bool { + self[0] && self[1] && self[2] && self[3] + } + + #[inline(always)] + pure fn not(&self) -> Vec4 { + Vec4::new(!self[0], !self[1], !self[2], !self[3]) + } } \ No newline at end of file