2016-12-05 05:44:42 +08:00
|
|
|
use alga::general::{Real, SubsetOf, SupersetOf};
|
|
|
|
use alga::linear::Rotation;
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::Allocator;
|
|
|
|
use crate::base::dimension::{DimMin, DimName, DimNameAdd, DimNameSum, U1};
|
|
|
|
use crate::base::{DefaultAllocator, MatrixN};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::geometry::{Isometry, Point, 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
|
|
|
*/
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, D: DimName, R1, R2> SubsetOf<Similarity<N2, D, R2>> for Similarity<N1, D, R1>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N1: Real + SubsetOf<N2>,
|
|
|
|
N2: Real + SupersetOf<N1>,
|
|
|
|
R1: Rotation<Point<N1, D>> + SubsetOf<R2>,
|
|
|
|
R2: Rotation<Point<N2, D>>,
|
|
|
|
DefaultAllocator: Allocator<N1, D> + Allocator<N2, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Similarity<N2, D, R2> {
|
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]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(sim: &Similarity<N2, D, R2>) -> bool {
|
2019-03-23 21:29:07 +08:00
|
|
|
crate::is_convertible::<_, Isometry<N1, D, R1>>(&sim.isometry)
|
|
|
|
&& crate::is_convertible::<_, N1>(&sim.scaling())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(sim: &Similarity<N2, D, R2>) -> Self {
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, D, R, C> SubsetOf<Transform<N2, D, C>> for Similarity<N1, D, R>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N1: Real,
|
|
|
|
N2: Real + SupersetOf<N1>,
|
|
|
|
C: SuperTCategoryOf<TAffine>,
|
|
|
|
R: Rotation<Point<N1, D>>
|
|
|
|
+ SubsetOf<MatrixN<N1, DimNameSum<D, U1>>>
|
|
|
|
+ SubsetOf<MatrixN<N2, DimNameSum<D, U1>>>,
|
|
|
|
D: DimNameAdd<U1> + DimMin<D, Output = D>, // needed by .determinant()
|
|
|
|
DefaultAllocator: Allocator<N1, D>
|
|
|
|
+ Allocator<N1, D, D>
|
|
|
|
+ Allocator<N1, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N2, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<(usize, usize), D>
|
|
|
|
+ Allocator<N2, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N2, D, D>
|
|
|
|
+ Allocator<N2, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Transform<N2, D, 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, D, 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, D, 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, N2, D, R> SubsetOf<MatrixN<N2, DimNameSum<D, U1>>> for Similarity<N1, D, R>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N1: Real,
|
|
|
|
N2: Real + SupersetOf<N1>,
|
|
|
|
R: Rotation<Point<N1, D>>
|
|
|
|
+ SubsetOf<MatrixN<N1, DimNameSum<D, U1>>>
|
|
|
|
+ SubsetOf<MatrixN<N2, DimNameSum<D, U1>>>,
|
|
|
|
D: DimNameAdd<U1> + DimMin<D, Output = D>, // needed by .determinant()
|
|
|
|
DefaultAllocator: Allocator<N1, D>
|
|
|
|
+ Allocator<N1, D, D>
|
|
|
|
+ Allocator<N1, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N2, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<(usize, usize), D>
|
|
|
|
+ Allocator<N2, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N2, D, D>
|
|
|
|
+ Allocator<N2, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> MatrixN<N2, DimNameSum<D, U1>> {
|
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: &MatrixN<N2, DimNameSum<D, U1>>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
let mut rot = m.fixed_slice::<D, D>(0, 0).clone_owned();
|
2018-10-13 16:25:34 +08:00
|
|
|
if rot
|
|
|
|
.fixed_columns_mut::<U1>(0)
|
2018-02-02 19:26:35 +08:00
|
|
|
.try_normalize_mut(N2::zero())
|
|
|
|
.is_some()
|
2018-10-13 16:25:34 +08:00
|
|
|
&& rot
|
|
|
|
.fixed_columns_mut::<U1>(1)
|
2018-02-02 19:26:35 +08:00
|
|
|
.try_normalize_mut(N2::zero())
|
|
|
|
.is_some()
|
2018-10-13 16:25:34 +08:00
|
|
|
&& rot
|
|
|
|
.fixed_columns_mut::<U1>(2)
|
2018-02-02 19:26:35 +08:00
|
|
|
.try_normalize_mut(N2::zero())
|
|
|
|
.is_some()
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
// FIXME: could we avoid explicit the computation of the determinant?
|
|
|
|
// (its sign is needed to see if the scaling factor is negative).
|
|
|
|
if rot.determinant() < N2::zero() {
|
|
|
|
rot.fixed_columns_mut::<U1>(0).neg_mut();
|
|
|
|
rot.fixed_columns_mut::<U1>(1).neg_mut();
|
|
|
|
rot.fixed_columns_mut::<U1>(2).neg_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
let bottom = m.fixed_slice::<U1, D>(D::dim(), 0);
|
|
|
|
// Scalar types agree.
|
|
|
|
m.iter().all(|e| SupersetOf::<N1>::is_in_subset(e)) &&
|
|
|
|
// The normalized block part is a rotation.
|
|
|
|
// rot.is_special_orthogonal(N2::default_epsilon().sqrt()) &&
|
|
|
|
// The bottom row is (0, 0, ..., 1)
|
2018-02-02 19:26:35 +08:00
|
|
|
bottom.iter().all(|e| e.is_zero()) && m[(D::dim(), D::dim())] == N2::one()
|
|
|
|
} else {
|
2016-12-05 05:44:42 +08:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(m: &MatrixN<N2, DimNameSum<D, U1>>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
let mut mm = m.clone_owned();
|
|
|
|
let na = mm.fixed_slice_mut::<D, U1>(0, 0).normalize_mut();
|
|
|
|
let nb = mm.fixed_slice_mut::<D, U1>(0, 1).normalize_mut();
|
|
|
|
let nc = mm.fixed_slice_mut::<D, U1>(0, 2).normalize_mut();
|
|
|
|
|
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
|
|
|
|
|
|
|
// FIXME: could we avoid the explicit computation of the determinant?
|
|
|
|
// (its sign is needed to see if the scaling factor is negative).
|
|
|
|
if mm.fixed_slice::<D, D>(0, 0).determinant() < N2::zero() {
|
|
|
|
mm.fixed_slice_mut::<D, U1>(0, 0).neg_mut();
|
|
|
|
mm.fixed_slice_mut::<D, U1>(0, 1).neg_mut();
|
|
|
|
mm.fixed_slice_mut::<D, U1>(0, 2).neg_mut();
|
|
|
|
scale = -scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
let t = m.fixed_slice::<D, U1>(0, D::dim()).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
|
|
|
|
2019-03-23 21:29:07 +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
|
|
|
|
|
|
|
impl<N: Real, D: DimName, R> From<Similarity<N, D, R>> for MatrixN<N, DimNameSum<D, U1>>
|
|
|
|
where
|
|
|
|
D: DimNameAdd<U1>,
|
|
|
|
R: SubsetOf<MatrixN<N, DimNameSum<D, U1>>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(sim: Similarity<N, D, R>) -> Self {
|
|
|
|
sim.to_homogeneous()
|
|
|
|
}
|
|
|
|
}
|