From 037cf95542dc0f79d0ac542d5393e787a243bebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Mon, 12 Aug 2013 16:50:50 +0200 Subject: [PATCH] Add (pointwise) Add and Sub impl for matrices. --- src/mat.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/mat_macros.rs | 22 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/mat.rs b/src/mat.rs index 926bb2a7..eaa56619 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -37,6 +37,8 @@ pub struct Mat1 { mat_impl!(Mat1, m11) mat_cast_impl!(Mat1, m11) +add_impl!(Mat1, m11) +sub_impl!(Mat1, m11) one_impl!(Mat1, _1) iterable_impl!(Mat1, 1) iterable_mut_impl!(Mat1, 1) @@ -65,6 +67,10 @@ mat_impl!(Mat2, m11, m12, m21, m22) mat_cast_impl!(Mat2, m11, m12, m21, m22) +add_impl!(Mat2, m11, m12, + m21, m22) +sub_impl!(Mat2, m11, m12, + m21, m22) one_impl!(Mat2, _1, _0, _0, _1) iterable_impl!(Mat2, 2) @@ -97,6 +103,12 @@ mat_impl!(Mat3, m11, m12, m13, mat_cast_impl!(Mat3, m11, m12, m13, m21, m22, m23, m31, m32, m33) +add_impl!(Mat3, m11, m12, m13, + m21, m22, m23, + m31, m32, m33) +sub_impl!(Mat3, m11, m12, m13, + m21, m22, m23, + m31, m32, m33) one_impl!(Mat3, _1, _0, _0, _0, _1, _0, _0, _0, _1) @@ -137,6 +149,18 @@ mat_cast_impl!(Mat4, m31, m32, m33, m34, m41, m42, m43, m44 ) +add_impl!(Mat4, + m11, m12, m13, m14, + m21, m22, m23, m24, + m31, m32, m33, m34, + m41, m42, m43, m44 +) +sub_impl!(Mat4, + m11, m12, m13, m14, + m21, m22, m23, m24, + m31, m32, m33, m34, + m41, m42, m43, m44 +) one_impl!(Mat4, _1, _0, _0, _0, _0, _1, _0, _0, _0, _0, _1, _0, @@ -188,6 +212,20 @@ one_impl!(Mat5, _0, _0, _0, _1, _0, _0, _0, _0, _0, _1 ) +add_impl!(Mat5, + m11, m12, m13, m14, m15, + m21, m22, m23, m24, m25, + m31, m32, m33, m34, m35, + m41, m42, m43, m44, m45, + m51, m52, m53, m54, m55 +) +sub_impl!(Mat5, + m11, m12, m13, m14, m15, + m21, m22, m23, m24, m25, + m31, m32, m33, m34, m35, + m41, m42, m43, m44, m45, + m51, m52, m53, m54, m55 +) iterable_impl!(Mat5, 5) iterable_mut_impl!(Mat5, 5) dim_impl!(Mat5, 5) @@ -231,6 +269,22 @@ mat_cast_impl!(Mat6, m51, m52, m53, m54, m55, m56, m61, m62, m63, m64, m65, m66 ) +add_impl!(Mat6, + m11, m12, m13, m14, m15, m16, + m21, m22, m23, m24, m25, m26, + m31, m32, m33, m34, m35, m36, + m41, m42, m43, m44, m45, m46, + m51, m52, m53, m54, m55, m56, + m61, m62, m63, m64, m65, m66 +) +sub_impl!(Mat6, + m11, m12, m13, m14, m15, m16, + m21, m22, m23, m24, m25, m26, + m31, m32, m33, m34, m35, m36, + m41, m42, m43, m44, m45, m46, + m51, m52, m53, m54, m55, m56, + m61, m62, m63, m64, m65, m66 +) one_impl!(Mat6, _1, _0, _0, _0, _0, _0, _0, _1, _0, _0, _0, _0, diff --git a/src/mat_macros.rs b/src/mat_macros.rs index e135eb29..64444327 100644 --- a/src/mat_macros.rs +++ b/src/mat_macros.rs @@ -25,6 +25,28 @@ macro_rules! mat_cast_impl( ) ) +macro_rules! add_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> Add<$t, $t> for $t { + #[inline] + pub fn add(&self, other: &$t) -> $t { + $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN )*) + } + } + ) +) + +macro_rules! sub_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> Sub<$t, $t> for $t { + #[inline] + pub fn sub(&self, other: &$t) -> $t { + $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN )*) + } + } + ) +) + macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( impl Iterable for $t {