From 8c173c8e514230b3597268129d466a1133a3eab8 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 3 Sep 2013 15:43:27 +1000 Subject: [PATCH] Impl Neg operator trait for vectors and matricies --- src/cgmath/matrix.rs | 10 ++++++++++ src/cgmath/vector.rs | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index efb1ee2..7dd76a5 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -135,6 +135,7 @@ pub trait Matrix V: Clone + Vector + Array, VSlice > : Array ++ Neg { #[inline] fn c<'a>(&'a self, c: uint) -> &'a V { self.i(c) } @@ -173,6 +174,11 @@ pub trait Matrix *self.mut_cr(cb, rb) = tmp; } + #[inline] + fn neg_self(&mut self) { + for c in self.mut_iter() { *c = c.neg() } + } + // fn swap_cr(&mut self, (ca, ra): (uint, uint), (cb, rb): (uint, uint)) { // let tmp = self.cr(ca, ra).clone(); // *self.mut_cr(ca, ra) = self.cr(cb, rb).clone(); @@ -201,6 +207,10 @@ 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> Matrix, ..2], Vec2, [S, ..2]> for Mat2 diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index fcf5efe..ac69fdb 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -61,8 +61,8 @@ pub trait Vector Slice > : Array ++ Neg { - #[inline] fn neg(&self) -> Self { self.map(|x| x.neg()) } #[inline] fn neg_self(&mut self) { for x in self.mut_iter() { *x = x.neg() } } #[inline] fn add_s(&self, s: S) -> Self { self.map(|x| x.add(&s)) } @@ -100,6 +100,10 @@ pub trait Vector } } +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;