use num::{Zero, One}; use std::fmt; use approx::ApproxEq; use alga::general::{Real, ClosedNeg}; use core::{Scalar, ColumnVector, OwnedSquareMatrix}; use core::dimension::{DimName, DimNameSum, DimNameAdd, U1}; use core::storage::{Storage, StorageMut, Owned}; use core::allocator::Allocator; /// A translation with an owned vector storage. pub type OwnedTranslation = TranslationBase>::Alloc>>; /// A translation. #[repr(C)] #[derive(Hash, Debug, Clone, Copy)] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct TranslationBase*/> { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. pub vector: ColumnVector } impl TranslationBase where N: Scalar, S: Storage { /// Creates a new translation from the given vector. #[inline] pub fn from_vector(vector: ColumnVector) -> TranslationBase { TranslationBase { vector: vector } } /// Inverts `self`. #[inline] pub fn inverse(&self) -> OwnedTranslation where N: ClosedNeg { TranslationBase::from_vector(-&self.vector) } /// Converts this translation into its equivalent homogeneous transformation matrix. #[inline] pub fn to_homogeneous(&self) -> OwnedSquareMatrix, S::Alloc> where N: Zero + One, D: DimNameAdd, S::Alloc: Allocator, DimNameSum> { let mut res = OwnedSquareMatrix::::identity(); res.fixed_slice_mut::(0, D::dim()).copy_from(&self.vector); res } } impl TranslationBase where N: Scalar + ClosedNeg, S: StorageMut { /// Inverts `self` in-place. #[inline] pub fn inverse_mut(&mut self) { self.vector.neg_mut() } } impl Eq for TranslationBase where N: Scalar + Eq, S: Storage { } impl PartialEq for TranslationBase where N: Scalar + PartialEq, S: Storage { #[inline] fn eq(&self, right: &TranslationBase) -> bool { self.vector == right.vector } } impl ApproxEq for TranslationBase where N: Scalar + ApproxEq, S: Storage, N::Epsilon: Copy { type Epsilon = N::Epsilon; #[inline] fn default_epsilon() -> Self::Epsilon { N::default_epsilon() } #[inline] fn default_max_relative() -> Self::Epsilon { N::default_max_relative() } #[inline] fn default_max_ulps() -> u32 { N::default_max_ulps() } #[inline] fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { self.vector.relative_eq(&other.vector, epsilon, max_relative) } #[inline] fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.vector.ulps_eq(&other.vector, epsilon, max_ulps) } } /* * * Display * */ impl fmt::Display for TranslationBase where N: Real + fmt::Display, S: Storage, S::Alloc: Allocator { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let precision = f.precision().unwrap_or(3); try!(writeln!(f, "TranslationBase {{")); try!(write!(f, "{:.*}", precision, self.vector)); writeln!(f, "}}") } } // // /* // // * // // * Absolute // // * // // */ // // impl Absolute for $t { // // type AbsoluteValue = $submatrix; // // // // #[inline] // // fn abs(m: &$t) -> $submatrix { // // Absolute::abs(&m.submatrix) // // } // // } // */