use num::{Zero, One}; use std::fmt; use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; 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)] pub struct TranslationBase*/> { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. pub vector: ColumnVector } #[cfg(feature = "serde-serialize")] impl Serialize for TranslationBase where N: Scalar, D: DimName, ColumnVector: Serialize, { fn serialize(&self, serializer: T) -> Result where T: Serializer { self.vector.serialize(serializer) } } #[cfg(feature = "serde-serialize")] impl<'de, N, D, S> Deserialize<'de> for TranslationBase where N: Scalar, D: DimName, ColumnVector: Deserialize<'de>, { fn deserialize(deserializer: T) -> Result where T: Deserializer<'de> { ColumnVector::deserialize(deserializer).map(|x| TranslationBase { vector: x }) } } #[cfg(feature = "abomonation-serialize")] impl Abomonation for TranslationBase where N: Scalar, D: DimName, ColumnVector: Abomonation { unsafe fn entomb(&self, writer: &mut Vec) { self.vector.entomb(writer) } unsafe fn embalm(&mut self) { self.vector.embalm() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { self.vector.exhume(bytes) } } 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) // // } // // } // */