From 74fd3e1a041bdf6dfc255fcb6940e0d9377eb431 Mon Sep 17 00:00:00 2001 From: Jason Orendorff Date: Sun, 17 Apr 2016 02:25:08 -0500 Subject: [PATCH] Support `f64 * Vec3` and so on. To be specific, support is added for `N op T` where `N` is `f32` or `f64`, `op` is one of `+` `-` `*`, and `T` is one of the `Vec`, `DVec`, `Mat`, or `DMat` generic types. These are all cases where `T op N` is already supported. Rust does not support generic impls in this case, but `f32` and `f64` cover many common cases. Fixes #182. --- src/structs/dmat_macros.rs | 90 ++++++++++++++++++++++++++++++++++++++ src/structs/mat_macros.rs | 56 +++++++++++++++++++++++- src/structs/vec_macros.rs | 54 +++++++++++++++++++++++ src/structs/vecn_macros.rs | 90 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 289 insertions(+), 1 deletion(-) diff --git a/src/structs/dmat_macros.rs b/src/structs/dmat_macros.rs index 4891fb3a..8cd70f34 100644 --- a/src/structs/dmat_macros.rs +++ b/src/structs/dmat_macros.rs @@ -634,6 +634,36 @@ macro_rules! dmat_impl( } } + impl Mul<$dmat> for f32 { + type Output = $dmat; + + #[inline] + fn mul(self, right: $dmat) -> $dmat { + let mut res = right; + + for mij in res.mij.iter_mut() { + *mij = self * *mij; + } + + res + } + } + + impl Mul<$dmat> for f64 { + type Output = $dmat; + + #[inline] + fn mul(self, right: $dmat) -> $dmat { + let mut res = right; + + for mij in res.mij.iter_mut() { + *mij = self * *mij; + } + + res + } + } + impl> Div for $dmat { type Output = $dmat; @@ -664,6 +694,36 @@ macro_rules! dmat_impl( } } + impl Add<$dmat> for f32 { + type Output = $dmat; + + #[inline] + fn add(self, right: $dmat) -> $dmat { + let mut res = right; + + for mij in res.mij.iter_mut() { + *mij = self + *mij; + } + + res + } + } + + impl Add<$dmat> for f64 { + type Output = $dmat; + + #[inline] + fn add(self, right: $dmat) -> $dmat { + let mut res = right; + + for mij in res.mij.iter_mut() { + *mij = self + *mij; + } + + res + } + } + impl> Add<$dmat> for $dmat { type Output = $dmat; @@ -715,6 +775,36 @@ macro_rules! dmat_impl( } } + impl Sub<$dmat> for f32 { + type Output = $dmat; + + #[inline] + fn sub(self, right: $dmat) -> $dmat { + let mut res = right; + + for mij in res.mij.iter_mut() { + *mij = self - *mij; + } + + res + } + } + + impl Sub<$dmat> for f64 { + type Output = $dmat; + + #[inline] + fn sub(self, right: $dmat) -> $dmat { + let mut res = right; + + for mij in res.mij.iter_mut() { + *mij = self - *mij; + } + + res + } + } + impl> Sub<$dmat> for $dmat { type Output = $dmat; diff --git a/src/structs/mat_macros.rs b/src/structs/mat_macros.rs index 8f028d0d..16a7d44b 100644 --- a/src/structs/mat_macros.rs +++ b/src/structs/mat_macros.rs @@ -118,6 +118,24 @@ macro_rules! mat_mul_scalar_impl( $t::new($(self.$compN * *right),+) } } + + impl Mul<$t> for f32 { + type Output = $t; + + #[inline] + fn mul(self, right: $t) -> $t { + $t::new($(self * right.$compN),+) + } + } + + impl Mul<$t> for f64 { + type Output = $t; + + #[inline] + fn mul(self, right: $t) -> $t { + $t::new($(self * right.$compN),+) + } + } ) ); @@ -144,6 +162,24 @@ macro_rules! mat_add_scalar_impl( $t::new($(self.$compN + *right),+) } } + + impl Add<$t> for f32 { + type Output = $t; + + #[inline] + fn add(self, right: $t) -> $t { + $t::new($(self + right.$compN),+) + } + } + + impl Add<$t> for f64 { + type Output = $t; + + #[inline] + fn add(self, right: $t) -> $t { + $t::new($(self + right.$compN),+) + } + } ) ); @@ -175,7 +211,7 @@ macro_rules! repeat_impl( macro_rules! mat_sub_scalar_impl( ($t: ident, $($compN: ident),+) => ( - impl Sub for $t { + impl> Sub for $t { type Output = $t; #[inline] @@ -183,6 +219,24 @@ macro_rules! mat_sub_scalar_impl( $t::new($(self.$compN - *right),+) } } + + impl Sub for $t { + type Output = $t; + + #[inline] + fn sub(self, right: $t) -> $t { + $t::new($(self - right.$compN),+) + } + } + + impl Sub for $t { + type Output = $t; + + #[inline] + fn sub(self, right: $t) -> $t { + $t::new($(self - right.$compN),+) + } + } ) ); diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index f07e27ba..0aa3cb46 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -386,6 +386,24 @@ macro_rules! scalar_add_impl( $t::new($(self.$compN + right),+) } } + + impl Add<$t> for f32 { + type Output = $t; + + #[inline] + fn add(self, right: $t) -> $t { + $t::new($(self + right.$compN),+) + } + } + + impl Add<$t> for f64 { + type Output = $t; + + #[inline] + fn add(self, right: $t) -> $t { + $t::new($(self + right.$compN),+) + } + } ) ); @@ -412,6 +430,24 @@ macro_rules! scalar_sub_impl( $t::new($(self.$compN - right),+) } } + + impl Sub<$t> for f32 { + type Output = $t; + + #[inline] + fn sub(self, right: $t) -> $t { + $t::new($(self - right.$compN),+) + } + } + + impl Sub<$t> for f64 { + type Output = $t; + + #[inline] + fn sub(self, right: $t) -> $t { + $t::new($(self - right.$compN),+) + } + } ) ); @@ -437,6 +473,24 @@ macro_rules! scalar_mul_impl( $t::new($(self.$compN * right),+) } } + + impl Mul<$t> for f32 { + type Output = $t; + + #[inline] + fn mul(self, right: $t) -> $t { + $t::new($(self * right.$compN),+) + } + } + + impl Mul<$t> for f64 { + type Output = $t; + + #[inline] + fn mul(self, right: $t) -> $t { + $t::new($(self * right.$compN),+) + } + } ) ); diff --git a/src/structs/vecn_macros.rs b/src/structs/vecn_macros.rs index c4bd8945..dcdf7a7b 100644 --- a/src/structs/vecn_macros.rs +++ b/src/structs/vecn_macros.rs @@ -264,6 +264,36 @@ macro_rules! vecn_dvec_common_impl( } } + impl<$($param : ArrayLength),*> Mul<$vecn> for f32 { + type Output = $vecn; + + #[inline] + fn mul(self, right: $vecn) -> $vecn { + let mut res = right; + + for e in res.as_mut().iter_mut() { + *e = self * *e; + } + + res + } + } + + impl<$($param : ArrayLength),*> Mul<$vecn> for f64 { + type Output = $vecn; + + #[inline] + fn mul(self, right: $vecn) -> $vecn { + let mut res = right; + + for e in res.as_mut().iter_mut() { + *e = self * *e; + } + + res + } + } + impl + Zero $(, $param : ArrayLength)*> Div for $vecn { type Output = $vecn; @@ -294,6 +324,36 @@ macro_rules! vecn_dvec_common_impl( } } + impl<$($param : ArrayLength),*> Add<$vecn> for f32 { + type Output = $vecn; + + #[inline] + fn add(self, right: $vecn) -> $vecn { + let mut res = right; + + for e in res.as_mut().iter_mut() { + *e = self + *e; + } + + res + } + } + + impl<$($param : ArrayLength),*> Add<$vecn> for f64 { + type Output = $vecn; + + #[inline] + fn add(self, right: $vecn) -> $vecn { + let mut res = right; + + for e in res.as_mut().iter_mut() { + *e = self + *e; + } + + res + } + } + impl + Zero $(, $param : ArrayLength)*> Sub for $vecn { type Output = $vecn; @@ -308,5 +368,35 @@ macro_rules! vecn_dvec_common_impl( res } } + + impl<$($param : ArrayLength),*> Sub<$vecn> for f32 { + type Output = $vecn; + + #[inline] + fn sub(self, right: $vecn) -> $vecn { + let mut res = right; + + for e in res.as_mut().iter_mut() { + *e = self - *e; + } + + res + } + } + + impl<$($param : ArrayLength),*> Sub<$vecn> for f64 { + type Output = $vecn; + + #[inline] + fn sub(self, right: $vecn) -> $vecn { + let mut res = right; + + for e in res.as_mut().iter_mut() { + *e = self - *e; + } + + res + } + } ) );