2014-05-26 17:10:04 +00:00
|
|
|
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
|
2015-03-14 02:49:46 +00:00
|
|
|
// refer to the Cargo.toml file at the top-level directory of this distribution.
|
2013-07-11 23:18:05 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-04-05 01:19:11 +00:00
|
|
|
use std::f64;
|
2013-10-19 14:00:44 +00:00
|
|
|
use std::fmt;
|
2014-05-28 01:59:03 +00:00
|
|
|
use std::mem;
|
2015-01-03 21:29:26 +00:00
|
|
|
use std::ops::*;
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-03-15 02:53:57 +00:00
|
|
|
use rand::{Rand, Rng};
|
2015-09-29 11:36:57 +00:00
|
|
|
use rust_num::{Float, One, Zero};
|
2015-04-25 03:28:31 +00:00
|
|
|
use rust_num::traits::cast;
|
2015-03-15 02:53:57 +00:00
|
|
|
|
2014-10-14 00:10:54 +00:00
|
|
|
use angle::{Angle, Rad, acos, sin, sin_cos, rad};
|
2014-05-28 01:59:03 +00:00
|
|
|
use approx::ApproxEq;
|
2015-05-06 08:27:52 +00:00
|
|
|
use matrix::{Matrix3, Matrix4};
|
2015-04-05 01:19:11 +00:00
|
|
|
use num::BaseFloat;
|
2014-05-28 01:59:03 +00:00
|
|
|
use point::Point3;
|
2015-05-06 08:35:09 +00:00
|
|
|
use rotation::{Rotation, Rotation3, Basis3};
|
2014-04-14 01:30:24 +00:00
|
|
|
use vector::{Vector3, Vector, EuclideanVector};
|
2013-07-11 23:18:05 +00:00
|
|
|
|
2015-03-15 02:53:57 +00:00
|
|
|
|
2014-05-25 08:29:19 +00:00
|
|
|
/// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector
|
|
|
|
/// form.
|
2015-02-08 18:25:42 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
2015-09-20 15:32:53 +00:00
|
|
|
pub struct Quaternion<S> {
|
|
|
|
pub s: S,
|
|
|
|
pub v: Vector3<S>,
|
|
|
|
}
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2014-05-26 17:10:04 +00:00
|
|
|
impl<S: BaseFloat> Quaternion<S> {
|
2013-08-26 05:08:25 +00:00
|
|
|
/// Construct a new quaternion from one scalar component and three
|
|
|
|
/// imaginary components
|
|
|
|
#[inline]
|
2014-04-14 01:30:24 +00:00
|
|
|
pub fn new(w: S, xi: S, yj: S, zk: S) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(w, Vector3::new(xi, yj, zk))
|
2013-08-26 05:08:25 +00:00
|
|
|
}
|
2013-07-12 01:22:14 +00:00
|
|
|
|
2013-08-26 05:08:25 +00:00
|
|
|
/// Construct a new quaternion from a scalar and a vector
|
2013-07-11 23:18:05 +00:00
|
|
|
#[inline]
|
2014-04-14 01:30:24 +00:00
|
|
|
pub fn from_sv(s: S, v: Vector3<S>) -> Quaternion<S> {
|
|
|
|
Quaternion { s: s, v: v }
|
2013-07-11 23:18:05 +00:00
|
|
|
}
|
2013-09-03 07:28:43 +00:00
|
|
|
|
|
|
|
/// The additive identity, ie: `q = 0 + 0i + 0j + 0i`
|
|
|
|
#[inline]
|
2014-04-14 01:30:24 +00:00
|
|
|
pub fn zero() -> Quaternion<S> {
|
2015-09-29 11:36:57 +00:00
|
|
|
Quaternion::new(S::zero(), S::zero(), S::zero(), S::zero())
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
|
|
|
|
#[inline]
|
2015-10-01 08:52:44 +00:00
|
|
|
pub fn one() -> Quaternion<S> {
|
2015-09-29 11:36:57 +00:00
|
|
|
Quaternion::from_sv(S::one(), Vector3::zero())
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
/// The dot product of the quaternion and `q`.
|
2013-09-03 07:28:43 +00:00
|
|
|
#[inline]
|
2015-12-12 07:39:31 +00:00
|
|
|
pub fn dot(self, other: Quaternion<S>) -> S {
|
|
|
|
self.s * other.s + self.v.dot(other.v)
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 08:29:19 +00:00
|
|
|
/// The conjugate of the quaternion.
|
2013-09-03 07:28:43 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn conjugate(self) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(self.s, -self.v)
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The squared magnitude of the quaternion. This is useful for
|
|
|
|
/// magnitude comparisons where the exact magnitude does not need to be
|
|
|
|
/// calculated.
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn magnitude2(self) -> S {
|
2013-09-03 07:28:43 +00:00
|
|
|
self.s * self.s + self.v.length2()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The magnitude of the quaternion
|
|
|
|
///
|
|
|
|
/// # Performance notes
|
|
|
|
///
|
|
|
|
/// For instances where the exact magnitude of the quaternion does not need
|
|
|
|
/// to be known, for example for quaternion-quaternion magnitude comparisons,
|
|
|
|
/// it is advisable to use the `magnitude2` method instead.
|
|
|
|
#[inline]
|
|
|
|
pub fn magnitude(&self) -> S {
|
2014-04-02 09:24:04 +00:00
|
|
|
self.magnitude2().sqrt()
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 08:29:19 +00:00
|
|
|
/// Normalize this quaternion, returning the new quaternion.
|
2013-09-03 07:28:43 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn normalize(self) -> Quaternion<S> {
|
2015-09-30 08:05:20 +00:00
|
|
|
self * (S::one() / self.magnitude())
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 08:29:19 +00:00
|
|
|
/// Do a normalized linear interpolation with `other`, by `amount`.
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn nlerp(self, other: Quaternion<S>, amount: S) -> Quaternion<S> {
|
2015-12-12 07:39:31 +00:00
|
|
|
(self * (S::one() - amount) + other * amount).normalize()
|
2015-09-30 08:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl<S: BaseFloat> Neg for Quaternion<S> {
|
2015-11-08 06:41:57 +00:00
|
|
|
type Output = Quaternion<S>;
|
|
|
|
|
|
|
|
#[inline]
|
2015-12-12 07:39:31 +00:00
|
|
|
fn neg(self) -> Quaternion<S> { Quaternion::from_sv(-self.s, -self.v) }
|
2015-11-08 06:41:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl<'a, S: BaseFloat> Neg for &'a Quaternion<S> {
|
2015-09-30 08:05:20 +00:00
|
|
|
type Output = Quaternion<S>;
|
|
|
|
|
|
|
|
#[inline]
|
2015-12-12 07:39:31 +00:00
|
|
|
fn neg(self) -> Quaternion<S> { Quaternion::from_sv(-self.s, -self.v) }
|
2015-11-08 06:41:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
/// Generates a binary operator implementation for the permutations of by-ref and by-val
|
|
|
|
macro_rules! impl_binary_operator {
|
|
|
|
// When the right operand is a scalar
|
|
|
|
(<$S:ident> $Binop:ident<$Rhs:ident> for $Lhs:ty { fn $binop:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr } }) => {
|
|
|
|
impl<$S: BaseFloat> $Binop<$Rhs> for $Lhs {
|
|
|
|
type Output = $Output;
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: $Rhs) -> $Output {
|
|
|
|
let ($lhs, $rhs) = (self, other); $body
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl<'a, $S: BaseFloat> $Binop<$Rhs> for &'a $Lhs {
|
|
|
|
type Output = $Output;
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: $Rhs) -> $Output {
|
|
|
|
let ($lhs, $rhs) = (self, other); $body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// When the right operand is a compound type
|
|
|
|
(<$S:ident> $Binop:ident<$Rhs:ty> for $Lhs:ty { fn $binop:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr } }) => {
|
|
|
|
impl<$S: BaseFloat> $Binop<$Rhs> for $Lhs {
|
|
|
|
type Output = $Output;
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: $Rhs) -> $Output {
|
|
|
|
let ($lhs, $rhs) = (self, other); $body
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl<'a, $S: BaseFloat> $Binop<&'a $Rhs> for $Lhs {
|
|
|
|
type Output = $Output;
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: &'a $Rhs) -> $Output {
|
|
|
|
let ($lhs, $rhs) = (self, other); $body
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl<'a, $S: BaseFloat> $Binop<$Rhs> for &'a $Lhs {
|
|
|
|
type Output = $Output;
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: $Rhs) -> $Output {
|
|
|
|
let ($lhs, $rhs) = (self, other); $body
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl<'a, 'b, $S: BaseFloat> $Binop<&'a $Rhs> for &'b $Lhs {
|
|
|
|
type Output = $Output;
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: &'a $Rhs) -> $Output {
|
|
|
|
let ($lhs, $rhs) = (self, other); $body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-09-30 08:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl_binary_operator!(<S> Mul<S> for Quaternion<S> {
|
|
|
|
fn mul(lhs, rhs) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs)
|
2015-09-30 08:05:20 +00:00
|
|
|
}
|
2015-12-12 07:39:31 +00:00
|
|
|
});
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl_binary_operator!(<S> Div<S> for Quaternion<S> {
|
|
|
|
fn div(lhs, rhs) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs)
|
2015-09-30 08:05:20 +00:00
|
|
|
}
|
2015-12-12 07:39:31 +00:00
|
|
|
});
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl_binary_operator!(<S> Mul<Vector3<S> > for Quaternion<S> {
|
|
|
|
fn mul(lhs, rhs) -> Vector3<S> {{
|
|
|
|
let rhs = rhs.clone();
|
|
|
|
let two: S = cast(2i8).unwrap();
|
|
|
|
let tmp = lhs.v.cross(rhs) + (rhs * lhs.s);
|
|
|
|
(lhs.v.cross(tmp) * two) + rhs
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
impl_binary_operator!(<S> Add<Quaternion<S> > for Quaternion<S> {
|
|
|
|
fn add(lhs, rhs) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(lhs.s + rhs.s, lhs.v + rhs.v)
|
|
|
|
}
|
|
|
|
});
|
2015-09-30 08:05:20 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
impl_binary_operator!(<S> Sub<Quaternion<S> > for Quaternion<S> {
|
|
|
|
fn sub(lhs, rhs) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v)
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
2015-12-12 07:39:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
impl_binary_operator!(<S> Mul<Quaternion<S> > for Quaternion<S> {
|
|
|
|
fn mul(lhs, rhs) -> Quaternion<S> {
|
|
|
|
Quaternion::new(lhs.s * rhs.s - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z,
|
|
|
|
lhs.s * rhs.v.x + lhs.v.x * rhs.s + lhs.v.y * rhs.v.z - lhs.v.z * rhs.v.y,
|
|
|
|
lhs.s * rhs.v.y + lhs.v.y * rhs.s + lhs.v.z * rhs.v.x - lhs.v.x * rhs.v.z,
|
|
|
|
lhs.s * rhs.v.z + lhs.v.z * rhs.s + lhs.v.x * rhs.v.y - lhs.v.y * rhs.v.x)
|
|
|
|
}
|
|
|
|
});
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-11-03 03:00:39 +00:00
|
|
|
impl<S: BaseFloat> ApproxEq for Quaternion<S> {
|
|
|
|
type Epsilon = S;
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
#[inline]
|
|
|
|
fn approx_eq_eps(&self, other: &Quaternion<S>, epsilon: &S) -> bool {
|
|
|
|
self.s.approx_eq_eps(&other.s, epsilon) &&
|
|
|
|
self.v.approx_eq_eps(&other.v, epsilon)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 17:10:04 +00:00
|
|
|
impl<S: BaseFloat> Quaternion<S> {
|
2013-09-03 07:28:43 +00:00
|
|
|
/// Spherical Linear Intoperlation
|
|
|
|
///
|
2014-05-25 08:29:19 +00:00
|
|
|
/// Return the spherical linear interpolation between the quaternion and
|
2013-09-03 07:28:43 +00:00
|
|
|
/// `other`. Both quaternions should be normalized first.
|
|
|
|
///
|
|
|
|
/// # Performance notes
|
|
|
|
///
|
2014-05-25 08:29:19 +00:00
|
|
|
/// The `acos` operation used in `slerp` is an expensive operation, so
|
|
|
|
/// unless your quarternions are far away from each other it's generally
|
|
|
|
/// more advisable to use `nlerp` when you know your rotations are going
|
|
|
|
/// to be small.
|
2013-09-03 07:28:43 +00:00
|
|
|
///
|
|
|
|
/// - [Understanding Slerp, Then Not Using It]
|
|
|
|
/// (http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/)
|
|
|
|
/// - [Arcsynthesis OpenGL tutorial]
|
|
|
|
/// (http://www.arcsynthesis.org/gltut/Positioning/Tut08%20Interpolation.html)
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn slerp(self, other: Quaternion<S>, amount: S) -> Quaternion<S> {
|
2013-09-03 07:28:43 +00:00
|
|
|
let dot = self.dot(other);
|
2014-06-26 04:26:15 +00:00
|
|
|
let dot_threshold = cast(0.9995f64).unwrap();
|
2013-09-03 07:28:43 +00:00
|
|
|
|
|
|
|
// if quaternions are close together use `nlerp`
|
|
|
|
if dot > dot_threshold {
|
|
|
|
self.nlerp(other, amount)
|
|
|
|
} else {
|
|
|
|
// stay within the domain of acos()
|
2014-02-15 22:22:21 +00:00
|
|
|
// TODO REMOVE WHEN https://github.com/mozilla/rust/issues/12068 IS RESOLVED
|
2015-09-29 11:36:57 +00:00
|
|
|
let robust_dot = if dot > S::one() {
|
|
|
|
S::one()
|
|
|
|
} else if dot < -S::one() {
|
|
|
|
-S::one()
|
2014-02-15 22:22:21 +00:00
|
|
|
} else {
|
|
|
|
dot
|
|
|
|
};
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2013-10-12 19:54:24 +00:00
|
|
|
let theta: Rad<S> = acos(robust_dot.clone());
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-09-29 11:36:57 +00:00
|
|
|
let scale1 = sin(theta.mul_s(S::one() - amount));
|
2013-10-12 19:54:24 +00:00
|
|
|
let scale2 = sin(theta.mul_s(amount));
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-12-12 07:39:31 +00:00
|
|
|
(self * scale1 + other * scale2) * sin(theta).recip()
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-14 00:10:54 +00:00
|
|
|
|
|
|
|
/// Convert a Quaternion to Eular angles
|
|
|
|
/// This is a polar singularity aware conversion
|
2014-11-15 14:59:43 +00:00
|
|
|
///
|
2014-10-14 00:10:54 +00:00
|
|
|
/// Based on:
|
|
|
|
/// - [Maths - Conversion Quaternion to Euler]
|
|
|
|
/// (http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/)
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn to_euler(self) -> (Rad<S>, Rad<S>, Rad<S>) {
|
2014-10-14 00:10:54 +00:00
|
|
|
let sig: S = cast(0.499f64).unwrap();
|
|
|
|
let two: S = cast(2f64).unwrap();
|
|
|
|
let one: S = cast(1f64).unwrap();
|
|
|
|
|
|
|
|
let (qw, qx, qy, qz) = (self.s, self.v.x, self.v.y, self.v.z);
|
2015-09-30 08:05:20 +00:00
|
|
|
let (sqw, sqx, sqy, sqz) = (qw * qw, qx * qx, qy * qy, qz * qz);
|
2014-10-14 00:10:54 +00:00
|
|
|
|
|
|
|
let unit = sqx + sqy + sqz + sqw;
|
2015-09-30 08:05:20 +00:00
|
|
|
let test = qx * qy + qz * qw;
|
2014-11-15 14:59:43 +00:00
|
|
|
|
2014-10-14 00:10:54 +00:00
|
|
|
if test > sig * unit {
|
|
|
|
(
|
2015-09-29 11:36:57 +00:00
|
|
|
rad(S::zero()),
|
2014-11-25 04:04:34 +00:00
|
|
|
rad(cast(f64::consts::FRAC_PI_2).unwrap()),
|
2014-10-14 00:10:54 +00:00
|
|
|
rad(two * qx.atan2(qw)),
|
|
|
|
)
|
|
|
|
} else if test < -sig * unit {
|
2014-11-25 04:04:34 +00:00
|
|
|
let y: S = cast(f64::consts::FRAC_PI_2).unwrap();
|
2014-10-14 00:10:54 +00:00
|
|
|
(
|
2015-09-29 11:36:57 +00:00
|
|
|
rad(S::zero()),
|
2014-10-14 00:10:54 +00:00
|
|
|
rad(-y),
|
|
|
|
rad(two * qx.atan2(qw)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
2015-09-30 08:05:20 +00:00
|
|
|
rad((two * (qy * qw - qx * qz)).atan2(one - two * (sqy + sqz))),
|
|
|
|
rad((two * (qx * qy + qz * qw)).asin()),
|
|
|
|
rad((two * (qx * qw - qy * qz)).atan2(one - two * (sqx + sqz))),
|
2014-10-14 00:10:54 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
impl<S: BaseFloat> From<Quaternion<S>> for Matrix3<S> {
|
2013-09-03 07:28:43 +00:00
|
|
|
/// Convert the quaternion to a 3 x 3 rotation matrix
|
2015-05-06 08:27:52 +00:00
|
|
|
fn from(quat: Quaternion<S>) -> Matrix3<S> {
|
|
|
|
let x2 = quat.v.x + quat.v.x;
|
|
|
|
let y2 = quat.v.y + quat.v.y;
|
|
|
|
let z2 = quat.v.z + quat.v.z;
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
let xx2 = x2 * quat.v.x;
|
|
|
|
let xy2 = x2 * quat.v.y;
|
|
|
|
let xz2 = x2 * quat.v.z;
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
let yy2 = y2 * quat.v.y;
|
|
|
|
let yz2 = y2 * quat.v.z;
|
|
|
|
let zz2 = z2 * quat.v.z;
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
let sy2 = y2 * quat.s;
|
|
|
|
let sz2 = z2 * quat.s;
|
|
|
|
let sx2 = x2 * quat.s;
|
2013-09-03 07:28:43 +00:00
|
|
|
|
2015-09-29 11:36:57 +00:00
|
|
|
Matrix3::new(S::one() - yy2 - zz2, xy2 + sz2, xz2 - sy2,
|
|
|
|
xy2 - sz2, S::one() - xx2 - zz2, yz2 + sx2,
|
|
|
|
xz2 + sy2, yz2 - sx2, S::one() - xx2 - yy2)
|
2013-09-03 07:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
impl<S: BaseFloat> From<Quaternion<S>> for Matrix4<S> {
|
2014-01-29 02:01:57 +00:00
|
|
|
/// Convert the quaternion to a 4 x 4 rotation matrix
|
2015-05-06 08:27:52 +00:00
|
|
|
fn from(quat: Quaternion<S>) -> Matrix4<S> {
|
|
|
|
let x2 = quat.v.x + quat.v.x;
|
|
|
|
let y2 = quat.v.y + quat.v.y;
|
|
|
|
let z2 = quat.v.z + quat.v.z;
|
|
|
|
|
|
|
|
let xx2 = x2 * quat.v.x;
|
|
|
|
let xy2 = x2 * quat.v.y;
|
|
|
|
let xz2 = x2 * quat.v.z;
|
|
|
|
|
|
|
|
let yy2 = y2 * quat.v.y;
|
|
|
|
let yz2 = y2 * quat.v.z;
|
|
|
|
let zz2 = z2 * quat.v.z;
|
|
|
|
|
|
|
|
let sy2 = y2 * quat.s;
|
|
|
|
let sz2 = z2 * quat.s;
|
|
|
|
let sx2 = x2 * quat.s;
|
2014-01-29 02:01:57 +00:00
|
|
|
|
2015-09-29 11:36:57 +00:00
|
|
|
Matrix4::new(S::one() - yy2 - zz2, xy2 + sz2, xz2 - sy2, S::zero(),
|
|
|
|
xy2 - sz2, S::one() - xx2 - zz2, yz2 + sx2, S::zero(),
|
|
|
|
xz2 + sy2, yz2 - sx2, S::one() - xx2 - yy2, S::zero(),
|
|
|
|
S::zero(), S::zero(), S::zero(), S::one())
|
2014-01-29 02:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:19:32 +00:00
|
|
|
impl<S: BaseFloat> fmt::Debug for Quaternion<S> {
|
2014-02-25 08:56:22 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-09 22:06:45 +00:00
|
|
|
write!(f, "{:?} + {:?}i + {:?}j + {:?}k",
|
2014-02-25 08:56:22 +00:00
|
|
|
self.s,
|
|
|
|
self.v.x,
|
|
|
|
self.v.y,
|
|
|
|
self.v.z)
|
2013-09-03 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 00:24:34 +00:00
|
|
|
|
|
|
|
// Quaternion Rotation impls
|
|
|
|
|
2015-05-06 08:35:09 +00:00
|
|
|
impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> {
|
2014-01-30 00:24:34 +00:00
|
|
|
#[inline]
|
2015-05-06 08:35:09 +00:00
|
|
|
fn from(quat: Quaternion<S>) -> Basis3<S> { Basis3::from_quaternion(&quat) }
|
2014-01-30 00:24:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat> Rotation<Point3<S>> for Quaternion<S> {
|
2014-01-30 00:24:34 +00:00
|
|
|
#[inline]
|
2015-10-01 08:52:44 +00:00
|
|
|
fn one() -> Quaternion<S> { Quaternion::one() }
|
2014-01-30 00:24:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn look_at(dir: Vector3<S>, up: Vector3<S>) -> Quaternion<S> {
|
2015-05-06 08:38:15 +00:00
|
|
|
Matrix3::look_at(dir, up).into()
|
2014-01-30 00:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn between_vectors(a: Vector3<S>, b: Vector3<S>) -> Quaternion<S> {
|
2014-01-30 00:24:34 +00:00
|
|
|
//http://stackoverflow.com/questions/1171849/
|
|
|
|
//finding-quaternion-representing-the-rotation-from-one-vector-to-another
|
2015-09-29 11:36:57 +00:00
|
|
|
Quaternion::from_sv(S::one() + a.dot(b), a.cross(b)).normalize()
|
2014-01-30 00:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-12-12 07:39:31 +00:00
|
|
|
fn rotate_vector(&self, vec: Vector3<S>) -> Vector3<S> { self * vec }
|
2014-01-30 00:24:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-09-30 08:05:20 +00:00
|
|
|
fn concat(&self, other: &Quaternion<S>) -> Quaternion<S> { self * other }
|
2014-01-30 00:24:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-09-30 08:05:20 +00:00
|
|
|
fn concat_self(&mut self, other: &Quaternion<S>) { *self = &*self * other; }
|
2014-01-30 00:24:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-12-12 07:39:31 +00:00
|
|
|
fn invert(&self) -> Quaternion<S> { self.conjugate() / self.magnitude2() }
|
2014-01-30 00:24:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn invert_self(&mut self) { *self = self.invert() }
|
|
|
|
}
|
|
|
|
|
2014-12-15 12:49:57 +00:00
|
|
|
impl<S: BaseFloat> Rotation3<S> for Quaternion<S> where S: 'static {
|
2014-01-30 00:24:34 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Quaternion<S> {
|
2014-06-26 04:26:15 +00:00
|
|
|
let (s, c) = sin_cos(angle.mul_s(cast(0.5f64).unwrap()));
|
2014-04-14 01:30:24 +00:00
|
|
|
Quaternion::from_sv(c, axis.mul_s(s))
|
2014-01-30 00:24:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:10:54 +00:00
|
|
|
/// - [Maths - Conversion Euler to Quaternion]
|
|
|
|
/// (http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm)
|
2014-04-14 01:30:24 +00:00
|
|
|
fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Quaternion<S> {
|
2014-10-14 00:10:54 +00:00
|
|
|
let (s1, c1) = sin_cos(x.mul_s(cast(0.5f64).unwrap()));
|
|
|
|
let (s2, c2) = sin_cos(y.mul_s(cast(0.5f64).unwrap()));
|
|
|
|
let (s3, c3) = sin_cos(z.mul_s(cast(0.5f64).unwrap()));
|
|
|
|
|
|
|
|
Quaternion::new(c1 * c2 * c3 - s1 * s2 * s3,
|
|
|
|
s1 * s2 * c3 + c1 * c2 * s3,
|
|
|
|
s1 * c2 * c3 + c1 * s2 * s3,
|
|
|
|
c1 * s2 * c3 - s1 * c2 * s3)
|
2014-01-30 00:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-15 02:53:57 +00:00
|
|
|
|
2015-09-20 15:32:53 +00:00
|
|
|
impl<S: BaseFloat> Into<[S; 4]> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn into(self) -> [S; 4] {
|
|
|
|
match self.into() { (w, xi, yj, zk) => [w, xi, yj, zk] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> AsRef<[S; 4]> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &[S; 4] {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> AsMut<[S; 4]> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn as_mut(&mut self) -> &mut [S; 4] {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> From<[S; 4]> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn from(v: [S; 4]) -> Quaternion<S> {
|
|
|
|
Quaternion::new(v[0], v[1], v[2], v[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: BaseFloat> From<&'a [S; 4]> for &'a Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn from(v: &'a [S; 4]) -> &'a Quaternion<S> {
|
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: BaseFloat> From<&'a mut [S; 4]> for &'a mut Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn from(v: &'a mut [S; 4]) -> &'a mut Quaternion<S> {
|
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> Into<(S, S, S, S)> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn into(self) -> (S, S, S, S) {
|
|
|
|
match self { Quaternion { s, v: Vector3 { x, y, z } } => (s, x, y, z) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> AsRef<(S, S, S, S)> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &(S, S, S, S) {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> AsMut<(S, S, S, S)> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn as_mut(&mut self) -> &mut (S, S, S, S) {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseFloat> From<(S, S, S, S)> for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn from(v: (S, S, S, S)) -> Quaternion<S> {
|
|
|
|
match v { (w, xi, yj, zk) => Quaternion::new(w, xi, yj, zk) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: BaseFloat> From<&'a (S, S, S, S)> for &'a Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn from(v: &'a (S, S, S, S)) -> &'a Quaternion<S> {
|
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: BaseFloat> From<&'a mut (S, S, S, S)> for &'a mut Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn from(v: &'a mut (S, S, S, S)) -> &'a mut Quaternion<S> {
|
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:56:03 +00:00
|
|
|
macro_rules! index_operators {
|
|
|
|
($S:ident, $Output:ty, $I:ty) => {
|
|
|
|
impl<$S: BaseFloat> Index<$I> for Quaternion<$S> {
|
|
|
|
type Output = $Output;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index<'a>(&'a self, i: $I) -> &'a $Output {
|
|
|
|
let v: &[$S; 4] = self.as_ref(); &v[i]
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 15:32:53 +00:00
|
|
|
|
2015-09-20 21:56:03 +00:00
|
|
|
impl<$S: BaseFloat> IndexMut<$I> for Quaternion<$S> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output {
|
|
|
|
let v: &mut [$S; 4] = self.as_mut(); &mut v[i]
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 15:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:56:03 +00:00
|
|
|
index_operators!(S, S, usize);
|
|
|
|
index_operators!(S, [S], Range<usize>);
|
|
|
|
index_operators!(S, [S], RangeTo<usize>);
|
|
|
|
index_operators!(S, [S], RangeFrom<usize>);
|
|
|
|
index_operators!(S, [S], RangeFull);
|
2015-09-20 15:32:53 +00:00
|
|
|
|
2015-03-15 02:53:57 +00:00
|
|
|
impl<S: BaseFloat + Rand> Rand for Quaternion<S> {
|
|
|
|
#[inline]
|
|
|
|
fn rand<R: Rng>(rng: &mut R) -> Quaternion<S> {
|
|
|
|
Quaternion::from_sv(rng.gen(), rng.gen())
|
|
|
|
}
|
|
|
|
}
|
2015-09-27 07:20:02 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use quaternion::*;
|
|
|
|
use vector::*;
|
|
|
|
|
|
|
|
const QUATERNION: Quaternion<f32> = Quaternion {
|
|
|
|
s: 1.0,
|
|
|
|
v: Vector3 { x: 2.0, y: 3.0, z: 4.0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into() {
|
|
|
|
let v = QUATERNION;
|
|
|
|
{
|
|
|
|
let v: [f32; 4] = v.into();
|
|
|
|
assert_eq!(v, [1.0, 2.0, 3.0, 4.0]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: (f32, f32, f32, f32) = v.into();
|
|
|
|
assert_eq!(v, (1.0, 2.0, 3.0, 4.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_ref() {
|
|
|
|
let v = QUATERNION;
|
|
|
|
{
|
|
|
|
let v: &[f32; 4] = v.as_ref();
|
|
|
|
assert_eq!(v, &[1.0, 2.0, 3.0, 4.0]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &(f32, f32, f32, f32) = v.as_ref();
|
|
|
|
assert_eq!(v, &(1.0, 2.0, 3.0, 4.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_mut() {
|
|
|
|
let mut v = QUATERNION;
|
|
|
|
{
|
|
|
|
let v: &mut[f32; 4] = v.as_mut();
|
|
|
|
assert_eq!(v, &mut [1.0, 2.0, 3.0, 4.0]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &mut(f32, f32, f32, f32) = v.as_mut();
|
|
|
|
assert_eq!(v, &mut (1.0, 2.0, 3.0, 4.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
assert_eq!(Quaternion::from([1.0, 2.0, 3.0, 4.0]), QUATERNION);
|
|
|
|
{
|
|
|
|
let v = &[1.0, 2.0, 3.0, 4.0];
|
|
|
|
let v: &Quaternion<_> = From::from(v);
|
|
|
|
assert_eq!(v, &QUATERNION);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut [1.0, 2.0, 3.0, 4.0];
|
|
|
|
let v: &mut Quaternion<_> = From::from(v);
|
|
|
|
assert_eq!(v, &QUATERNION);
|
|
|
|
}
|
|
|
|
assert_eq!(Quaternion::from((1.0, 2.0, 3.0, 4.0)), QUATERNION);
|
|
|
|
{
|
|
|
|
let v = &(1.0, 2.0, 3.0, 4.0);
|
|
|
|
let v: &Quaternion<_> = From::from(v);
|
|
|
|
assert_eq!(v, &QUATERNION);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut (1.0, 2.0, 3.0, 4.0);
|
|
|
|
let v: &mut Quaternion<_> = From::from(v);
|
|
|
|
assert_eq!(v, &QUATERNION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|