nalgebra/src/geometry/unit_complex_conversion.rs

265 lines
7.2 KiB
Rust
Raw Normal View History

2017-02-13 01:17:09 +08:00
use num::Zero;
use num_complex::Complex;
2020-03-21 19:16:46 +08:00
use simba::scalar::{RealField, SubsetOf, SupersetOf};
use simba::simd::{PrimitiveSimdValue, SimdRealField};
2017-02-13 01:17:09 +08:00
use crate::base::{Matrix2, Matrix3, Scalar};
2019-03-23 21:29:07 +08:00
use crate::geometry::{
2020-03-21 19:16:46 +08:00
AbstractRotation, Isometry, Rotation2, Similarity, SuperTCategoryOf, TAffine, Transform,
Translation, UnitComplex,
};
2017-02-13 01:17:09 +08:00
/*
* This file provides the following conversions:
* =============================================
*
* UnitComplex -> UnitComplex
* UnitComplex -> Rotation<U1>
* UnitComplex -> Isometry<2>
* UnitComplex -> Similarity<2>
* UnitComplex -> Transform<2>
2017-02-13 01:17:09 +08:00
* UnitComplex -> Matrix<U3> (homogeneous)
*
* NOTE:
* UnitComplex -> Complex is already provided by: Unit<T> -> T
*/
2021-04-11 17:00:38 +08:00
impl<T1, T2> SubsetOf<UnitComplex<T2>> for UnitComplex<T1>
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
{
2017-02-13 01:17:09 +08:00
#[inline]
2021-04-11 17:00:38 +08:00
fn to_superset(&self) -> UnitComplex<T2> {
2017-02-13 01:17:09 +08:00
UnitComplex::new_unchecked(self.as_ref().to_superset())
}
#[inline]
2021-04-11 17:00:38 +08:00
fn is_in_subset(uq: &UnitComplex<T2>) -> bool {
crate::is_convertible::<_, Complex<T1>>(uq.as_ref())
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn from_superset_unchecked(uq: &UnitComplex<T2>) -> Self {
2019-03-23 21:29:07 +08:00
Self::new_unchecked(crate::convert_ref_unchecked(uq.as_ref()))
2017-02-13 01:17:09 +08:00
}
}
2021-04-11 17:00:38 +08:00
impl<T1, T2> SubsetOf<Rotation2<T2>> for UnitComplex<T1>
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
{
2017-02-13 01:17:09 +08:00
#[inline]
2021-04-11 17:00:38 +08:00
fn to_superset(&self) -> Rotation2<T2> {
let q: UnitComplex<T2> = self.to_superset();
2017-02-13 01:17:09 +08:00
q.to_rotation_matrix().to_superset()
}
#[inline]
2021-04-11 17:00:38 +08:00
fn is_in_subset(rot: &Rotation2<T2>) -> bool {
crate::is_convertible::<_, Rotation2<T1>>(rot)
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn from_superset_unchecked(rot: &Rotation2<T2>) -> Self {
let q = UnitComplex::<T2>::from_rotation_matrix(rot);
2019-03-23 21:29:07 +08:00
crate::convert_unchecked(q)
2017-02-13 01:17:09 +08:00
}
}
2021-04-11 17:00:38 +08:00
impl<T1, T2, R> SubsetOf<Isometry<T2, R, 2>> for UnitComplex<T1>
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, 2> + SupersetOf<Self>,
2018-02-02 19:26:35 +08:00
{
2017-02-13 01:17:09 +08:00
#[inline]
2021-04-11 17:00:38 +08:00
fn to_superset(&self) -> Isometry<T2, R, 2> {
2019-03-23 21:29:07 +08:00
Isometry::from_parts(Translation::identity(), crate::convert_ref(self))
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn is_in_subset(iso: &Isometry<T2, R, 2>) -> bool {
2017-02-13 01:17:09 +08:00
iso.translation.vector.is_zero()
}
#[inline]
2021-04-11 17:00:38 +08:00
fn from_superset_unchecked(iso: &Isometry<T2, R, 2>) -> Self {
2019-03-23 21:29:07 +08:00
crate::convert_ref_unchecked(&iso.rotation)
2017-02-13 01:17:09 +08:00
}
}
2021-04-11 17:00:38 +08:00
impl<T1, T2, R> SubsetOf<Similarity<T2, R, 2>> for UnitComplex<T1>
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, 2> + SupersetOf<Self>,
2018-02-02 19:26:35 +08:00
{
2017-02-13 01:17:09 +08:00
#[inline]
2021-04-11 17:00:38 +08:00
fn to_superset(&self) -> Similarity<T2, R, 2> {
Similarity::from_isometry(crate::convert_ref(self), T2::one())
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn is_in_subset(sim: &Similarity<T2, R, 2>) -> bool {
sim.isometry.translation.vector.is_zero() && sim.scaling() == T2::one()
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn from_superset_unchecked(sim: &Similarity<T2, R, 2>) -> Self {
2019-03-23 21:29:07 +08:00
crate::convert_ref_unchecked(&sim.isometry)
2017-02-13 01:17:09 +08:00
}
}
2021-04-11 17:00:38 +08:00
impl<T1, T2, C> SubsetOf<Transform<T2, C, 2>> for UnitComplex<T1>
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>,
{
2017-02-13 01:17:09 +08:00
#[inline]
2021-04-11 17:00:38 +08:00
fn to_superset(&self) -> Transform<T2, C, 2> {
Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn is_in_subset(t: &Transform<T2, C, 2>) -> bool {
2017-02-13 01:17:09 +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, 2>) -> Self {
2017-02-13 01:17:09 +08:00
Self::from_superset_unchecked(t.matrix())
}
}
2021-04-11 17:00:38 +08:00
impl<T1: RealField, T2: RealField + SupersetOf<T1>> SubsetOf<Matrix3<T2>> for UnitComplex<T1> {
#[inline]
2021-04-11 17:00:38 +08:00
fn to_superset(&self) -> Matrix3<T2> {
2017-02-13 01:17:09 +08:00
self.to_homogeneous().to_superset()
}
#[inline]
2021-04-11 17:00:38 +08:00
fn is_in_subset(m: &Matrix3<T2>) -> bool {
crate::is_convertible::<_, Rotation2<T1>>(m)
2017-02-13 01:17:09 +08:00
}
#[inline]
2021-04-11 17:00:38 +08:00
fn from_superset_unchecked(m: &Matrix3<T2>) -> Self {
let rot: Rotation2<T1> = crate::convert_ref_unchecked(m);
2017-02-13 01:17:09 +08:00
Self::from_rotation_matrix(&rot)
}
}
2021-04-11 17:00:38 +08:00
impl<T: SimdRealField> From<UnitComplex<T>> for Rotation2<T>
where
2021-04-11 17:00:38 +08:00
T::Element: SimdRealField,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(q: UnitComplex<T>) -> Self {
q.to_rotation_matrix()
}
}
2021-04-11 17:00:38 +08:00
impl<T: SimdRealField> From<Rotation2<T>> for UnitComplex<T>
where
2021-04-11 17:00:38 +08:00
T::Element: SimdRealField,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(q: Rotation2<T>) -> Self {
Self::from_rotation_matrix(&q)
}
}
2021-04-11 17:00:38 +08:00
impl<T: SimdRealField> From<UnitComplex<T>> for Matrix3<T>
where
2021-04-11 17:00:38 +08:00
T::Element: SimdRealField,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(q: UnitComplex<T>) -> Matrix3<T> {
q.to_homogeneous()
}
}
2021-04-11 17:00:38 +08:00
impl<T: SimdRealField> From<UnitComplex<T>> for Matrix2<T>
where
2021-04-11 17:00:38 +08:00
T::Element: SimdRealField,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(q: UnitComplex<T>) -> Self {
q.to_rotation_matrix().into_inner()
}
}
2021-04-11 17:00:38 +08:00
impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitComplex<T::Element>; 2]> for UnitComplex<T>
where
2021-04-11 17:00:38 +08:00
T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
T::Element: Scalar + Copy,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(arr: [UnitComplex<T::Element>; 2]) -> Self {
Self::new_unchecked(Complex {
2021-04-11 17:00:38 +08:00
re: T::from([arr[0].re, arr[1].re]),
im: T::from([arr[0].im, arr[1].im]),
})
}
}
2021-04-11 17:00:38 +08:00
impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitComplex<T::Element>; 4]> for UnitComplex<T>
where
2021-04-11 17:00:38 +08:00
T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
T::Element: Scalar + Copy,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(arr: [UnitComplex<T::Element>; 4]) -> Self {
Self::new_unchecked(Complex {
2021-04-11 17:00:38 +08:00
re: T::from([arr[0].re, arr[1].re, arr[2].re, arr[3].re]),
im: T::from([arr[0].im, arr[1].im, arr[2].im, arr[3].im]),
})
}
}
2021-04-11 17:00:38 +08:00
impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitComplex<T::Element>; 8]> for UnitComplex<T>
where
2021-04-11 17:00:38 +08:00
T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
T::Element: Scalar + Copy,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(arr: [UnitComplex<T::Element>; 8]) -> Self {
Self::new_unchecked(Complex {
2021-04-11 17:00:38 +08:00
re: T::from([
arr[0].re, arr[1].re, arr[2].re, arr[3].re, arr[4].re, arr[5].re, arr[6].re,
arr[7].re,
]),
2021-04-11 17:00:38 +08:00
im: T::from([
arr[0].im, arr[1].im, arr[2].im, arr[3].im, arr[4].im, arr[5].im, arr[6].im,
arr[7].im,
]),
})
}
}
2021-04-11 17:00:38 +08:00
impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitComplex<T::Element>; 16]> for UnitComplex<T>
where
2021-04-11 17:00:38 +08:00
T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
T::Element: Scalar + Copy,
{
#[inline]
2021-04-11 17:00:38 +08:00
fn from(arr: [UnitComplex<T::Element>; 16]) -> Self {
Self::new_unchecked(Complex {
2021-04-11 17:00:38 +08:00
re: T::from([
arr[0].re, arr[1].re, arr[2].re, arr[3].re, arr[4].re, arr[5].re, arr[6].re,
arr[7].re, arr[8].re, arr[9].re, arr[10].re, arr[11].re, arr[12].re, arr[13].re,
arr[14].re, arr[15].re,
]),
2021-04-11 17:00:38 +08:00
im: T::from([
arr[0].im, arr[1].im, arr[2].im, arr[3].im, arr[4].im, arr[5].im, arr[6].im,
arr[7].im, arr[8].im, arr[9].im, arr[10].im, arr[11].im, arr[12].im, arr[13].im,
arr[14].im, arr[15].im,
]),
})
}
}