From d700a7b35fdd443125532ac9480a5c2495f85634 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 14 Jul 2013 19:00:36 +1000 Subject: [PATCH] Implement min and max methods for vectors --- src/math/math.rs | 7 ---- src/math/vec.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/math/math.rs b/src/math/math.rs index 9159c72..97e8a22 100644 --- a/src/math/math.rs +++ b/src/math/math.rs @@ -50,10 +50,3 @@ pub trait Dimensioned { pub trait SwapComponents { pub fn swap(&mut self, a: uint, b: uint); } - -pub trait ComponentWise { - pub fn component_add(&self) -> T; - pub fn component_mul(&self) -> T; - pub fn component_min(&self) -> T; - pub fn component_max(&self) -> T; -} diff --git a/src/math/vec.rs b/src/math/vec.rs index dcf2126..03ebc0e 100644 --- a/src/math/vec.rs +++ b/src/math/vec.rs @@ -78,6 +78,12 @@ pub trait OrdVec: Vec { pub fn ge_v(&self, other: &Self) -> BV; pub fn gt_v(&self, other: &Self) -> BV; + pub fn min_t(&self, other: T) -> Self; + pub fn max_t(&self, other: T) -> Self; + + pub fn min_v(&self, other: &Self) -> Self; + pub fn max_v(&self, other: &Self) -> Self; + pub fn comp_min(&self) -> T; pub fn comp_max(&self) -> T; } @@ -473,6 +479,30 @@ impl OrdVec> for Vec2 { *self.index(1) > *other.index(1)) } + #[inline] + pub fn min_t(&self, value: T) -> Vec2 { + Vec2::new(self.index(0).min(&value), + self.index(1).min(&value)) + } + + #[inline] + pub fn max_t(&self, value: T) -> Vec2 { + Vec2::new(self.index(0).max(&value), + self.index(1).max(&value)) + } + + #[inline] + pub fn min_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(self.index(0).min(other.index(0)), + self.index(1).min(other.index(1))) + } + + #[inline] + pub fn max_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(self.index(0).max(other.index(0)), + self.index(1).max(other.index(1))) + } + /// Returns the smallest component of the vector. #[inline] pub fn comp_min(&self) -> T { @@ -1107,6 +1137,34 @@ impl OrdVec> for Vec3 { *self.index(2) > *other.index(2)) } + #[inline] + pub fn min_t(&self, value: T) -> Vec3 { + Vec3::new(self.index(0).min(&value), + self.index(1).min(&value), + self.index(2).min(&value)) + } + + #[inline] + pub fn max_t(&self, value: T) -> Vec3 { + Vec3::new(self.index(0).max(&value), + self.index(1).max(&value), + self.index(2).max(&value)) + } + + #[inline] + pub fn min_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(self.index(0).min(other.index(0)), + self.index(1).min(other.index(1)), + self.index(2).min(other.index(2))) + } + + #[inline] + pub fn max_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(self.index(0).max(other.index(0)), + self.index(1).max(other.index(1)), + self.index(2).max(other.index(2))) + } + /// Returns the smallest component of the vector. #[inline] pub fn comp_min(&self) -> T { @@ -1771,6 +1829,38 @@ impl OrdVec> for Vec4 { *self.index(3) > *other.index(3)) } + #[inline] + pub fn min_t(&self, value: T) -> Vec4 { + Vec4::new(self.index(0).min(&value), + self.index(1).min(&value), + self.index(2).min(&value), + self.index(3).min(&value)) + } + + #[inline] + pub fn max_t(&self, value: T) -> Vec4 { + Vec4::new(self.index(0).max(&value), + self.index(1).max(&value), + self.index(2).max(&value), + self.index(3).max(&value)) + } + + #[inline] + pub fn min_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(self.index(0).min(other.index(0)), + self.index(1).min(other.index(1)), + self.index(2).min(other.index(2)), + self.index(3).min(other.index(3))) + } + + #[inline] + pub fn max_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(self.index(0).max(other.index(0)), + self.index(1).max(other.index(1)), + self.index(2).max(other.index(2)), + self.index(3).max(other.index(3))) + } + /// Returns the smallest component of the vector. #[inline] pub fn comp_min(&self) -> T {