2012-10-29 05:47:53 +00:00
|
|
|
use cast::transmute;
|
2012-10-29 11:53:25 +00:00
|
|
|
use vec::raw::buf_as_slice;
|
2012-10-29 05:47:53 +00:00
|
|
|
use ptr::to_unsafe_ptr;
|
2012-09-29 10:25:49 +00:00
|
|
|
use cmp::Eq;
|
2012-10-29 05:47:53 +00:00
|
|
|
use std::cmp::FuzzyEq;
|
|
|
|
use math::*;
|
2012-10-29 11:53:25 +00:00
|
|
|
use matrix::{Mat3, Mat4};
|
|
|
|
use vector::Vec3;
|
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-01 06:23:49 +00:00
|
|
|
pure fn mul_f(value: T) -> self;
|
|
|
|
pure fn div_f(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-10-01 06:23:49 +00:00
|
|
|
pure fn exact_eq(other: &self) -> bool;
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
pure fn conjugate() -> self;
|
|
|
|
pure fn inverse() -> self;
|
|
|
|
pure fn magnitude2() -> T;
|
|
|
|
pure fn magnitude() -> T;
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
pure fn to_Mat3() -> Mat3;
|
|
|
|
pure fn to_Mat4() -> Mat4;
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Quat struct definition
|
|
|
|
//
|
2012-10-28 04:26:37 +00:00
|
|
|
pub struct Quat { w: float, x: float, y: float, z: float }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Quat Constructor
|
|
|
|
//
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pub pure fn Quat(w: float, x: float, y: float, z: float) -> Quat {
|
2012-10-28 04:26:37 +00:00
|
|
|
Quat { w: w, x: x, y: y, z: z }
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 14:11:27 +00:00
|
|
|
pub mod Quat {
|
|
|
|
pub const zero :Quat = Quat { w: 0f, x: 0f, y: 0f, z: 0f };
|
|
|
|
pub const identity :Quat = Quat { w: 1f, x: 0f, y: 0f, z: 0f };
|
|
|
|
}
|
|
|
|
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
|
|
|
// Quaternion Implementation
|
|
|
|
//
|
2012-09-29 08:41:48 +00:00
|
|
|
pub impl Quat: Quaternion<float> {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-07 10:48:47 +00:00
|
|
|
pure fn dim() -> uint { 4 }
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn mul_f(value: float) -> Quat {
|
2012-09-24 02:54:24 +00:00
|
|
|
Quat(self[0] * value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] * value,
|
|
|
|
self[2] * value,
|
|
|
|
self[3] * value)
|
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn div_f(value: float) -> Quat {
|
2012-09-24 02:54:24 +00:00
|
|
|
Quat(self[0] / value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] / value,
|
|
|
|
self[2] / value,
|
|
|
|
self[3] / value)
|
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn add_q(other: &Quat) -> Quat{
|
2012-09-24 02:54:24 +00:00
|
|
|
Quat(self[0] + other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + other[1],
|
|
|
|
self[2] + other[2],
|
|
|
|
self[3] + other[3])
|
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn sub_q(other: &Quat) -> Quat{
|
2012-09-24 02:54:24 +00:00
|
|
|
Quat(self[0] - other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - other[1],
|
|
|
|
self[2] - other[2],
|
|
|
|
self[3] - other[3])
|
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn mul_q(other: &Quat) -> Quat {
|
2012-10-28 04:26:37 +00:00
|
|
|
Quat(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-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn exact_eq(other: &Quat) -> bool {
|
2012-09-07 10:48:47 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2] &&
|
|
|
|
self[3] == other[3]
|
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
|
|
|
pure fn conjugate() -> Quat {
|
2012-10-28 04:26:37 +00:00
|
|
|
Quat(self.w, -self.x, -self.y, -self.z)
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
|
|
|
pure fn inverse() -> Quat {
|
2012-09-07 14:17:26 +00:00
|
|
|
self.conjugate().mul_f((1f / self.magnitude2()))
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-07 10:48:47 +00:00
|
|
|
pure fn magnitude2() -> float {
|
2012-10-28 04:26:37 +00:00
|
|
|
self.w * self.w +
|
|
|
|
self.x * self.x +
|
|
|
|
self.y * self.y +
|
|
|
|
self.z * self.z
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-07 10:48:47 +00:00
|
|
|
pure fn magnitude() -> float {
|
2012-09-10 22:06:31 +00:00
|
|
|
self.magnitude2().sqrt()
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
|
|
|
pure fn to_Mat3() -> Mat3 {
|
2012-10-28 04:26:37 +00:00
|
|
|
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
|
|
|
|
2012-10-28 04:26:37 +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
|
|
|
|
2012-10-28 04:26:37 +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
|
|
|
|
2012-10-28 04:26:37 +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-09-24 02:54:24 +00:00
|
|
|
return Mat3(1f - yy2 - zz2, xy2 - wz2, xz2 + wy2,
|
2012-09-07 14:17:26 +00:00
|
|
|
xy2 + wz2, 1f - xx2 - zz2, yz2 - wx2,
|
|
|
|
xz2 - wy2, yz2 + wx2, 1f - xx2 - yy2);
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
|
|
|
pure fn to_Mat4() -> Mat4 {
|
|
|
|
self.to_Mat3().to_Mat4()
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 10:37:46 +00:00
|
|
|
pub impl Quat: Index<uint, float> {
|
|
|
|
#[inline]
|
2012-10-07 06:42:47 +00:00
|
|
|
pure fn index(i: uint) -> float {
|
2012-10-29 05:47:53 +00:00
|
|
|
unsafe {
|
|
|
|
do buf_as_slice(
|
|
|
|
transmute::<*Quat, *float>(
|
|
|
|
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
|
2012-10-28 04:26:37 +00:00
|
|
|
}
|
2012-09-29 10:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 10:25:49 +00:00
|
|
|
pub impl Quat: Neg<Quat> {
|
|
|
|
#[inline]
|
|
|
|
pure fn neg() -> Quat {
|
|
|
|
Quat(-self[0], -self[1], -self[2], -self[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl Quat: Eq {
|
|
|
|
#[inline]
|
|
|
|
pure fn eq(other: &Quat) -> bool {
|
|
|
|
self.fuzzy_eq(other)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pure fn ne(other: &Quat) -> bool {
|
|
|
|
!(self == *other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl Quat: FuzzyEq {
|
|
|
|
#[inline]
|
|
|
|
pure fn fuzzy_eq(other: &Quat) -> bool {
|
|
|
|
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-09-29 08:41:48 +00:00
|
|
|
pub impl Quat: ToStr {
|
2012-10-18 05:40:36 +00:00
|
|
|
pure fn to_str() -> ~str {
|
2012-10-28 04:26:37 +00:00
|
|
|
fmt!("Quat[ %f, %f, %f, %f ]", self.w, self.x, self.y, self.z)
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
}
|