2021-10-19 20:02:07 +08:00
|
|
|
use num::{One, Zero};
|
|
|
|
|
|
|
|
use simba::scalar::{RealField, SubsetOf, SupersetOf};
|
|
|
|
use simba::simd::PrimitiveSimdValue;
|
|
|
|
|
|
|
|
use crate::base::allocator::Allocator;
|
|
|
|
use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
|
2021-10-21 21:56:07 +08:00
|
|
|
use crate::base::{Const, DefaultAllocator, OMatrix, OVector, SVector, Scalar};
|
2021-10-19 20:02:07 +08:00
|
|
|
|
2021-10-19 22:18:25 +08:00
|
|
|
use crate::geometry::{Scale, SuperTCategoryOf, TAffine, Transform};
|
2021-10-19 22:01:02 +08:00
|
|
|
use crate::Point;
|
2021-10-19 20:02:07 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file provides the following conversions:
|
|
|
|
* =============================================
|
|
|
|
*
|
2021-10-19 20:21:26 +08:00
|
|
|
* Scale -> Scale
|
|
|
|
* Scale -> Transform
|
|
|
|
* Scale -> Matrix (homogeneous)
|
2021-10-19 20:02:07 +08:00
|
|
|
*/
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T1, T2, const D: usize> SubsetOf<Scale<T2, D>> for Scale<T1, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T1: Scalar,
|
|
|
|
T2: Scalar + SupersetOf<T1>,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn to_superset(&self) -> Scale<T2, D> {
|
|
|
|
Scale::from(self.vector.to_superset())
|
2021-10-19 20:02:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn is_in_subset(rot: &Scale<T2, D>) -> bool {
|
2021-10-19 20:02:07 +08:00
|
|
|
crate::is_convertible::<_, SVector<T1, D>>(&rot.vector)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from_superset_unchecked(rot: &Scale<T2, D>) -> Self {
|
|
|
|
Scale {
|
2021-10-19 20:02:07 +08:00
|
|
|
vector: rot.vector.to_subset_unchecked(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T1, T2, C, const D: usize> SubsetOf<Transform<T2, C, D>> for Scale<T1, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
|
|
|
C: SuperTCategoryOf<TAffine>,
|
|
|
|
Const<D>: DimNameAdd<U1>,
|
2021-10-19 22:18:25 +08:00
|
|
|
DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
2021-10-19 21:55:06 +08:00
|
|
|
+ Allocator<T1, DimNameSum<Const<D>, U1>, U1>
|
2021-10-19 20:02:07 +08:00
|
|
|
+ Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn to_superset(&self) -> Transform<T2, C, D> {
|
|
|
|
Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_in_subset(t: &Transform<T2, C, D>) -> bool {
|
|
|
|
<Self as SubsetOf<_>>::is_in_subset(t.matrix())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_superset_unchecked(t: &Transform<T2, C, D>) -> Self {
|
|
|
|
Self::from_superset_unchecked(t.matrix())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T1, T2, const D: usize>
|
2021-10-19 20:21:26 +08:00
|
|
|
SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>> for Scale<T1, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T1: RealField,
|
|
|
|
T2: RealField + SupersetOf<T1>,
|
|
|
|
Const<D>: DimNameAdd<U1>,
|
2021-10-19 22:18:25 +08:00
|
|
|
DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
2021-10-19 21:55:06 +08:00
|
|
|
+ Allocator<T1, DimNameSum<Const<D>, U1>, U1>
|
2021-10-19 20:02:07 +08:00
|
|
|
+ Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn to_superset(&self) -> OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> {
|
|
|
|
self.to_homogeneous().to_superset()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_in_subset(m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>) -> bool {
|
2021-10-21 21:56:07 +08:00
|
|
|
if m[(D, D)] != T2::one() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for i in 0..D + 1 {
|
|
|
|
for j in 0..D + 1 {
|
|
|
|
if i != j && m[(i, j)] != T2::zero() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 02:27:54 +08:00
|
|
|
true
|
2021-10-19 20:02:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
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);
|
|
|
|
Self {
|
|
|
|
vector: crate::convert_unchecked(t.into_owned()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:01:02 +08:00
|
|
|
impl<T: Scalar + Zero + One, const D: usize> From<Scale<T, D>>
|
2021-10-19 20:02:07 +08:00
|
|
|
for OMatrix<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
|
|
|
where
|
|
|
|
Const<D>: DimNameAdd<U1>,
|
2021-10-19 22:18:25 +08:00
|
|
|
DefaultAllocator: Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
|
2021-10-19 21:55:06 +08:00
|
|
|
+ Allocator<T, DimNameSum<Const<D>, U1>, U1>
|
|
|
|
+ Allocator<T, Const<D>>,
|
2021-10-19 20:02:07 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from(t: Scale<T, D>) -> Self {
|
2021-10-19 20:02:07 +08:00
|
|
|
t.to_homogeneous()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<OVector<T, Const<D>>> for Scale<T, D> {
|
2021-10-19 20:02:07 +08:00
|
|
|
#[inline]
|
|
|
|
fn from(vector: OVector<T, Const<D>>) -> Self {
|
2021-10-19 20:21:26 +08:00
|
|
|
Scale { vector }
|
2021-10-19 20:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<[T; D]> for Scale<T, D> {
|
2021-10-19 20:02:07 +08:00
|
|
|
#[inline]
|
|
|
|
fn from(coords: [T; D]) -> Self {
|
2021-10-19 20:21:26 +08:00
|
|
|
Scale {
|
2021-10-19 20:02:07 +08:00
|
|
|
vector: coords.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<Point<T, D>> for Scale<T, D> {
|
2021-10-19 20:02:07 +08:00
|
|
|
#[inline]
|
|
|
|
fn from(pt: Point<T, D>) -> Self {
|
2021-10-19 20:21:26 +08:00
|
|
|
Scale { vector: pt.coords }
|
2021-10-19 20:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T: Scalar, const D: usize> From<Scale<T, D>> for [T; D] {
|
2021-10-19 20:02:07 +08:00
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from(t: Scale<T, D>) -> Self {
|
2021-10-19 20:02:07 +08:00
|
|
|
t.vector.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:18:25 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Scale<T::Element, D>; 2]> for Scale<T, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
|
|
|
|
T::Element: Scalar,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from(arr: [Scale<T::Element, D>; 2]) -> Self {
|
2021-10-19 20:02:07 +08:00
|
|
|
Self::from(OVector::from([
|
|
|
|
arr[0].vector.clone(),
|
|
|
|
arr[1].vector.clone(),
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:18:25 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Scale<T::Element, D>; 4]> for Scale<T, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
|
|
|
|
T::Element: Scalar,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from(arr: [Scale<T::Element, D>; 4]) -> Self {
|
2021-10-19 20:02:07 +08:00
|
|
|
Self::from(OVector::from([
|
|
|
|
arr[0].vector.clone(),
|
|
|
|
arr[1].vector.clone(),
|
|
|
|
arr[2].vector.clone(),
|
|
|
|
arr[3].vector.clone(),
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:18:25 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Scale<T::Element, D>; 8]> for Scale<T, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
|
|
|
|
T::Element: Scalar,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from(arr: [Scale<T::Element, D>; 8]) -> Self {
|
2021-10-19 20:02:07 +08:00
|
|
|
Self::from(OVector::from([
|
|
|
|
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(),
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:21:26 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Scale<T::Element, D>; 16]>
|
|
|
|
for Scale<T, D>
|
2021-10-19 20:02:07 +08:00
|
|
|
where
|
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
|
|
|
|
T::Element: Scalar,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-10-19 20:21:26 +08:00
|
|
|
fn from(arr: [Scale<T::Element, D>; 16]) -> Self {
|
2021-10-19 20:02:07 +08:00
|
|
|
Self::from(OVector::from([
|
|
|
|
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(),
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|