2016-12-05 05:44:42 +08:00
|
|
|
use num::Zero;
|
|
|
|
|
|
|
|
use alga::general::{SubsetOf, SupersetOf, Real};
|
|
|
|
use alga::linear::Rotation as AlgaRotation;
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
use core::{Vector4, Matrix4};
|
|
|
|
use core::dimension::U3;
|
|
|
|
use geometry::{Quaternion, UnitQuaternion, Rotation, Isometry, Similarity,
|
|
|
|
Transform, SuperTCategoryOf, TAffine, Translation,
|
|
|
|
Rotation3, Point3};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file provides the following conversions:
|
|
|
|
* =============================================
|
|
|
|
*
|
|
|
|
* Quaternion -> Quaternion
|
|
|
|
* UnitQuaternion -> UnitQuaternion
|
2017-08-03 01:37:44 +08:00
|
|
|
* UnitQuaternion -> Rotation<U3>
|
|
|
|
* UnitQuaternion -> Isometry<U3>
|
|
|
|
* UnitQuaternion -> Similarity<U3>
|
|
|
|
* UnitQuaternion -> Transform<U3>
|
2016-12-05 05:44:42 +08:00
|
|
|
* UnitQuaternion -> Matrix<U4> (homogeneous)
|
|
|
|
*
|
|
|
|
* NOTE:
|
|
|
|
* UnitQuaternion -> Quaternion is already provided by: Unit<T> -> T
|
|
|
|
*/
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2> SubsetOf<Quaternion<N2>> for Quaternion<N1>
|
2016-12-05 05:44:42 +08:00
|
|
|
where N1: Real,
|
2017-08-03 01:37:44 +08:00
|
|
|
N2: Real + SupersetOf<N1> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Quaternion<N2> {
|
|
|
|
Quaternion::from_vector(self.coords.to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(q: &Quaternion<N2>) -> bool {
|
|
|
|
::is_convertible::<_, Vector4<N1>>(&q.coords)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(q: &Quaternion<N2>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_vector(q.coords.to_subset_unchecked())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2> SubsetOf<UnitQuaternion<N2>> for UnitQuaternion<N1>
|
2016-12-05 05:44:42 +08:00
|
|
|
where N1: Real,
|
2017-08-03 01:37:44 +08:00
|
|
|
N2: Real + SupersetOf<N1> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> UnitQuaternion<N2> {
|
|
|
|
UnitQuaternion::new_unchecked(self.as_ref().to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(uq: &UnitQuaternion<N2>) -> bool {
|
|
|
|
::is_convertible::<_, Quaternion<N1>>(uq.as_ref())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(uq: &UnitQuaternion<N2>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::new_unchecked(::convert_ref_unchecked(uq.as_ref()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2> SubsetOf<Rotation<N2, U3>> for UnitQuaternion<N1>
|
2016-12-05 05:44:42 +08:00
|
|
|
where N1: Real,
|
2017-08-03 01:37:44 +08:00
|
|
|
N2: Real + SupersetOf<N1> {
|
|
|
|
#[inline]
|
|
|
|
fn to_superset(&self) -> Rotation3<N2> {
|
|
|
|
let q: UnitQuaternion<N2> = self.to_superset();
|
2016-12-05 05:44:42 +08:00
|
|
|
q.to_rotation_matrix()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(rot: &Rotation3<N2>) -> bool {
|
|
|
|
::is_convertible::<_, Rotation3<N1>>(rot)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(rot: &Rotation3<N2>) -> Self {
|
|
|
|
let q = UnitQuaternion::<N2>::from_rotation_matrix(rot);
|
2016-12-05 05:44:42 +08:00
|
|
|
::convert_unchecked(q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, R> SubsetOf<Isometry<N2, U3, R>> for UnitQuaternion<N1>
|
2016-12-05 05:44:42 +08:00
|
|
|
where N1: Real,
|
|
|
|
N2: Real + SupersetOf<N1>,
|
2017-08-03 01:37:44 +08:00
|
|
|
R: AlgaRotation<Point3<N2>> + SupersetOf<UnitQuaternion<N1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Isometry<N2, U3, R> {
|
|
|
|
Isometry::from_parts(Translation::identity(), ::convert_ref(self))
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(iso: &Isometry<N2, U3, R>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
iso.translation.vector.is_zero()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(iso: &Isometry<N2, U3, R>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
::convert_ref_unchecked(&iso.rotation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, R> SubsetOf<Similarity<N2, U3, R>> for UnitQuaternion<N1>
|
2016-12-05 05:44:42 +08:00
|
|
|
where N1: Real,
|
|
|
|
N2: Real + SupersetOf<N1>,
|
2017-08-03 01:37:44 +08:00
|
|
|
R: AlgaRotation<Point3<N2>> + SupersetOf<UnitQuaternion<N1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Similarity<N2, U3, R> {
|
|
|
|
Similarity::from_isometry(::convert_ref(self), N2::one())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(sim: &Similarity<N2, U3, R>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
sim.isometry.translation.vector.is_zero() &&
|
|
|
|
sim.scaling() == N2::one()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(sim: &Similarity<N2, U3, R>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
::convert_ref_unchecked(&sim.isometry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, C> SubsetOf<Transform<N2, U3, C>> for UnitQuaternion<N1>
|
2016-12-05 05:44:42 +08:00
|
|
|
where N1: Real,
|
|
|
|
N2: Real + SupersetOf<N1>,
|
2017-08-03 01:37:44 +08:00
|
|
|
C: SuperTCategoryOf<TAffine> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Transform<N2, U3, C> {
|
|
|
|
Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(t: &Transform<N2, U3, C>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
<Self as SubsetOf<_>>::is_in_subset(t.matrix())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(t: &Transform<N2, U3, C>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_superset_unchecked(t.matrix())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1: Real, N2: Real + SupersetOf<N1>> SubsetOf<Matrix4<N2>> for UnitQuaternion<N1> {
|
|
|
|
#[inline]
|
|
|
|
fn to_superset(&self) -> Matrix4<N2> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.to_homogeneous().to_superset()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(m: &Matrix4<N2>) -> bool {
|
|
|
|
::is_convertible::<_, Rotation3<N1>>(m)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(m: &Matrix4<N2>) -> Self {
|
|
|
|
let rot: Rotation3<N1> = ::convert_ref_unchecked(m);
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_rotation_matrix(&rot)
|
|
|
|
}
|
|
|
|
}
|