Fixes for core->std and changes to use

This commit is contained in:
Brendan Zabarauskas 2013-05-24 07:05:25 +10:00
parent 0361be55c5
commit f800a0ae04
5 changed files with 284 additions and 240 deletions

View file

@ -1,8 +1,7 @@
use core::cast::transmute; use std::cast::transmute;
use core::cmp::ApproxEq; use std::cmp::ApproxEq;
use core::num::Zero::zero; use std::num::{Zero, One};
use core::num::One::one; use std::util;
use core::util;
use vec::*; use vec::*;
use quat::Quat; use quat::Quat;
@ -343,8 +342,8 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
*/ */
#[inline(always)] #[inline(always)]
fn from_value(value: T) -> Mat2<T> { fn from_value(value: T) -> Mat2<T> {
BaseMat2::new(value, zero(), BaseMat2::new(value, Zero::zero(),
zero(), value) Zero::zero(), value)
} }
/** /**
@ -360,8 +359,8 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
*/ */
#[inline(always)] #[inline(always)]
fn identity() -> Mat2<T> { fn identity() -> Mat2<T> {
BaseMat2::new( one::<T>(), zero::<T>(), BaseMat2::new( One::one::<T>(), Zero::zero::<T>(),
zero::<T>(), one::<T>()) Zero::zero::<T>(), One::one::<T>())
} }
/** /**
@ -377,8 +376,8 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
*/ */
#[inline(always)] #[inline(always)]
fn zero() -> Mat2<T> { fn zero() -> Mat2<T> {
BaseMat2::new(zero::<T>(), zero::<T>(), BaseMat2::new(Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>()) Zero::zero::<T>(), Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
@ -426,7 +425,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
#[inline(always)] #[inline(always)]
fn inverse(&self) -> Option<Mat2<T>> { fn inverse(&self) -> Option<Mat2<T>> {
let d = self.determinant(); let d = self.determinant();
if d.approx_eq(&zero()) { if d.approx_eq(&Zero::zero()) {
None None
} else { } else {
Some(BaseMat2::new( self[1][1]/d, -self[0][1]/d, Some(BaseMat2::new( self[1][1]/d, -self[0][1]/d,
@ -515,8 +514,8 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
#[inline(always)] #[inline(always)]
fn is_diagonal(&self) -> bool { fn is_diagonal(&self) -> bool {
self[0][1].approx_eq(&zero()) && self[0][1].approx_eq(&Zero::zero()) &&
self[1][0].approx_eq(&zero()) self[1][0].approx_eq(&Zero::zero())
} }
#[inline(always)] #[inline(always)]
@ -532,7 +531,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
#[inline(always)] #[inline(always)]
fn is_invertible(&self) -> bool { fn is_invertible(&self) -> bool {
!self.determinant().approx_eq(&zero()) !self.determinant().approx_eq(&Zero::zero())
} }
#[inline(always)] #[inline(always)]
@ -612,9 +611,9 @@ impl<T:Copy + Float + NumAssign> BaseMat2<T, Vec2<T>> for Mat2<T> {
*/ */
#[inline(always)] #[inline(always)]
fn to_mat3(&self) -> Mat3<T> { fn to_mat3(&self) -> Mat3<T> {
BaseMat3::new(self[0][0], self[0][1], zero(), BaseMat3::new( self[0][0], self[0][1], Zero::zero(),
self[1][0], self[1][1], zero(), self[1][0], self[1][1], Zero::zero(),
zero(), zero(), one()) Zero::zero(), Zero::zero(), One::one())
} }
/** /**
@ -634,10 +633,10 @@ impl<T:Copy + Float + NumAssign> BaseMat2<T, Vec2<T>> for Mat2<T> {
*/ */
#[inline(always)] #[inline(always)]
fn to_mat4(&self) -> Mat4<T> { fn to_mat4(&self) -> Mat4<T> {
BaseMat4::new(self[0][0], self[0][1], zero(), zero(), BaseMat4::new( self[0][0], self[0][1], Zero::zero(), Zero::zero(),
self[1][0], self[1][1], zero(), zero(), self[1][0], self[1][1], Zero::zero(), Zero::zero(),
zero(), zero(), one(), zero(), Zero::zero(), Zero::zero(), One::one(), Zero::zero(),
zero(), zero(), zero(), one()) Zero::zero(), Zero::zero(), Zero::zero(), One::one())
} }
} }
@ -720,9 +719,9 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
*/ */
#[inline(always)] #[inline(always)]
fn from_value(value: T) -> Mat3<T> { fn from_value(value: T) -> Mat3<T> {
BaseMat3::new(value, zero(), zero(), BaseMat3::new(value, Zero::zero(), Zero::zero(),
zero(), value, zero(), Zero::zero(), value, Zero::zero(),
zero(), zero(), value) Zero::zero(), Zero::zero(), value)
} }
/** /**
@ -740,9 +739,9 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
*/ */
#[inline(always)] #[inline(always)]
fn identity() -> Mat3<T> { fn identity() -> Mat3<T> {
BaseMat3::new( one::<T>(), zero::<T>(), zero::<T>(), BaseMat3::new(One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), one::<T>(), zero::<T>(), Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), one::<T>()) Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>())
} }
/** /**
@ -760,9 +759,9 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
*/ */
#[inline(always)] #[inline(always)]
fn zero() -> Mat3<T> { fn zero() -> Mat3<T> {
BaseMat3::new(zero::<T>(), zero::<T>(), zero::<T>(), BaseMat3::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>()) Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
@ -823,7 +822,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
// #[inline(always)] // #[inline(always)]
fn inverse(&self) -> Option<Mat3<T>> { fn inverse(&self) -> Option<Mat3<T>> {
let d = self.determinant(); let d = self.determinant();
if d.approx_eq(&zero()) { if d.approx_eq(&Zero::zero()) {
None None
} else { } else {
let m: Mat3<T> = BaseMat3::from_cols(self[1].cross(&self[2]).div_t(d), let m: Mat3<T> = BaseMat3::from_cols(self[1].cross(&self[2]).div_t(d),
@ -925,14 +924,14 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
#[inline(always)] #[inline(always)]
fn is_diagonal(&self) -> bool { fn is_diagonal(&self) -> bool {
self[0][1].approx_eq(&zero()) && self[0][1].approx_eq(&Zero::zero()) &&
self[0][2].approx_eq(&zero()) && self[0][2].approx_eq(&Zero::zero()) &&
self[1][0].approx_eq(&zero()) && self[1][0].approx_eq(&Zero::zero()) &&
self[1][2].approx_eq(&zero()) && self[1][2].approx_eq(&Zero::zero()) &&
self[2][0].approx_eq(&zero()) && self[2][0].approx_eq(&Zero::zero()) &&
self[2][1].approx_eq(&zero()) self[2][1].approx_eq(&Zero::zero())
} }
#[inline(always)] #[inline(always)]
@ -954,7 +953,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
#[inline(always)] #[inline(always)]
fn is_invertible(&self) -> bool { fn is_invertible(&self) -> bool {
!self.determinant().approx_eq(&zero()) !self.determinant().approx_eq(&Zero::zero())
} }
#[inline(always)] #[inline(always)]
@ -1027,9 +1026,9 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
let cos_theta = radians.cos(); let cos_theta = radians.cos();
let sin_theta = radians.sin(); let sin_theta = radians.sin();
BaseMat3::new( one(), zero(), zero(), BaseMat3::new( One::one(), Zero::zero(), Zero::zero(),
zero(), cos_theta, sin_theta, Zero::zero(), cos_theta, sin_theta,
zero(), -sin_theta, cos_theta) Zero::zero(), -sin_theta, cos_theta)
} }
/** /**
@ -1041,9 +1040,9 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
let cos_theta = radians.cos(); let cos_theta = radians.cos();
let sin_theta = radians.sin(); let sin_theta = radians.sin();
BaseMat3::new(cos_theta, zero(), -sin_theta, BaseMat3::new( cos_theta, Zero::zero(), -sin_theta,
zero(), one(), zero(), Zero::zero(), One::one(), Zero::zero(),
sin_theta, zero(), cos_theta) sin_theta, Zero::zero(), cos_theta)
} }
/** /**
@ -1055,9 +1054,9 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
let cos_theta = radians.cos(); let cos_theta = radians.cos();
let sin_theta = radians.sin(); let sin_theta = radians.sin();
BaseMat3::new( cos_theta, sin_theta, zero(), BaseMat3::new( cos_theta, sin_theta, Zero::zero(),
-sin_theta, cos_theta, zero(), -sin_theta, cos_theta, Zero::zero(),
zero(), zero(), one()) Zero::zero(), Zero::zero(), One::one())
} }
/** /**
@ -1091,15 +1090,15 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Mat3<T> { fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Mat3<T> {
let c = radians.cos(); let c = radians.cos();
let s = radians.sin(); let s = radians.sin();
let _1_c = one::<T>() - c; let _1_c = One::one::<T>() - c;
let x = axis.x; let x = axis.x;
let y = axis.y; let y = axis.y;
let z = axis.z; let z = axis.z;
BaseMat3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y, BaseMat3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y,
_1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x, _1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x,
_1_c*x*z + s*y, _1_c*y*z - s*x, _1_c*z*z + c) _1_c*x*z + s*y, _1_c*y*z - s*x, _1_c*z*z + c)
} }
#[inline(always)] #[inline(always)]
@ -1133,10 +1132,10 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
*/ */
#[inline(always)] #[inline(always)]
fn to_mat4(&self) -> Mat4<T> { fn to_mat4(&self) -> Mat4<T> {
BaseMat4::new(self[0][0], self[0][1], self[0][2], zero(), BaseMat4::new( self[0][0], self[0][1], self[0][2], Zero::zero(),
self[1][0], self[1][1], self[1][2], zero(), self[1][0], self[1][1], self[1][2], Zero::zero(),
self[2][0], self[2][1], self[2][2], zero(), self[2][0], self[2][1], self[2][2], Zero::zero(),
zero(), zero(), zero(), one()) Zero::zero(), Zero::zero(), Zero::zero(), One::one())
} }
/** /**
@ -1154,7 +1153,7 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
let _1: T = num::cast(1.0); let _1: T = num::cast(1.0);
let half: T = num::cast(0.5); let half: T = num::cast(0.5);
if trace >= zero() { if trace >= Zero::zero() {
s = (_1 + trace).sqrt(); s = (_1 + trace).sqrt();
w = half * s; w = half * s;
s = half / s; s = half / s;
@ -1272,10 +1271,10 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
*/ */
#[inline(always)] #[inline(always)]
fn from_value(value: T) -> Mat4<T> { fn from_value(value: T) -> Mat4<T> {
BaseMat4::new(value, zero(), zero(), zero(), BaseMat4::new(value, Zero::zero(), Zero::zero(), Zero::zero(),
zero(), value, zero(), zero(), Zero::zero(), value, Zero::zero(), Zero::zero(),
zero(), zero(), value, zero(), Zero::zero(), Zero::zero(), value, Zero::zero(),
zero(), zero(), zero(), value) Zero::zero(), Zero::zero(), Zero::zero(), value)
} }
/** /**
@ -1295,10 +1294,10 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
*/ */
#[inline(always)] #[inline(always)]
fn identity() -> Mat4<T> { fn identity() -> Mat4<T> {
BaseMat4::new( one::<T>(), zero::<T>(), zero::<T>(), zero::<T>(), BaseMat4::new(One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), one::<T>(), zero::<T>(), zero::<T>(), Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), one::<T>(), zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), one::<T>()) Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>())
} }
/** /**
@ -1318,10 +1317,10 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
*/ */
#[inline(always)] #[inline(always)]
fn zero() -> Mat4<T> { fn zero() -> Mat4<T> {
BaseMat4::new(zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(), BaseMat4::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>()) Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
@ -1410,7 +1409,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
fn inverse(&self) -> Option<Mat4<T>> { fn inverse(&self) -> Option<Mat4<T>> {
let d = self.determinant(); let d = self.determinant();
if d.approx_eq(&zero()) { if d.approx_eq(&Zero::zero()) {
None None
} else { } else {
@ -1556,21 +1555,21 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
#[inline(always)] #[inline(always)]
fn is_diagonal(&self) -> bool { fn is_diagonal(&self) -> bool {
self[0][1].approx_eq(&zero()) && self[0][1].approx_eq(&Zero::zero()) &&
self[0][2].approx_eq(&zero()) && self[0][2].approx_eq(&Zero::zero()) &&
self[0][3].approx_eq(&zero()) && self[0][3].approx_eq(&Zero::zero()) &&
self[1][0].approx_eq(&zero()) && self[1][0].approx_eq(&Zero::zero()) &&
self[1][2].approx_eq(&zero()) && self[1][2].approx_eq(&Zero::zero()) &&
self[1][3].approx_eq(&zero()) && self[1][3].approx_eq(&Zero::zero()) &&
self[2][0].approx_eq(&zero()) && self[2][0].approx_eq(&Zero::zero()) &&
self[2][1].approx_eq(&zero()) && self[2][1].approx_eq(&Zero::zero()) &&
self[2][3].approx_eq(&zero()) && self[2][3].approx_eq(&Zero::zero()) &&
self[3][0].approx_eq(&zero()) && self[3][0].approx_eq(&Zero::zero()) &&
self[3][1].approx_eq(&zero()) && self[3][1].approx_eq(&Zero::zero()) &&
self[3][2].approx_eq(&zero()) self[3][2].approx_eq(&Zero::zero())
} }
#[inline(always)] #[inline(always)]
@ -1599,7 +1598,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
#[inline(always)] #[inline(always)]
fn is_invertible(&self) -> bool { fn is_invertible(&self) -> bool {
!self.determinant().approx_eq(&zero()) !self.determinant().approx_eq(&Zero::zero())
} }
#[inline(always)] #[inline(always)]

View file

@ -7,9 +7,9 @@
* Sir William Hamilton * Sir William Hamilton
*/ */
use core::cmp::ApproxEq; use std::cast::transmute;
use core::num::Zero::zero; use std::cmp::ApproxEq;
use core::num::One::one; use std::num::{Zero, One};
use mat::{Mat3, BaseMat3}; use mat::{Mat3, BaseMat3};
use vec::{Vec3, BaseVec3, AffineVec, NumVec, NumVec3}; use vec::{Vec3, BaseVec3, AffineVec, NumVec, NumVec3};
@ -68,7 +68,10 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
*/ */
#[inline(always)] #[inline(always)]
fn identity() -> Quat<T> { fn identity() -> Quat<T> {
Quat::new(one(), zero(), zero(), zero()) Quat::new(One::one(),
Zero::zero(),
Zero::zero(),
Zero::zero())
} }
/** /**
@ -78,25 +81,37 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
*/ */
#[inline(always)] #[inline(always)]
fn zero() -> Quat<T> { fn zero() -> Quat<T> {
Quat::new(zero(), zero(), zero(), zero()) Quat::new(Zero::zero(),
Zero::zero(),
Zero::zero(),
Zero::zero())
} }
#[inline(always)] #[inline(always)]
fn from_angle_x(radians: T) -> Quat<T> { fn from_angle_x(radians: T) -> Quat<T> {
let _2 = num::cast(2); let _2 = num::cast(2);
Quat::new((radians / _2).cos(), radians.sin(), zero(), zero()) Quat::new((radians / _2).cos(),
radians.sin(),
Zero::zero(),
Zero::zero())
} }
#[inline(always)] #[inline(always)]
fn from_angle_y(radians: T) -> Quat<T> { fn from_angle_y(radians: T) -> Quat<T> {
let _2 = num::cast(2); let _2 = num::cast(2);
Quat::new((radians / _2).cos(), zero(), radians.sin(), zero()) Quat::new((radians / _2).cos(),
Zero::zero(),
radians.sin(),
Zero::zero())
} }
#[inline(always)] #[inline(always)]
fn from_angle_z(radians: T) -> Quat<T> { fn from_angle_z(radians: T) -> Quat<T> {
let _2 = num::cast(2); let _2 = num::cast(2);
Quat::new((radians / _2).cos(), zero(), zero(), radians.sin()) Quat::new((radians / _2).cos(),
Zero::zero(),
Zero::zero(),
radians.sin())
} }
#[inline(always)] #[inline(always)]
@ -273,7 +288,7 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
*/ */
#[inline(always)] #[inline(always)]
fn normalize(&self) -> Quat<T> { fn normalize(&self) -> Quat<T> {
self.mul_t(one::<T>()/self.magnitude()) self.mul_t(One::one::<T>()/self.magnitude())
} }
/** /**
@ -285,7 +300,7 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
*/ */
#[inline(always)] #[inline(always)]
fn nlerp(&self, other: &Quat<T>, amount: T) -> Quat<T> { fn nlerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
self.mul_t(one::<T>() - amount).add_q(&other.mul_t(amount)).normalize() self.mul_t(One::one::<T>() - amount).add_q(&other.mul_t(amount)).normalize()
} }
/** /**
@ -316,12 +331,13 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
let dot_threshold = num::cast(0.9995); let dot_threshold = num::cast(0.9995);
if dot > dot_threshold { if dot > dot_threshold {
return self.nlerp(other, amount); // if quaternions are close together use `nlerp` return self.nlerp(other, amount); // if quaternions are close together use `nlerp`
} else { } else {
let robust_dot = dot.clamp(&-one::<T>(), &one()); // stay within the domain of acos() let robust_dot = dot.clamp(&-One::one::<T>(),
&One::one()); // stay within the domain of acos()
let theta_0 = robust_dot.acos(); // the angle between the quaternions let theta_0 = robust_dot.acos(); // the angle between the quaternions
let theta = theta_0 * amount; // the fraction of theta specified by `amount` let theta = theta_0 * amount; // the fraction of theta specified by `amount`
let q = other.sub_q(&self.mul_t(robust_dot)) let q = other.sub_q(&self.mul_t(robust_dot))
.normalize(); .normalize();
@ -362,7 +378,7 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
let sz2 = z2 * self.s; let sz2 = z2 * self.s;
let sx2 = x2 * self.s; let sx2 = x2 * self.s;
let _1: T = one(); let _1: T = One::one();
BaseMat3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2, BaseMat3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2,
xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2, xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2,
@ -373,7 +389,7 @@ pub impl<T:Copy + Float + NumAssign> Quat<T> {
impl<T:Copy> Index<uint, T> for Quat<T> { impl<T:Copy> Index<uint, T> for Quat<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> T { fn index(&self, i: &uint) -> T {
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 4) |slice| { slice[*i] } } unsafe { transmute::<Quat<T>,[T,..4]>(*self)[*i] }
} }
} }

View file

@ -1,5 +1,4 @@
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use numeric::*;
use mat::*; use mat::*;
use quat::*; use quat::*;
@ -32,13 +31,13 @@ fn test_quat() {
fn test_quat_2() { fn test_quat_2() {
let v = vec3::new(1f32, 0f32, 0f32); let v = vec3::new(1f32, 0f32, 0f32);
let q = quat::from_angle_axis(radians(-45f32), &vec3::new(0f32, 0f32, -1f32)); let q = quat::from_angle_axis((-45f32).radians(), &vec3::new(0f32, 0f32, -1f32));
// http://www.wolframalpha.com/input/?i={1,0}+rotate+-45+degrees // http://www.wolframalpha.com/input/?i={1,0}+rotate+-45+degrees
assert!(q.mul_v(&v).fuzzy_eq(&vec3::new(1f32/sqrt(2f32), 1f32/sqrt(2f32), 0f32))); assert!(q.mul_v(&v).fuzzy_eq(&vec3::new(1f32/2f32.sqrt(), 1f32/2f32.sqrt(), 0f32)));
assert!(q.mul_v(&v).length() == v.length()); assert!(q.mul_v(&v).length() == v.length());
assert!(q.to_mat3().fuzzy_eq(&mat3::new( 1f32/sqrt(2f32), 1f32/sqrt(2f32), 0f32, assert!(q.to_mat3().fuzzy_eq(&mat3::new( 1f32/2f32.sqrt(), 1f32/2f32.sqrt(), 0f32,
-1f32/sqrt(2f32), 1f32/sqrt(2f32), 0f32, -1f32/2f32.sqrt(), 1f32/2f32.sqrt(), 0f32,
0f32, 0f32, 1f32))); 0f32, 0f32, 1f32)));
} }

View file

@ -1,6 +1,5 @@
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use numeric::float::Float::frac_pi_2; use std::num::Real::{frac_pi_2, frac_pi_3};
use numeric::float::Float::frac_pi_3;
use vec::*; use vec::*;

View file

@ -1,8 +1,7 @@
use core::cast::transmute; use std::cast::transmute;
use core::cmp::ApproxEq; use std::cmp::ApproxEq;
use core::num::Zero::zero; use std::num::{Zero, One};
use core::num::One::one; use std::util;
use core::util;
use num::NumAssign; use num::NumAssign;
@ -538,7 +537,7 @@ impl<T:Copy + Eq> BaseVec<T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
unsafe { unsafe {
&'a mut transmute::< &mut transmute::<
&'a mut Vec2<T>, &'a mut Vec2<T>,
&'a mut [T,..2] &'a mut [T,..2]
>(self)[i] >(self)[i]
@ -568,18 +567,20 @@ impl<T:Copy + Eq> Index<uint, T> for Vec2<T> {
impl<T:Copy + Num + NumAssign> NumVec<T> for Vec2<T> { impl<T:Copy + Num + NumAssign> NumVec<T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn identity() -> Vec2<T> { fn identity() -> Vec2<T> {
BaseVec2::new(one::<T>(), one::<T>()) BaseVec2::new(One::one::<T>(),
One::one::<T>())
} }
#[inline(always)] #[inline(always)]
fn zero() -> Vec2<T> { fn zero() -> Vec2<T> {
BaseVec2::new(zero::<T>(), zero::<T>()) BaseVec2::new(Zero::zero::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self[0] == zero() && self[0] == Zero::zero() &&
self[1] == zero() self[1] == Zero::zero()
} }
#[inline(always)] #[inline(always)]
@ -665,12 +666,14 @@ impl<T:Copy + Num> Neg<Vec2<T>> for Vec2<T> {
impl<T:Copy + Num> NumVec2<T> for Vec2<T> { impl<T:Copy + Num> NumVec2<T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn unit_x() -> Vec2<T> { fn unit_x() -> Vec2<T> {
BaseVec2::new(one::<T>(), zero::<T>()) BaseVec2::new(One::one::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn unit_y() -> Vec2<T> { fn unit_y() -> Vec2<T> {
BaseVec2::new(zero::<T>(), one::<T>()) BaseVec2::new(Zero::zero::<T>(),
One::one::<T>())
} }
#[inline(always)] #[inline(always)]
@ -682,7 +685,7 @@ impl<T:Copy + Num> NumVec2<T> for Vec2<T> {
impl<T:Copy + Num> ToHomogeneous<Vec3<T>> for Vec2<T> { impl<T:Copy + Num> ToHomogeneous<Vec3<T>> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn to_homogeneous(&self) -> Vec3<T> { fn to_homogeneous(&self) -> Vec3<T> {
BaseVec3::new(self.x, self.y, zero()) BaseVec3::new(self.x, self.y, Zero::zero())
} }
} }
@ -714,7 +717,7 @@ impl<T:Copy + Real + NumAssign> AffineVec<T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn normalize(&self) -> Vec2<T> { fn normalize(&self) -> Vec2<T> {
self.mul_t(one::<T>()/self.length()) self.mul_t(One::one::<T>()/self.length())
} }
#[inline(always)] #[inline(always)]
@ -729,7 +732,7 @@ impl<T:Copy + Real + NumAssign> AffineVec<T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn normalize_self(&mut self) { fn normalize_self(&mut self) {
let n = one::<T>() / self.length(); let n = One::one::<T>() / self.length();
self.mul_self_t(n); self.mul_self_t(n);
} }
@ -845,7 +848,7 @@ impl<T:Copy + Eq> BaseVec<T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
unsafe { unsafe {
&'a mut transmute::< &mut transmute::<
&'a mut Vec3<T>, &'a mut Vec3<T>,
&'a mut [T,..3] &'a mut [T,..3]
>(self)[i] >(self)[i]
@ -875,19 +878,23 @@ impl<T:Copy + Eq> Index<uint, T> for Vec3<T> {
impl<T:Copy + Num + NumAssign> NumVec<T> for Vec3<T> { impl<T:Copy + Num + NumAssign> NumVec<T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn identity() -> Vec3<T> { fn identity() -> Vec3<T> {
BaseVec3::new(one::<T>(), one::<T>(), one::<T>()) BaseVec3::new(One::one::<T>(),
One::one::<T>(),
One::one::<T>())
} }
#[inline(always)] #[inline(always)]
fn zero() -> Vec3<T> { fn zero() -> Vec3<T> {
BaseVec3::new(zero::<T>(), zero::<T>(), zero::<T>()) BaseVec3::new(Zero::zero::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self[0] == zero() && self[0] == Zero::zero() &&
self[1] == zero() && self[1] == Zero::zero() &&
self[2] == zero() self[2] == Zero::zero()
} }
#[inline(always)] #[inline(always)]
@ -975,17 +982,23 @@ impl<T:Copy + Num> Neg<Vec3<T>> for Vec3<T> {
impl<T:Copy + Num> NumVec3<T> for Vec3<T> { impl<T:Copy + Num> NumVec3<T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn unit_x() -> Vec3<T> { fn unit_x() -> Vec3<T> {
BaseVec3::new(one::<T>(), zero::<T>(), zero::<T>()) BaseVec3::new(One::one::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn unit_y() -> Vec3<T> { fn unit_y() -> Vec3<T> {
BaseVec3::new(zero::<T>(), one::<T>(), zero::<T>()) BaseVec3::new(Zero::zero::<T>(),
One::one::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn unit_z() -> Vec3<T> { fn unit_z() -> Vec3<T> {
BaseVec3::new(zero::<T>(), zero::<T>(), one::<T>()) BaseVec3::new(Zero::zero::<T>(),
Zero::zero::<T>(),
One::one::<T>())
} }
#[inline(always)] #[inline(always)]
@ -1004,7 +1017,7 @@ impl<T:Copy + Num> NumVec3<T> for Vec3<T> {
impl<T:Copy + Num> ToHomogeneous<Vec4<T>> for Vec3<T> { impl<T:Copy + Num> ToHomogeneous<Vec4<T>> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn to_homogeneous(&self) -> Vec4<T> { fn to_homogeneous(&self) -> Vec4<T> {
BaseVec4::new(self.x, self.y, self.z, zero()) BaseVec4::new(self.x, self.y, self.z, Zero::zero())
} }
} }
@ -1036,7 +1049,7 @@ impl<T:Copy + Real + NumAssign> AffineVec<T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn normalize(&self) -> Vec3<T> { fn normalize(&self) -> Vec3<T> {
self.mul_t(one::<T>()/self.length()) self.mul_t(One::one::<T>()/self.length())
} }
#[inline(always)] #[inline(always)]
@ -1051,7 +1064,7 @@ impl<T:Copy + Real + NumAssign> AffineVec<T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn normalize_self(&mut self) { fn normalize_self(&mut self) {
let n = one::<T>() / self.length(); let n = One::one::<T>() / self.length();
self.mul_self_t(n); self.mul_self_t(n);
} }
@ -1169,7 +1182,7 @@ impl<T:Copy + Eq> BaseVec<T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
unsafe { unsafe {
&'a mut transmute::< &mut transmute::<
&'a mut Vec4<T>, &'a mut Vec4<T>,
&'a mut [T,..4] &'a mut [T,..4]
>(self)[i] >(self)[i]
@ -1199,20 +1212,26 @@ impl<T:Copy + Eq> Index<uint, T> for Vec4<T> {
impl<T:Copy + Num + NumAssign> NumVec<T> for Vec4<T> { impl<T:Copy + Num + NumAssign> NumVec<T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn identity() -> Vec4<T> { fn identity() -> Vec4<T> {
BaseVec4::new(one::<T>(), one::<T>(), one::<T>(), one::<T>()) BaseVec4::new(One::one::<T>(),
One::one::<T>(),
One::one::<T>(),
One::one::<T>())
} }
#[inline(always)] #[inline(always)]
fn zero() -> Vec4<T> { fn zero() -> Vec4<T> {
BaseVec4::new(zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>()) BaseVec4::new(Zero::zero::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self[0] == zero() && self[0] == Zero::zero() &&
self[1] == zero() && self[1] == Zero::zero() &&
self[2] == zero() && self[2] == Zero::zero() &&
self[3] == zero() self[3] == Zero::zero()
} }
#[inline(always)] #[inline(always)]
@ -1302,22 +1321,34 @@ impl<T:Copy + Num> Neg<Vec4<T>> for Vec4<T> {
impl<T:Copy + Num> NumVec4<T> for Vec4<T> { impl<T:Copy + Num> NumVec4<T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn unit_x() -> Vec4<T> { fn unit_x() -> Vec4<T> {
BaseVec4::new(one::<T>(), zero::<T>(), zero::<T>(), zero::<T>()) BaseVec4::new(One::one::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn unit_y() -> Vec4<T> { fn unit_y() -> Vec4<T> {
BaseVec4::new(zero::<T>(), one::<T>(), zero::<T>(), zero::<T>()) BaseVec4::new(Zero::zero::<T>(),
One::one::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn unit_z() -> Vec4<T> { fn unit_z() -> Vec4<T> {
BaseVec4::new(zero::<T>(), zero::<T>(), one::<T>(), zero::<T>()) BaseVec4::new(Zero::zero::<T>(),
Zero::zero::<T>(),
One::one::<T>(),
Zero::zero::<T>())
} }
#[inline(always)] #[inline(always)]
fn unit_w() -> Vec4<T> { fn unit_w() -> Vec4<T> {
BaseVec4::new(zero::<T>(), zero::<T>(), zero::<T>(), one::<T>()) BaseVec4::new(Zero::zero::<T>(),
Zero::zero::<T>(),
Zero::zero::<T>(),
One::one::<T>())
} }
} }
@ -1349,7 +1380,7 @@ impl<T:Copy + Real + NumAssign> AffineVec<T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn normalize(&self) -> Vec4<T> { fn normalize(&self) -> Vec4<T> {
self.mul_t(one::<T>()/self.length()) self.mul_t(One::one::<T>()/self.length())
} }
#[inline(always)] #[inline(always)]
@ -1364,7 +1395,7 @@ impl<T:Copy + Real + NumAssign> AffineVec<T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn normalize_self(&mut self) { fn normalize_self(&mut self) {
let n = one::<T>() / self.length(); let n = One::one::<T>() / self.length();
self.mul_self_t(n); self.mul_self_t(n);
} }