Move all the rotation-related free functions to a `RotationWithTranslation` trait.

This is not a very good name though.
This commit is contained in:
Sébastien Crozet 2013-09-04 17:00:56 +02:00
parent 70cee0ea3d
commit 539e34c2bf
2 changed files with 58 additions and 75 deletions

View File

@ -15,16 +15,13 @@ pub use traits::mat_cast::MatCast;
pub use traits::column::Column; pub use traits::column::Column;
pub use traits::inv::Inv; pub use traits::inv::Inv;
pub use traits::rlmul::{RMul, LMul}; 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::transformation::{Transformation, Transform};
pub use traits::translation::{Translation, Translate}; pub use traits::translation::{Translation, Translate};
pub use traits::transpose::{Transpose}; pub use traits::transpose::{Transpose};
pub use traits::homogeneous::{ToHomogeneous, FromHomogeneous}; pub use traits::homogeneous::{ToHomogeneous, FromHomogeneous};
pub use traits::row::Row; 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; mod mat_macros;
/// Special identity matrix. All its operation are no-ops. /// Special identity matrix. All its operation are no-ops.

View File

@ -26,78 +26,64 @@ pub trait Rotate<V> {
fn inv_rotate(&self, &V) -> V; fn inv_rotate(&self, &V) -> V;
} }
/** /// 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<LV: Neg<LV>, AV>: Rotation<AV> + Translation<LV> {
/**
* Applies a rotation centered on a specific point. * Applies a rotation centered on a specific point.
* *
* - `m`: the object to be rotated. * - `m`: the object to be rotated.
* - `ammount`: the rotation to apply. * - `ammount`: the rotation to apply.
* - `point`: the center of rotation. * - `point`: the center of rotation.
*/ */
#[inline] #[inline]
pub fn rotated_wrt_point<M: Translation<LV> + Rotation<AV>, fn rotated_wrt_point(&self, ammount: &AV, center: &LV) -> Self {
LV: Neg<LV>, let mut res = self.translated(&-center);
AV>(
m: &M,
ammount: &AV,
center: &LV)
-> M {
let mut res = m.translated(&-center);
res.rotate_by(ammount); res.rotate_by(ammount);
res.translate_by(center); res.translate_by(center);
res res
} }
/// Rotates an object using a specific center of rotation. /// Rotates an object using a specific center of rotation.
/// ///
/// # Arguments /// # Arguments
/// * `m` - the object to be rotated /// * `m` - the object to be rotated
/// * `ammount` - the rotation to be applied /// * `ammount` - the rotation to be applied
/// * `center` - the new center of rotation /// * `center` - the new center of rotation
#[inline] #[inline]
pub fn rotate_wrt_point<M: Rotation<AV> + Translation<LV>, fn rotate_wrt_point(&mut self, ammount: &AV, center: &LV) {
LV: Neg<LV>, self.translate_by(&-center);
AV>( self.rotate_by(ammount);
m: &mut M, self.translate_by(center);
ammount: &AV, }
center: &LV) {
m.translate_by(&-center);
m.rotate_by(ammount);
m.translate_by(center);
}
/** /**
* Applies a rotation centered on the input translation. * Applies a rotation centered on the input translation.
* *
* # Arguments * # Arguments
* * `m` - the object to be rotated. * * `m` - the object to be rotated.
* * `ammount` - the rotation to apply. * * `ammount` - the rotation to apply.
*/ */
#[inline] #[inline]
pub fn rotated_wrt_center<M: Rotation<AV> + Translation<LV>, fn rotated_wrt_center(&self, ammount: &AV) -> Self {
LV: Neg<LV>, self.rotated_wrt_point(ammount, &self.translation())
AV>( }
m: &M,
ammount: &AV)
-> M {
rotated_wrt_point(m, ammount, &m.translation())
}
/** /**
* Applies a rotation centered on the input translation. * Applies a rotation centered on the input translation.
* *
* # Arguments * # Arguments
* * `m` - the object to be rotated. * * `m` - the object to be rotated.
* * `ammount` - the rotation to apply. * * `ammount` - the rotation to apply.
*/ */
#[inline] #[inline]
pub fn rotate_wrt_center<M: Translation<LV> + Rotation<AV>, fn rotate_wrt_center(&mut self, ammount: &AV) {
LV: Neg<LV>, let center = self.translation();
AV>( self.rotate_wrt_point(ammount, &center)
m: &mut M, }
ammount: &AV) {
let t = m.translation();
rotate_wrt_point(m, ammount, &t)
} }
impl<LV: Neg<LV>, AV, M: Rotation<AV> + Translation<LV>> RotationWithTranslation<LV, AV> for M;