use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::{One, Zero}; use std::fmt; use std::hash; #[cfg(feature = "serde-serialize")] use serde; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; use alga::general::{ClosedNeg, Real}; use core::allocator::Allocator; use core::dimension::{DimName, DimNameAdd, DimNameSum, U1}; use core::storage::Owned; use core::{DefaultAllocator, MatrixN, Scalar, VectorN}; /// A translation. #[repr(C)] #[derive(Debug)] pub struct Translation where DefaultAllocator: Allocator, { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. pub vector: VectorN, } impl hash::Hash for Translation where DefaultAllocator: Allocator, Owned: hash::Hash, { fn hash(&self, state: &mut H) { self.vector.hash(state) } } impl Copy for Translation where DefaultAllocator: Allocator, Owned: Copy, { } impl Clone for Translation where DefaultAllocator: Allocator, Owned: Clone, { #[inline] fn clone(&self) -> Self { Translation::from_vector(self.vector.clone()) } } #[cfg(feature = "abomonation-serialize")] impl Abomonation for Translation where N: Scalar, D: DimName, VectorN: Abomonation, DefaultAllocator: Allocator, { 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) } } #[cfg(feature = "serde-serialize")] impl serde::Serialize for Translation where DefaultAllocator: Allocator, Owned: serde::Serialize, { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { self.vector.serialize(serializer) } } #[cfg(feature = "serde-serialize")] impl<'a, N: Scalar, D: DimName> serde::Deserialize<'a> for Translation where DefaultAllocator: Allocator, Owned: serde::Deserialize<'a>, { fn deserialize(deserializer: Des) -> Result where Des: serde::Deserializer<'a>, { let matrix = VectorN::::deserialize(deserializer)?; Ok(Translation::from_vector(matrix)) } } impl Translation where DefaultAllocator: Allocator, { /// Creates a new translation from the given vector. #[inline] pub fn from_vector(vector: VectorN) -> Translation { Translation { vector: vector } } /// Inverts `self`. #[inline] pub fn inverse(&self) -> Translation where N: ClosedNeg, { Translation::from_vector(-&self.vector) } /// Converts this translation into its equivalent homogeneous transformation matrix. #[inline] pub fn to_homogeneous(&self) -> MatrixN> where N: Zero + One, D: DimNameAdd, DefaultAllocator: Allocator, DimNameSum>, { let mut res = MatrixN::>::identity(); res.fixed_slice_mut::(0, D::dim()) .copy_from(&self.vector); res } /// Inverts `self` in-place. #[inline] pub fn inverse_mut(&mut self) where N: ClosedNeg, { self.vector.neg_mut() } } impl Eq for Translation where DefaultAllocator: Allocator, { } impl PartialEq for Translation where DefaultAllocator: Allocator, { #[inline] fn eq(&self, right: &Translation) -> bool { self.vector == right.vector } } impl AbsDiffEq for Translation where DefaultAllocator: Allocator, N::Epsilon: Copy, { type Epsilon = N::Epsilon; #[inline] fn default_epsilon() -> Self::Epsilon { N::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { self.vector.abs_diff_eq(&other.vector, epsilon) } } impl RelativeEq for Translation where DefaultAllocator: Allocator, N::Epsilon: Copy, { #[inline] fn default_max_relative() -> Self::Epsilon { N::default_max_relative() } #[inline] fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool { self.vector .relative_eq(&other.vector, epsilon, max_relative) } } impl UlpsEq for Translation where DefaultAllocator: Allocator, N::Epsilon: Copy, { #[inline] fn default_max_ulps() -> u32 { N::default_max_ulps() } #[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 Translation where DefaultAllocator: Allocator + Allocator, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let precision = f.precision().unwrap_or(3); try!(writeln!(f, "Translation {{")); try!(write!(f, "{:.*}", precision, self.vector)); writeln!(f, "}}") } }