From b84e154898fd3f6b46ef1a7cbf96ce31b41d477d Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 3 Sep 2013 23:35:06 +1000 Subject: [PATCH] Impl One for vectors and matricies --- src/cgmath/matrix.rs | 27 ++++++++++++++++----------- src/cgmath/vector.rs | 7 ++++++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index c5d2cdb..2e70715 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -15,7 +15,7 @@ //! Column major, square matrix types and traits. -use std::num::{one, zero, sin, cos}; +use std::num::{Zero, zero, One, one, sin, cos}; use array::*; use quaternion::{Quat, ToQuat}; @@ -23,15 +23,15 @@ use vector::*; use util::half; /// A 2 x 2, column major matrix -#[deriving(Clone, Eq)] +#[deriving(Clone, Eq, Zero)] pub struct Mat2 { x: Vec2, y: Vec2 } /// A 3 x 3, column major matrix -#[deriving(Clone, Eq)] +#[deriving(Clone, Eq, Zero)] pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } /// A 4 x 4, column major matrix -#[deriving(Clone, Eq)] +#[deriving(Clone, Eq, Zero)] pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } // Conversion traits @@ -159,17 +159,22 @@ impl Mat4 { } } +impl One for Mat2 { #[inline] fn one() -> Mat2 { Mat2::ident() } } +impl One for Mat3 { #[inline] fn one() -> Mat3 { Mat3::ident() } } +impl One for Mat4 { #[inline] fn one() -> Mat4 { Mat4::ident() } } + array!(impl Mat2 -> [Vec2, ..2]) array!(impl Mat3 -> [Vec3, ..3]) array!(impl Mat4 -> [Vec4, ..4]) pub trait Matrix < - S: Num + Clone + ApproxEq, Slice, + S: Clone + Float, Slice, V: Clone + Vector + Array, VSlice > : Array + Neg ++ Zero + One { #[inline] fn c<'a>(&'a self, c: uint) -> &'a V { self.i(c) } @@ -241,11 +246,11 @@ pub trait Matrix // pub fn is_symmetric(&self) -> bool; } -impl Neg> for Mat2 { #[inline] fn neg(&self) -> Mat2 { self.map(|c| c.neg()) } } -impl Neg> for Mat3 { #[inline] fn neg(&self) -> Mat3 { self.map(|c| c.neg()) } } -impl Neg> for Mat4 { #[inline] fn neg(&self) -> Mat4 { self.map(|c| c.neg()) } } +impl Neg> for Mat2 { #[inline] fn neg(&self) -> Mat2 { self.map(|c| c.neg()) } } +impl Neg> for Mat3 { #[inline] fn neg(&self) -> Mat3 { self.map(|c| c.neg()) } } +impl Neg> for Mat4 { #[inline] fn neg(&self) -> Mat4 { self.map(|c| c.neg()) } } -impl> +impl Matrix, ..2], Vec2, [S, ..2]> for Mat2 { @@ -286,7 +291,7 @@ for Mat2 } } -impl> +impl Matrix, ..3], Vec3, [S, ..3]> for Mat3 { @@ -333,7 +338,7 @@ for Mat3 } } -impl> +impl Matrix, ..4], Vec4, [S, ..4]> for Mat4 { diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index 06ff90d..9fb0c24 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::num::{zero, one}; +use std::num::{Zero, zero, One, one}; use std::num::{sqrt, atan2}; use array::*; @@ -69,6 +69,10 @@ 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() } } + /// 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. @@ -79,6 +83,7 @@ pub trait Vector > : Array + Neg ++ Zero + One { // TODO: These method impls use iterators and higher order functions to // provide generic impls for vector types of different dimensions. We