nalgebra/geometry/
dual_quaternion_construction.rs

1use crate::{
2    DualQuaternion, Isometry3, Quaternion, Scalar, SimdRealField, Translation3, UnitDualQuaternion,
3    UnitQuaternion,
4};
5use num::{One, Zero};
6#[cfg(feature = "arbitrary")]
7use quickcheck::{Arbitrary, Gen};
8use simba::scalar::SupersetOf;
9
10impl<T: Scalar> DualQuaternion<T> {
11    /// Creates a dual quaternion from its rotation and translation components.
12    ///
13    /// # Example
14    /// ```
15    /// # use nalgebra::{DualQuaternion, Quaternion};
16    /// let rot = Quaternion::new(1.0, 2.0, 3.0, 4.0);
17    /// let trans = Quaternion::new(5.0, 6.0, 7.0, 8.0);
18    ///
19    /// let dq = DualQuaternion::from_real_and_dual(rot, trans);
20    /// assert_eq!(dq.real.w, 1.0);
21    /// ```
22    #[inline]
23    pub fn from_real_and_dual(real: Quaternion<T>, dual: Quaternion<T>) -> Self {
24        Self { real, dual }
25    }
26
27    /// The dual quaternion multiplicative identity.
28    ///
29    /// # Example
30    ///
31    /// ```
32    /// # use nalgebra::{DualQuaternion, Quaternion};
33    ///
34    /// let dq1 = DualQuaternion::identity();
35    /// let dq2 = DualQuaternion::from_real_and_dual(
36    ///     Quaternion::new(1.,2.,3.,4.),
37    ///     Quaternion::new(5.,6.,7.,8.)
38    /// );
39    ///
40    /// assert_eq!(dq1 * dq2, dq2);
41    /// assert_eq!(dq2 * dq1, dq2);
42    /// ```
43    #[inline]
44    pub fn identity() -> Self
45    where
46        T: SimdRealField,
47    {
48        Self::from_real_and_dual(
49            Quaternion::from_real(T::one()),
50            Quaternion::from_real(T::zero()),
51        )
52    }
53
54    /// Cast the components of `self` to another type.
55    ///
56    /// # Example
57    /// ```
58    /// # use nalgebra::{Quaternion, DualQuaternion};
59    /// let q = DualQuaternion::from_real(Quaternion::new(1.0f64, 2.0, 3.0, 4.0));
60    /// let q2 = q.cast::<f32>();
61    /// assert_eq!(q2, DualQuaternion::from_real(Quaternion::new(1.0f32, 2.0, 3.0, 4.0)));
62    /// ```
63    pub fn cast<To: Scalar>(self) -> DualQuaternion<To>
64    where
65        DualQuaternion<To>: SupersetOf<Self>,
66    {
67        crate::convert(self)
68    }
69}
70
71impl<T: SimdRealField> DualQuaternion<T>
72where
73    T::Element: SimdRealField,
74{
75    /// Creates a dual quaternion from only its real part, with no translation
76    /// component.
77    ///
78    /// # Example
79    /// ```
80    /// # use nalgebra::{DualQuaternion, Quaternion};
81    /// let rot = Quaternion::new(1.0, 2.0, 3.0, 4.0);
82    ///
83    /// let dq = DualQuaternion::from_real(rot);
84    /// assert_eq!(dq.real.w, 1.0);
85    /// assert_eq!(dq.dual.w, 0.0);
86    /// ```
87    #[inline]
88    pub fn from_real(real: Quaternion<T>) -> Self {
89        Self {
90            real,
91            dual: Quaternion::zero(),
92        }
93    }
94}
95
96impl<T: SimdRealField> One for DualQuaternion<T>
97where
98    T::Element: SimdRealField,
99{
100    #[inline]
101    fn one() -> Self {
102        Self::identity()
103    }
104}
105
106impl<T: SimdRealField> Zero for DualQuaternion<T>
107where
108    T::Element: SimdRealField,
109{
110    #[inline]
111    fn zero() -> Self {
112        DualQuaternion::from_real_and_dual(Quaternion::zero(), Quaternion::zero())
113    }
114
115    #[inline]
116    fn is_zero(&self) -> bool {
117        self.real.is_zero() && self.dual.is_zero()
118    }
119}
120
121#[cfg(feature = "arbitrary")]
122impl<T> Arbitrary for DualQuaternion<T>
123where
124    T: SimdRealField + Arbitrary + Send,
125    T::Element: SimdRealField,
126{
127    #[inline]
128    fn arbitrary(rng: &mut Gen) -> Self {
129        Self::from_real_and_dual(Arbitrary::arbitrary(rng), Arbitrary::arbitrary(rng))
130    }
131}
132
133impl<T: SimdRealField> UnitDualQuaternion<T> {
134    /// The unit dual quaternion multiplicative identity, which also represents
135    /// the identity transformation as an isometry.
136    ///
137    /// ```
138    /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3, Point3};
139    /// let ident = UnitDualQuaternion::identity();
140    /// let point = Point3::new(1.0, -4.3, 3.33);
141    ///
142    /// assert_eq!(ident * point, point);
143    /// assert_eq!(ident, ident.inverse());
144    /// ```
145    #[inline]
146    pub fn identity() -> Self {
147        Self::new_unchecked(DualQuaternion::identity())
148    }
149
150    /// Cast the components of `self` to another type.
151    ///
152    /// # Example
153    /// ```
154    /// # use nalgebra::UnitDualQuaternion;
155    /// let q = UnitDualQuaternion::<f64>::identity();
156    /// let q2 = q.cast::<f32>();
157    /// assert_eq!(q2, UnitDualQuaternion::<f32>::identity());
158    /// ```
159    pub fn cast<To: Scalar>(self) -> UnitDualQuaternion<To>
160    where
161        UnitDualQuaternion<To>: SupersetOf<Self>,
162    {
163        crate::convert(self)
164    }
165}
166
167impl<T: SimdRealField> UnitDualQuaternion<T>
168where
169    T::Element: SimdRealField,
170{
171    /// Return a dual quaternion representing the translation and orientation
172    /// given by the provided rotation quaternion and translation vector.
173    ///
174    /// ```
175    /// # #[macro_use] extern crate approx;
176    /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3, Point3};
177    /// let dq = UnitDualQuaternion::from_parts(
178    ///     Vector3::new(0.0, 3.0, 0.0).into(),
179    ///     UnitQuaternion::from_euler_angles(std::f32::consts::FRAC_PI_2, 0.0, 0.0)
180    /// );
181    /// let point = Point3::new(1.0, 2.0, 3.0);
182    ///
183    /// assert_relative_eq!(dq * point, Point3::new(1.0, 0.0, 2.0), epsilon = 1.0e-6);
184    /// ```
185    #[inline]
186    pub fn from_parts(translation: Translation3<T>, rotation: UnitQuaternion<T>) -> Self {
187        let half: T = crate::convert(0.5f64);
188        UnitDualQuaternion::new_unchecked(DualQuaternion {
189            real: rotation.clone().into_inner(),
190            dual: Quaternion::from_parts(T::zero(), translation.vector)
191                * rotation.into_inner()
192                * half,
193        })
194    }
195
196    /// Return a unit dual quaternion representing the translation and orientation
197    /// given by the provided isometry.
198    ///
199    /// ```
200    /// # #[macro_use] extern crate approx;
201    /// # use nalgebra::{Isometry3, UnitDualQuaternion, UnitQuaternion, Vector3, Point3};
202    /// let iso = Isometry3::from_parts(
203    ///     Vector3::new(0.0, 3.0, 0.0).into(),
204    ///     UnitQuaternion::from_euler_angles(std::f32::consts::FRAC_PI_2, 0.0, 0.0)
205    /// );
206    /// let dq = UnitDualQuaternion::from_isometry(&iso);
207    /// let point = Point3::new(1.0, 2.0, 3.0);
208    ///
209    /// assert_relative_eq!(dq * point, iso * point, epsilon = 1.0e-6);
210    /// ```
211    #[inline]
212    pub fn from_isometry(isometry: &Isometry3<T>) -> Self {
213        // TODO: take the isometry by-move instead of cloning it.
214        let isometry = isometry.clone();
215        UnitDualQuaternion::from_parts(isometry.translation, isometry.rotation)
216    }
217
218    /// Creates a dual quaternion from a unit quaternion rotation.
219    ///
220    /// # Example
221    /// ```
222    /// # #[macro_use] extern crate approx;
223    /// # use nalgebra::{UnitQuaternion, UnitDualQuaternion, Quaternion};
224    /// let q = Quaternion::new(1.0, 2.0, 3.0, 4.0);
225    /// let rot = UnitQuaternion::new_normalize(q);
226    ///
227    /// let dq = UnitDualQuaternion::from_rotation(rot);
228    /// assert_relative_eq!(dq.as_ref().real.norm(), 1.0, epsilon = 1.0e-6);
229    /// assert_eq!(dq.as_ref().dual.norm(), 0.0);
230    /// ```
231    #[inline]
232    pub fn from_rotation(rotation: UnitQuaternion<T>) -> Self {
233        Self::new_unchecked(DualQuaternion::from_real(rotation.into_inner()))
234    }
235}
236
237impl<T: SimdRealField> One for UnitDualQuaternion<T>
238where
239    T::Element: SimdRealField,
240{
241    #[inline]
242    fn one() -> Self {
243        Self::identity()
244    }
245}
246
247#[cfg(feature = "arbitrary")]
248impl<T> Arbitrary for UnitDualQuaternion<T>
249where
250    T: SimdRealField + Arbitrary + Send,
251    T::Element: SimdRealField,
252{
253    #[inline]
254    fn arbitrary(rng: &mut Gen) -> Self {
255        Self::new_normalize(Arbitrary::arbitrary(rng))
256    }
257}