From 0d3d40637ff6ce49f9c10b5a9a3ee0ee8ec623a2 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 6 Sep 2013 12:32:07 +1000 Subject: [PATCH] Add comp_{min, max} and cross_self methods for vectors --- src/cgmath/matrix.rs | 12 +++++------ src/cgmath/point.rs | 4 ++-- src/cgmath/vector.rs | 47 ++++++++++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 36d5ba3..63e719a 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -40,11 +40,11 @@ approx_eq!(impl Mat3) approx_eq!(impl Mat4) // Conversion traits -pub trait ToMat2 { fn to_mat2(&self) -> Mat2; } -pub trait ToMat3 { fn to_mat3(&self) -> Mat3; } -pub trait ToMat4 { fn to_mat4(&self) -> Mat4; } +pub trait ToMat2 { fn to_mat2(&self) -> Mat2; } +pub trait ToMat3 { fn to_mat3(&self) -> Mat3; } +pub trait ToMat4 { fn to_mat4(&self) -> Mat4; } -impl Mat2 { +impl Mat2 { #[inline] pub fn new(c0r0: S, c0r1: S, c1r0: S, c1r1: S) -> Mat2 { @@ -85,7 +85,7 @@ impl Mat2 { } } -impl Mat3 { +impl Mat3 { #[inline] pub fn new(c0r0:S, c0r1:S, c0r2:S, c1r0:S, c1r1:S, c1r2:S, @@ -128,7 +128,7 @@ impl Mat3 { } } -impl Mat4 { +impl Mat4 { #[inline] pub fn new(c0r0: S, c0r1: S, c0r2: S, c0r3: S, c1r0: S, c1r1: S, c1r2: S, c1r3: S, diff --git a/src/cgmath/point.rs b/src/cgmath/point.rs index 4cb3612..d447896 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -79,8 +79,8 @@ pub trait Point array!(impl Point2 -> [S, ..2]) array!(impl Point3 -> [S, ..3]) -impl Point, [S, ..2]> for Point2; -impl Point, [S, ..3]> for Point3; +impl Point, [S, ..2]> for Point2; +impl Point, [S, ..3]> for Point3; impl ToStr for Point2 { fn to_str(&self) -> ~str { diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index e09a235..335a216 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -31,9 +31,9 @@ pub struct Vec3 { x: S, y: S, z: S } pub struct Vec4 { x: S, y: S, z: S, w: S } // Conversion traits -pub trait ToVec2 { fn to_vec2(&self) -> Vec2; } -pub trait ToVec3 { fn to_vec3(&self) -> Vec3; } -pub trait ToVec4 { fn to_vec4(&self) -> Vec4; } +pub trait ToVec2 { fn to_vec2(&self) -> Vec2; } +pub trait ToVec3 { fn to_vec3(&self) -> Vec3; } +pub trait ToVec4 { fn to_vec4(&self) -> Vec4; } approx_eq!(impl Vec2) approx_eq!(impl Vec3) @@ -42,7 +42,7 @@ approx_eq!(impl Vec4) // Utility macro for generating associated functions for the vectors macro_rules! vec( (impl $Self:ident <$S:ident> { $($field:ident),+ }) => ( - impl<$S: Clone + Num> $Self<$S> { + impl<$S: Clone + Num + Ord> $Self<$S> { #[inline] pub fn new($($field: $S),+) -> $Self<$S> { $Self { $($field: $field),+ } @@ -73,16 +73,16 @@ array!(impl Vec2 -> [S, ..2]) array!(impl Vec3 -> [S, ..3]) array!(impl Vec4 -> [S, ..4]) -impl One for Vec2 { #[inline] fn one() -> Vec2 { Vec2::ident() } } -impl One for Vec3 { #[inline] fn one() -> Vec3 { Vec3::ident() } } -impl One for Vec4 { #[inline] fn one() -> Vec4 { Vec4::ident() } } +impl One for Vec2 { #[inline] fn one() -> Vec2 { Vec2::ident() } } +impl One for Vec3 { #[inline] fn one() -> Vec3 { Vec3::ident() } } +impl One for Vec4 { #[inline] fn one() -> Vec4 { Vec4::ident() } } /// A trait that specifies a range of numeric operations for vectors. Not all /// of these make sense from a linear algebra point of view, but are included /// for pragmatic reasons. pub trait Vector < - S: Clone + Num, + S: Clone + Num + Ord, Slice > : Array @@ -131,19 +131,25 @@ pub trait Vector #[inline] fn comp_add(&self) -> S { self.iter().fold(zero::(), |a, b| a.add(b)) } /// The product of each component of the vector. - #[inline] fn comp_mul(&self) -> S { self.iter().fold(one::(), |a, b| a.mul(b)) } + #[inline] fn comp_mul(&self) -> S { self.iter().fold(one::(), |a, b| a.mul(b)) } + + /// The minimum component of the vector. + #[inline] fn comp_min(&self) -> S { self.iter().min().unwrap().clone() } + + /// The maximum component of the vector. + #[inline] fn comp_max(&self) -> S { self.iter().max().unwrap().clone() } } -impl Neg> for Vec2 { #[inline] fn neg(&self) -> Vec2 { self.map(|x| x.neg()) } } -impl Neg> for Vec3 { #[inline] fn neg(&self) -> Vec3 { self.map(|x| x.neg()) } } -impl Neg> for Vec4 { #[inline] fn neg(&self) -> Vec4 { self.map(|x| x.neg()) } } +impl Neg> for Vec2 { #[inline] fn neg(&self) -> Vec2 { self.map(|x| x.neg()) } } +impl Neg> for Vec3 { #[inline] fn neg(&self) -> Vec3 { self.map(|x| x.neg()) } } +impl Neg> for Vec4 { #[inline] fn neg(&self) -> Vec4 { self.map(|x| x.neg()) } } -impl Vector for Vec2; -impl Vector for Vec3; -impl Vector for Vec4; +impl Vector for Vec2; +impl Vector for Vec3; +impl Vector for Vec4; /// Operations specific to numeric two-dimensional vectors. -impl Vec2 { +impl Vec2 { /// The perpendicular dot product of the vector and `other`. #[inline] pub fn perp_dot(&self, other: &Vec2) -> S { @@ -152,7 +158,7 @@ impl Vec2 { } /// Operations specific to numeric three-dimensional vectors. -impl Vec3 { +impl Vec3 { /// Returns the cross product of the vector and `other`. #[inline] pub fn cross(&self, other: &Vec3) -> Vec3 { @@ -160,6 +166,13 @@ impl Vec3 { (self.z * other.x) - (self.x * other.z), (self.x * other.y) - (self.y * other.x)) } + + /// Calculates the cross product of the vector and `other`, then stores the + /// result in `self`. + #[inline] + pub fn cross_self(&mut self, other: &Vec3) { + *self = self.cross(other) + } } /// Specifies geometric operations for vectors. This is only implemented for