Support `f64 * Vec3<f64>` and so on.

To be specific, support is added for `N op T<N>` 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<N> op N` is already supported.

Rust does not support generic impls in this case, but `f32` and `f64` cover
many common cases.

 Fixes #182.
This commit is contained in:
Jason Orendorff 2016-04-17 02:25:08 -05:00 committed by Sébastien Crozet
parent dea0ccc1fd
commit 74fd3e1a04
4 changed files with 289 additions and 1 deletions

View File

@ -634,6 +634,36 @@ macro_rules! dmat_impl(
} }
} }
impl Mul<$dmat<f32>> for f32 {
type Output = $dmat<f32>;
#[inline]
fn mul(self, right: $dmat<f32>) -> $dmat<f32> {
let mut res = right;
for mij in res.mij.iter_mut() {
*mij = self * *mij;
}
res
}
}
impl Mul<$dmat<f64>> for f64 {
type Output = $dmat<f64>;
#[inline]
fn mul(self, right: $dmat<f64>) -> $dmat<f64> {
let mut res = right;
for mij in res.mij.iter_mut() {
*mij = self * *mij;
}
res
}
}
impl<N: Copy + Div<N, Output = N>> Div<N> for $dmat<N> { impl<N: Copy + Div<N, Output = N>> Div<N> for $dmat<N> {
type Output = $dmat<N>; type Output = $dmat<N>;
@ -664,6 +694,36 @@ macro_rules! dmat_impl(
} }
} }
impl Add<$dmat<f32>> for f32 {
type Output = $dmat<f32>;
#[inline]
fn add(self, right: $dmat<f32>) -> $dmat<f32> {
let mut res = right;
for mij in res.mij.iter_mut() {
*mij = self + *mij;
}
res
}
}
impl Add<$dmat<f64>> for f64 {
type Output = $dmat<f64>;
#[inline]
fn add(self, right: $dmat<f64>) -> $dmat<f64> {
let mut res = right;
for mij in res.mij.iter_mut() {
*mij = self + *mij;
}
res
}
}
impl<N: Copy + Add<N, Output = N>> Add<$dmat<N>> for $dmat<N> { impl<N: Copy + Add<N, Output = N>> Add<$dmat<N>> for $dmat<N> {
type Output = $dmat<N>; type Output = $dmat<N>;
@ -715,6 +775,36 @@ macro_rules! dmat_impl(
} }
} }
impl Sub<$dmat<f32>> for f32 {
type Output = $dmat<f32>;
#[inline]
fn sub(self, right: $dmat<f32>) -> $dmat<f32> {
let mut res = right;
for mij in res.mij.iter_mut() {
*mij = self - *mij;
}
res
}
}
impl Sub<$dmat<f64>> for f64 {
type Output = $dmat<f64>;
#[inline]
fn sub(self, right: $dmat<f64>) -> $dmat<f64> {
let mut res = right;
for mij in res.mij.iter_mut() {
*mij = self - *mij;
}
res
}
}
impl<N: Copy + Sub<N, Output = N>> Sub<$dmat<N>> for $dmat<N> { impl<N: Copy + Sub<N, Output = N>> Sub<$dmat<N>> for $dmat<N> {
type Output = $dmat<N>; type Output = $dmat<N>;

View File

@ -118,6 +118,24 @@ macro_rules! mat_mul_scalar_impl(
$t::new($(self.$compN * *right),+) $t::new($(self.$compN * *right),+)
} }
} }
impl Mul<$t<f32>> for f32 {
type Output = $t<f32>;
#[inline]
fn mul(self, right: $t<f32>) -> $t<f32> {
$t::new($(self * right.$compN),+)
}
}
impl Mul<$t<f64>> for f64 {
type Output = $t<f64>;
#[inline]
fn mul(self, right: $t<f64>) -> $t<f64> {
$t::new($(self * right.$compN),+)
}
}
) )
); );
@ -144,6 +162,24 @@ macro_rules! mat_add_scalar_impl(
$t::new($(self.$compN + *right),+) $t::new($(self.$compN + *right),+)
} }
} }
impl Add<$t<f32>> for f32 {
type Output = $t<f32>;
#[inline]
fn add(self, right: $t<f32>) -> $t<f32> {
$t::new($(self + right.$compN),+)
}
}
impl Add<$t<f64>> for f64 {
type Output = $t<f64>;
#[inline]
fn add(self, right: $t<f64>) -> $t<f64> {
$t::new($(self + right.$compN),+)
}
}
) )
); );
@ -175,7 +211,7 @@ macro_rules! repeat_impl(
macro_rules! mat_sub_scalar_impl( macro_rules! mat_sub_scalar_impl(
($t: ident, $($compN: ident),+) => ( ($t: ident, $($compN: ident),+) => (
impl<N: Sub<N, Output = N> Sub<N> for $t<N> { impl<N: Sub<N, Output = N>> Sub<N> for $t<N> {
type Output = $t<N>; type Output = $t<N>;
#[inline] #[inline]
@ -183,6 +219,24 @@ macro_rules! mat_sub_scalar_impl(
$t::new($(self.$compN - *right),+) $t::new($(self.$compN - *right),+)
} }
} }
impl Sub<f32> for $t<f32> {
type Output = $t<f32>;
#[inline]
fn sub(self, right: $t<f32>) -> $t<f32> {
$t::new($(self - right.$compN),+)
}
}
impl Sub<f64> for $t<f64> {
type Output = $t<f64>;
#[inline]
fn sub(self, right: $t<f64>) -> $t<f64> {
$t::new($(self - right.$compN),+)
}
}
) )
); );

View File

@ -386,6 +386,24 @@ macro_rules! scalar_add_impl(
$t::new($(self.$compN + right),+) $t::new($(self.$compN + right),+)
} }
} }
impl Add<$t<f32>> for f32 {
type Output = $t<f32>;
#[inline]
fn add(self, right: $t<f32>) -> $t<f32> {
$t::new($(self + right.$compN),+)
}
}
impl Add<$t<f64>> for f64 {
type Output = $t<f64>;
#[inline]
fn add(self, right: $t<f64>) -> $t<f64> {
$t::new($(self + right.$compN),+)
}
}
) )
); );
@ -412,6 +430,24 @@ macro_rules! scalar_sub_impl(
$t::new($(self.$compN - right),+) $t::new($(self.$compN - right),+)
} }
} }
impl Sub<$t<f32>> for f32 {
type Output = $t<f32>;
#[inline]
fn sub(self, right: $t<f32>) -> $t<f32> {
$t::new($(self - right.$compN),+)
}
}
impl Sub<$t<f64>> for f64 {
type Output = $t<f64>;
#[inline]
fn sub(self, right: $t<f64>) -> $t<f64> {
$t::new($(self - right.$compN),+)
}
}
) )
); );
@ -437,6 +473,24 @@ macro_rules! scalar_mul_impl(
$t::new($(self.$compN * right),+) $t::new($(self.$compN * right),+)
} }
} }
impl Mul<$t<f32>> for f32 {
type Output = $t<f32>;
#[inline]
fn mul(self, right: $t<f32>) -> $t<f32> {
$t::new($(self * right.$compN),+)
}
}
impl Mul<$t<f64>> for f64 {
type Output = $t<f64>;
#[inline]
fn mul(self, right: $t<f64>) -> $t<f64> {
$t::new($(self * right.$compN),+)
}
}
) )
); );

View File

@ -264,6 +264,36 @@ macro_rules! vecn_dvec_common_impl(
} }
} }
impl<$($param : ArrayLength<N>),*> Mul<$vecn<f32 $(, $param)*>> for f32 {
type Output = $vecn<f32 $(, $param)*>;
#[inline]
fn mul(self, right: $vecn<f32 $(, $param)*>) -> $vecn<f32 $(, $param)*> {
let mut res = right;
for e in res.as_mut().iter_mut() {
*e = self * *e;
}
res
}
}
impl<$($param : ArrayLength<N>),*> Mul<$vecn<f64 $(, $param)*>> for f64 {
type Output = $vecn<f64 $(, $param)*>;
#[inline]
fn mul(self, right: $vecn<f64 $(, $param)*>) -> $vecn<f64 $(, $param)*> {
let mut res = right;
for e in res.as_mut().iter_mut() {
*e = self * *e;
}
res
}
}
impl<N: Copy + Div<N, Output = N> + Zero $(, $param : ArrayLength<N>)*> Div<N> for $vecn<N $(, $param)*> { impl<N: Copy + Div<N, Output = N> + Zero $(, $param : ArrayLength<N>)*> Div<N> for $vecn<N $(, $param)*> {
type Output = $vecn<N $(, $param)*>; type Output = $vecn<N $(, $param)*>;
@ -294,6 +324,36 @@ macro_rules! vecn_dvec_common_impl(
} }
} }
impl<$($param : ArrayLength<f32>),*> Add<$vecn<f32 $(, $param)*>> for f32 {
type Output = $vecn<f32 $(, $param)*>;
#[inline]
fn add(self, right: $vecn<f32 $(, $param)*>) -> $vecn<f32 $(, $param)*> {
let mut res = right;
for e in res.as_mut().iter_mut() {
*e = self + *e;
}
res
}
}
impl<$($param : ArrayLength<f64>),*> Add<$vecn<f64 $(, $param)*>> for f64 {
type Output = $vecn<f64 $(, $param)*>;
#[inline]
fn add(self, right: $vecn<f64 $(, $param)*>) -> $vecn<f64 $(, $param)*> {
let mut res = right;
for e in res.as_mut().iter_mut() {
*e = self + *e;
}
res
}
}
impl<N: Copy + Sub<N, Output = N> + Zero $(, $param : ArrayLength<N>)*> Sub<N> for $vecn<N $(, $param)*> { impl<N: Copy + Sub<N, Output = N> + Zero $(, $param : ArrayLength<N>)*> Sub<N> for $vecn<N $(, $param)*> {
type Output = $vecn<N $(, $param)*>; type Output = $vecn<N $(, $param)*>;
@ -308,5 +368,35 @@ macro_rules! vecn_dvec_common_impl(
res res
} }
} }
impl<$($param : ArrayLength<f32>),*> Sub<$vecn<f32 $(, $param)*>> for f32 {
type Output = $vecn<f32 $(, $param)*>;
#[inline]
fn sub(self, right: $vecn<f32 $(, $param)*>) -> $vecn<f32 $(, $param)*> {
let mut res = right;
for e in res.as_mut().iter_mut() {
*e = self - *e;
}
res
}
}
impl<$($param : ArrayLength<f64>),*> Sub<$vecn<f64 $(, $param)*>> for f64 {
type Output = $vecn<f64 $(, $param)*>;
#[inline]
fn sub(self, right: $vecn<f64 $(, $param)*>) -> $vecn<f64 $(, $param)*> {
let mut res = right;
for e in res.as_mut().iter_mut() {
*e = self - *e;
}
res
}
}
) )
); );