2016-12-05 05:44:42 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
|
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
|
|
|
|
use core::storage::Owned;
|
|
|
|
|
#[cfg(feature = "arbitrary")]
|
|
|
|
|
use core::dimension::U4;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
use rand::{Rand, Rng};
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use num::{One, Zero};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
use alga::general::Real;
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use core::{Unit, Vector, Vector3, Vector4};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use core::storage::Storage;
|
|
|
|
|
use core::dimension::U3;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use geometry::{Quaternion, Rotation, UnitQuaternion};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real> Quaternion<N> {
|
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]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_vector(vector: Vector4<N>) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
Quaternion { 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.
|
|
|
|
|
///
|
2017-02-13 01:17:09 +08:00
|
|
|
|
/// The storage order is `[ x, y, z, w ]`.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new(w: N, x: N, y: N, z: N) -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let v = Vector4::<N>::new(x, y, z, w);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::from_vector(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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 ].
|
|
|
|
|
#[inline]
|
|
|
|
|
// FIXME: take a reference to `vector`?
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_parts<SB>(scalar: N, vector: Vector<N, U3, SB>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::new(scalar, vector[0], vector[1], vector[2])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new quaternion from its polar decomposition.
|
|
|
|
|
///
|
|
|
|
|
/// Note that `axis` is assumed to be a unit vector.
|
|
|
|
|
// FIXME: take a reference to `axis`?
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_polar_decomposition<SB>(scale: N, theta: N, axis: Unit<Vector<N, U3, SB>>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let rot = UnitQuaternion::<N>::from_axis_angle(&axis, theta * ::convert(2.0f64));
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
rot.unwrap() * scale
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The quaternion multiplicative identity.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn identity() -> Self {
|
|
|
|
|
Self::new(N::one(), N::zero(), N::zero(), N::zero())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real> One for Quaternion<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn one() -> Self {
|
|
|
|
|
Self::identity()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real> Zero for Quaternion<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn zero() -> Self {
|
|
|
|
|
Self::new(N::zero(), N::zero(), N::zero(), N::zero())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn is_zero(&self) -> bool {
|
|
|
|
|
self.coords.is_zero()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real + Rand> Rand for Quaternion<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn rand<R: Rng>(rng: &mut R) -> Self {
|
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")]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real + Arbitrary> Arbitrary for Quaternion<N>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
Owned<N, U4>: Send,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
Quaternion::new(
|
|
|
|
|
N::arbitrary(g),
|
|
|
|
|
N::arbitrary(g),
|
|
|
|
|
N::arbitrary(g),
|
|
|
|
|
N::arbitrary(g),
|
|
|
|
|
)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real> UnitQuaternion<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// The quaternion multiplicative identity.
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new quaternion from a unit vector (the rotation axis) and an angle
|
|
|
|
|
/// (the rotation angle).
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_axis_angle<SB>(axis: &Unit<Vector<N, U3, SB>>, angle: N) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
let (sang, cang) = (angle / ::convert(2.0f64)).sin_cos();
|
|
|
|
|
|
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.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_quaternion(q: Quaternion<N>) -> Self {
|
2016-12-05 05:44:42 +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.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_euler_angles(roll: N, pitch: N, yaw: N) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
let (sr, cr) = (roll * ::convert(0.5f64)).sin_cos();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
let (sp, cp) = (pitch * ::convert(0.5f64)).sin_cos();
|
2018-02-02 19:26:35 +08:00
|
|
|
|
let (sy, cy) = (yaw * ::convert(0.5f64)).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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds an unit quaternion from a rotation matrix.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_rotation_matrix(rotmat: &Rotation<N, U3>) -> Self {
|
2017-02-13 01:17:09 +08:00
|
|
|
|
// Robust matrix to quaternion transformation.
|
|
|
|
|
// See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion
|
|
|
|
|
let tr = rotmat[(0, 0)] + rotmat[(1, 1)] + rotmat[(2, 2)];
|
|
|
|
|
let res;
|
|
|
|
|
|
|
|
|
|
let _0_25: N = ::convert(0.25);
|
|
|
|
|
|
|
|
|
|
if tr > N::zero() {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
let denom = (tr + N::one()).sqrt() * ::convert(2.0);
|
|
|
|
|
res = Quaternion::new(
|
|
|
|
|
_0_25 * denom,
|
|
|
|
|
(rotmat[(2, 1)] - rotmat[(1, 2)]) / denom,
|
|
|
|
|
(rotmat[(0, 2)] - rotmat[(2, 0)]) / denom,
|
|
|
|
|
(rotmat[(1, 0)] - rotmat[(0, 1)]) / denom,
|
|
|
|
|
);
|
|
|
|
|
} else if rotmat[(0, 0)] > rotmat[(1, 1)] && rotmat[(0, 0)] > rotmat[(2, 2)] {
|
|
|
|
|
let denom = (N::one() + rotmat[(0, 0)] - rotmat[(1, 1)] - rotmat[(2, 2)]).sqrt()
|
|
|
|
|
* ::convert(2.0);
|
|
|
|
|
res = Quaternion::new(
|
|
|
|
|
(rotmat[(2, 1)] - rotmat[(1, 2)]) / denom,
|
|
|
|
|
_0_25 * denom,
|
|
|
|
|
(rotmat[(0, 1)] + rotmat[(1, 0)]) / denom,
|
|
|
|
|
(rotmat[(0, 2)] + rotmat[(2, 0)]) / denom,
|
|
|
|
|
);
|
|
|
|
|
} else if rotmat[(1, 1)] > rotmat[(2, 2)] {
|
|
|
|
|
let denom = (N::one() + rotmat[(1, 1)] - rotmat[(0, 0)] - rotmat[(2, 2)]).sqrt()
|
|
|
|
|
* ::convert(2.0);
|
|
|
|
|
res = Quaternion::new(
|
|
|
|
|
(rotmat[(0, 2)] - rotmat[(2, 0)]) / denom,
|
|
|
|
|
(rotmat[(0, 1)] + rotmat[(1, 0)]) / denom,
|
|
|
|
|
_0_25 * denom,
|
|
|
|
|
(rotmat[(1, 2)] + rotmat[(2, 1)]) / denom,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
let denom = (N::one() + rotmat[(2, 2)] - rotmat[(0, 0)] - rotmat[(1, 1)]).sqrt()
|
|
|
|
|
* ::convert(2.0);
|
|
|
|
|
res = Quaternion::new(
|
|
|
|
|
(rotmat[(1, 0)] - rotmat[(0, 1)]) / denom,
|
|
|
|
|
(rotmat[(0, 2)] + rotmat[(2, 0)]) / denom,
|
|
|
|
|
(rotmat[(1, 2)] + rotmat[(2, 1)]) / denom,
|
|
|
|
|
_0_25 * denom,
|
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
|
|
Self::new_unchecked(res)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The unit quaternion needed to make `a` and `b` be collinear and point toward the same
|
|
|
|
|
/// direction.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn rotation_between<SB, SC>(a: &Vector<N, U3, SB>, b: &Vector<N, U3, SC>) -> Option<Self>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::scaled_rotation_between(a, b, N::one())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The smallest rotation needed to make `a` and `b` collinear and point toward the same
|
|
|
|
|
/// direction, raised to the power `s`.
|
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub fn scaled_rotation_between<SB, SC>(
|
|
|
|
|
a: &Vector<N, U3, SB>,
|
|
|
|
|
b: &Vector<N, U3, SC>,
|
|
|
|
|
s: N,
|
|
|
|
|
) -> Option<Self>
|
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
// FIXME: code duplication with Rotation.
|
2018-02-02 19:26:35 +08:00
|
|
|
|
if let (Some(na), Some(nb)) = (
|
|
|
|
|
Unit::try_new(a.clone_owned(), N::zero()),
|
|
|
|
|
Unit::try_new(b.clone_owned(), N::zero()),
|
|
|
|
|
) {
|
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.
|
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub fn rotation_between_axis<SB, SC>(
|
|
|
|
|
a: &Unit<Vector<N, U3, SB>>,
|
|
|
|
|
b: &Unit<Vector<N, U3, SC>>,
|
|
|
|
|
) -> Option<Self>
|
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
2018-02-02 19:26:25 +08:00
|
|
|
|
Self::scaled_rotation_between_axis(a, b, N::one())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The smallest rotation needed to make `a` and `b` collinear and point toward the same
|
|
|
|
|
/// direction, raised to the power `s`.
|
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub fn scaled_rotation_between_axis<SB, SC>(
|
|
|
|
|
na: &Unit<Vector<N, U3, SB>>,
|
|
|
|
|
nb: &Unit<Vector<N, U3, SC>>,
|
|
|
|
|
s: N,
|
|
|
|
|
) -> Option<Self>
|
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
2018-02-02 19:26:25 +08:00
|
|
|
|
// FIXME: code duplication with Rotation.
|
|
|
|
|
let c = na.cross(&nb);
|
|
|
|
|
|
|
|
|
|
if let Some(axis) = Unit::try_new(c, N::default_epsilon()) {
|
|
|
|
|
let cos = na.dot(&nb);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2018-02-02 19:26:25 +08:00
|
|
|
|
// The cosinus may be out of [-1, 1] because of innacuracies.
|
|
|
|
|
if cos <= -N::one() {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
return None;
|
|
|
|
|
} else if cos >= N::one() {
|
|
|
|
|
return Some(Self::identity());
|
|
|
|
|
} else {
|
|
|
|
|
return Some(Self::from_axis_angle(&axis, cos.acos() * s));
|
2018-02-02 19:26:25 +08:00
|
|
|
|
}
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else if na.dot(&nb) < N::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.
|
|
|
|
|
return 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`.
|
|
|
|
|
///
|
|
|
|
|
/// It maps the view direction `dir` to the positive `z` axis.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * dir - The look direction, that is, direction the matrix `z` axis will be aligned with.
|
|
|
|
|
/// * up - The vertical direction. The only requirement of this parameter is to not be
|
|
|
|
|
/// collinear
|
|
|
|
|
/// to `dir`. Non-collinearity is not checked.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn new_observer_frame<SB, SC>(dir: &Vector<N, U3, SB>, up: &Vector<N, U3, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Self::from_rotation_matrix(&Rotation::<N, U3>::new_observer_frame(dir, up))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds a right-handed look-at view matrix without translation.
|
|
|
|
|
///
|
|
|
|
|
/// This conforms to the common notion of right handed look-at matrix from the computer
|
|
|
|
|
/// graphics community.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * eye - The eye position.
|
|
|
|
|
/// * target - The target position.
|
|
|
|
|
/// * up - A vector approximately aligned with required the vertical axis. The only
|
|
|
|
|
/// requirement of this parameter is to not be collinear to `target - eye`.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn look_at_rh<SB, SC>(dir: &Vector<N, U3, SB>, up: &Vector<N, U3, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::new_observer_frame(&-dir, up).inverse()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds a left-handed look-at view matrix without translation.
|
|
|
|
|
///
|
|
|
|
|
/// This conforms to the common notion of left handed look-at matrix from the computer
|
|
|
|
|
/// graphics community.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * eye - The eye position.
|
|
|
|
|
/// * target - The target position.
|
|
|
|
|
/// * up - A vector approximately aligned with required the vertical axis. The only
|
|
|
|
|
/// requirement of this parameter is to not be collinear to `target - eye`.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn look_at_lh<SB, SC>(dir: &Vector<N, U3, SB>, up: &Vector<N, U3, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
SC: Storage<N, U3>,
|
|
|
|
|
{
|
|
|
|
|
Self::new_observer_frame(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.
|
|
|
|
|
///
|
2018-05-04 17:22:24 +08:00
|
|
|
|
/// If `axisangle` has a magnitude smaller than `N::default_epsilon()`, this returns the identity rotation.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn new<SB>(axisangle: Vector<N, U3, SB>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
let two: N = ::convert(2.0f64);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let q = Quaternion::<N>::from_parts(N::zero(), 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-05-03 18:06:11 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new_eps<SB>(axisangle: Vector<N, U3, SB>, eps: N) -> Self
|
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
|
|
|
|
let two: N = ::convert(2.0f64);
|
|
|
|
|
let q = Quaternion::<N>::from_parts(N::zero(), axisangle / two).exp_eps(eps);
|
|
|
|
|
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 `N::default_epsilon()`, this returns the identity rotation.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Same as `Self::new(axisangle)`.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_scaled_axis<SB>(axisangle: Vector<N, U3, SB>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
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-05-03 18:06:11 +08:00
|
|
|
|
/// Same as `Self::new(axisangle)`.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_scaled_axis_eps<SB>(axisangle: Vector<N, U3, SB>, eps: N) -> Self
|
|
|
|
|
where
|
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
|
{
|
|
|
|
|
Self::new_eps(axisangle, eps)
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real> One for UnitQuaternion<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn one() -> Self {
|
|
|
|
|
Self::identity()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real + Rand> Rand for UnitQuaternion<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn rand<R: Rng>(rng: &mut R) -> Self {
|
|
|
|
|
let axisangle = Vector3::rand(rng);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
UnitQuaternion::from_scaled_axis(axisangle)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Real + Arbitrary> Arbitrary for UnitQuaternion<N>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
Owned<N, U4>: Send,
|
|
|
|
|
Owned<N, U3>: Send,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
|
|
|
|
let axisangle = Vector3::arbitrary(g);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
UnitQuaternion::from_scaled_axis(axisangle)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|