1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use simba::scalar::{RealField, SubsetOf, SupersetOf};
use simba::simd::SimdRealField;

use crate::base::dimension::U3;
use crate::base::{Matrix4, Vector4};
use crate::geometry::{
    DualQuaternion, Isometry3, Similarity3, SuperTCategoryOf, TAffine, Transform, Translation3,
    UnitDualQuaternion, UnitQuaternion,
};

/*
 * This file provides the following conversions:
 * =============================================
 *
 * DualQuaternion     -> DualQuaternion
 * UnitDualQuaternion -> UnitDualQuaternion
 * UnitDualQuaternion -> Isometry<U3>
 * UnitDualQuaternion -> Similarity<U3>
 * UnitDualQuaternion -> Transform<U3>
 * UnitDualQuaternion -> Matrix<U4> (homogeneous)
 *
 * NOTE:
 * UnitDualQuaternion -> DualQuaternion is already provided by: Unit<T> -> T
 */

impl<N1, N2> SubsetOf<DualQuaternion<N2>> for DualQuaternion<N1>
where
    N1: SimdRealField,
    N2: SimdRealField + SupersetOf<N1>,
{
    #[inline]
    fn to_superset(&self) -> DualQuaternion<N2> {
        DualQuaternion::from_real_and_dual(self.real.to_superset(), self.dual.to_superset())
    }

    #[inline]
    fn is_in_subset(dq: &DualQuaternion<N2>) -> bool {
        crate::is_convertible::<_, Vector4<N1>>(&dq.real.coords)
            && crate::is_convertible::<_, Vector4<N1>>(&dq.dual.coords)
    }

    #[inline]
    fn from_superset_unchecked(dq: &DualQuaternion<N2>) -> Self {
        DualQuaternion::from_real_and_dual(
            dq.real.to_subset_unchecked(),
            dq.dual.to_subset_unchecked(),
        )
    }
}

impl<N1, N2> SubsetOf<UnitDualQuaternion<N2>> for UnitDualQuaternion<N1>
where
    N1: SimdRealField,
    N2: SimdRealField + SupersetOf<N1>,
{
    #[inline]
    fn to_superset(&self) -> UnitDualQuaternion<N2> {
        UnitDualQuaternion::new_unchecked(self.as_ref().to_superset())
    }

    #[inline]
    fn is_in_subset(dq: &UnitDualQuaternion<N2>) -> bool {
        crate::is_convertible::<_, DualQuaternion<N1>>(dq.as_ref())
    }

    #[inline]
    fn from_superset_unchecked(dq: &UnitDualQuaternion<N2>) -> Self {
        Self::new_unchecked(crate::convert_ref_unchecked(dq.as_ref()))
    }
}

impl<N1, N2> SubsetOf<Isometry3<N2>> for UnitDualQuaternion<N1>
where
    N1: RealField,
    N2: RealField + SupersetOf<N1>,
{
    #[inline]
    fn to_superset(&self) -> Isometry3<N2> {
        let dq: UnitDualQuaternion<N2> = self.to_superset();
        let iso = dq.to_isometry();
        crate::convert_unchecked(iso)
    }

    #[inline]
    fn is_in_subset(iso: &Isometry3<N2>) -> bool {
        crate::is_convertible::<_, UnitQuaternion<N1>>(&iso.rotation)
            && crate::is_convertible::<_, Translation3<N1>>(&iso.translation)
    }

    #[inline]
    fn from_superset_unchecked(iso: &Isometry3<N2>) -> Self {
        let dq = UnitDualQuaternion::<N2>::from_isometry(iso);
        crate::convert_unchecked(dq)
    }
}

impl<N1, N2> SubsetOf<Similarity3<N2>> for UnitDualQuaternion<N1>
where
    N1: RealField,
    N2: RealField + SupersetOf<N1>,
{
    #[inline]
    fn to_superset(&self) -> Similarity3<N2> {
        Similarity3::from_isometry(crate::convert_ref(self), N2::one())
    }

    #[inline]
    fn is_in_subset(sim: &Similarity3<N2>) -> bool {
        sim.scaling() == N2::one()
    }

    #[inline]
    fn from_superset_unchecked(sim: &Similarity3<N2>) -> Self {
        crate::convert_ref_unchecked(&sim.isometry)
    }
}

impl<N1, N2, C> SubsetOf<Transform<N2, U3, C>> for UnitDualQuaternion<N1>
where
    N1: RealField,
    N2: RealField + SupersetOf<N1>,
    C: SuperTCategoryOf<TAffine>,
{
    #[inline]
    fn to_superset(&self) -> Transform<N2, U3, C> {
        Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
    }

    #[inline]
    fn is_in_subset(t: &Transform<N2, U3, C>) -> bool {
        <Self as SubsetOf<_>>::is_in_subset(t.matrix())
    }

    #[inline]
    fn from_superset_unchecked(t: &Transform<N2, U3, C>) -> Self {
        Self::from_superset_unchecked(t.matrix())
    }
}

impl<N1: RealField, N2: RealField + SupersetOf<N1>> SubsetOf<Matrix4<N2>>
    for UnitDualQuaternion<N1>
{
    #[inline]
    fn to_superset(&self) -> Matrix4<N2> {
        self.to_homogeneous().to_superset()
    }

    #[inline]
    fn is_in_subset(m: &Matrix4<N2>) -> bool {
        crate::is_convertible::<_, Isometry3<N1>>(m)
    }

    #[inline]
    fn from_superset_unchecked(m: &Matrix4<N2>) -> Self {
        let iso: Isometry3<N1> = crate::convert_ref_unchecked(m);
        Self::from_isometry(&iso)
    }
}

impl<N: SimdRealField + RealField> From<UnitDualQuaternion<N>> for Matrix4<N>
where
    N::Element: SimdRealField,
{
    #[inline]
    fn from(dq: UnitDualQuaternion<N>) -> Self {
        dq.to_homogeneous()
    }
}

impl<N: SimdRealField> From<UnitDualQuaternion<N>> for Isometry3<N>
where
    N::Element: SimdRealField,
{
    #[inline]
    fn from(dq: UnitDualQuaternion<N>) -> Self {
        dq.to_isometry()
    }
}

impl<N: SimdRealField> From<Isometry3<N>> for UnitDualQuaternion<N>
where
    N::Element: SimdRealField,
{
    #[inline]
    fn from(iso: Isometry3<N>) -> Self {
        Self::from_isometry(&iso)
    }
}