cgmath/src/quaternion.rs

299 lines
8 KiB
Rust
Raw Normal View History

2012-10-29 05:47:53 +00:00
use cast::transmute;
2012-09-29 10:25:49 +00:00
use cmp::Eq;
2012-11-01 01:34:38 +00:00
use num::from_int;
use ptr::to_unsafe_ptr;
use vec::raw::buf_as_slice;
2012-10-29 05:47:53 +00:00
use std::cmp::FuzzyEq;
2012-11-01 01:34:38 +00:00
use funs::exp::*;
use funs::trig::*;
use funs::common::*;
2012-11-02 00:10:27 +00:00
use math::*;
use matrix::{Mat3, Mat4};
use ncast::*;
use vector::Vec3;
2012-09-07 10:48:47 +00:00
2012-11-05 02:39:58 +00:00
// These quaternion type aliases are not actually specified in the GLSL spec
// but they follow the same nomenclature
2012-11-06 02:23:06 +00:00
pub type quat4 = Quat<f32>; /// a single-precision floating-point quaternion
pub type dquat4 = Quat<f64>; /// a double-precision floating-point quaternion
2012-11-05 02:39:58 +00:00
2012-09-07 10:48:47 +00:00
//
// Quaternion
//
2012-09-29 08:41:48 +00:00
pub trait Quaternion<T> {
2012-09-07 10:48:47 +00:00
pure fn dim() -> uint;
2012-10-30 04:35:02 +00:00
pure fn mul_t(value: T) -> self;
pure fn div_t(value: T) -> self;
2012-09-07 10:48:47 +00:00
2012-10-01 06:23:49 +00:00
// pure fn mul_v(other: &Vec3) -> Vec3;
2012-09-07 10:48:47 +00:00
2012-10-01 06:23:49 +00:00
pure fn add_q(other: &self) -> self;
pure fn sub_q(other: &self) -> self;
pure fn mul_q(other: &self) -> self;
2012-09-07 10:48:47 +00:00
2012-11-09 07:30:18 +00:00
pure fn dot(other: &self) -> T;
2012-09-07 10:48:47 +00:00
pure fn conjugate() -> self;
pure fn inverse() -> self;
2012-11-05 07:40:31 +00:00
pure fn length2() -> T;
pure fn length() -> T;
2012-11-09 07:31:40 +00:00
pure fn normalize() -> self;
2012-09-07 10:48:47 +00:00
pure fn nlerp(other: &self, amount: T) -> self;
pure fn slerp(other: &self, amount: T) -> self;
2012-11-01 01:34:38 +00:00
pure fn to_Mat3() -> Mat3<T>;
pure fn to_Mat4() -> Mat4<T>;
}
pub trait ToQuat<T> {
pure fn to_Quat() -> Quat<T>;
2012-09-07 10:48:47 +00:00
}
2012-11-01 01:34:38 +00:00
pub struct Quat<T> { w: T, x: T, y: T, z: T }
2012-09-07 10:48:47 +00:00
2012-10-29 14:11:27 +00:00
pub mod Quat {
2012-11-01 07:41:42 +00:00
#[inline(always)]
2012-11-01 14:01:06 +00:00
pub pure fn new<T>(w: T, x: T, y: T, z: T) -> Quat<T> {
Quat { w: move w, x: move x, y: move y, z: move z }
2012-11-01 07:41:42 +00:00
}
2012-11-04 04:39:33 +00:00
#[inline(always)]
pub pure fn from_sv<T:Copy>(s: T, v: Vec3<T>) -> Quat<T> {
Quat::new(s, v.x, v.y, v.z)
}
#[inline(always)]
2012-11-06 02:01:39 +00:00
pub pure fn zero<T:Copy NumCast>() -> Quat<T> {
2012-11-04 04:39:33 +00:00
let _0 = cast(0);
Quat::new(_0, _0, _0, _0)
}
#[inline(always)]
2012-11-06 02:01:39 +00:00
pub pure fn identity<T:Copy NumCast>() -> Quat<T> {
2012-11-04 04:39:33 +00:00
let _0 = cast(0);
Quat::new(cast(1), _0, _0, _0)
}
2012-10-29 14:11:27 +00:00
}
pub impl<T:Copy Num NumCast Trig Exp Extent Ord FuzzyEq> Quat<T>: Quaternion<T> {
#[inline(always)]
2012-09-07 10:48:47 +00:00
pure fn dim() -> uint { 4 }
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn mul_t(value: T) -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(self[0] * value,
self[1] * value,
self[2] * value,
self[3] * value)
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn div_t(value: T) -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(self[0] / value,
self[1] / value,
self[2] / value,
self[3] / value)
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn add_q(other: &Quat<T>) -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(self[0] + other[0],
self[1] + other[1],
self[2] + other[2],
self[3] + other[3])
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn sub_q(other: &Quat<T>) -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(self[0] - other[0],
self[1] - other[1],
self[2] - other[2],
self[3] - other[3])
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn mul_q(other: &Quat<T>) -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(self.w * other.w - self.x * other.x - self.y * other.y - self.z * other.z,
self.w * other.x + self.x * other.w + self.y * other.z - self.z * other.y,
self.w * other.y + self.y * other.w + self.z * other.x - self.x * other.z,
self.w * other.z + self.z * other.w + self.x * other.y - self.y * other.x)
2012-09-07 10:48:47 +00:00
}
2012-11-09 07:30:18 +00:00
#[inline(always)]
pure fn dot(other: &Quat<T>) -> T {
self.w * other.w +
self.x * other.x +
self.y * other.y +
self.z * other.z
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn conjugate() -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(self.w, -self.x, -self.y, -self.z)
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn inverse() -> Quat<T> {
let mut n: T = from_int(1);
2012-11-05 07:40:31 +00:00
n /= self.length2();
2012-11-01 01:34:38 +00:00
self.conjugate().mul_t(n)
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-05 07:40:31 +00:00
pure fn length2() -> T {
self.w * self.w +
self.x * self.x +
self.y * self.y +
self.z * self.z
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-05 07:40:31 +00:00
pure fn length() -> T {
self.length2().sqrt()
2012-09-07 10:48:47 +00:00
}
2012-11-09 07:31:40 +00:00
#[inline(always)]
pure fn normalize() -> Quat<T> {
let mut n: T = cast(1);
n /= self.length();
return self.mul_t(n);
}
#[inline(always)]
pure fn nlerp(other: &Quat<T>, amount: T) -> Quat<T> {
let _1: T = cast(1);
self.mul_t(_1 - amount).add_q(&other.mul_t(amount)).normalize()
}
/**
* Spherical Linear Intoperlation
*
* Both quaternions should be normalized first, or else strange things will
* will happen...
*
* Note: The `acos` 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.
*
* See *[Understanding Slerp, Then Not Using It]
* (http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/)*
* for more information. The [Arcsynthesis OpenGL tutorial]
* (http://www.arcsynthesis.org/gltut/Positioning/Tut08%20Interpolation.html)
* also provides a good explanation.
*/
#[inline(always)]
pure fn slerp(other: &Quat<T>, amount: T) -> Quat<T> {
let dot: T = cast(self.dot(other));
// if quaternions are close together use `nlerp`
let dot_threshold = cast(0.9995);
if dot > dot_threshold { return self.nlerp(other, amount) }
let robust_dot = dot.clamp(&-cast(1), &cast(1)); // stay within the domain of acos()
let theta_0 = acos(&robust_dot); // the angle between the quaternions
let theta = theta_0 * amount; // the fraction of theta specified by `amount`
let q = other.sub_q(&self.mul_t(robust_dot))
.normalize();
self.mul_t(cos(&theta)).add_q(&q.mul_t(sin(&theta)))
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn to_Mat3() -> Mat3<T> {
let x2 = self.x + self.x;
let y2 = self.y + self.y;
let z2 = self.z + self.z;
2012-09-07 10:48:47 +00:00
let xx2 = x2 * self.x;
let xy2 = x2 * self.y;
let xz2 = x2 * self.z;
2012-09-07 10:48:47 +00:00
let yy2 = y2 * self.y;
let yz2 = y2 * self.z;
let zz2 = z2 * self.z;
2012-09-07 10:48:47 +00:00
let wy2 = y2 * self.w;
let wz2 = z2 * self.w;
let wx2 = x2 * self.w;
2012-09-07 10:48:47 +00:00
2012-11-01 01:34:38 +00:00
let _1: T = from_int(1);
2012-11-01 07:41:42 +00:00
Mat3::new(_1 - yy2 - zz2, xy2 - wz2, xz2 + wy2,
xy2 + wz2, _1 - xx2 - zz2, yz2 - wx2,
xz2 - wy2, yz2 + wx2, _1 - xx2 - yy2)
2012-09-07 10:48:47 +00:00
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn to_Mat4() -> Mat4<T> {
self.to_Mat3().to_Mat4()
2012-09-07 10:48:47 +00:00
}
}
2012-11-01 01:34:38 +00:00
pub impl<T:Copy> Quat<T>: Index<uint, T> {
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn index(i: uint) -> T {
2012-11-09 07:29:25 +00:00
unsafe { do buf_as_slice(
transmute::<*Quat<T>, *T>(
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
}
2012-09-29 10:37:46 +00:00
}
}
2012-11-01 01:34:38 +00:00
pub impl<T:Copy Neg<T>> Quat<T>: Neg<Quat<T>> {
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn neg() -> Quat<T> {
2012-11-01 07:41:42 +00:00
Quat::new(-self[0], -self[1], -self[2], -self[3])
2012-09-29 10:25:49 +00:00
}
}
2012-11-01 01:34:38 +00:00
// TODO: make work for T:Integer
pub impl<T:Copy FuzzyEq> Quat<T>: Eq {
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn eq(other: &Quat<T>) -> bool {
2012-09-29 10:25:49 +00:00
self.fuzzy_eq(other)
}
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn ne(other: &Quat<T>) -> bool {
2012-09-29 10:25:49 +00:00
!(self == *other)
}
}
2012-11-01 01:34:38 +00:00
pub impl<T:Copy Eq> Quat<T>: ExactEq {
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn exact_eq(other: &Quat<T>) -> bool {
2012-10-30 02:45:18 +00:00
self[0] == other[0] &&
self[1] == other[1] &&
self[2] == other[2] &&
self[3] == other[3]
}
}
2012-11-01 01:34:38 +00:00
pub impl<T:Copy FuzzyEq> Quat<T>: FuzzyEq {
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn fuzzy_eq(other: &Quat<T>) -> bool {
2012-09-29 10:25:49 +00:00
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
}
2012-11-01 01:34:38 +00:00
pub impl<T:Copy> Quat<T>: ToPtr<T> {
2012-10-30 03:24:50 +00:00
#[inline(always)]
2012-11-01 01:34:38 +00:00
pure fn to_ptr() -> *T {
2012-10-30 03:24:50 +00:00
to_unsafe_ptr(&self[0])
}
2012-09-07 10:48:47 +00:00
}