2020-03-24 17:16:31 +08:00
|
|
|
use num::Zero;
|
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::{RealField, SubsetOf, SupersetOf};
|
2020-03-24 17:16:31 +08:00
|
|
|
use simba::simd::{PrimitiveSimdValue, SimdRealField, SimdValue};
|
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::{DimMin, DimNameAdd, DimNameSum, U1};
|
2021-04-11 17:00:38 +08:00
|
|
|
use crate::base::{Const, DefaultAllocator, OMatrix, 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,
|
|
|
|
};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file provides the following conversions:
|
|
|
|
* =============================================
|
|
|
|
*
|
2017-08-03 01:37:44 +08:00
|
|
|
* Similarity -> Similarity
|
|
|
|
* Similarity -> Transform
|
|
|
|
* Similarity -> Matrix (homogeneous)
|
2016-12-05 05:44:42 +08:00
|
|
|
*/
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, R1, R2, const D: usize> SubsetOf<Similarity<T2, R2, D>> for Similarity<T1, R1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T1: RealField + SubsetOf<T2>,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
|
|
|
R1: AbstractRotation<T1, D> + SubsetOf<R2>,
|
|
|
|
R2: 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, R2, D> {
|
2018-02-02 19:26:35 +08:00
|
|
|
Similarity::from_isometry(self.isometry.to_superset(), self.scaling().to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(sim: &Similarity<T2, R2, D>) -> bool {
|
|
|
|
crate::is_convertible::<_, Isometry<T1, R1, D>>(&sim.isometry)
|
|
|
|
&& crate::is_convertible::<_, T1>(&sim.scaling())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(sim: &Similarity<T2, R2, D>) -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
Similarity::from_isometry(
|
2016-12-05 05:44:42 +08:00
|
|
|
sim.isometry.to_subset_unchecked(),
|
2018-02-02 19:26:35 +08:00
|
|
|
sim.scaling().to_subset_unchecked(),
|
2016-12-05 05:44:42 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T1, T2, R, C, const D: usize> SubsetOf<Transform<T2, C, D>> for Similarity<T1, R, 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-11 17:00:38 +08:00
|
|
|
R: AbstractRotation<T1, D>
|
|
|
|
+ SubsetOf<OMatrix<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>>
|
|
|
|
+ SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>>,
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1> + DimMin<Const<D>, Output = Const<D>>, // needed by .determinant()
|
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<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
|
2021-04-07 20:29:20 +08:00
|
|
|
// + Allocator<(usize, usize), D>
|
2021-04-11 17:00:38 +08:00
|
|
|
// + Allocator<T1, D>
|
|
|
|
// + Allocator<T1, D, D>
|
|
|
|
// + Allocator<T2, D, 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) -> 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, R, const D: usize>
|
|
|
|
SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>>
|
|
|
|
for Similarity<T1, R, 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<T1, D>
|
|
|
|
+ SubsetOf<OMatrix<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>>
|
|
|
|
+ SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>>,
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1> + DimMin<Const<D>, Output = Const<D>>, // needed by .determinant()
|
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<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>, // + Allocator<(usize, usize), D>
|
|
|
|
// + Allocator<T1, D>
|
|
|
|
// + Allocator<T1, D, D>
|
|
|
|
// + Allocator<T2, D, 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 mut rot = m.fixed_slice::<D, D>(0, 0).clone_owned();
|
2018-10-13 16:25:34 +08:00
|
|
|
if rot
|
2021-04-11 17:00:38 +08:00
|
|
|
.fixed_columns_mut::<1>(0)
|
|
|
|
.try_normalize_mut(T2::zero())
|
2018-02-02 19:26:35 +08:00
|
|
|
.is_some()
|
2018-10-13 16:25:34 +08:00
|
|
|
&& rot
|
2021-04-11 17:00:38 +08:00
|
|
|
.fixed_columns_mut::<1>(1)
|
|
|
|
.try_normalize_mut(T2::zero())
|
2018-02-02 19:26:35 +08:00
|
|
|
.is_some()
|
2018-10-13 16:25:34 +08:00
|
|
|
&& rot
|
2021-04-11 17:00:38 +08:00
|
|
|
.fixed_columns_mut::<1>(2)
|
|
|
|
.try_normalize_mut(T2::zero())
|
2018-02-02 19:26:35 +08:00
|
|
|
.is_some()
|
|
|
|
{
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: could we avoid explicit the computation of the determinant?
|
2016-12-05 05:44:42 +08:00
|
|
|
// (its sign is needed to see if the scaling factor is negative).
|
2021-04-11 17:00:38 +08:00
|
|
|
if rot.determinant() < T2::zero() {
|
|
|
|
rot.fixed_columns_mut::<1>(0).neg_mut();
|
|
|
|
rot.fixed_columns_mut::<1>(1).neg_mut();
|
|
|
|
rot.fixed_columns_mut::<1>(2).neg_mut();
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let bottom = m.fixed_slice::<1, D>(D, 0);
|
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 normalized block part is a rotation.
|
2021-04-11 17:00:38 +08:00
|
|
|
// rot.is_special_orthogonal(T2::default_epsilon().sqrt()) &&
|
2016-12-05 05:44:42 +08:00
|
|
|
// The bottom row is (0, 0, ..., 1)
|
2021-04-11 17:00:38 +08:00
|
|
|
bottom.iter().all(|e| e.is_zero()) && m[(D, D)] == T2::one()
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2016-12-05 05:44:42 +08:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2016-12-05 05:44:42 +08:00
|
|
|
let mut mm = m.clone_owned();
|
2021-04-11 17:00:38 +08:00
|
|
|
let na = mm.fixed_slice_mut::<D, 1>(0, 0).normalize_mut();
|
|
|
|
let nb = mm.fixed_slice_mut::<D, 1>(0, 1).normalize_mut();
|
|
|
|
let nc = mm.fixed_slice_mut::<D, 1>(0, 2).normalize_mut();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
let mut scale = (na + nb + nc) / crate::convert(3.0); // We take the mean, for robustness.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: could we avoid the explicit computation of the determinant?
|
2016-12-05 05:44:42 +08:00
|
|
|
// (its sign is needed to see if the scaling factor is negative).
|
2021-04-11 17:00:38 +08:00
|
|
|
if mm.fixed_slice::<D, D>(0, 0).determinant() < T2::zero() {
|
|
|
|
mm.fixed_slice_mut::<D, 1>(0, 0).neg_mut();
|
|
|
|
mm.fixed_slice_mut::<D, 1>(0, 1).neg_mut();
|
|
|
|
mm.fixed_slice_mut::<D, 1>(0, 2).neg_mut();
|
2016-12-05 05:44:42 +08:00
|
|
|
scale = -scale;
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let t = m.fixed_slice::<D, 1>(0, D).into_owned();
|
2018-10-28 14:33:39 +08:00
|
|
|
let t = Translation {
|
2019-03-23 21:29:07 +08:00
|
|
|
vector: crate::convert_unchecked(t),
|
2018-10-28 14:33:39 +08:00
|
|
|
};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
Self::from_parts(
|
|
|
|
t,
|
|
|
|
crate::convert_unchecked(mm),
|
|
|
|
crate::convert_unchecked(scale),
|
|
|
|
)
|
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: SimdRealField, R, const D: usize> From<Similarity<T, R, 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>,
|
2021-04-11 17:00:38 +08:00
|
|
|
R: SubsetOf<OMatrix<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>>,
|
|
|
|
DefaultAllocator: Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>, // + Allocator<T, D>
|
2018-10-13 16:25:34 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(sim: Similarity<T, R, D>) -> Self {
|
2018-10-13 16:25:34 +08:00
|
|
|
sim.to_homogeneous()
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 17:16:31 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Zero + PrimitiveSimdValue, R, const D: usize>
|
|
|
|
From<[Similarity<T::Element, R::Element, D>; 2]> for Similarity<T, R, D>
|
2020-03-24 17:16:31 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 2]>,
|
|
|
|
R: SimdValue + AbstractRotation<T, D> + From<[<R as SimdValue>::Element; 2]>,
|
|
|
|
R::Element: AbstractRotation<T::Element, D>,
|
|
|
|
T::Element: Scalar + Zero + Copy,
|
2020-03-24 17:16:31 +08:00
|
|
|
R::Element: Scalar + Zero + Copy,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Similarity<T::Element, R::Element, D>; 2]) -> Self {
|
2020-03-24 17:16:31 +08:00
|
|
|
let iso = Isometry::from([arr[0].isometry.clone(), arr[1].isometry.clone()]);
|
2021-04-11 17:00:38 +08:00
|
|
|
let scale = T::from([arr[0].scaling(), arr[1].scaling()]);
|
2020-03-24 17:16:31 +08:00
|
|
|
|
|
|
|
Self::from_isometry(iso, scale)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Zero + PrimitiveSimdValue, R, const D: usize>
|
|
|
|
From<[Similarity<T::Element, R::Element, D>; 4]> for Similarity<T, R, D>
|
2020-03-24 17:16:31 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 4]>,
|
|
|
|
R: SimdValue + AbstractRotation<T, D> + From<[<R as SimdValue>::Element; 4]>,
|
|
|
|
R::Element: AbstractRotation<T::Element, D>,
|
|
|
|
T::Element: Scalar + Zero + Copy,
|
2020-03-24 17:16:31 +08:00
|
|
|
R::Element: Scalar + Zero + Copy,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Similarity<T::Element, R::Element, D>; 4]) -> Self {
|
2020-03-24 17:16:31 +08:00
|
|
|
let iso = Isometry::from([
|
|
|
|
arr[0].isometry.clone(),
|
|
|
|
arr[1].isometry.clone(),
|
|
|
|
arr[2].isometry.clone(),
|
|
|
|
arr[3].isometry.clone(),
|
|
|
|
]);
|
2021-04-11 17:00:38 +08:00
|
|
|
let scale = T::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].scaling(),
|
|
|
|
arr[1].scaling(),
|
|
|
|
arr[2].scaling(),
|
|
|
|
arr[3].scaling(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Self::from_isometry(iso, scale)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Zero + PrimitiveSimdValue, R, const D: usize>
|
|
|
|
From<[Similarity<T::Element, R::Element, D>; 8]> for Similarity<T, R, D>
|
2020-03-24 17:16:31 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 8]>,
|
|
|
|
R: SimdValue + AbstractRotation<T, D> + From<[<R as SimdValue>::Element; 8]>,
|
|
|
|
R::Element: AbstractRotation<T::Element, D>,
|
|
|
|
T::Element: Scalar + Zero + Copy,
|
2020-03-24 17:16:31 +08:00
|
|
|
R::Element: Scalar + Zero + Copy,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Similarity<T::Element, R::Element, D>; 8]) -> Self {
|
2020-03-24 17:16:31 +08:00
|
|
|
let iso = Isometry::from([
|
|
|
|
arr[0].isometry.clone(),
|
|
|
|
arr[1].isometry.clone(),
|
|
|
|
arr[2].isometry.clone(),
|
|
|
|
arr[3].isometry.clone(),
|
|
|
|
arr[4].isometry.clone(),
|
|
|
|
arr[5].isometry.clone(),
|
|
|
|
arr[6].isometry.clone(),
|
|
|
|
arr[7].isometry.clone(),
|
|
|
|
]);
|
2021-04-11 17:00:38 +08:00
|
|
|
let scale = T::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].scaling(),
|
|
|
|
arr[1].scaling(),
|
|
|
|
arr[2].scaling(),
|
|
|
|
arr[3].scaling(),
|
|
|
|
arr[4].scaling(),
|
|
|
|
arr[5].scaling(),
|
|
|
|
arr[6].scaling(),
|
|
|
|
arr[7].scaling(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Self::from_isometry(iso, scale)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Zero + PrimitiveSimdValue, R, const D: usize>
|
|
|
|
From<[Similarity<T::Element, R::Element, D>; 16]> for Similarity<T, R, D>
|
2020-03-24 17:16:31 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 16]>,
|
|
|
|
R: SimdValue + AbstractRotation<T, D> + From<[<R as SimdValue>::Element; 16]>,
|
|
|
|
R::Element: AbstractRotation<T::Element, D>,
|
|
|
|
T::Element: Scalar + Zero + Copy,
|
2020-03-24 17:16:31 +08:00
|
|
|
R::Element: Scalar + Zero + Copy,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Similarity<T::Element, R::Element, D>; 16]) -> Self {
|
2020-03-24 17:16:31 +08:00
|
|
|
let iso = Isometry::from([
|
|
|
|
arr[0].isometry.clone(),
|
|
|
|
arr[1].isometry.clone(),
|
|
|
|
arr[2].isometry.clone(),
|
|
|
|
arr[3].isometry.clone(),
|
|
|
|
arr[4].isometry.clone(),
|
|
|
|
arr[5].isometry.clone(),
|
|
|
|
arr[6].isometry.clone(),
|
|
|
|
arr[7].isometry.clone(),
|
|
|
|
arr[8].isometry.clone(),
|
|
|
|
arr[9].isometry.clone(),
|
|
|
|
arr[10].isometry.clone(),
|
|
|
|
arr[11].isometry.clone(),
|
|
|
|
arr[12].isometry.clone(),
|
|
|
|
arr[13].isometry.clone(),
|
|
|
|
arr[14].isometry.clone(),
|
|
|
|
arr[15].isometry.clone(),
|
|
|
|
]);
|
2021-04-11 17:00:38 +08:00
|
|
|
let scale = T::from([
|
2020-03-24 17:16:31 +08:00
|
|
|
arr[0].scaling(),
|
|
|
|
arr[1].scaling(),
|
|
|
|
arr[2].scaling(),
|
|
|
|
arr[3].scaling(),
|
|
|
|
arr[4].scaling(),
|
|
|
|
arr[5].scaling(),
|
|
|
|
arr[6].scaling(),
|
|
|
|
arr[7].scaling(),
|
|
|
|
arr[8].scaling(),
|
|
|
|
arr[9].scaling(),
|
|
|
|
arr[10].scaling(),
|
|
|
|
arr[11].scaling(),
|
|
|
|
arr[12].scaling(),
|
|
|
|
arr[13].scaling(),
|
|
|
|
arr[14].scaling(),
|
|
|
|
arr[15].scaling(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Self::from_isometry(iso, scale)
|
|
|
|
}
|
|
|
|
}
|