From 820790bfa9c889199178c42e6eaed40d5acad152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Thu, 3 Oct 2013 18:25:57 +0200 Subject: [PATCH] Add pointwise multiplication and division for vectors. --- src/vec.rs | 14 ++++++++++++++ src/vec_macros.rs | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/vec.rs b/src/vec.rs index 78fcb2ea..9723063d 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -55,6 +55,8 @@ container_impl!(Vec1) // (specialized) basis_impl!(Vec1, 1) add_impl!(Vec1, Vec1AddRhs, x) sub_impl!(Vec1, Vec1SubRhs, x) +mul_impl!(Vec1, Vec1MulRhs, x) +div_impl!(Vec1, Vec1DivRhs, x) neg_impl!(Vec1, x) dot_impl!(Vec1, x) scalar_mul_impl!(Vec1, f64, Vec1MulRhs, x) @@ -150,6 +152,8 @@ container_impl!(Vec2) // (specialized) basis_impl!(Vec2, 1) add_impl!(Vec2, Vec2AddRhs, x, y) sub_impl!(Vec2, Vec2SubRhs, x, y) +mul_impl!(Vec2, Vec2MulRhs, x, y) +div_impl!(Vec2, Vec2DivRhs, x, y) neg_impl!(Vec2, x, y) dot_impl!(Vec2, x, y) scalar_mul_impl!(Vec2, f64, Vec2MulRhs, x, y) @@ -247,6 +251,8 @@ container_impl!(Vec3) // (specialized) basis_impl!(Vec3, 1) add_impl!(Vec3, Vec3AddRhs, x, y, z) sub_impl!(Vec3, Vec3SubRhs, x, y, z) +mul_impl!(Vec3, Vec3MulRhs, x, y, z) +div_impl!(Vec3, Vec3DivRhs, x, y, z) neg_impl!(Vec3, x, y, z) dot_impl!(Vec3, x, y, z) scalar_mul_impl!(Vec3, f64, Vec3MulRhs, x, y, z) @@ -356,6 +362,8 @@ container_impl!(PVec3) // (specialized) basis_impl!(PVec3, 1) add_impl!(PVec3, PVec3AddRhs, x, y, z) sub_impl!(PVec3, PVec3SubRhs, x, y, z) +mul_impl!(PVec3, PVec3MulRhs, x, y, z) +div_impl!(PVec3, PVec3DivRhs, x, y, z) neg_impl!(PVec3, x, y, z) dot_impl!(PVec3, x, y, z) scalar_mul_impl!(PVec3, f64, PVec3MulRhs, x, y, z) @@ -457,6 +465,8 @@ container_impl!(Vec4) basis_impl!(Vec4, Vec4MulRhs, 4) add_impl!(Vec4, Vec4AddRhs, x, y, z, w) sub_impl!(Vec4, Vec4SubRhs, x, y, z, w) +mul_impl!(Vec4, Vec4MulRhs, x, y, z, w) +div_impl!(Vec4, Vec4DivRhs, x, y, z, w) neg_impl!(Vec4, x, y, z, w) dot_impl!(Vec4, x, y, z, w) scalar_mul_impl!(Vec4, f64, Vec4MulRhs, x, y, z, w) @@ -558,6 +568,8 @@ container_impl!(Vec5) basis_impl!(Vec5, Vec5MulRhs, 5) add_impl!(Vec5, Vec5AddRhs, x, y, z, w, a) sub_impl!(Vec5, Vec5SubRhs, x, y, z, w, a) +mul_impl!(Vec5, Vec5MulRhs, x, y, z, w, a) +div_impl!(Vec5, Vec5DivRhs, x, y, z, w, a) neg_impl!(Vec5, x, y, z, w, a) dot_impl!(Vec5, x, y, z, w, a) scalar_mul_impl!(Vec5, f64, Vec5MulRhs, x, y, z, w, a) @@ -661,6 +673,8 @@ container_impl!(Vec6) basis_impl!(Vec6, Vec6MulRhs, 6) add_impl!(Vec6, Vec6AddRhs, x, y, z, w, a, b) sub_impl!(Vec6, Vec6SubRhs, x, y, z, w, a, b) +mul_impl!(Vec6, Vec6MulRhs, x, y, z, w, a, b) +div_impl!(Vec6, Vec6DivRhs, x, y, z, w, a, b) neg_impl!(Vec6, x, y, z, w, a, b) dot_impl!(Vec6, x, y, z, w, a, b) scalar_mul_impl!(Vec6, f64, Vec6MulRhs, x, y, z, w, a, b) diff --git a/src/vec_macros.rs b/src/vec_macros.rs index 9caea5c7..5e3ad163 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -287,6 +287,28 @@ macro_rules! sub_impl( ) ) +macro_rules! mul_impl( + ($t: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> $trhs> for $t { + #[inline] + fn binop(left: &$t, right: &$t) -> $t { + $t::new(left.$comp0 * right.$comp0 $(, left.$compN * right.$compN)*) + } + } + ) +) + +macro_rules! div_impl( + ($t: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> $trhs> for $t { + #[inline] + fn binop(left: &$t, right: &$t) -> $t { + $t::new(left.$comp0 / right.$comp0 $(, left.$compN / right.$compN)*) + } + } + ) +) + macro_rules! neg_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> Neg<$t> for $t {