From 539e34c2bfcbf8eb47abd04e164315b142a57f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Wed, 4 Sep 2013 17:00:56 +0200 Subject: [PATCH] Move all the rotation-related free functions to a `RotationWithTranslation` trait. This is not a very good name though. --- src/mat.rs | 5 +- src/traits/rotation.rs | 128 ++++++++++++++++++----------------------- 2 files changed, 58 insertions(+), 75 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index c2bdfec4..25800349 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -15,16 +15,13 @@ pub use traits::mat_cast::MatCast; pub use traits::column::Column; pub use traits::inv::Inv; pub use traits::rlmul::{RMul, LMul}; -pub use traits::rotation::{Rotation, Rotate}; +pub use traits::rotation::{Rotation, Rotate, RotationWithTranslation}; pub use traits::transformation::{Transformation, Transform}; pub use traits::translation::{Translation, Translate}; pub use traits::transpose::{Transpose}; pub use traits::homogeneous::{ToHomogeneous, FromHomogeneous}; pub use traits::row::Row; -// functions -pub use traits::rotation::{rotated_wrt_point, rotate_wrt_point, rotated_wrt_center, rotate_wrt_center}; - mod mat_macros; /// Special identity matrix. All its operation are no-ops. diff --git a/src/traits/rotation.rs b/src/traits/rotation.rs index 46822520..a383ba05 100644 --- a/src/traits/rotation.rs +++ b/src/traits/rotation.rs @@ -26,78 +26,64 @@ pub trait Rotate { fn inv_rotate(&self, &V) -> V; } -/** - * Applies a rotation centered on a specific point. - * - * - `m`: the object to be rotated. - * - `ammount`: the rotation to apply. - * - `point`: the center of rotation. - */ -#[inline] -pub fn rotated_wrt_point + Rotation, - LV: Neg, - AV>( - m: &M, - ammount: &AV, - center: &LV) - -> M { - let mut res = m.translated(&-center); +/// Utilities to make rotations with regard to a point different than the origin. +// NOTE: we cannot call this an Isometry since an isometry really does not need to have a rotation +// nor a translation (this can be a reflexion). +pub trait RotationWithTranslation, AV>: Rotation + Translation { + /** + * Applies a rotation centered on a specific point. + * + * - `m`: the object to be rotated. + * - `ammount`: the rotation to apply. + * - `point`: the center of rotation. + */ + #[inline] + fn rotated_wrt_point(&self, ammount: &AV, center: &LV) -> Self { + let mut res = self.translated(&-center); - res.rotate_by(ammount); - res.translate_by(center); + res.rotate_by(ammount); + res.translate_by(center); - res + res + } + + /// Rotates an object using a specific center of rotation. + /// + /// # Arguments + /// * `m` - the object to be rotated + /// * `ammount` - the rotation to be applied + /// * `center` - the new center of rotation + #[inline] + fn rotate_wrt_point(&mut self, ammount: &AV, center: &LV) { + self.translate_by(&-center); + self.rotate_by(ammount); + self.translate_by(center); + } + + /** + * Applies a rotation centered on the input translation. + * + * # Arguments + * * `m` - the object to be rotated. + * * `ammount` - the rotation to apply. + */ + #[inline] + fn rotated_wrt_center(&self, ammount: &AV) -> Self { + self.rotated_wrt_point(ammount, &self.translation()) + } + + /** + * Applies a rotation centered on the input translation. + * + * # Arguments + * * `m` - the object to be rotated. + * * `ammount` - the rotation to apply. + */ + #[inline] + fn rotate_wrt_center(&mut self, ammount: &AV) { + let center = self.translation(); + self.rotate_wrt_point(ammount, ¢er) + } } -/// Rotates an object using a specific center of rotation. -/// -/// # Arguments -/// * `m` - the object to be rotated -/// * `ammount` - the rotation to be applied -/// * `center` - the new center of rotation -#[inline] -pub fn rotate_wrt_point + Translation, - LV: Neg, - AV>( - m: &mut M, - ammount: &AV, - center: &LV) { - m.translate_by(&-center); - m.rotate_by(ammount); - m.translate_by(center); -} - -/** - * Applies a rotation centered on the input translation. - * - * # Arguments - * * `m` - the object to be rotated. - * * `ammount` - the rotation to apply. - */ -#[inline] -pub fn rotated_wrt_center + Translation, - LV: Neg, - AV>( - m: &M, - ammount: &AV) - -> M { - rotated_wrt_point(m, ammount, &m.translation()) -} - -/** - * Applies a rotation centered on the input translation. - * - * # Arguments - * * `m` - the object to be rotated. - * * `ammount` - the rotation to apply. - */ -#[inline] -pub fn rotate_wrt_center + Rotation, - LV: Neg, - AV>( - m: &mut M, - ammount: &AV) { - let t = m.translation(); - - rotate_wrt_point(m, ammount, &t) -} +impl, AV, M: Rotation + Translation> RotationWithTranslation for M;