use num::Zero; use alga::general::{Real, SubsetOf, SupersetOf}; use alga::linear::Rotation as AlgaRotation; #[cfg(feature = "mint")] use mint; use base::allocator::Allocator; use base::dimension::{DimMin, DimName, DimNameAdd, DimNameSum, U1}; use base::{DefaultAllocator, Matrix2, Matrix3, Matrix4, MatrixN}; use geometry::{ Isometry, Point, Rotation, Rotation2, Rotation3, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, UnitComplex, UnitQuaternion, }; /* * This file provides the following conversions: * ============================================= * * Rotation -> Rotation * Rotation3 -> UnitQuaternion * Rotation2 -> UnitComplex * Rotation -> Isometry * Rotation -> Similarity * Rotation -> Transform * Rotation -> Matrix (homogeneous) * mint::EulerAngles -> Rotation */ impl SubsetOf> for Rotation where N1: Real, N2: Real + SupersetOf, DefaultAllocator: Allocator + Allocator, { #[inline] fn to_superset(&self) -> Rotation { Rotation::from_matrix_unchecked(self.matrix().to_superset()) } #[inline] fn is_in_subset(rot: &Rotation) -> bool { ::is_convertible::<_, MatrixN>(rot.matrix()) } #[inline] unsafe fn from_superset_unchecked(rot: &Rotation) -> Self { Rotation::from_matrix_unchecked(rot.matrix().to_subset_unchecked()) } } impl SubsetOf> for Rotation3 where N1: Real, N2: Real + SupersetOf, { #[inline] fn to_superset(&self) -> UnitQuaternion { let q = UnitQuaternion::::from_rotation_matrix(self); q.to_superset() } #[inline] fn is_in_subset(q: &UnitQuaternion) -> bool { ::is_convertible::<_, UnitQuaternion>(q) } #[inline] unsafe fn from_superset_unchecked(q: &UnitQuaternion) -> Self { let q: UnitQuaternion = ::convert_ref_unchecked(q); q.to_rotation_matrix() } } impl SubsetOf> for Rotation2 where N1: Real, N2: Real + SupersetOf, { #[inline] fn to_superset(&self) -> UnitComplex { let q = UnitComplex::::from_rotation_matrix(self); q.to_superset() } #[inline] fn is_in_subset(q: &UnitComplex) -> bool { ::is_convertible::<_, UnitComplex>(q) } #[inline] unsafe fn from_superset_unchecked(q: &UnitComplex) -> Self { let q: UnitComplex = ::convert_ref_unchecked(q); q.to_rotation_matrix() } } impl SubsetOf> for Rotation where N1: Real, N2: Real + SupersetOf, R: AlgaRotation> + SupersetOf>, DefaultAllocator: Allocator + Allocator, { #[inline] fn to_superset(&self) -> Isometry { Isometry::from_parts(Translation::identity(), ::convert_ref(self)) } #[inline] fn is_in_subset(iso: &Isometry) -> bool { iso.translation.vector.is_zero() } #[inline] unsafe fn from_superset_unchecked(iso: &Isometry) -> Self { ::convert_ref_unchecked(&iso.rotation) } } impl SubsetOf> for Rotation where N1: Real, N2: Real + SupersetOf, R: AlgaRotation> + SupersetOf>, DefaultAllocator: Allocator + Allocator, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_parts(Translation::identity(), ::convert_ref(self), N2::one()) } #[inline] fn is_in_subset(sim: &Similarity) -> bool { sim.isometry.translation.vector.is_zero() && sim.scaling() == N2::one() } #[inline] unsafe fn from_superset_unchecked(sim: &Similarity) -> Self { ::convert_ref_unchecked(&sim.isometry.rotation) } } impl SubsetOf> for Rotation where N1: Real, N2: Real + SupersetOf, C: SuperTCategoryOf, D: DimNameAdd + DimMin, // needed by .is_special_orthogonal() DefaultAllocator: Allocator + Allocator + Allocator, DimNameSum> + Allocator, DimNameSum> + Allocator<(usize, usize), D>, { // needed by .is_special_orthogonal() #[inline] fn to_superset(&self) -> Transform { Transform::from_matrix_unchecked(self.to_homogeneous().to_superset()) } #[inline] fn is_in_subset(t: &Transform) -> bool { >::is_in_subset(t.matrix()) } #[inline] unsafe fn from_superset_unchecked(t: &Transform) -> Self { Self::from_superset_unchecked(t.matrix()) } } impl SubsetOf>> for Rotation where N1: Real, N2: Real + SupersetOf, D: DimNameAdd + DimMin, // needed by .is_special_orthogonal() DefaultAllocator: Allocator + Allocator + Allocator, DimNameSum> + Allocator, DimNameSum> + Allocator<(usize, usize), D>, { // needed by .is_special_orthogonal() #[inline] fn to_superset(&self) -> MatrixN> { self.to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &MatrixN>) -> bool { let rot = m.fixed_slice::(0, 0); let bottom = m.fixed_slice::(D::dim(), 0); // Scalar types agree. m.iter().all(|e| SupersetOf::::is_in_subset(e)) && // The block part is a rotation. rot.is_special_orthogonal(N2::default_epsilon() * ::convert(100.0)) && // The bottom row is (0, 0, ..., 1) bottom.iter().all(|e| e.is_zero()) && m[(D::dim(), D::dim())] == N2::one() } #[inline] unsafe fn from_superset_unchecked(m: &MatrixN>) -> Self { let r = m.fixed_slice::(0, 0); Self::from_matrix_unchecked(::convert_unchecked(r.into_owned())) } } #[cfg(feature = "mint")] impl From> for Rotation3 { fn from(euler: mint::EulerAngles) -> Self { Self::from_euler_angles(euler.a, euler.b, euler.c) } } impl From> for Matrix3 { #[inline] fn from(q: Rotation2) -> Matrix3 { q.to_homogeneous() } } impl From> for Matrix2 { #[inline] fn from(q: Rotation2) -> Matrix2 { q.into_inner() } } impl From> for Matrix4 { #[inline] fn from(q: Rotation3) -> Matrix4 { q.to_homogeneous() } } impl From> for Matrix3 { #[inline] fn from(q: Rotation3) -> Matrix3 { q.into_inner() } }