Added scalar arithmetic operators for Quaternions
This commit is contained in:
parent
3febc46d5a
commit
9096e409d1
2 changed files with 61 additions and 0 deletions
|
@ -167,6 +167,31 @@ impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
|
|||
}
|
||||
});
|
||||
|
||||
macro_rules! impl_scalar_mul {
|
||||
($S:ident) => {
|
||||
impl_operator!(Mul<Quaternion<$S>> for $S {
|
||||
fn mul(scalar, quat) -> Quaternion<$S> {
|
||||
Quaternion::from_sv(scalar * quat.s, scalar * quat.v)
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_scalar_div {
|
||||
($S:ident) => {
|
||||
impl_operator!(Div<Quaternion<$S>> for $S {
|
||||
fn div(scalar, quat) -> Quaternion<$S> {
|
||||
Quaternion::from_sv(scalar / quat.s, scalar / quat.v)
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
impl_scalar_mul!(f32);
|
||||
impl_scalar_mul!(f64);
|
||||
impl_scalar_div!(f32);
|
||||
impl_scalar_div!(f64);
|
||||
|
||||
impl<S: BaseFloat> ApproxEq for Quaternion<S> {
|
||||
type Epsilon = S;
|
||||
|
||||
|
|
|
@ -16,6 +16,42 @@
|
|||
#[macro_use]
|
||||
extern crate cgmath;
|
||||
|
||||
macro_rules! impl_test_mul {
|
||||
($s:expr, $v:expr) => (
|
||||
// point * scalar ops
|
||||
assert_eq!($v * $s, Quaternion::from_sv($v.s * $s, $v.v * $s));
|
||||
assert_eq!($s * $v, Quaternion::from_sv($s * $v.s, $s * $v.v));
|
||||
assert_eq!(&$v * $s, $v * $s);
|
||||
assert_eq!($s * &$v, $s * $v);
|
||||
// commutativity
|
||||
assert_eq!($v * $s, $s * $v);
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! impl_test_div {
|
||||
($s:expr, $v:expr) => (
|
||||
// point / scalar ops
|
||||
assert_eq!($v / $s, Quaternion::from_sv($v.s / $s, $v.v / $s));
|
||||
assert_eq!($s / $v, Quaternion::from_sv($s / $v.s, $s / $v.v));
|
||||
assert_eq!(&$v / $s, $v / $s);
|
||||
assert_eq!($s / &$v, $s / $v);
|
||||
)
|
||||
}
|
||||
|
||||
mod operators {
|
||||
use cgmath::*;
|
||||
|
||||
#[test]
|
||||
fn test_mul() {
|
||||
impl_test_mul!(2.0f32, Quaternion::from_euler(rad(1f32), rad(1f32), rad(1f32)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div() {
|
||||
impl_test_div!(2.0f32, Quaternion::from_euler(rad(1f32), rad(1f32), rad(1f32)));
|
||||
}
|
||||
}
|
||||
|
||||
mod to_from_euler {
|
||||
use std::f32;
|
||||
|
||||
|
|
Loading…
Reference in a new issue