2016-12-05 05:44:42 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::base::dimension::U4;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::base::storage::Owned;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2018-05-23 05:58:14 +08:00
|
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2021-03-02 19:25:12 +08:00
|
|
|
|
#[cfg(feature = "rand-no-std")]
|
|
|
|
|
use rand::{
|
2021-04-11 20:09:01 +08:00
|
|
|
|
distributions::{uniform::SampleUniform, Distribution, OpenClosed01, Standard, Uniform},
|
2021-03-02 19:25:12 +08:00
|
|
|
|
Rng,
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use num::{One, Zero};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2021-03-06 00:08:46 +08:00
|
|
|
|
use simba::scalar::{RealField, SupersetOf};
|
2020-12-16 22:02:02 +08:00
|
|
|
|
use simba::simd::SimdBool;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::base::dimension::U3;
|
|
|
|
|
use crate::base::storage::Storage;
|
2020-03-21 19:16:46 +08:00
|
|
|
|
use crate::base::{Matrix3, Matrix4, Unit, Vector, Vector3, Vector4};
|
2020-03-23 16:16:01 +08:00
|
|
|
|
use crate::{Scalar, SimdRealField};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::geometry::{Quaternion, Rotation3, UnitQuaternion};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2021-04-12 16:32:17 +08:00
|
|
|
|
impl<T> Quaternion<T> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Creates a quaternion from a 4D vector. The quaternion scalar part corresponds to the `w`
|
|
|
|
|
/// vector component.
|
|
|
|
|
#[inline]
|
2021-04-12 16:32:17 +08:00
|
|
|
|
// #[deprecated(note = "Use `::from` instead.")] // Don't deprecate because this one can be a const-fn.
|
|
|
|
|
pub const fn from_vector(vector: Vector4<T>) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
|
Self { coords: vector }
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new quaternion from its individual components. Note that the arguments order does
|
|
|
|
|
/// **not** follow the storage order.
|
|
|
|
|
///
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// The storage order is `[ i, j, k, w ]` while the arguments for this functions are in the
|
|
|
|
|
/// order `(w, i, j, k)`.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Quaternion, Vector4};
|
|
|
|
|
/// let q = Quaternion::new(1.0, 2.0, 3.0, 4.0);
|
|
|
|
|
/// assert!(q.i == 2.0 && q.j == 3.0 && q.k == 4.0 && q.w == 1.0);
|
|
|
|
|
/// assert_eq!(*q.as_vector(), Vector4::new(2.0, 3.0, 4.0, 1.0));
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-12 16:32:17 +08:00
|
|
|
|
pub const fn new(w: T, i: T, j: T, k: T) -> Self {
|
|
|
|
|
Self::from_vector(Vector4::new(i, j, k, w))
|
2019-02-27 10:12:30 +08:00
|
|
|
|
}
|
2021-03-06 00:08:46 +08:00
|
|
|
|
|
|
|
|
|
/// Cast the components of `self` to another type.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::Quaternion;
|
|
|
|
|
/// let q = Quaternion::new(1.0f64, 2.0, 3.0, 4.0);
|
|
|
|
|
/// let q2 = q.cast::<f32>();
|
|
|
|
|
/// assert_eq!(q2, Quaternion::new(1.0f32, 2.0, 3.0, 4.0));
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn cast<To: Scalar>(self) -> Quaternion<To>
|
|
|
|
|
where
|
2021-04-12 16:32:17 +08:00
|
|
|
|
T: Scalar,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
To: SupersetOf<T>,
|
2021-03-06 00:08:46 +08:00
|
|
|
|
{
|
|
|
|
|
crate::convert(self)
|
|
|
|
|
}
|
2020-03-23 16:16:01 +08:00
|
|
|
|
}
|
2019-02-27 10:12:30 +08:00
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> Quaternion<T> {
|
2019-02-27 10:12:30 +08:00
|
|
|
|
/// Constructs a pure quaternion.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_imag(vector: Vector3<T>) -> Self {
|
|
|
|
|
Self::from_parts(T::zero(), vector)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new quaternion from its scalar and vector parts. Note that the arguments order does
|
|
|
|
|
/// **not** follow the storage order.
|
|
|
|
|
///
|
|
|
|
|
/// The storage order is [ vector, scalar ].
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Quaternion, Vector3, Vector4};
|
|
|
|
|
/// let w = 1.0;
|
|
|
|
|
/// let ijk = Vector3::new(2.0, 3.0, 4.0);
|
|
|
|
|
/// let q = Quaternion::from_parts(w, ijk);
|
|
|
|
|
/// assert!(q.i == 2.0 && q.j == 3.0 && q.k == 4.0 && q.w == 1.0);
|
|
|
|
|
/// assert_eq!(*q.as_vector(), Vector4::new(2.0, 3.0, 4.0, 1.0));
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: take a reference to `vector`?
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_parts<SB>(scalar: T, vector: Vector<T, U3, SB>) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::new(scalar, vector[0], vector[1], vector[2])
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 16:08:42 +08:00
|
|
|
|
/// Constructs a real quaternion.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_real(r: T) -> Self {
|
2019-03-18 16:08:42 +08:00
|
|
|
|
Self::from_parts(r, Vector3::zero())
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// The quaternion multiplicative identity.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::Quaternion;
|
|
|
|
|
/// let q = Quaternion::identity();
|
|
|
|
|
/// let q2 = Quaternion::new(1.0, 2.0, 3.0, 4.0);
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(q * q2, q2);
|
|
|
|
|
/// assert_eq!(q2 * q, q2);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn identity() -> Self {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Self::from_real(T::one())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: merge with the previous block.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> Quaternion<T>
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::Element: SimdRealField,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
{
|
2020-03-21 19:16:46 +08:00
|
|
|
|
/// Creates a new quaternion from its polar decomposition.
|
|
|
|
|
///
|
|
|
|
|
/// Note that `axis` is assumed to be a unit vector.
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: take a reference to `axis`?
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_polar_decomposition<SB>(scale: T, theta: T, axis: Unit<Vector<T, U3, SB>>) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let rot = UnitQuaternion::<T>::from_axis_angle(&axis, theta * crate::convert(2.0f64));
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
|
|
|
|
rot.into_inner() * scale
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> One for Quaternion<T>
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::Element: SimdRealField,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn one() -> Self {
|
|
|
|
|
Self::identity()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> Zero for Quaternion<T>
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::Element: SimdRealField,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn zero() -> Self {
|
2019-02-27 10:12:30 +08:00
|
|
|
|
Self::from(Vector4::zero())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn is_zero(&self) -> bool {
|
|
|
|
|
self.coords.is_zero()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 19:25:12 +08:00
|
|
|
|
#[cfg(feature = "rand-no-std")]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> Distribution<Quaternion<T>> for Standard
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Standard: Distribution<T>,
|
2018-05-23 05:58:14 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Quaternion<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Quaternion::new(rng.gen(), rng.gen(), rng.gen(), rng.gen())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField + Arbitrary> Arbitrary for Quaternion<T>
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Owned<T, U4>: Send,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-03-01 00:52:14 +08:00
|
|
|
|
fn arbitrary(g: &mut Gen) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
|
Self::new(
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::arbitrary(g),
|
|
|
|
|
T::arbitrary(g),
|
|
|
|
|
T::arbitrary(g),
|
|
|
|
|
T::arbitrary(g),
|
2018-02-02 19:26:35 +08:00
|
|
|
|
)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> UnitQuaternion<T>
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::Element: SimdRealField,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
{
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// The rotation identity.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Vector3, Point3};
|
|
|
|
|
/// let q = UnitQuaternion::identity();
|
|
|
|
|
/// let q2 = UnitQuaternion::new(Vector3::new(1.0, 2.0, 3.0));
|
|
|
|
|
/// let v = Vector3::new_random();
|
|
|
|
|
/// let p = Point3::from(v);
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(q * q2, q2);
|
|
|
|
|
/// assert_eq!(q2 * q, q2);
|
|
|
|
|
/// assert_eq!(q * v, v);
|
|
|
|
|
/// assert_eq!(q * p, p);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn identity() -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Self::new_unchecked(Quaternion::identity())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 00:08:46 +08:00
|
|
|
|
/// Cast the components of `self` to another type.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::UnitQuaternion;
|
|
|
|
|
/// # use approx::assert_relative_eq;
|
|
|
|
|
/// let q = UnitQuaternion::from_euler_angles(1.0f64, 2.0, 3.0);
|
|
|
|
|
/// let q2 = q.cast::<f32>();
|
|
|
|
|
/// assert_relative_eq!(q2, UnitQuaternion::from_euler_angles(1.0f32, 2.0, 3.0), epsilon = 1.0e-6);
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn cast<To: Scalar>(self) -> UnitQuaternion<To>
|
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
To: SupersetOf<T>,
|
2021-03-06 00:08:46 +08:00
|
|
|
|
{
|
|
|
|
|
crate::convert(self)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Creates a new quaternion from a unit vector (the rotation axis) and an angle
|
|
|
|
|
/// (the rotation angle).
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Point3, Vector3};
|
|
|
|
|
/// let axis = Vector3::y_axis();
|
|
|
|
|
/// let angle = f32::consts::FRAC_PI_2;
|
|
|
|
|
/// // Point and vector being transformed in the tests.
|
|
|
|
|
/// let pt = Point3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let vec = Vector3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let q = UnitQuaternion::from_axis_angle(&axis, angle);
|
|
|
|
|
///
|
2018-12-10 23:52:19 +08:00
|
|
|
|
/// assert_eq!(q.axis().unwrap(), axis);
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_eq!(q.angle(), angle);
|
|
|
|
|
/// assert_relative_eq!(q * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// // A zero vector yields an identity.
|
|
|
|
|
/// assert_eq!(UnitQuaternion::from_scaled_axis(Vector3::<f32>::zeros()), UnitQuaternion::identity());
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_axis_angle<SB>(axis: &Unit<Vector<T, U3, SB>>, angle: T) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2020-03-22 06:22:55 +08:00
|
|
|
|
let (sang, cang) = (angle / crate::convert(2.0f64)).simd_sin_cos();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let q = Quaternion::from_parts(cang, axis.as_ref() * sang);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::new_unchecked(q)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new unit quaternion from a quaternion.
|
|
|
|
|
///
|
|
|
|
|
/// The input quaternion will be normalized.
|
2018-11-04 15:47:17 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_quaternion(q: Quaternion<T>) -> Self {
|
2018-11-04 15:47:17 +08:00
|
|
|
|
Self::new_normalize(q)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new unit quaternion from Euler angles.
|
|
|
|
|
///
|
|
|
|
|
/// The primitive rotations are applied in order: 1 roll − 2 pitch − 3 yaw.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use nalgebra::UnitQuaternion;
|
|
|
|
|
/// let rot = UnitQuaternion::from_euler_angles(0.1, 0.2, 0.3);
|
|
|
|
|
/// let euler = rot.euler_angles();
|
|
|
|
|
/// assert_relative_eq!(euler.0, 0.1, epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(euler.1, 0.2, epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(euler.2, 0.3, epsilon = 1.0e-6);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_euler_angles(roll: T, pitch: T, yaw: T) -> Self {
|
2020-03-22 06:22:55 +08:00
|
|
|
|
let (sr, cr) = (roll * crate::convert(0.5f64)).simd_sin_cos();
|
|
|
|
|
let (sp, cp) = (pitch * crate::convert(0.5f64)).simd_sin_cos();
|
|
|
|
|
let (sy, cy) = (yaw * crate::convert(0.5f64)).simd_sin_cos();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let q = Quaternion::new(
|
2018-02-02 19:26:35 +08:00
|
|
|
|
cr * cp * cy + sr * sp * sy,
|
|
|
|
|
sr * cp * cy - cr * sp * sy,
|
|
|
|
|
cr * sp * cy + sr * cp * sy,
|
|
|
|
|
cr * cp * sy - sr * sp * cy,
|
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
Self::new_unchecked(q)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 21:26:25 +08:00
|
|
|
|
/// Builds an unit quaternion from a basis assumed to be orthonormal.
|
|
|
|
|
///
|
|
|
|
|
/// In order to get a valid unit-quaternion, the input must be an
|
|
|
|
|
/// orthonormal basis, i.e., all vectors are normalized, and the are
|
|
|
|
|
/// all orthogonal to each other. These invariants are not checked
|
|
|
|
|
/// by this method.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_basis_unchecked(basis: &[Vector3<T>; 3]) -> Self {
|
2021-02-22 21:26:25 +08:00
|
|
|
|
let rot = Rotation3::from_basis_unchecked(basis);
|
|
|
|
|
Self::from_rotation_matrix(&rot)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Builds an unit quaternion from a rotation matrix.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use nalgebra::{Rotation3, UnitQuaternion, Vector3};
|
|
|
|
|
/// let axis = Vector3::y_axis();
|
|
|
|
|
/// let angle = 0.1;
|
|
|
|
|
/// let rot = Rotation3::from_axis_angle(&axis, angle);
|
|
|
|
|
/// let q = UnitQuaternion::from_rotation_matrix(&rot);
|
|
|
|
|
/// assert_relative_eq!(q.to_rotation_matrix(), rot, epsilon = 1.0e-6);
|
2018-12-10 23:52:19 +08:00
|
|
|
|
/// assert_relative_eq!(q.axis().unwrap(), rot.axis().unwrap(), epsilon = 1.0e-6);
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_relative_eq!(q.angle(), rot.angle(), epsilon = 1.0e-6);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_rotation_matrix(rotmat: &Rotation3<T>) -> Self {
|
2017-02-13 01:17:09 +08:00
|
|
|
|
// Robust matrix to quaternion transformation.
|
2019-06-12 02:56:50 +08:00
|
|
|
|
// See https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion
|
2017-02-13 01:17:09 +08:00
|
|
|
|
let tr = rotmat[(0, 0)] + rotmat[(1, 1)] + rotmat[(2, 2)];
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let quarter: T = crate::convert(0.25);
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let res = tr.simd_gt(T::zero()).if_else3(
|
2020-03-22 06:22:55 +08:00
|
|
|
|
|| {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let denom = (tr + T::one()).simd_sqrt() * crate::convert(2.0);
|
2020-03-22 06:22:55 +08:00
|
|
|
|
Quaternion::new(
|
2020-10-26 20:37:11 +08:00
|
|
|
|
quarter * denom,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
(rotmat[(2, 1)] - rotmat[(1, 2)]) / denom,
|
|
|
|
|
(rotmat[(0, 2)] - rotmat[(2, 0)]) / denom,
|
|
|
|
|
(rotmat[(1, 0)] - rotmat[(0, 1)]) / denom,
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
(
|
|
|
|
|
|| rotmat[(0, 0)].simd_gt(rotmat[(1, 1)]) & rotmat[(0, 0)].simd_gt(rotmat[(2, 2)]),
|
|
|
|
|
|| {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let denom = (T::one() + rotmat[(0, 0)] - rotmat[(1, 1)] - rotmat[(2, 2)])
|
2020-03-22 06:22:55 +08:00
|
|
|
|
.simd_sqrt()
|
|
|
|
|
* crate::convert(2.0);
|
|
|
|
|
Quaternion::new(
|
|
|
|
|
(rotmat[(2, 1)] - rotmat[(1, 2)]) / denom,
|
2020-10-26 20:37:11 +08:00
|
|
|
|
quarter * denom,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
(rotmat[(0, 1)] + rotmat[(1, 0)]) / denom,
|
|
|
|
|
(rotmat[(0, 2)] + rotmat[(2, 0)]) / denom,
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
|| rotmat[(1, 1)].simd_gt(rotmat[(2, 2)]),
|
|
|
|
|
|| {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let denom = (T::one() + rotmat[(1, 1)] - rotmat[(0, 0)] - rotmat[(2, 2)])
|
2020-03-22 06:22:55 +08:00
|
|
|
|
.simd_sqrt()
|
|
|
|
|
* crate::convert(2.0);
|
|
|
|
|
Quaternion::new(
|
|
|
|
|
(rotmat[(0, 2)] - rotmat[(2, 0)]) / denom,
|
|
|
|
|
(rotmat[(0, 1)] + rotmat[(1, 0)]) / denom,
|
2020-10-26 20:37:11 +08:00
|
|
|
|
quarter * denom,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
(rotmat[(1, 2)] + rotmat[(2, 1)]) / denom,
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
|| {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let denom = (T::one() + rotmat[(2, 2)] - rotmat[(0, 0)] - rotmat[(1, 1)])
|
2020-03-22 06:22:55 +08:00
|
|
|
|
.simd_sqrt()
|
|
|
|
|
* crate::convert(2.0);
|
|
|
|
|
Quaternion::new(
|
|
|
|
|
(rotmat[(1, 0)] - rotmat[(0, 1)]) / denom,
|
|
|
|
|
(rotmat[(0, 2)] + rotmat[(2, 0)]) / denom,
|
|
|
|
|
(rotmat[(1, 2)] + rotmat[(2, 1)]) / denom,
|
2020-10-26 20:37:11 +08:00
|
|
|
|
quarter * denom,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
);
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
|
|
Self::new_unchecked(res)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 05:41:46 +08:00
|
|
|
|
/// Builds an unit quaternion by extracting the rotation part of the given transformation `m`.
|
|
|
|
|
///
|
|
|
|
|
/// This is an iterative method. See `.from_matrix_eps` to provide mover
|
|
|
|
|
/// convergence parameters and starting solution.
|
|
|
|
|
/// This implements "A Robust Method to Extract the Rotational Part of Deformations" by Müller et al.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_matrix(m: &Matrix3<T>) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2019-02-19 05:41:46 +08:00
|
|
|
|
Rotation3::from_matrix(m).into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds an unit quaternion by extracting the rotation part of the given transformation `m`.
|
|
|
|
|
///
|
|
|
|
|
/// This implements "A Robust Method to Extract the Rotational Part of Deformations" by Müller et al.
|
|
|
|
|
///
|
|
|
|
|
/// # Parameters
|
|
|
|
|
///
|
|
|
|
|
/// * `m`: the matrix from which the rotational part is to be extracted.
|
|
|
|
|
/// * `eps`: the angular errors tolerated between the current rotation and the optimal one.
|
|
|
|
|
/// * `max_iter`: the maximum number of iterations. Loops indefinitely until convergence if set to `0`.
|
|
|
|
|
/// * `guess`: an estimate of the solution. Convergence will be significantly faster if an initial solution close
|
|
|
|
|
/// to the actual solution is provided. Can be set to `UnitQuaternion::identity()` if no other
|
|
|
|
|
/// guesses come to mind.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_matrix_eps(m: &Matrix3<T>, eps: T, max_iter: usize, guess: Self) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2019-02-19 05:41:46 +08:00
|
|
|
|
let guess = Rotation3::from(guess);
|
|
|
|
|
Rotation3::from_matrix_eps(m, eps, max_iter, guess).into()
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// The unit quaternion needed to make `a` and `b` be collinear and point toward the same
|
2020-07-16 15:27:06 +08:00
|
|
|
|
/// direction. Returns `None` if both `a` and `b` are collinear and point to opposite directions, as then the
|
|
|
|
|
/// rotation desired is not unique.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use nalgebra::{Vector3, UnitQuaternion};
|
|
|
|
|
/// let a = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// let b = Vector3::new(3.0, 1.0, 2.0);
|
2018-12-10 23:52:19 +08:00
|
|
|
|
/// let q = UnitQuaternion::rotation_between(&a, &b).unwrap();
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_relative_eq!(q * a, b);
|
|
|
|
|
/// assert_relative_eq!(q.inverse() * b, a);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn rotation_between<SB, SC>(a: &Vector<T, U3, SB>, b: &Vector<T, U3, SC>) -> Option<Self>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Self::scaled_rotation_between(a, b, T::one())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The smallest rotation needed to make `a` and `b` collinear and point toward the same
|
|
|
|
|
/// direction, raised to the power `s`.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use nalgebra::{Vector3, UnitQuaternion};
|
|
|
|
|
/// let a = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// let b = Vector3::new(3.0, 1.0, 2.0);
|
2018-12-10 23:52:19 +08:00
|
|
|
|
/// let q2 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.2).unwrap();
|
|
|
|
|
/// let q5 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.5).unwrap();
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_relative_eq!(q2 * q2 * q2 * q2 * q2 * a, b, epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q5 * q5 * a, b, epsilon = 1.0e-6);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub fn scaled_rotation_between<SB, SC>(
|
2021-04-11 17:00:38 +08:00
|
|
|
|
a: &Vector<T, U3, SB>,
|
|
|
|
|
b: &Vector<T, U3, SC>,
|
|
|
|
|
s: T,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
) -> Option<Self>
|
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: code duplication with Rotation.
|
2018-02-02 19:26:35 +08:00
|
|
|
|
if let (Some(na), Some(nb)) = (
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Unit::try_new(a.clone_owned(), T::zero()),
|
|
|
|
|
Unit::try_new(b.clone_owned(), T::zero()),
|
2018-02-02 19:26:35 +08:00
|
|
|
|
) {
|
2018-02-02 19:26:25 +08:00
|
|
|
|
Self::scaled_rotation_between_axis(&na, &nb, s)
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else {
|
2018-02-02 19:26:25 +08:00
|
|
|
|
Some(Self::identity())
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2018-02-02 19:26:25 +08:00
|
|
|
|
/// The unit quaternion needed to make `a` and `b` be collinear and point toward the same
|
|
|
|
|
/// direction.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use nalgebra::{Unit, Vector3, UnitQuaternion};
|
|
|
|
|
/// let a = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0));
|
|
|
|
|
/// let b = Unit::new_normalize(Vector3::new(3.0, 1.0, 2.0));
|
2018-12-10 23:52:19 +08:00
|
|
|
|
/// let q = UnitQuaternion::rotation_between(&a, &b).unwrap();
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_relative_eq!(q * a, b);
|
|
|
|
|
/// assert_relative_eq!(q.inverse() * b, a);
|
|
|
|
|
/// ```
|
2018-02-02 19:26:25 +08:00
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub fn rotation_between_axis<SB, SC>(
|
2021-04-11 17:00:38 +08:00
|
|
|
|
a: &Unit<Vector<T, U3, SB>>,
|
|
|
|
|
b: &Unit<Vector<T, U3, SC>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
) -> Option<Self>
|
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Self::scaled_rotation_between_axis(a, b, T::one())
|
2018-02-02 19:26:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The smallest rotation needed to make `a` and `b` collinear and point toward the same
|
|
|
|
|
/// direction, raised to the power `s`.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use nalgebra::{Unit, Vector3, UnitQuaternion};
|
|
|
|
|
/// let a = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0));
|
|
|
|
|
/// let b = Unit::new_normalize(Vector3::new(3.0, 1.0, 2.0));
|
2018-12-10 23:52:19 +08:00
|
|
|
|
/// let q2 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.2).unwrap();
|
|
|
|
|
/// let q5 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.5).unwrap();
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_relative_eq!(q2 * q2 * q2 * q2 * q2 * a, b, epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q5 * q5 * a, b, epsilon = 1.0e-6);
|
|
|
|
|
/// ```
|
2018-02-02 19:26:25 +08:00
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub fn scaled_rotation_between_axis<SB, SC>(
|
2021-04-11 17:00:38 +08:00
|
|
|
|
na: &Unit<Vector<T, U3, SB>>,
|
|
|
|
|
nb: &Unit<Vector<T, U3, SC>>,
|
|
|
|
|
s: T,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
) -> Option<Self>
|
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: code duplication with Rotation.
|
2018-02-02 19:26:25 +08:00
|
|
|
|
let c = na.cross(&nb);
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if let Some(axis) = Unit::try_new(c, T::default_epsilon()) {
|
2018-02-02 19:26:25 +08:00
|
|
|
|
let cos = na.dot(&nb);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
|
// The cosinus may be out of [-1, 1] because of inaccuracies.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if cos <= -T::one() {
|
2020-10-11 17:01:20 +08:00
|
|
|
|
None
|
2021-04-11 17:00:38 +08:00
|
|
|
|
} else if cos >= T::one() {
|
2020-10-11 17:01:20 +08:00
|
|
|
|
Some(Self::identity())
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else {
|
2020-10-11 17:01:20 +08:00
|
|
|
|
Some(Self::from_axis_angle(&axis, cos.acos() * s))
|
2018-02-02 19:26:25 +08:00
|
|
|
|
}
|
2021-04-11 17:00:38 +08:00
|
|
|
|
} else if na.dot(&nb) < T::zero() {
|
2018-02-02 19:26:25 +08:00
|
|
|
|
// PI
|
|
|
|
|
//
|
|
|
|
|
// The rotation axis is undefined but the angle not zero. This is not a
|
|
|
|
|
// simple rotation.
|
2020-10-11 17:01:20 +08:00
|
|
|
|
None
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else {
|
2018-02-02 19:26:25 +08:00
|
|
|
|
// Zero
|
|
|
|
|
Some(Self::identity())
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates an unit quaternion that corresponds to the local frame of an observer standing at the
|
|
|
|
|
/// origin and looking toward `dir`.
|
|
|
|
|
///
|
2018-11-04 15:47:17 +08:00
|
|
|
|
/// It maps the `z` axis to the direction `dir`.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// * dir - The look direction. It does not need to be normalized.
|
|
|
|
|
/// * up - The vertical direction. It does not need to be normalized.
|
|
|
|
|
/// The only requirement of this parameter is to not be collinear to `dir`. Non-collinearity
|
|
|
|
|
/// is not checked.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Vector3};
|
|
|
|
|
/// let dir = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// let up = Vector3::y();
|
|
|
|
|
///
|
2019-01-17 05:41:25 +08:00
|
|
|
|
/// let q = UnitQuaternion::face_towards(&dir, &up);
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// assert_relative_eq!(q * Vector3::z(), dir.normalize());
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn face_towards<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2019-02-19 05:41:46 +08:00
|
|
|
|
Self::from_rotation_matrix(&Rotation3::face_towards(dir, up))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 17:17:00 +08:00
|
|
|
|
/// Deprecated: Use [UnitQuaternion::face_towards] instead.
|
2020-03-21 19:16:46 +08:00
|
|
|
|
#[deprecated(note = "renamed to `face_towards`")]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn new_observer_frames<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
|
2019-01-17 17:17:00 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2019-01-17 17:17:00 +08:00
|
|
|
|
{
|
|
|
|
|
Self::face_towards(dir, up)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Builds a right-handed look-at view matrix without translation.
|
|
|
|
|
///
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// It maps the view direction `dir` to the **negative** `z` axis.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// This conforms to the common notion of right handed look-at matrix from the computer
|
|
|
|
|
/// graphics community.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// * dir − The view direction. It does not need to be normalized.
|
|
|
|
|
/// * up - A vector approximately aligned with required the vertical axis. It does not need
|
|
|
|
|
/// to be normalized. The only requirement of this parameter is to not be collinear to `dir`.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Vector3};
|
|
|
|
|
/// let dir = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// let up = Vector3::y();
|
|
|
|
|
///
|
|
|
|
|
/// let q = UnitQuaternion::look_at_rh(&dir, &up);
|
|
|
|
|
/// assert_relative_eq!(q * dir.normalize(), -Vector3::z());
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn look_at_rh<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2019-01-17 05:41:25 +08:00
|
|
|
|
Self::face_towards(&-dir, up).inverse()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds a left-handed look-at view matrix without translation.
|
|
|
|
|
///
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// It maps the view direction `dir` to the **positive** `z` axis.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// This conforms to the common notion of left handed look-at matrix from the computer
|
|
|
|
|
/// graphics community.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// * dir − The view direction. It does not need to be normalized.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// * up - A vector approximately aligned with required the vertical axis. The only
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// requirement of this parameter is to not be collinear to `dir`.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Vector3};
|
|
|
|
|
/// let dir = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// let up = Vector3::y();
|
|
|
|
|
///
|
|
|
|
|
/// let q = UnitQuaternion::look_at_lh(&dir, &up);
|
|
|
|
|
/// assert_relative_eq!(q * dir.normalize(), Vector3::z());
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn look_at_lh<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
|
|
|
|
SC: Storage<T, U3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2019-01-17 05:41:25 +08:00
|
|
|
|
Self::face_towards(dir, up).inverse()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new unit quaternion rotation from a rotation axis scaled by the rotation angle.
|
|
|
|
|
///
|
2021-04-11 17:00:38 +08:00
|
|
|
|
/// If `axisangle` has a magnitude smaller than `T::default_epsilon()`, this returns the identity rotation.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Point3, Vector3};
|
|
|
|
|
/// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
|
|
|
|
|
/// // Point and vector being transformed in the tests.
|
|
|
|
|
/// let pt = Point3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let vec = Vector3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let q = UnitQuaternion::new(axisangle);
|
|
|
|
|
///
|
|
|
|
|
/// assert_relative_eq!(q * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// // A zero vector yields an identity.
|
|
|
|
|
/// assert_eq!(UnitQuaternion::new(Vector3::<f32>::zeros()), UnitQuaternion::identity());
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn new<SB>(axisangle: Vector<T, U3, SB>) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let two: T = crate::convert(2.0f64);
|
|
|
|
|
let q = Quaternion::<T>::from_imag(axisangle / two).exp();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::new_unchecked(q)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new unit quaternion rotation from a rotation axis scaled by the rotation angle.
|
|
|
|
|
///
|
2018-05-04 17:22:24 +08:00
|
|
|
|
/// If `axisangle` has a magnitude smaller than `eps`, this returns the identity rotation.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Point3, Vector3};
|
|
|
|
|
/// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
|
|
|
|
|
/// // Point and vector being transformed in the tests.
|
|
|
|
|
/// let pt = Point3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let vec = Vector3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let q = UnitQuaternion::new_eps(axisangle, 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// assert_relative_eq!(q * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// // An almost zero vector yields an identity.
|
|
|
|
|
/// assert_eq!(UnitQuaternion::new_eps(Vector3::new(1.0e-8, 1.0e-9, 1.0e-7), 1.0e-6), UnitQuaternion::identity());
|
|
|
|
|
/// ```
|
2018-05-03 18:06:11 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn new_eps<SB>(axisangle: Vector<T, U3, SB>, eps: T) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let two: T = crate::convert(2.0f64);
|
|
|
|
|
let q = Quaternion::<T>::from_imag(axisangle / two).exp_eps(eps);
|
2018-05-03 18:06:11 +08:00
|
|
|
|
Self::new_unchecked(q)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new unit quaternion rotation from a rotation axis scaled by the rotation angle.
|
|
|
|
|
///
|
2021-04-11 17:00:38 +08:00
|
|
|
|
/// If `axisangle` has a magnitude smaller than `T::default_epsilon()`, this returns the identity rotation.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Same as `Self::new(axisangle)`.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Point3, Vector3};
|
|
|
|
|
/// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
|
|
|
|
|
/// // Point and vector being transformed in the tests.
|
|
|
|
|
/// let pt = Point3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let vec = Vector3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let q = UnitQuaternion::from_scaled_axis(axisangle);
|
|
|
|
|
///
|
|
|
|
|
/// assert_relative_eq!(q * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// // A zero vector yields an identity.
|
|
|
|
|
/// assert_eq!(UnitQuaternion::from_scaled_axis(Vector3::<f32>::zeros()), UnitQuaternion::identity());
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_scaled_axis<SB>(axisangle: Vector<T, U3, SB>) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::new(axisangle)
|
|
|
|
|
}
|
2018-05-03 18:06:11 +08:00
|
|
|
|
|
|
|
|
|
/// Creates a new unit quaternion rotation from a rotation axis scaled by the rotation angle.
|
|
|
|
|
///
|
2018-05-04 17:22:24 +08:00
|
|
|
|
/// If `axisangle` has a magnitude smaller than `eps`, this returns the identity rotation.
|
2018-11-01 16:56:57 +08:00
|
|
|
|
/// Same as `Self::new_eps(axisangle, eps)`.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion, Point3, Vector3};
|
|
|
|
|
/// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
|
|
|
|
|
/// // Point and vector being transformed in the tests.
|
|
|
|
|
/// let pt = Point3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let vec = Vector3::new(4.0, 5.0, 6.0);
|
|
|
|
|
/// let q = UnitQuaternion::from_scaled_axis_eps(axisangle, 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// assert_relative_eq!(q * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
/// assert_relative_eq!(q * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
|
|
|
|
|
///
|
|
|
|
|
/// // An almost zero vector yields an identity.
|
|
|
|
|
/// assert_eq!(UnitQuaternion::from_scaled_axis_eps(Vector3::new(1.0e-8, 1.0e-9, 1.0e-7), 1.0e-6), UnitQuaternion::identity());
|
|
|
|
|
/// ```
|
2018-05-03 18:06:11 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn from_scaled_axis_eps<SB>(axisangle: Vector<T, U3, SB>, eps: T) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
SB: Storage<T, U3>,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2018-05-03 18:06:11 +08:00
|
|
|
|
Self::new_eps(axisangle, eps)
|
|
|
|
|
}
|
2019-09-03 01:52:49 +08:00
|
|
|
|
|
2019-09-04 10:40:38 +08:00
|
|
|
|
/// Create the mean unit quaternion from a data structure implementing IntoIterator
|
|
|
|
|
/// returning unit quaternions.
|
|
|
|
|
///
|
|
|
|
|
/// The method will panic if the iterator does not return any quaternions.
|
2019-09-03 01:52:49 +08:00
|
|
|
|
///
|
|
|
|
|
/// Algorithm from: Oshman, Yaakov, and Avishy Carmi. "Attitude estimation from vector
|
|
|
|
|
/// observations using a genetic-algorithm-embedded quaternion particle filter." Journal of
|
|
|
|
|
/// Guidance, Control, and Dynamics 29.4 (2006): 879-891.
|
2020-03-21 19:16:46 +08:00
|
|
|
|
///
|
2019-09-03 01:52:49 +08:00
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
|
/// # use std::f32;
|
|
|
|
|
/// # use nalgebra::{UnitQuaternion};
|
|
|
|
|
/// let q1 = UnitQuaternion::from_euler_angles(0.0, 0.0, 0.0);
|
|
|
|
|
/// let q2 = UnitQuaternion::from_euler_angles(-0.1, 0.0, 0.0);
|
|
|
|
|
/// let q3 = UnitQuaternion::from_euler_angles(0.1, 0.0, 0.0);
|
|
|
|
|
///
|
|
|
|
|
/// let quat_vec = vec![q1, q2, q3];
|
2019-09-04 19:04:15 +08:00
|
|
|
|
/// let q_mean = UnitQuaternion::mean_of(quat_vec);
|
2019-09-03 01:52:49 +08:00
|
|
|
|
///
|
|
|
|
|
/// let euler_angles_mean = q_mean.euler_angles();
|
|
|
|
|
/// assert_relative_eq!(euler_angles_mean.0, 0.0, epsilon = 1.0e-7)
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
2020-03-22 06:22:55 +08:00
|
|
|
|
pub fn mean_of(unit_quaternions: impl IntoIterator<Item = Self>) -> Self
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: RealField,
|
2020-04-05 23:15:43 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let quaternions_matrix: Matrix4<T> = unit_quaternions
|
2019-09-04 10:40:38 +08:00
|
|
|
|
.into_iter()
|
2019-09-03 01:52:49 +08:00
|
|
|
|
.map(|q| q.as_vector() * q.as_vector().transpose())
|
|
|
|
|
.sum();
|
|
|
|
|
|
2019-09-04 10:40:38 +08:00
|
|
|
|
assert!(!quaternions_matrix.is_zero());
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
2019-09-03 01:52:49 +08:00
|
|
|
|
let eigen_matrix = quaternions_matrix
|
2021-04-11 17:00:38 +08:00
|
|
|
|
.try_symmetric_eigen(T::RealField::default_epsilon(), 10)
|
2019-09-04 10:40:38 +08:00
|
|
|
|
.expect("Quaternions matrix could not be diagonalized. This behavior should not be possible.");
|
2019-09-03 01:52:49 +08:00
|
|
|
|
|
|
|
|
|
let max_eigenvalue_index = eigen_matrix
|
|
|
|
|
.eigenvalues
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|v| *v == eigen_matrix.eigenvalues.max())
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let max_eigenvector = eigen_matrix.eigenvectors.column(max_eigenvalue_index);
|
|
|
|
|
UnitQuaternion::from_quaternion(Quaternion::new(
|
|
|
|
|
max_eigenvector[0],
|
|
|
|
|
max_eigenvector[1],
|
|
|
|
|
max_eigenvector[2],
|
|
|
|
|
max_eigenvector[3],
|
|
|
|
|
))
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> One for UnitQuaternion<T>
|
2020-04-05 23:15:43 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::Element: SimdRealField,
|
2020-03-22 06:22:55 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn one() -> Self {
|
|
|
|
|
Self::identity()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 19:25:12 +08:00
|
|
|
|
#[cfg(feature = "rand-no-std")]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdRealField> Distribution<UnitQuaternion<T>> for Standard
|
2020-03-22 06:22:55 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T::Element: SimdRealField,
|
|
|
|
|
OpenClosed01: Distribution<T>,
|
|
|
|
|
T: SampleUniform,
|
2018-05-23 05:58:14 +08:00
|
|
|
|
{
|
2018-07-08 07:03:08 +08:00
|
|
|
|
/// Generate a uniformly distributed random rotation quaternion.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> UnitQuaternion<T> {
|
2018-07-04 10:06:03 +08:00
|
|
|
|
// Ken Shoemake's Subgroup Algorithm
|
|
|
|
|
// Uniform random rotations.
|
|
|
|
|
// In D. Kirk, editor, Graphics Gems III, pages 124-132. Academic, New York, 1992.
|
|
|
|
|
let x0 = rng.sample(OpenClosed01);
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let twopi = Uniform::new(T::zero(), T::simd_two_pi());
|
2021-04-10 14:13:46 +08:00
|
|
|
|
let theta1 = rng.sample(&twopi);
|
|
|
|
|
let theta2 = rng.sample(&twopi);
|
2020-03-22 06:22:55 +08:00
|
|
|
|
let s1 = theta1.simd_sin();
|
|
|
|
|
let c1 = theta1.simd_cos();
|
|
|
|
|
let s2 = theta2.simd_sin();
|
|
|
|
|
let c2 = theta2.simd_cos();
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let r1 = (T::one() - x0).simd_sqrt();
|
2020-03-22 06:22:55 +08:00
|
|
|
|
let r2 = x0.simd_sqrt();
|
2018-07-04 10:06:03 +08:00
|
|
|
|
Unit::new_unchecked(Quaternion::new(s1 * r1, c1 * r1, s2 * r2, c2 * r2))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: RealField + Arbitrary> Arbitrary for UnitQuaternion<T>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
Owned<T, U4>: Send,
|
|
|
|
|
Owned<T, U3>: Send,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2021-03-01 00:52:14 +08:00
|
|
|
|
fn arbitrary(g: &mut Gen) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
let axisangle = Vector3::arbitrary(g);
|
2019-02-17 05:29:41 +08:00
|
|
|
|
Self::from_scaled_axis(axisangle)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-04 10:06:03 +08:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2021-04-10 12:00:12 +08:00
|
|
|
|
#[cfg(feature = "rand")]
|
2018-07-04 10:06:03 +08:00
|
|
|
|
mod tests {
|
2018-12-18 22:44:53 +08:00
|
|
|
|
extern crate rand_xorshift;
|
2018-07-04 10:06:03 +08:00
|
|
|
|
use super::*;
|
2018-12-18 22:44:53 +08:00
|
|
|
|
use rand::SeedableRng;
|
2018-07-04 10:06:03 +08:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn random_unit_quats_are_unit() {
|
2018-12-18 22:44:53 +08:00
|
|
|
|
let mut rng = rand_xorshift::XorShiftRng::from_seed([0xAB; 16]);
|
2018-07-04 10:06:03 +08:00
|
|
|
|
for _ in 0..1000 {
|
|
|
|
|
let x = rng.gen::<UnitQuaternion<f32>>();
|
2018-12-10 04:08:14 +08:00
|
|
|
|
assert!(relative_eq!(x.into_inner().norm(), 1.0))
|
2018-07-04 10:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|