2018-10-13 16:25:34 +08:00
|
|
|
use num::{One, Zero};
|
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::{RealField, SubsetOf, SupersetOf};
|
2020-03-23 16:16:01 +08:00
|
|
|
use simba::simd::PrimitiveSimdValue;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::Allocator;
|
2021-04-07 20:29:20 +08:00
|
|
|
use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
|
2021-04-11 17:00:38 +08:00
|
|
|
use crate::base::{Const, DefaultAllocator, DimName, OMatrix, OVector, SVector, Scalar};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use crate::geometry::{
|
|
|
|
AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation,
|
2021-01-29 07:46:14 +08:00
|
|
|
Translation3, UnitDualQuaternion, UnitQuaternion,
|
2020-03-21 19:16:46 +08:00
|
|
|
};
|
2021-04-27 19:17:51 +08:00
|
|
|
use crate::Point;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file provides the following conversions:
|
|
|
|
* =============================================
|
|
|
|
*
|
2017-08-03 01:37:44 +08:00
|
|
|
* Translation -> Translation
|
|
|
|
* Translation -> Isometry
|
2021-01-29 06:25:32 +08:00
|
|
|
* Translation3 -> UnitDualQuaternion
|
2017-08-03 01:37:44 +08:00
|
|
|
* Translation -> Similarity
|
|
|
|
* Translation -> Transform
|
|
|
|
* Translation -> Matrix (homogeneous)
|
2016-12-05 05:44:42 +08:00
|
|
|
*/
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, const D: usize> SubsetOf<Translation<T2, D>> for Translation<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: Scalar,
|
|
|
|
T2: Scalar + SupersetOf<T1>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> Translation<T2, D> {
|
2018-10-28 14:33:14 +08:00
|
|
|
Translation::from(self.vector.to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(rot: &Translation<T2, D>) -> bool {
|
|
|
|
crate::is_convertible::<_, SVector<T1, D>>(&rot.vector)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(rot: &Translation<T2, D>) -> Self {
|
2018-10-28 14:33:14 +08:00
|
|
|
Translation {
|
|
|
|
vector: rot.vector.to_subset_unchecked(),
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, R, const D: usize> SubsetOf<Isometry<T2, R, D>> for Translation<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
|
|
|
R: AbstractRotation<T2, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> Isometry<T2, R, D> {
|
2017-08-03 01:37:44 +08:00
|
|
|
Isometry::from_parts(self.to_superset(), R::identity())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(iso: &Isometry<T2, R, D>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
iso.rotation == R::identity()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(iso: &Isometry<T2, R, D>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_superset_unchecked(&iso.translation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2> SubsetOf<UnitDualQuaternion<T2>> for Translation3<T1>
|
2021-01-29 06:25:32 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
2021-01-29 06:25:32 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> UnitDualQuaternion<T2> {
|
2021-06-18 15:45:37 +08:00
|
|
|
let dq = UnitDualQuaternion::<T1>::from_parts(*self, UnitQuaternion::identity());
|
2021-01-29 06:25:32 +08:00
|
|
|
dq.to_superset()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(dq: &UnitDualQuaternion<T2>) -> bool {
|
|
|
|
crate::is_convertible::<_, Translation<T1, 3>>(&dq.translation())
|
2021-01-29 07:46:14 +08:00
|
|
|
&& dq.rotation() == UnitQuaternion::identity()
|
2021-01-29 06:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(dq: &UnitDualQuaternion<T2>) -> Self {
|
|
|
|
let dq: UnitDualQuaternion<T1> = crate::convert_ref_unchecked(dq);
|
2021-01-29 06:25:32 +08:00
|
|
|
dq.translation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, R, const D: usize> SubsetOf<Similarity<T2, R, D>> for Translation<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
|
|
|
R: AbstractRotation<T2, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> Similarity<T2, R, D> {
|
|
|
|
Similarity::from_parts(self.to_superset(), R::identity(), T2::one())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(sim: &Similarity<T2, R, D>) -> bool {
|
|
|
|
sim.isometry.rotation == R::identity() && sim.scaling() == T2::one()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(sim: &Similarity<T2, R, D>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_superset_unchecked(&sim.isometry.translation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, C, const D: usize> SubsetOf<Transform<T2, C, D>> for Translation<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
2018-02-02 19:26:35 +08:00
|
|
|
C: SuperTCategoryOf<TAffine>,
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1>,
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
|
|
|
+ Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> Transform<T2, C, D> {
|
2017-08-03 01:37:44 +08:00
|
|
|
Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(t: &Transform<T2, C, D>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
<Self as SubsetOf<_>>::is_in_subset(t.matrix())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(t: &Transform<T2, C, D>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_superset_unchecked(t.matrix())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, const D: usize>
|
|
|
|
SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>> for Translation<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1>,
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
|
|
|
+ Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
|
|
|
|
// + Allocator<T1, D>
|
|
|
|
// + Allocator<T2, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.to_homogeneous().to_superset()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>) -> bool {
|
|
|
|
let id = m.generic_slice((0, 0), (DimNameSum::<Const<D>, U1>::name(), Const::<D>));
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
// Scalar types agree.
|
2021-04-11 17:00:38 +08:00
|
|
|
m.iter().all(|e| SupersetOf::<T1>::is_in_subset(e)) &&
|
2016-12-05 05:44:42 +08:00
|
|
|
// The block part does nothing.
|
2021-04-11 17:00:38 +08:00
|
|
|
id.is_identity(T2::zero()) &&
|
2016-12-05 05:44:42 +08:00
|
|
|
// The normalization factor is one.
|
2021-04-11 17:00:38 +08:00
|
|
|
m[(D, D)] == T2::one()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(
|
|
|
|
m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
|
|
|
|
) -> Self {
|
|
|
|
let t = m.fixed_slice::<D, 1>(0, D);
|
2018-10-28 14:33:14 +08:00
|
|
|
Self {
|
2019-03-23 21:29:07 +08:00
|
|
|
vector: crate::convert_unchecked(t.into_owned()),
|
2018-10-28 14:33:14 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 16:25:34 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Zero + One, const D: usize> From<Translation<T, D>>
|
|
|
|
for OMatrix<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
2018-10-13 16:25:34 +08:00
|
|
|
where
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1>,
|
|
|
|
DefaultAllocator:
|
2021-04-11 17:00:38 +08:00
|
|
|
Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> + Allocator<T, Const<D>>,
|
2018-10-13 16:25:34 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(t: Translation<T, D>) -> Self {
|
2018-10-13 16:25:34 +08:00
|
|
|
t.to_homogeneous()
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 14:33:14 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<OVector<T, Const<D>>> for Translation<T, D> {
|
2018-10-28 14:33:14 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(vector: OVector<T, Const<D>>) -> Self {
|
2018-10-28 14:33:14 +08:00
|
|
|
Translation { vector }
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 16:16:01 +08:00
|
|
|
|
2021-04-27 19:17:51 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<[T; D]> for Translation<T, D> {
|
|
|
|
#[inline]
|
|
|
|
fn from(coords: [T; D]) -> Self {
|
|
|
|
Translation {
|
|
|
|
vector: coords.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Scalar, const D: usize> From<Point<T, D>> for Translation<T, D> {
|
|
|
|
#[inline]
|
|
|
|
fn from(pt: Point<T, D>) -> Self {
|
2021-06-18 15:45:37 +08:00
|
|
|
Translation { vector: pt.coords }
|
2021-04-27 19:17:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 15:45:37 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<Translation<T, D>> for [T; D] {
|
2021-04-27 19:17:51 +08:00
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(t: Translation<T, D>) -> Self {
|
|
|
|
t.vector.into()
|
2021-04-27 19:17:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 2]>
|
|
|
|
for Translation<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
|
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Translation<T::Element, D>; 2]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].vector.clone(),
|
|
|
|
arr[1].vector.clone(),
|
|
|
|
]))
|
2020-03-23 16:16:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 4]>
|
|
|
|
for Translation<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
|
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Translation<T::Element, D>; 4]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].vector.clone(),
|
|
|
|
arr[1].vector.clone(),
|
|
|
|
arr[2].vector.clone(),
|
|
|
|
arr[3].vector.clone(),
|
2020-03-23 16:16:01 +08:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 8]>
|
|
|
|
for Translation<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
|
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Translation<T::Element, D>; 8]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].vector.clone(),
|
|
|
|
arr[1].vector.clone(),
|
|
|
|
arr[2].vector.clone(),
|
|
|
|
arr[3].vector.clone(),
|
|
|
|
arr[4].vector.clone(),
|
|
|
|
arr[5].vector.clone(),
|
|
|
|
arr[6].vector.clone(),
|
|
|
|
arr[7].vector.clone(),
|
2020-03-23 16:16:01 +08:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 16]>
|
|
|
|
for Translation<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
|
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Translation<T::Element, D>; 16]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].vector.clone(),
|
|
|
|
arr[1].vector.clone(),
|
|
|
|
arr[2].vector.clone(),
|
|
|
|
arr[3].vector.clone(),
|
|
|
|
arr[4].vector.clone(),
|
|
|
|
arr[5].vector.clone(),
|
|
|
|
arr[6].vector.clone(),
|
|
|
|
arr[7].vector.clone(),
|
|
|
|
arr[8].vector.clone(),
|
|
|
|
arr[9].vector.clone(),
|
|
|
|
arr[10].vector.clone(),
|
|
|
|
arr[11].vector.clone(),
|
|
|
|
arr[12].vector.clone(),
|
|
|
|
arr[13].vector.clone(),
|
|
|
|
arr[14].vector.clone(),
|
|
|
|
arr[15].vector.clone(),
|
2020-03-23 16:16:01 +08:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|