From 91e14670eddaa9459ffa6779e2298096dd09501c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Mon, 28 Mar 2016 17:05:44 +0200 Subject: [PATCH] Add multiplications between Iso, Rot, and Sim. --- src/structs/iso_macros.rs | 17 +----- src/structs/sim_macros.rs | 16 +++--- tests/transforms.rs | 106 ++++++++++++++++++++++++-------------- 3 files changed, 75 insertions(+), 64 deletions(-) diff --git a/src/structs/iso_macros.rs b/src/structs/iso_macros.rs index ccb778ee..6c3fcc37 100644 --- a/src/structs/iso_macros.rs +++ b/src/structs/iso_macros.rs @@ -111,13 +111,8 @@ macro_rules! iso_mul_pnt_impl( } } - impl Mul<$t> for $tv { - type Output = $tv; - #[inline] - fn mul(self, right: $t) -> $tv { - (self + right.translation) * right.rotation - } - } + // NOTE: there is no viable pre-multiplication definition because of the translation + // component. ) ); @@ -131,14 +126,6 @@ macro_rules! iso_mul_vec_impl( self.rotation * right } } - - impl Mul<$t> for $tv { - type Output = $tv; - #[inline] - fn mul(self, right: $t) -> $tv { - self * right.rotation - } - } ) ); diff --git a/src/structs/sim_macros.rs b/src/structs/sim_macros.rs index 97ad7bfa..65f3e371 100644 --- a/src/structs/sim_macros.rs +++ b/src/structs/sim_macros.rs @@ -189,17 +189,13 @@ macro_rules! sim_mul_pnt_vec_impl( #[inline] fn mul(self, right: $tv) -> $tv { - (self.isometry * right) * self.scale + self.isometry * (right * self.scale) } } - impl Mul<$t> for $tv { - type Output = $tv; - #[inline] - fn mul(self, right: $t) -> $tv { - (self * right.scale) * right.isometry - } - } + + // NOTE: there is no viable pre-multiplication definition because of the translation + // component. ) ); @@ -208,12 +204,12 @@ macro_rules! sim_transform_impl( impl Transform<$tp> for $t { #[inline] fn transform(&self, p: &$tp) -> $tp { - self.isometry.transform(p) * self.scale + self.isometry.transform(&(*p * self.scale)) } #[inline] fn inv_transform(&self, p: &$tp) -> $tp { - self.isometry.inv_transform(&(*p / self.scale)) + self.isometry.inv_transform(p) / self.scale } } ) diff --git a/tests/transforms.rs b/tests/transforms.rs index 7a109d64..3b7ec6cb 100644 --- a/tests/transforms.rs +++ b/tests/transforms.rs @@ -2,21 +2,8 @@ extern crate nalgebra as na; extern crate rand; use rand::random; -use na::{Pnt2, Pnt3, Vec3, Vec1, Rot2, Rot3, Persp3, PerspMat3, Ortho3, OrthoMat3, Iso2, Iso3, - Sim2, Sim3, BaseFloat, Transform}; - -macro_rules! test_transform_inv_transform_impl( - ($t: ty, $p: ty) => ( - for _ in 0usize .. 10000 { - let randmat: $t = random(); - let expected: $p = random(); - - let computed = randmat.inv_transform(&randmat.transform(&expected)); - - assert!(na::approx_eq(&computed, &expected)); - } - ); -); +use na::{Pnt2, Pnt3, Vec2, Vec3, Vec1, Rot2, Rot3, Persp3, PerspMat3, Ortho3, OrthoMat3, Iso2, + Iso3, Sim2, Sim3, BaseFloat, Transform}; #[test] fn test_rotation2() { @@ -211,32 +198,73 @@ fn test_ortho() { assert!(na::approx_eq(&pm.zfar(), &61.0)); } -#[test] -fn test_transform_inv_transform_rot2() { - test_transform_inv_transform_impl!(Rot2, Pnt2); -} +macro_rules! test_transform_inv_transform_impl( + ($fnname: ident, $t: ty, $p: ty) => ( + #[test] + fn $fnname() { + for _ in 0usize .. 10000 { + let randmat: $t = random(); + let expected: $p = random(); + + let computed = randmat.inv_transform(&randmat.transform(&expected)); + println!("computed: {}, expected: {}", computed, expected); + + assert!(na::approx_eq(&computed, &expected)); + } + } + ); +); -#[test] -fn test_transform_inv_transform_rot3() { - test_transform_inv_transform_impl!(Rot3, Pnt3); -} +test_transform_inv_transform_impl!(test_transform_inv_transform_rot2, Rot2, Pnt2); +test_transform_inv_transform_impl!(test_transform_inv_transform_rot3, Rot3, Pnt3); +test_transform_inv_transform_impl!(test_transform_inv_transform_iso2, Iso2, Pnt2); +test_transform_inv_transform_impl!(test_transform_inv_transform_iso3, Iso3, Pnt3); +test_transform_inv_transform_impl!(test_transform_inv_transform_sim2, Sim2, Pnt2); +test_transform_inv_transform_impl!(test_transform_inv_transform_sim3, Sim3, Pnt3); -#[test] -fn test_transform_inv_transform_iso2() { - test_transform_inv_transform_impl!(Iso2, Pnt2); -} +macro_rules! test_transform_mul_assoc( + ($fnname: ident, $t1: ty, $t2: ty, $p: ty) => ( + #[test] + fn $fnname() { + for _ in 0usize .. 10000 { + let t1: $t1 = random(); + let t2: $t2 = random(); + let p: $p = random(); -#[test] -fn test_transform_inv_transform_iso3() { - test_transform_inv_transform_impl!(Iso3, Pnt3); -} + let t1p = t1 * p; + let t2p = t2 * p; + let t1t2 = t1 * t2; + let t2t1 = t2 * t1; + + assert!(na::approx_eq(&(t1t2 * p), &(t1 * t2p))); + assert!(na::approx_eq(&(t2t1 * p), &(t2 * t1p))); + } + } + ); +); -#[test] -fn test_transform_inv_transform_sim2() { - test_transform_inv_transform_impl!(Sim2, Pnt2); -} +test_transform_mul_assoc!(test_transform_inv_transform_sim3_sim3_pnt3, Sim3, Sim3, Pnt3); +test_transform_mul_assoc!(test_transform_inv_transform_sim3_iso3_pnt3, Sim3, Iso3, Pnt3); +test_transform_mul_assoc!(test_transform_inv_transform_sim3_rot3_pnt3, Sim3, Rot3, Pnt3); +test_transform_mul_assoc!(test_transform_inv_transform_iso3_iso3_pnt3, Iso3, Iso3, Pnt3); +test_transform_mul_assoc!(test_transform_inv_transform_iso3_rot3_pnt3, Iso3, Rot3, Pnt3); +test_transform_mul_assoc!(test_transform_inv_transform_rot3_rot3_pnt3, Rot3, Rot3, Pnt3); +test_transform_mul_assoc!(test_transform_inv_transform_sim3_sim3_vec3, Sim3, Sim3, Vec3); +test_transform_mul_assoc!(test_transform_inv_transform_sim3_iso3_vec3, Sim3, Iso3, Vec3); +test_transform_mul_assoc!(test_transform_inv_transform_sim3_rot3_vec3, Sim3, Rot3, Vec3); +test_transform_mul_assoc!(test_transform_inv_transform_iso3_iso3_vec3, Iso3, Iso3, Vec3); +test_transform_mul_assoc!(test_transform_inv_transform_iso3_rot3_vec3, Iso3, Rot3, Vec3); +test_transform_mul_assoc!(test_transform_inv_transform_rot3_rot3_vec3, Rot3, Rot3, Vec3); -#[test] -fn test_transform_inv_transform_sim3() { - test_transform_inv_transform_impl!(Sim3, Pnt3); -} +test_transform_mul_assoc!(test_transform_inv_transform_sim2_sim2_pnt2, Sim2, Sim2, Pnt2); +test_transform_mul_assoc!(test_transform_inv_transform_sim2_iso2_pnt2, Sim2, Iso2, Pnt2); +test_transform_mul_assoc!(test_transform_inv_transform_sim2_rot2_pnt2, Sim2, Rot2, Pnt2); +test_transform_mul_assoc!(test_transform_inv_transform_iso2_iso2_pnt2, Iso2, Iso2, Pnt2); +test_transform_mul_assoc!(test_transform_inv_transform_iso2_rot2_pnt2, Iso2, Rot2, Pnt2); +test_transform_mul_assoc!(test_transform_inv_transform_rot2_rot2_pnt2, Rot2, Rot2, Pnt2); +test_transform_mul_assoc!(test_transform_inv_transform_sim2_sim2_vec2, Sim2, Sim2, Vec2); +test_transform_mul_assoc!(test_transform_inv_transform_sim2_iso2_vec2, Sim2, Iso2, Vec2); +test_transform_mul_assoc!(test_transform_inv_transform_sim2_rot2_vec2, Sim2, Rot2, Vec2); +test_transform_mul_assoc!(test_transform_inv_transform_iso2_iso2_vec2, Iso2, Iso2, Vec2); +test_transform_mul_assoc!(test_transform_inv_transform_iso2_rot2_vec2, Iso2, Rot2, Vec2); +test_transform_mul_assoc!(test_transform_inv_transform_rot2_rot2_vec2, Rot2, Rot2, Vec2);