2012-12-05 08:09:53 +00:00
|
|
|
/**
|
|
|
|
* > Every morning in the early part of October 1843, on my coming down to
|
|
|
|
* breakfast, your brother William Edward and yourself used to ask me: "Well,
|
|
|
|
* Papa, can you multiply triples?" Whereto I was always obliged to reply,
|
|
|
|
* with a sad shake of the head, "No, I can only add and subtract them."
|
|
|
|
*
|
|
|
|
* Sir William Hamilton
|
|
|
|
*/
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
use core::cast::transmute;
|
2012-11-22 01:09:04 +00:00
|
|
|
use core::cmp::{Eq, Ord};
|
2012-11-22 00:38:39 +00:00
|
|
|
use core::ptr::to_unsafe_ptr;
|
2012-11-15 02:23:39 +00:00
|
|
|
use core::vec::raw::buf_as_slice;
|
|
|
|
|
2013-02-09 22:42:06 +00:00
|
|
|
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
2013-01-27 22:22:15 +00:00
|
|
|
use numeric::*;
|
|
|
|
use numeric::number::Number;
|
|
|
|
use numeric::number::Number::{zero,one};
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
use mat::{
|
|
|
|
Mat3,
|
|
|
|
Matrix3
|
|
|
|
};
|
2013-01-29 01:13:44 +00:00
|
|
|
|
|
|
|
use vec::{
|
|
|
|
Vec3,
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector3,
|
2013-02-09 22:42:41 +00:00
|
|
|
EuclideanVector,
|
|
|
|
NumericVector,
|
|
|
|
NumericVector3,
|
2013-01-29 01:13:44 +00:00
|
|
|
vec3,
|
|
|
|
dvec3,
|
|
|
|
};
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-05 01:38:30 +00:00
|
|
|
/**
|
|
|
|
* A quaternion in scalar/vector form
|
|
|
|
*
|
2012-12-05 01:51:18 +00:00
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. Should be a floating point type.
|
|
|
|
*
|
2012-12-05 01:38:30 +00:00
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `s` - the scalar component
|
|
|
|
* * `v` - a vector containing the three imaginary components
|
|
|
|
*/
|
2013-03-28 10:56:38 +00:00
|
|
|
#[deriving(Eq)]
|
2012-11-21 04:01:21 +00:00
|
|
|
pub struct Quat<T> { s: T, v: Vec3<T> }
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2013-03-28 23:32:23 +00:00
|
|
|
pub impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Quat<T> {
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
|
|
|
* Construct the quaternion from one scalar component and three
|
|
|
|
* imaginary components
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `w` - the scalar component
|
|
|
|
* * `xi` - the fist imaginary component
|
|
|
|
* * `yj` - the second imaginary component
|
|
|
|
* * `zk` - the third imaginary component
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn new(w: T, xi: T, yj: T, zk: T) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Quat::from_sv(w, Vector3::new(xi, yj, zk))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
|
|
|
* Construct the quaternion from a scalar and a vector
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `s` - the scalar component
|
|
|
|
* * `v` - a vector containing the three imaginary components
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_sv(s: T, v: Vec3<T>) -> Quat<T> {
|
2012-12-21 04:08:43 +00:00
|
|
|
Quat { s: s, v: v }
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-14 06:04:46 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn identity() -> Quat<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
Quat::new(one(), zero(), zero(), zero())
|
2012-12-14 06:04:46 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-14 06:04:46 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The additive identity, ie: `q = 0 + 0i + 0j + 0i`
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn zero() -> Quat<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
Quat::new(zero(), zero(), zero(), zero())
|
2012-12-14 06:04:46 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_angle_x(radians: T) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
let _2 = Number::from(2);
|
|
|
|
Quat::new(cos(radians / _2), sin(radians), zero(), zero())
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_angle_y(radians: T) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
let _2 = Number::from(2);
|
|
|
|
Quat::new(cos(radians / _2), zero(), sin(radians), zero())
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_angle_z(radians: T) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
let _2 = Number::from(2);
|
|
|
|
Quat::new(cos(radians / _2), zero(), zero(), sin(radians))
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
// http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion
|
|
|
|
let _2 = Number::from(2);
|
|
|
|
let xdiv2 = radians_x / _2;
|
|
|
|
let ydiv2 = radians_y / _2;
|
|
|
|
let zdiv2 = radians_z / _2;
|
|
|
|
Quat::new(cos(zdiv2) * cos(xdiv2) * cos(ydiv2) + sin(zdiv2) * sin(xdiv2) * sin(ydiv2),
|
|
|
|
sin(zdiv2) * cos(xdiv2) * cos(ydiv2) - cos(zdiv2) * sin(xdiv2) * sin(ydiv2),
|
|
|
|
cos(zdiv2) * sin(xdiv2) * cos(ydiv2) + sin(zdiv2) * cos(xdiv2) * sin(ydiv2),
|
|
|
|
cos(zdiv2) * cos(xdiv2) * sin(ydiv2) - sin(zdiv2) * sin(xdiv2) * cos(ydiv2))
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
let half = radians / Number::from(2);
|
|
|
|
Quat::from_sv(cos(half), axis.mul_t(sin(half)))
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn from_axes(x: Vec3<T>, y: Vec3<T>, z: Vec3<T>) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
let m: Mat3<T> = Matrix3::from_axes(x, y, z); m.to_quat()
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-03-28 10:35:51 +00:00
|
|
|
fn get_angle_axis(&self) -> (T, Vec3<T>) {
|
2013-02-06 21:26:33 +00:00
|
|
|
fail!(~"Not yet implemented.")
|
2013-01-29 09:26:48 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:37:25 +00:00
|
|
|
fn look_at(dir: &Vec3<T>, up: &Vec3<T>) -> Quat<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
let m: Mat3<T> = Matrix3::look_at(dir, up); m.to_quat()
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The result of multiplying the quaternion a scalar
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn mul_t(&self, value: T) -> Quat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Quat::new(self[0] * value,
|
|
|
|
self[1] * value,
|
|
|
|
self[2] * value,
|
|
|
|
self[3] * value)
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The result of dividing the quaternion a scalar
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn div_t(&self, value: T) -> Quat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Quat::new(self[0] / value,
|
|
|
|
self[1] / value,
|
|
|
|
self[2] / value,
|
|
|
|
self[3] / value)
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The result of multiplying the quaternion by a vector
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn mul_v(&self, vec: &Vec3<T>) -> Vec3<T> {
|
2012-11-21 04:01:21 +00:00
|
|
|
let tmp = self.v.cross(vec).add_v(&vec.mul_t(self.s));
|
2012-12-03 06:19:53 +00:00
|
|
|
self.v.cross(&tmp).mul_t(Number::from(2)).add_v(vec)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
2013-03-28 09:45:43 +00:00
|
|
|
* The sum of this quaternion and `other`
|
2012-12-28 03:47:34 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn add_q(&self, other: &Quat<T>) -> Quat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Quat::new(self[0] + other[0],
|
|
|
|
self[1] + other[1],
|
|
|
|
self[2] + other[2],
|
|
|
|
self[3] + other[3])
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
2013-03-28 09:45:43 +00:00
|
|
|
* The sum of this quaternion and `other`
|
2012-12-28 03:47:34 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn sub_q(&self, other: &Quat<T>) -> Quat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Quat::new(self[0] - other[0],
|
|
|
|
self[1] - other[1],
|
|
|
|
self[2] - other[2],
|
|
|
|
self[3] - other[3])
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The the result of multipliplying the quaternion by `other`
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn mul_q(&self, other: &Quat<T>) -> Quat<T> {
|
2012-11-21 04:01:21 +00:00
|
|
|
Quat::new(self.s * other.s - self.v.x * other.v.x - self.v.y * other.v.y - self.v.z * other.v.z,
|
2013-03-28 09:45:43 +00:00
|
|
|
self.s * other.v.x + self.v.x * other.s + self.v.y * other.v.z - self.v.z * other.v.y,
|
|
|
|
self.s * other.v.y + self.v.y * other.s + self.v.z * other.v.x - self.v.x * other.v.z,
|
|
|
|
self.s * other.v.z + self.v.z * other.s + self.v.x * other.v.y - self.v.y * other.v.x)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The dot product of the quaternion and `other`
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn dot(&self, other: &Quat<T>) -> T {
|
2012-11-21 04:01:21 +00:00
|
|
|
self.s * other.s + self.v.dot(&other.v)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The conjugate of the quaternion
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn conjugate(&self) -> Quat<T> {
|
2012-11-21 04:01:21 +00:00
|
|
|
Quat::from_sv(self.s, -self.v)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The multiplicative inverse of the quaternion
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn inverse(&self) -> Quat<T> {
|
2012-12-05 01:38:30 +00:00
|
|
|
self.conjugate().div_t(self.magnitude2())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The squared magnitude of the quaternion. This is useful for
|
|
|
|
* magnitude comparisons where the exact magnitude does not need to be
|
|
|
|
* calculated.
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn magnitude2(&self) -> T {
|
2012-11-21 04:01:21 +00:00
|
|
|
self.s * self.s + self.v.length2()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn magnitude(&self) -> T {
|
2012-12-05 01:38:30 +00:00
|
|
|
self.magnitude2().sqrt()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The normalized quaternion
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn normalize(&self) -> Quat<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
self.mul_t(one::<T>()/self.magnitude())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* Normalised linear interpolation
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The intoperlated quaternion
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn nlerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
self.mul_t(one::<T>() - amount).add_q(&other.mul_t(amount)).normalize()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
/**
|
|
|
|
* Spherical Linear Intoperlation
|
|
|
|
*
|
2012-12-28 03:47:34 +00:00
|
|
|
* Perform a spherical linear interpolation between the quaternion and
|
|
|
|
* `other`. Both quaternions should be normalized first.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The intoperlated quaternion
|
2012-11-15 02:23:39 +00:00
|
|
|
*
|
2012-12-05 01:38:30 +00:00
|
|
|
* # Performance notes
|
2012-11-15 02:23:39 +00:00
|
|
|
*
|
2012-12-05 01:38:30 +00:00
|
|
|
* The `acos` operation used in `slerp` is an expensive operation, so unless
|
|
|
|
* your quarternions a far away from each other it's generally more advisable
|
|
|
|
* to use `nlerp` when you know your rotations are going to be small.
|
|
|
|
*
|
|
|
|
* - [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)
|
2012-11-15 02:23:39 +00:00
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn slerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
|
2012-12-03 22:24:03 +00:00
|
|
|
let dot = self.dot(other);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 06:19:53 +00:00
|
|
|
let dot_threshold = Number::from(0.9995);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-08 02:59:37 +00:00
|
|
|
if dot > dot_threshold {
|
|
|
|
return self.nlerp(other, amount); // if quaternions are close together use `nlerp`
|
|
|
|
} else {
|
2013-01-27 22:22:15 +00:00
|
|
|
let robust_dot = dot.clamp(-one::<T>(), one()); // stay within the domain of acos()
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-27 22:22:15 +00:00
|
|
|
let theta_0 = acos(robust_dot); // the angle between the quaternions
|
2012-12-08 02:59:37 +00:00
|
|
|
let theta = theta_0 * amount; // the fraction of theta specified by `amount`
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-08 02:59:37 +00:00
|
|
|
let q = other.sub_q(&self.mul_t(robust_dot))
|
|
|
|
.normalize();
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-27 22:22:15 +00:00
|
|
|
return self.mul_t(cos(theta)).add_q(&q.mul_t(sin(theta)));
|
2012-12-08 02:59:37 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 06:41:21 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A pointer to the first component of the quaternion
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn to_ptr(&self) -> *T {
|
2012-12-28 06:41:21 +00:00
|
|
|
unsafe {
|
|
|
|
transmute::<*Quat<T>, *T>(
|
|
|
|
to_unsafe_ptr(self)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 03:47:34 +00:00
|
|
|
/**
|
|
|
|
* Convert the quaternion to a 3 x 3 rotation matrix
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn to_mat3(&self) -> Mat3<T> {
|
2012-11-21 04:01:21 +00:00
|
|
|
let x2 = self.v.x + self.v.x;
|
|
|
|
let y2 = self.v.y + self.v.y;
|
|
|
|
let z2 = self.v.z + self.v.z;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-11-21 04:01:21 +00:00
|
|
|
let xx2 = x2 * self.v.x;
|
|
|
|
let xy2 = x2 * self.v.y;
|
|
|
|
let xz2 = x2 * self.v.z;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-11-21 04:01:21 +00:00
|
|
|
let yy2 = y2 * self.v.y;
|
|
|
|
let yz2 = y2 * self.v.z;
|
|
|
|
let zz2 = z2 * self.v.z;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-11-21 04:01:21 +00:00
|
|
|
let sy2 = y2 * self.s;
|
|
|
|
let sz2 = z2 * self.s;
|
|
|
|
let sx2 = x2 * self.s;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-30 05:26:01 +00:00
|
|
|
let _1: T = one();
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
Matrix3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2,
|
|
|
|
xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2,
|
|
|
|
xz2 + sy2, yz2 - sx2, _1 - xx2 - yy2)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy> Index<uint, T> for Quat<T> {
|
2012-12-28 03:47:34 +00:00
|
|
|
#[inline(always)]
|
2013-03-29 00:33:39 +00:00
|
|
|
fn index(&self, i: uint) -> T {
|
2012-12-28 03:47:34 +00:00
|
|
|
unsafe { do buf_as_slice(
|
|
|
|
transmute::<*Quat<T>, *T>(
|
2013-03-29 00:33:39 +00:00
|
|
|
to_unsafe_ptr(self)), 4) |slice| { slice[i] }
|
2012-12-28 03:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Quat<T>> for Quat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn neg(&self) -> Quat<T> {
|
2012-12-01 04:19:21 +00:00
|
|
|
Quat::new(-self[0], -self[1], -self[2], -self[3])
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Quat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn fuzzy_eq(&self, other: &Quat<T>) -> bool {
|
2013-02-09 22:42:06 +00:00
|
|
|
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-02-09 22:42:06 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 10:35:51 +00:00
|
|
|
fn fuzzy_eq_eps(&self, other: &Quat<T>, epsilon: &T) -> bool {
|
2013-02-09 22:42:06 +00:00
|
|
|
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
|
|
|
self[1].fuzzy_eq_eps(&other[1], epsilon) &&
|
|
|
|
self[2].fuzzy_eq_eps(&other[2], epsilon) &&
|
|
|
|
self[3].fuzzy_eq_eps(&other[3], epsilon)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-08 00:00:50 +00:00
|
|
|
}
|
2013-01-29 01:13:44 +00:00
|
|
|
|
|
|
|
// GLSL-style type aliases for quaternions. These are not present in the GLSL
|
|
|
|
// specification, but they roughly follow the same nomenclature.
|
|
|
|
|
|
|
|
pub type quat = Quat<f32>; /// a single-precision floating-point quaternion
|
|
|
|
pub type dquat = Quat<f64>; /// a double-precision floating-point quaternion
|
|
|
|
|
|
|
|
// Static method wrappers for GLSL-style types
|
|
|
|
|
2013-03-28 23:32:23 +00:00
|
|
|
pub impl quat {
|
2013-03-28 10:37:25 +00:00
|
|
|
#[inline(always)] fn new(w: f32, xi: f32, yj: f32, zk: f32) -> quat { Quat::new(w, xi, yj, zk) }
|
|
|
|
#[inline(always)] fn from_sv(s: f32, v: vec3) -> quat { Quat::from_sv(s, v) }
|
|
|
|
#[inline(always)] fn identity() -> quat { Quat::identity() }
|
|
|
|
#[inline(always)] fn zero() -> quat { Quat::zero() }
|
|
|
|
|
|
|
|
#[inline(always)] fn from_angle_x(radians: f32) -> quat { Quat::from_angle_x(radians) }
|
|
|
|
#[inline(always)] fn from_angle_y(radians: f32) -> quat { Quat::from_angle_y(radians) }
|
|
|
|
#[inline(always)] fn from_angle_z(radians: f32) -> quat { Quat::from_angle_z(radians) }
|
|
|
|
#[inline(always)] fn from_angle_xyz(radians_x: f32, radians_y: f32, radians_z: f32)
|
2013-01-29 01:13:44 +00:00
|
|
|
-> quat { Quat::from_angle_xyz(radians_x, radians_y, radians_z) }
|
2013-03-28 10:37:25 +00:00
|
|
|
#[inline(always)] fn from_angle_axis(radians: f32, axis: &vec3) -> quat { Quat::from_angle_axis(radians, axis) }
|
|
|
|
#[inline(always)] fn from_axes(x: vec3, y: vec3, z: vec3) -> quat { Quat::from_axes(x, y, z) }
|
|
|
|
#[inline(always)] fn look_at(dir: &vec3, up: &vec3) -> quat { Quat::look_at(dir, up) }
|
2013-01-29 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 23:32:23 +00:00
|
|
|
pub impl dquat {
|
2013-03-28 10:37:25 +00:00
|
|
|
#[inline(always)] fn new(w: f64, xi: f64, yj: f64, zk: f64) -> dquat { Quat::new(w, xi, yj, zk) }
|
|
|
|
#[inline(always)] fn from_sv(s: f64, v: dvec3) -> dquat { Quat::from_sv(s, v) }
|
|
|
|
#[inline(always)] fn identity() -> dquat { Quat::identity() }
|
|
|
|
#[inline(always)] fn zero() -> dquat { Quat::zero() }
|
|
|
|
|
|
|
|
#[inline(always)] fn from_angle_x(radians: f64) -> dquat { Quat::from_angle_x(radians) }
|
|
|
|
#[inline(always)] fn from_angle_y(radians: f64) -> dquat { Quat::from_angle_y(radians) }
|
|
|
|
#[inline(always)] fn from_angle_z(radians: f64) -> dquat { Quat::from_angle_z(radians) }
|
|
|
|
#[inline(always)] fn from_angle_xyz(radians_x: f64, radians_y: f64, radians_z: f64)
|
2013-01-29 01:13:44 +00:00
|
|
|
-> dquat { Quat::from_angle_xyz(radians_x, radians_y, radians_z) }
|
2013-03-28 10:37:25 +00:00
|
|
|
#[inline(always)] fn from_angle_axis(radians: f64, axis: &dvec3) -> dquat { Quat::from_angle_axis(radians, axis) }
|
|
|
|
#[inline(always)] fn from_axes(x: dvec3, y: dvec3, z: dvec3) -> dquat { Quat::from_axes(x, y, z) }
|
|
|
|
#[inline(always)] fn look_at(dir: &dvec3, up: &dvec3) -> dquat { Quat::look_at(dir, up) }
|
2013-02-17 08:16:41 +00:00
|
|
|
}
|