Rename and move constructors

This commit is contained in:
Brendan Zabarauskas 2012-11-01 17:41:42 +10:00
parent 372d67f2c1
commit 35058611f3
7 changed files with 492 additions and 531 deletions

View file

@ -59,41 +59,33 @@ pub trait Matrix4<T> {
//
pub struct Mat2<T> { data:[Vec2<T> * 2] }
//
// Mat2 Constructor
//
#[inline(always)]
pub pure fn Mat2<T:Copy>(m00: T, m01: T,
m10: T, m11: T) -> Mat2<T> {
Mat2 { data: [ Vec2(m00, m01),
Vec2(m10, m11) ] }
}
//
// Construct Mat2 from column vectors
//
#[inline(always)]
pub pure fn Mat2_v<T:Copy>(col0: &Vec2<T>, col1: &Vec2<T>) -> Mat2<T> {
Mat2 { data: [ *move col0, *move col1 ] }
}
pub mod Mat2 {
#[inline(always)]
pub pure fn zero<T: Num>() -> Mat2<T> {
Mat2 { data: [ Vec2::zero(),
Vec2::zero() ] }
pub pure fn new<T:Copy>(m00: T, m01: T,
m10: T, m11: T) -> Mat2<T> {
Mat2::from_cols(&Vec2::new(m00, m01),
&Vec2::new(m10, m11))
}
#[inline(always)]
pub pure fn from_cols<T:Copy>(col0: &Vec2<T>, col1: &Vec2<T>) -> Mat2<T> {
Mat2 { data: [ *col0, *col1 ] }
}
#[inline(always)]
pub pure fn identity<T: Num>() -> Mat2<T> {
pub pure fn zero<T:Copy Num>() -> Mat2<T> {
Mat2 { data: [ Vec2::zero(),
Vec2::zero() ] }
}
#[inline(always)]
pub pure fn identity<T:Copy Num>() -> Mat2<T> {
Mat2 { data: [ Vec2::unit_x(),
Vec2::unit_y() ] }
}
}
//
// Matrix2x2 Implementation
//
pub impl<T:Copy Num Sqrt FuzzyEq> Mat2<T>: Matrix<T, Vec2<T>> {
#[inline(always)]
pure fn rows() -> uint { 2 }
@ -106,8 +98,8 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat2<T>: Matrix<T, Vec2<T>> {
#[inline(always)]
pure fn row(i: uint) -> Vec2<T> {
Vec2(self[0][i],
self[1][i])
Vec2::new(self[0][i],
self[1][i])
}
#[inline(always)]
@ -117,35 +109,35 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat2<T>: Matrix<T, Vec2<T>> {
#[inline(always)]
pure fn mul_t(value: T) -> Mat2<T> {
Mat2_v(&self[0].mul_t(value),
&self[1].mul_t(value))
Mat2::from_cols(&self[0].mul_t(value),
&self[1].mul_t(value))
}
#[inline(always)]
pure fn mul_v(other: &Vec2<T>) -> Vec2<T> {
Vec2(self[0][0] * other[0] + self[1][0] * other[1],
self[0][1] * other[0] + self[1][1] * other[1])
Vec2::new(self[0][0] * other[0] + self[1][0] * other[1],
self[0][1] * other[0] + self[1][1] * other[1])
}
#[inline(always)]
pure fn add_m(other: &Mat2<T>) -> Mat2<T> {
Mat2_v(&self[0].add_v(&other[0]),
&self[1].add_v(&other[1]))
Mat2::from_cols(&self[0].add_v(&other[0]),
&self[1].add_v(&other[1]))
}
#[inline(always)]
pure fn sub_m(other: &Mat2<T>) -> Mat2<T> {
Mat2_v(&self[0].sub_v(&other[0]),
&self[1].sub_v(&other[1]))
Mat2::from_cols(&self[0].sub_v(&other[0]),
&self[1].sub_v(&other[1]))
}
#[inline(always)]
pure fn mul_m(other: &Mat2<T>) -> Mat2<T> {
Mat2(self[0][0] * other[0][0] + self[1][0] * other[0][1],
self[0][1] * other[0][0] + self[1][1] * other[0][1],
self[0][0] * other[1][0] + self[1][0] * other[1][1],
self[0][1] * other[1][0] + self[1][1] * other[1][1])
Mat2::new(self[0][0] * other[0][0] + self[1][0] * other[0][1],
self[0][1] * other[0][0] + self[1][1] * other[0][1],
self[0][0] * other[1][0] + self[1][0] * other[1][1],
self[0][1] * other[1][0] + self[1][1] * other[1][1])
}
// TODO - inversion is harrrd D:
@ -154,8 +146,8 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat2<T>: Matrix<T, Vec2<T>> {
#[inline(always)]
pure fn transpose() -> Mat2<T> {
Mat2(self[0][0], self[1][0],
self[0][1], self[1][1])
Mat2::new(self[0][0], self[1][0],
self[0][1], self[1][1])
}
#[inline(always)]
@ -191,7 +183,7 @@ pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
pub impl<T:Copy Neg<T>> Mat2<T>: Neg<Mat2<T>> {
#[inline(always)]
pure fn neg() -> Mat2<T> {
Mat2_v(&-self[0], &-self[1])
Mat2::from_cols(&-self[0], &-self[1])
}
}
@ -234,45 +226,37 @@ pub impl<T:Copy FuzzyEq> Mat2<T>: FuzzyEq {
//
pub struct Mat3<T> { data:[Vec3<T> * 3] }
//
// Mat3 Constructor
//
#[inline(always)]
pub pure fn Mat3<T:Copy>(m00:T, m01:T, m02:T,
m10:T, m11:T, m12:T,
m20:T, m21:T, m22:T) -> Mat3<T> {
Mat3 { data: [ Vec3(m00, m01, m02),
Vec3(m10, m11, m12),
Vec3(m20, m21, m22) ] }
}
//
// Construct Mat3 from column vectors
//
#[inline(always)]
pub pure fn Mat3_v<T:Copy>(col0: &Vec3<T>, col1: &Vec3<T>, col2: &Vec3<T>) -> Mat3<T> {
Mat3 { data: [ *move col0, *move col1, *move col2 ] }
}
pub mod Mat3 {
#[inline(always)]
pub pure fn zero<T: Num>() -> Mat3<T> {
Mat3 { data: [ Vec3::zero(),
Vec3::zero(),
Vec3::zero() ] }
pub pure fn new<T:Copy>(m00:T, m01:T, m02:T,
m10:T, m11:T, m12:T,
m20:T, m21:T, m22:T) -> Mat3<T> {
Mat3::from_cols(&Vec3::new(m00, m01, m02),
&Vec3::new(m10, m11, m12),
&Vec3::new(m20, m21, m22))
}
#[inline(always)]
pub pure fn identity<T: Num>() -> Mat3<T> {
pub pure fn from_cols<T:Copy>(col0: &Vec3<T>, col1: &Vec3<T>, col2: &Vec3<T>) -> Mat3<T> {
Mat3 { data: [ *col0, *col1, *col2 ] }
}
#[inline(always)]
pub pure fn zero<T:Num>() -> Mat3<T> {
Mat3 { data: [ Vec3::zero(),
Vec3::zero(),
Vec3::zero() ] }
}
#[inline(always)]
pub pure fn identity<T:Num>() -> Mat3<T> {
Mat3 { data: [ Vec3::unit_x(),
Vec3::unit_y(),
Vec3::unit_z() ] }
}
}
//
// Matrix3x3 Implementation
//
pub impl<T:Copy Num Sqrt FuzzyEq> Mat3<T>: Matrix<T, Vec3<T>> {
#[inline(always)]
pure fn rows() -> uint { 3 }
@ -285,9 +269,9 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat3<T>: Matrix<T, Vec3<T>> {
#[inline(always)]
pure fn row(i: uint) -> Vec3<T> {
Vec3(self[0][i],
self[1][i],
self[2][i])
Vec3::new(self[0][i],
self[1][i],
self[2][i])
}
#[inline(always)]
@ -297,45 +281,45 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat3<T>: Matrix<T, Vec3<T>> {
#[inline(always)]
pure fn mul_t(value: T) -> Mat3<T> {
Mat3_v(&self[0].mul_t(value),
&self[1].mul_t(value),
&self[2].mul_t(value))
Mat3::from_cols(&self[0].mul_t(value),
&self[1].mul_t(value),
&self[2].mul_t(value))
}
#[inline(always)]
pure fn mul_v(other: &Vec3<T>) -> Vec3<T> {
Vec3(self[0][0] * other[0] + self[1][0] * other[1] + self[2][0] * other[2],
self[0][1] * other[0] + self[1][1] * other[1] + self[2][1] * other[2],
self[0][2] * other[0] + self[1][2] * other[1] + self[2][2] * other[2])
Vec3::new(self[0][0] * other[0] + self[1][0] * other[1] + self[2][0] * other[2],
self[0][1] * other[0] + self[1][1] * other[1] + self[2][1] * other[2],
self[0][2] * other[0] + self[1][2] * other[1] + self[2][2] * other[2])
}
#[inline(always)]
pure fn add_m(other: &Mat3<T>) -> Mat3<T> {
Mat3_v(&self[0].add_v(&other[0]),
&self[1].add_v(&other[1]),
&self[2].add_v(&other[2]))
Mat3::from_cols(&self[0].add_v(&other[0]),
&self[1].add_v(&other[1]),
&self[2].add_v(&other[2]))
}
#[inline(always)]
pure fn sub_m(other: &Mat3<T>) -> Mat3<T> {
Mat3_v(&self[0].sub_v(&other[0]),
&self[1].sub_v(&other[1]),
&self[2].sub_v(&other[2]))
Mat3::from_cols(&self[0].sub_v(&other[0]),
&self[1].sub_v(&other[1]),
&self[2].sub_v(&other[2]))
}
#[inline(always)]
pure fn mul_m(other: &Mat3<T>) -> Mat3<T> {
Mat3(self[0][0] * other[0][0] + self[1][0] * other[0][1] + self[2][0] * other[0][2],
self[0][1] * other[0][0] + self[1][1] * other[0][1] + self[2][1] * other[0][2],
self[0][2] * other[0][0] + self[1][2] * other[0][1] + self[2][2] * other[0][2],
self[0][0] * other[1][0] + self[1][0] * other[1][1] + self[2][0] * other[1][2],
self[0][1] * other[1][0] + self[1][1] * other[1][1] + self[2][1] * other[1][2],
self[0][2] * other[1][0] + self[1][2] * other[1][1] + self[2][2] * other[1][2],
self[0][0] * other[2][0] + self[1][0] * other[2][1] + self[2][0] * other[2][2],
self[0][1] * other[2][0] + self[1][1] * other[2][1] + self[2][1] * other[2][2],
self[0][2] * other[2][0] + self[1][2] * other[2][1] + self[2][2] * other[2][2])
Mat3::new(self[0][0] * other[0][0] + self[1][0] * other[0][1] + self[2][0] * other[0][2],
self[0][1] * other[0][0] + self[1][1] * other[0][1] + self[2][1] * other[0][2],
self[0][2] * other[0][0] + self[1][2] * other[0][1] + self[2][2] * other[0][2],
self[0][0] * other[1][0] + self[1][0] * other[1][1] + self[2][0] * other[1][2],
self[0][1] * other[1][0] + self[1][1] * other[1][1] + self[2][1] * other[1][2],
self[0][2] * other[1][0] + self[1][2] * other[1][1] + self[2][2] * other[1][2],
self[0][0] * other[2][0] + self[1][0] * other[2][1] + self[2][0] * other[2][2],
self[0][1] * other[2][0] + self[1][1] * other[2][1] + self[2][1] * other[2][2],
self[0][2] * other[2][0] + self[1][2] * other[2][1] + self[2][2] * other[2][2])
}
// TODO - inversion is harrrd D:
@ -344,9 +328,9 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat3<T>: Matrix<T, Vec3<T>> {
#[inline(always)]
pure fn transpose() -> Mat3<T> {
Mat3(self[0][0], self[1][0], self[2][0],
self[0][1], self[1][1], self[2][1],
self[0][2], self[1][2], self[2][2])
Mat3::new(self[0][0], self[1][0], self[2][0],
self[0][1], self[1][1], self[2][1],
self[0][2], self[1][2], self[2][2])
}
#[inline(always)]
@ -387,17 +371,17 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat3<T>: Matrix<T, Vec3<T>> {
pub impl<T:Copy Num Sqrt FuzzyEq> Mat3<T>: Matrix3<T> {
#[inline(always)]
pure fn scale(vec: &Vec3<T>) -> Mat3<T> {
self.mul_m(&Mat3( vec.x, from_int(0), from_int(0),
from_int(0), vec.y, from_int(0),
from_int(0), from_int(0), vec.z))
self.mul_m(&Mat3::new( vec.x, from_int(0), from_int(0),
from_int(0), vec.y, from_int(0),
from_int(0), from_int(0), vec.z))
}
#[inline(always)]
pure fn to_Mat4() -> Mat4<T> {
Mat4( self[0][0], self[0][1], self[0][2], from_int(0),
self[1][0], self[1][1], self[1][2], from_int(0),
self[2][0], self[2][1], self[2][2], from_int(0),
from_int(0), from_int(0), from_int(0), from_int(1))
Mat4::new( self[0][0], self[0][1], self[0][2], from_int(0),
self[1][0], self[1][1], self[1][2], from_int(0),
self[2][0], self[2][1], self[2][2], from_int(0),
from_int(0), from_int(0), from_int(0), from_int(1))
}
}
@ -440,7 +424,7 @@ pub impl<T:Copy Num NumCast Ord> Mat3<T>: ToQuat<T> {
z = (self[0][1] - self[1][0]).cast::<float>() * s;
}
Quat(from(w), from(x), from(y), from(z))
Quat::new(cast(w), cast(x), cast(y), cast(z))
}
}
@ -454,7 +438,7 @@ pub impl<T:Copy> Mat3<T>: Index<uint, Vec3<T>> {
pub impl<T:Copy Neg<T>> Mat3<T>: Neg<Mat3<T>> {
#[inline(always)]
pure fn neg() -> Mat3<T> {
Mat3_v(&-self[0], &-self[1], &-self[2])
Mat3::from_cols(&-self[0], &-self[1], &-self[2])
}
}
@ -506,39 +490,34 @@ pub impl<T:Copy> Mat3<T>: ToPtr<T> {
//
pub struct Mat4<T> { data:[Vec4<T> * 4] }
//
// Mat4 Constructor
//
#[inline(always)]
pub pure fn Mat4<T:Copy>(m00: T, m01: T, m02: T, m03: T,
m10: T, m11: T, m12: T, m13: T,
m20: T, m21: T, m22: T, m23: T,
m30: T, m31: T, m32: T, m33: T) -> Mat4<T> {
Mat4 { data: [ Vec4(m00, m01, m02, m03),
Vec4(m10, m11, m12, m13),
Vec4(m20, m21, m22, m23),
Vec4(m30, m31, m32, m33) ] }
}
//
// Construct Mat4 from column vectors
//
#[inline(always)]
pub pure fn Mat4_v<T:Copy>(col0: &Vec4<T>, col1: &Vec4<T>, col2: &Vec4<T>, col3: &Vec4<T>) -> Mat4<T> {
Mat4 { data: [ *move col0, *move col1, *move col2, *move col3 ] }
}
pub mod Mat4 {
#[inline(always)]
pub pure fn zero<T: Num>() -> Mat4<T> {
Mat4 { data: [ Vec4::zero(),
Vec4::zero(),
Vec4::zero(),
Vec4::zero() ] }
pub pure fn new<T:Copy>(m00: T, m01: T, m02: T, m03: T,
m10: T, m11: T, m12: T, m13: T,
m20: T, m21: T, m22: T, m23: T,
m30: T, m31: T, m32: T, m33: T) -> Mat4<T> {
Mat4::from_cols(&Vec4::new(m00, m01, m02, m03),
&Vec4::new(m10, m11, m12, m13),
&Vec4::new(m20, m21, m22, m23),
&Vec4::new(m30, m31, m32, m33))
}
#[inline(always)]
pub pure fn identity<T: Num>() -> Mat4<T> {
pub pure fn from_cols<T:Copy>(col0: &Vec4<T>, col1: &Vec4<T>, col2: &Vec4<T>, col3: &Vec4<T>) -> Mat4<T> {
Mat4 { data: [ *col0, *col1, *col2, *col3 ] }
}
#[inline(always)]
pub pure fn zero<T:Num>() -> Mat4<T> {
Mat4 { data: [ Vec4::zero(),
Vec4::zero(),
Vec4::zero(),
Vec4::zero() ] }
}
#[inline(always)]
pub pure fn identity<T:Num>() -> Mat4<T> {
Mat4 { data: [ Vec4::unit_x(),
Vec4::unit_y(),
Vec4::unit_z(),
@ -546,9 +525,6 @@ pub mod Mat4 {
}
}
//
// Matrix4x4 Implementation
//
pub impl<T:Copy Num Sqrt FuzzyEq> Mat4<T>: Matrix<T, Vec4<T>> {
#[inline(always)]
pure fn rows() -> uint { 4 }
@ -561,10 +537,10 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat4<T>: Matrix<T, Vec4<T>> {
#[inline(always)]
pure fn row(i: uint) -> Vec4<T> {
Vec4(self[0][i],
self[1][i],
self[2][i],
self[3][i])
Vec4::new(self[0][i],
self[1][i],
self[2][i],
self[3][i])
}
#[inline(always)]
@ -574,57 +550,57 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat4<T>: Matrix<T, Vec4<T>> {
#[inline(always)]
pure fn mul_t(value: T) -> Mat4<T> {
Mat4_v(&self[0].mul_t(value),
&self[1].mul_t(value),
&self[2].mul_t(value),
&self[3].mul_t(value))
Mat4::from_cols(&self[0].mul_t(value),
&self[1].mul_t(value),
&self[2].mul_t(value),
&self[3].mul_t(value))
}
#[inline(always)]
pure fn mul_v(other: &Vec4<T>) -> Vec4<T> {
Vec4(self[0][0] * other[0] + self[1][0] * other[1] + self[2][0] * other[2] + self[3][0] * other[3],
self[0][1] * other[0] + self[1][1] * other[1] + self[2][1] * other[2] + self[3][1] * other[3],
self[0][2] * other[0] + self[1][2] * other[1] + self[2][2] * other[2] + self[3][2] * other[3],
self[0][3] * other[0] + self[1][3] * other[1] + self[2][3] * other[2] + self[3][3] * other[3])
Vec4::new(self[0][0] * other[0] + self[1][0] * other[1] + self[2][0] * other[2] + self[3][0] * other[3],
self[0][1] * other[0] + self[1][1] * other[1] + self[2][1] * other[2] + self[3][1] * other[3],
self[0][2] * other[0] + self[1][2] * other[1] + self[2][2] * other[2] + self[3][2] * other[3],
self[0][3] * other[0] + self[1][3] * other[1] + self[2][3] * other[2] + self[3][3] * other[3])
}
#[inline(always)]
pure fn add_m(other: &Mat4<T>) -> Mat4<T> {
Mat4_v(&self[0].add_v(&other[0]),
&self[1].add_v(&other[1]),
&self[2].add_v(&other[2]),
&self[3].add_v(&other[3]))
Mat4::from_cols(&self[0].add_v(&other[0]),
&self[1].add_v(&other[1]),
&self[2].add_v(&other[2]),
&self[3].add_v(&other[3]))
}
#[inline(always)]
pure fn sub_m(other: &Mat4<T>) -> Mat4<T> {
Mat4_v(&self[0].sub_v(&other[0]),
&self[1].sub_v(&other[1]),
&self[2].sub_v(&other[2]),
&self[3].sub_v(&other[3]))
Mat4::from_cols(&self[0].sub_v(&other[0]),
&self[1].sub_v(&other[1]),
&self[2].sub_v(&other[2]),
&self[3].sub_v(&other[3]))
}
#[inline(always)]
pure fn mul_m(other: &Mat4<T>) -> Mat4<T> {
Mat4(self[0][0] * other[0][0] + self[1][0] * other[0][1] + self[2][0] * other[0][2] + self[3][0] * other[0][3],
self[0][1] * other[0][0] + self[1][1] * other[0][1] + self[2][1] * other[0][2] + self[3][1] * other[0][3],
self[0][2] * other[0][0] + self[1][2] * other[0][1] + self[2][2] * other[0][2] + self[3][2] * other[0][3],
self[0][3] * other[0][0] + self[1][3] * other[0][1] + self[2][3] * other[0][2] + self[3][3] * other[0][3],
self[0][0] * other[1][0] + self[1][0] * other[1][1] + self[2][0] * other[1][2] + self[3][0] * other[1][3],
self[0][1] * other[1][0] + self[1][1] * other[1][1] + self[2][1] * other[1][2] + self[3][1] * other[1][3],
self[0][2] * other[1][0] + self[1][2] * other[1][1] + self[2][2] * other[1][2] + self[3][2] * other[1][3],
self[0][3] * other[1][0] + self[1][3] * other[1][1] + self[2][3] * other[1][2] + self[3][3] * other[1][3],
self[0][0] * other[2][0] + self[1][0] * other[2][1] + self[2][0] * other[2][2] + self[3][0] * other[2][3],
self[0][1] * other[2][0] + self[1][1] * other[2][1] + self[2][1] * other[2][2] + self[3][1] * other[2][3],
self[0][2] * other[2][0] + self[1][2] * other[2][1] + self[2][2] * other[2][2] + self[3][2] * other[2][3],
self[0][3] * other[2][0] + self[1][3] * other[2][1] + self[2][3] * other[2][2] + self[3][3] * other[2][3],
self[0][0] * other[3][0] + self[1][0] * other[3][1] + self[2][0] * other[3][2] + self[3][0] * other[3][3],
self[0][1] * other[3][0] + self[1][1] * other[3][1] + self[2][1] * other[3][2] + self[3][1] * other[3][3],
self[0][2] * other[3][0] + self[1][2] * other[3][1] + self[2][2] * other[3][2] + self[3][2] * other[3][3],
self[0][3] * other[3][0] + self[1][3] * other[3][1] + self[2][3] * other[3][2] + self[3][3] * other[3][3])
Mat4::new(self[0][0] * other[0][0] + self[1][0] * other[0][1] + self[2][0] * other[0][2] + self[3][0] * other[0][3],
self[0][1] * other[0][0] + self[1][1] * other[0][1] + self[2][1] * other[0][2] + self[3][1] * other[0][3],
self[0][2] * other[0][0] + self[1][2] * other[0][1] + self[2][2] * other[0][2] + self[3][2] * other[0][3],
self[0][3] * other[0][0] + self[1][3] * other[0][1] + self[2][3] * other[0][2] + self[3][3] * other[0][3],
self[0][0] * other[1][0] + self[1][0] * other[1][1] + self[2][0] * other[1][2] + self[3][0] * other[1][3],
self[0][1] * other[1][0] + self[1][1] * other[1][1] + self[2][1] * other[1][2] + self[3][1] * other[1][3],
self[0][2] * other[1][0] + self[1][2] * other[1][1] + self[2][2] * other[1][2] + self[3][2] * other[1][3],
self[0][3] * other[1][0] + self[1][3] * other[1][1] + self[2][3] * other[1][2] + self[3][3] * other[1][3],
self[0][0] * other[2][0] + self[1][0] * other[2][1] + self[2][0] * other[2][2] + self[3][0] * other[2][3],
self[0][1] * other[2][0] + self[1][1] * other[2][1] + self[2][1] * other[2][2] + self[3][1] * other[2][3],
self[0][2] * other[2][0] + self[1][2] * other[2][1] + self[2][2] * other[2][2] + self[3][2] * other[2][3],
self[0][3] * other[2][0] + self[1][3] * other[2][1] + self[2][3] * other[2][2] + self[3][3] * other[2][3],
self[0][0] * other[3][0] + self[1][0] * other[3][1] + self[2][0] * other[3][2] + self[3][0] * other[3][3],
self[0][1] * other[3][0] + self[1][1] * other[3][1] + self[2][1] * other[3][2] + self[3][1] * other[3][3],
self[0][2] * other[3][0] + self[1][2] * other[3][1] + self[2][2] * other[3][2] + self[3][2] * other[3][3],
self[0][3] * other[3][0] + self[1][3] * other[3][1] + self[2][3] * other[3][2] + self[3][3] * other[3][3])
}
// TODO - inversion is harrrd D:
@ -633,10 +609,10 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat4<T>: Matrix<T, Vec4<T>> {
#[inline(always)]
pure fn transpose() -> Mat4<T> {
Mat4(self[0][0], self[1][0], self[2][0], self[3][0],
self[0][1], self[1][1], self[2][1], self[3][1],
self[0][2], self[1][2], self[2][2], self[3][2],
self[0][3], self[1][3], self[2][3], self[3][3])
Mat4::new(self[0][0], self[1][0], self[2][0], self[3][0],
self[0][1], self[1][1], self[2][1], self[3][1],
self[0][2], self[1][2], self[2][2], self[3][2],
self[0][3], self[1][3], self[2][3], self[3][3])
}
#[inline(always)]
@ -691,21 +667,21 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Mat4<T>: Matrix<T, Vec4<T>> {
pub impl<T:Copy Num Sqrt FuzzyEq> Mat4<T>: Matrix4<T> {
#[inline(always)]
pure fn scale(vec: &Vec3<T>) -> Mat4<T> {
self.mul_m(&Mat4( vec.x, from_int(0), from_int(0), from_int(0),
from_int(0), vec.y, from_int(0), from_int(0),
from_int(0), from_int(0), vec.z, from_int(0),
from_int(0), from_int(0), from_int(0), from_int(1)))
self.mul_m(&Mat4::new( vec.x, from_int(0), from_int(0), from_int(0),
from_int(0), vec.y, from_int(0), from_int(0),
from_int(0), from_int(0), vec.z, from_int(0),
from_int(0), from_int(0), from_int(0), from_int(1)))
}
#[inline(always)]
pure fn translate(vec: &Vec3<T>) -> Mat4<T> {
Mat4_v(&self[0],
&self[1],
&self[2],
&Vec4(self[3][0] + vec.x,
self[3][1] + vec.y,
self[3][2] + vec.z,
self[3][3]))
Mat4::from_cols(&self[0],
&self[1],
&self[2],
&Vec4::new(self[3][0] + vec.x,
self[3][1] + vec.y,
self[3][2] + vec.z,
self[3][3]))
}
}
@ -719,7 +695,7 @@ pub impl<T:Copy> Mat4<T>: Index<uint, Vec4<T>> {
pub impl<T:Copy Neg<T>> Mat4<T>: Neg<Mat4<T>> {
#[inline(always)]
pure fn neg() -> Mat4<T> {
Mat4_v(&-self[0], &-self[1], &-self[2], &-self[3])
Mat4::from_cols(&-self[0], &-self[1], &-self[2], &-self[3])
}
}

View file

@ -43,8 +43,8 @@ pure fn frustum<T:Copy NumCast Neg<T>>(left: float, right: float, bottom: float,
let m32 = cast(-(2f * far * near) / (far - near));
let m33 = cast(0f);
return Mat4(m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33);
return Mat4::new(m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33);
}

View file

@ -42,74 +42,66 @@ pub trait ToQuat<T> {
//
// Quat struct definition
//
pub struct Quat<T> { w: T, x: T, y: T, z: T }
//
// Quat Constructor
//
#[inline(always)]
pub pure fn Quat<T:Copy>(w: T, x: T, y: T, z: T) -> Quat<T> {
Quat { w: move w, x: move x, y: move y, z: move z }
}
pub mod Quat {
#[inline(always)]
pub pure fn new<T:Copy>(w: T, x: T, y: T, z: T) -> Quat<T> {
Quat { w: w, x: x, y: y, z: z }
}
#[inline(always)] pub pure fn zero <T: Num>() -> Quat<T> { Quat { w: from_int(0), x: from_int(0), y: from_int(0), z: from_int(0) } }
#[inline(always)] pub pure fn identity <T: Num>() -> Quat<T> { Quat { w: from_int(1), x: from_int(0), y: from_int(0), z: from_int(0) } }
}
//
// Quaternion Implementation
//
pub impl<T:Copy Num Sqrt FuzzyEq> Quat<T>: Quaternion<T> {
#[inline(always)]
pure fn dim() -> uint { 4 }
#[inline(always)]
pure fn mul_t(value: T) -> Quat<T> {
Quat(self[0] * value,
self[1] * value,
self[2] * value,
self[3] * value)
Quat::new(self[0] * value,
self[1] * value,
self[2] * value,
self[3] * value)
}
#[inline(always)]
pure fn div_t(value: T) -> Quat<T> {
Quat(self[0] / value,
self[1] / value,
self[2] / value,
self[3] / value)
Quat::new(self[0] / value,
self[1] / value,
self[2] / value,
self[3] / value)
}
#[inline(always)]
pure fn add_q(other: &Quat<T>) -> Quat<T> {
Quat(self[0] + other[0],
self[1] + other[1],
self[2] + other[2],
self[3] + other[3])
Quat::new(self[0] + other[0],
self[1] + other[1],
self[2] + other[2],
self[3] + other[3])
}
#[inline(always)]
pure fn sub_q(other: &Quat<T>) -> Quat<T> {
Quat(self[0] - other[0],
self[1] - other[1],
self[2] - other[2],
self[3] - other[3])
Quat::new(self[0] - other[0],
self[1] - other[1],
self[2] - other[2],
self[3] - other[3])
}
#[inline(always)]
pure fn mul_q(other: &Quat<T>) -> Quat<T> {
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)
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)
}
#[inline(always)]
pure fn conjugate() -> Quat<T> {
Quat(self.w, -self.x, -self.y, -self.z)
Quat::new(self.w, -self.x, -self.y, -self.z)
}
#[inline(always)]
@ -152,9 +144,9 @@ pub impl<T:Copy Num Sqrt FuzzyEq> Quat<T>: Quaternion<T> {
let _1: T = from_int(1);
Mat3(_1 - yy2 - zz2, xy2 - wz2, xz2 + wy2,
xy2 + wz2, _1 - xx2 - zz2, yz2 - wx2,
xz2 - wy2, yz2 + wx2, _1 - xx2 - yy2)
Mat3::new(_1 - yy2 - zz2, xy2 - wz2, xz2 + wy2,
xy2 + wz2, _1 - xx2 - zz2, yz2 - wx2,
xz2 - wy2, yz2 + wx2, _1 - xx2 - yy2)
}
#[inline(always)]
@ -177,7 +169,7 @@ pub impl<T:Copy> Quat<T>: Index<uint, T> {
pub impl<T:Copy Neg<T>> Quat<T>: Neg<Quat<T>> {
#[inline(always)]
pure fn neg() -> Quat<T> {
Quat(-self[0], -self[1], -self[2], -self[3])
Quat::new(-self[0], -self[1], -self[2], -self[3])
}
}

View file

@ -9,41 +9,41 @@ fn test_Mat2() {
Vec2 { x: 2f, y: 4f } ] };
let b = Mat2 { data: [ Vec2 { x: 2f, y: 4f },
Vec2 { x: 3f, y: 5f } ] };
let v1 = Vec2(1f, 2f);
let v1 = Vec2::new(1f, 2f);
let f1 = 0.5f;
assert a == Mat2(1f, 3f,
2f, 4f);
assert a == Mat2::new(1f, 3f,
2f, 4f);
assert a == Mat2_v(&Vec2(1f, 3f),
&Vec2(2f, 4f));
assert a == Mat2::from_cols(&Vec2::new(1f, 3f),
&Vec2::new(2f, 4f));
assert a[0] == Vec2(1f, 3f);
assert a[1] == Vec2(2f, 4f);
assert a[0] == Vec2::new(1f, 3f);
assert a[1] == Vec2::new(2f, 4f);
assert a.row(0) == Vec2(1f, 2f);
assert a.row(1) == Vec2(3f, 4f);
assert a.row(0) == Vec2::new(1f, 2f);
assert a.row(1) == Vec2::new(3f, 4f);
assert a.col(0) == Vec2(1f, 3f);
assert a.col(1) == Vec2(2f, 4f);
assert a.col(0) == Vec2::new(1f, 3f);
assert a.col(1) == Vec2::new(2f, 4f);
assert a.neg() == Mat2(-1f, -3f,
-2f, -4f);
assert a.neg() == Mat2::new(-1f, -3f,
-2f, -4f);
assert -a == a.neg();
assert a.mul_t(f1) == Mat2(0.5f, 1.5f,
1.0f, 2.0f);
assert a.mul_v(&v1) == Vec2(5f, 11f);
assert a.mul_t(f1) == Mat2::new(0.5f, 1.5f,
1.0f, 2.0f);
assert a.mul_v(&v1) == Vec2::new(5f, 11f);
assert a.add_m(&b) == Mat2(3f, 7f,
5f, 9f);
assert a.sub_m(&b) == Mat2(-1f, -1f,
-1f, -1f);
assert a.mul_m(&b) == Mat2(10.0, 22.0,
13.0, 29.0);
assert a.add_m(&b) == Mat2::new(3f, 7f,
5f, 9f);
assert a.sub_m(&b) == Mat2::new(-1f, -1f,
-1f, -1f);
assert a.mul_m(&b) == Mat2::new(10.0, 22.0,
13.0, 29.0);
assert a.transpose() == Mat2(1f, 2f,
3f, 4f);
assert a.transpose() == Mat2::new(1f, 2f,
3f, 4f);
// exact_eq
// fuzzy_eq
@ -59,8 +59,8 @@ fn test_Mat2() {
assert !a.is_diagonal();
assert a.is_rotated();
let c = Mat2(2f, 1f,
1f, 2f);
let c = Mat2::new(2f, 1f,
1f, 2f);
assert !c.is_identity();
assert c.is_symmetric();
assert !c.is_diagonal();
@ -75,52 +75,52 @@ fn test_Mat3() {
let b = Mat3 { data: [ Vec3 { x: 2f, y: 5f, z: 8f },
Vec3 { x: 3f, y: 6f, z: 9f },
Vec3 { x: 4f, y: 7f, z: 10f } ] };
let v1 = Vec3(1f, 2f, 3f);
let v1 = Vec3::new(1f, 2f, 3f);
let f1 = 0.5f;
assert a == Mat3(1f, 4f, 7f,
2f, 5f, 8f,
3f, 6f, 9f);
assert a == Mat3::new(1f, 4f, 7f,
2f, 5f, 8f,
3f, 6f, 9f);
assert a == Mat3_v(&Vec3(1f, 4f, 7f),
&Vec3(2f, 5f, 8f),
&Vec3(3f, 6f, 9f));
assert a == Mat3::from_cols(&Vec3::new(1f, 4f, 7f),
&Vec3::new(2f, 5f, 8f),
&Vec3::new(3f, 6f, 9f));
assert a[0] == Vec3(1f, 4f, 7f);
assert a[1] == Vec3(2f, 5f, 8f);
assert a[2] == Vec3(3f, 6f, 9f);
assert a[0] == Vec3::new(1f, 4f, 7f);
assert a[1] == Vec3::new(2f, 5f, 8f);
assert a[2] == Vec3::new(3f, 6f, 9f);
assert a.row(0) == Vec3(1f, 2f, 3f);
assert a.row(1) == Vec3(4f, 5f, 6f);
assert a.row(2) == Vec3(7f, 8f, 9f);
assert a.row(0) == Vec3::new(1f, 2f, 3f);
assert a.row(1) == Vec3::new(4f, 5f, 6f);
assert a.row(2) == Vec3::new(7f, 8f, 9f);
assert a.col(0) == Vec3(1f, 4f, 7f);
assert a.col(1) == Vec3(2f, 5f, 8f);
assert a.col(2) == Vec3(3f, 6f, 9f);
assert a.col(0) == Vec3::new(1f, 4f, 7f);
assert a.col(1) == Vec3::new(2f, 5f, 8f);
assert a.col(2) == Vec3::new(3f, 6f, 9f);
assert a.neg() == Mat3(-1f, -4f, -7f,
-2f, -5f, -8f,
-3f, -6f, -9f);
assert a.neg() == Mat3::new(-1f, -4f, -7f,
-2f, -5f, -8f,
-3f, -6f, -9f);
assert -a == a.neg();
assert a.mul_t(f1) == Mat3(0.5f, 2.0f, 3.5f,
1.0f, 2.5f, 4.0f,
1.5f, 3.0f, 4.5f);
assert a.mul_v(&v1) == Vec3(14f, 32f, 50f);
assert a.mul_t(f1) == Mat3::new(0.5f, 2.0f, 3.5f,
1.0f, 2.5f, 4.0f,
1.5f, 3.0f, 4.5f);
assert a.mul_v(&v1) == Vec3::new(14f, 32f, 50f);
assert a.add_m(&b) == Mat3(3f, 9f, 15f,
5f, 11f, 17f,
7f, 13f, 19f);
assert a.sub_m(&b) == Mat3(-1f, -1f, -1f,
-1f, -1f, -1f,
-1f, -1f, -1f);
assert a.mul_m(&b) == Mat3(36f, 81f, 126f,
42f, 96f, 150f,
48f, 111f, 174f);
assert a.add_m(&b) == Mat3::new(3f, 9f, 15f,
5f, 11f, 17f,
7f, 13f, 19f);
assert a.sub_m(&b) == Mat3::new(-1f, -1f, -1f,
-1f, -1f, -1f,
-1f, -1f, -1f);
assert a.mul_m(&b) == Mat3::new(36f, 81f, 126f,
42f, 96f, 150f,
48f, 111f, 174f);
assert a.transpose() == Mat3(1f, 2f, 3f,
4f, 5f, 6f,
7f, 8f, 9f);
assert a.transpose() == Mat3::new(1f, 2f, 3f,
4f, 5f, 6f,
7f, 8f, 9f);
// exact_eq
// fuzzy_eq
@ -136,18 +136,18 @@ fn test_Mat3() {
assert !a.is_diagonal();
assert a.is_rotated();
let c = Mat3(3f, 2f, 1f,
2f, 3f, 2f,
1f, 2f, 3f);
let c = Mat3::new(3f, 2f, 1f,
2f, 3f, 2f,
1f, 2f, 3f);
assert !c.is_identity();
assert c.is_symmetric();
assert !c.is_diagonal();
assert c.is_rotated();
assert a.to_Mat4() == Mat4(1f, 4f, 7f, 0f,
2f, 5f, 8f, 0f,
3f, 6f, 9f, 0f,
0f, 0f, 0f, 1f);
assert a.to_Mat4() == Mat4::new(1f, 4f, 7f, 0f,
2f, 5f, 8f, 0f,
3f, 6f, 9f, 0f,
0f, 0f, 0f, 1f);
// to_Quaternion
}
@ -162,63 +162,63 @@ fn test_Mat4() {
Vec4 { x: 3f, y: 7f, z: 11f, w: 15f },
Vec4 { x: 4f, y: 8f, z: 12f, w: 16f },
Vec4 { x: 5f, y: 9f, z: 13f, w: 17f } ] };
let v1 = Vec4(1f, 2f, 3f, 4f);
let v1 = Vec4::new(1f, 2f, 3f, 4f);
let f1 = 0.5f;
assert a == Mat4(1f, 5f, 9f, 13f,
2f, 6f, 10f, 14f,
3f, 7f, 11f, 15f,
4f, 8f, 12f, 16f);
assert a == Mat4::new(1f, 5f, 9f, 13f,
2f, 6f, 10f, 14f,
3f, 7f, 11f, 15f,
4f, 8f, 12f, 16f);
assert a == Mat4_v(&Vec4(1f, 5f, 9f, 13f),
&Vec4(2f, 6f, 10f, 14f),
&Vec4(3f, 7f, 11f, 15f),
&Vec4(4f, 8f, 12f, 16f));
assert a == Mat4::from_cols(&Vec4::new(1f, 5f, 9f, 13f),
&Vec4::new(2f, 6f, 10f, 14f),
&Vec4::new(3f, 7f, 11f, 15f),
&Vec4::new(4f, 8f, 12f, 16f));
assert a[0] == Vec4(1f, 5f, 9f, 13f);
assert a[1] == Vec4(2f, 6f, 10f, 14f);
assert a[2] == Vec4(3f, 7f, 11f, 15f);
assert a[3] == Vec4(4f, 8f, 12f, 16f);
assert a[0] == Vec4::new(1f, 5f, 9f, 13f);
assert a[1] == Vec4::new(2f, 6f, 10f, 14f);
assert a[2] == Vec4::new(3f, 7f, 11f, 15f);
assert a[3] == Vec4::new(4f, 8f, 12f, 16f);
assert a.row(0) == Vec4( 1f, 2f, 3f, 4f);
assert a.row(1) == Vec4( 5f, 6f, 7f, 8f);
assert a.row(2) == Vec4( 9f, 10f, 11f, 12f);
assert a.row(3) == Vec4(13f, 14f, 15f, 16f);
assert a.row(0) == Vec4::new( 1f, 2f, 3f, 4f);
assert a.row(1) == Vec4::new( 5f, 6f, 7f, 8f);
assert a.row(2) == Vec4::new( 9f, 10f, 11f, 12f);
assert a.row(3) == Vec4::new(13f, 14f, 15f, 16f);
assert a.col(0) == Vec4(1f, 5f, 9f, 13f);
assert a.col(1) == Vec4(2f, 6f, 10f, 14f);
assert a.col(2) == Vec4(3f, 7f, 11f, 15f);
assert a.col(3) == Vec4(4f, 8f, 12f, 16f);
assert a.col(0) == Vec4::new(1f, 5f, 9f, 13f);
assert a.col(1) == Vec4::new(2f, 6f, 10f, 14f);
assert a.col(2) == Vec4::new(3f, 7f, 11f, 15f);
assert a.col(3) == Vec4::new(4f, 8f, 12f, 16f);
assert a.neg() == Mat4(-1f, -5f, -9f, -13f,
-2f, -6f, -10f, -14f,
-3f, -7f, -11f, -15f,
-4f, -8f, -12f, -16f);
assert a.neg() == Mat4::new(-1f, -5f, -9f, -13f,
-2f, -6f, -10f, -14f,
-3f, -7f, -11f, -15f,
-4f, -8f, -12f, -16f);
assert -a == a.neg();
assert a.mul_t(f1) == Mat4(0.5f, 2.5f, 4.5f, 6.5f,
1.0f, 3.0f, 5.0f, 7.0f,
1.5f, 3.5f, 5.5f, 7.5f,
2.0f, 4.0f, 6.0f, 8.0f);
assert a.mul_v(&v1) == Vec4(30.0, 70.0, 110.0, 150.0);
assert a.mul_t(f1) == Mat4::new(0.5f, 2.5f, 4.5f, 6.5f,
1.0f, 3.0f, 5.0f, 7.0f,
1.5f, 3.5f, 5.5f, 7.5f,
2.0f, 4.0f, 6.0f, 8.0f);
assert a.mul_v(&v1) == Vec4::new(30.0, 70.0, 110.0, 150.0);
assert a.add_m(&b) == Mat4(3f, 11f, 19f, 27f,
5f, 13f, 21f, 29f,
7f, 15f, 23f, 31f,
9f, 17f, 25f, 33f);
assert a.sub_m(&b) == Mat4(-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f);
assert a.mul_m(&b) == Mat4(100f, 228f, 356f, 484f,
110f, 254f, 398f, 542f,
120f, 280f, 440f, 600f,
130f, 306f, 482f, 658f);
assert a.add_m(&b) == Mat4::new(3f, 11f, 19f, 27f,
5f, 13f, 21f, 29f,
7f, 15f, 23f, 31f,
9f, 17f, 25f, 33f);
assert a.sub_m(&b) == Mat4::new(-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f);
assert a.mul_m(&b) == Mat4::new(100f, 228f, 356f, 484f,
110f, 254f, 398f, 542f,
120f, 280f, 440f, 600f,
130f, 306f, 482f, 658f);
assert a.transpose() == Mat4( 1f, 2f, 3f, 4f,
5f, 6f, 7f, 8f,
9f, 10f, 11f, 12f,
13f, 14f, 15f, 16f);
assert a.transpose() == Mat4::new( 1f, 2f, 3f, 4f,
5f, 6f, 7f, 8f,
9f, 10f, 11f, 12f,
13f, 14f, 15f, 16f);
// exact_eq
// fuzzy_eq
@ -234,10 +234,10 @@ fn test_Mat4() {
assert !a.is_diagonal();
assert a.is_rotated();
let c = Mat4(4f, 3f, 2f, 1f,
3f, 4f, 3f, 2f,
2f, 3f, 4f, 3f,
1f, 2f, 3f, 4f);
let c = Mat4::new(4f, 3f, 2f, 1f,
3f, 4f, 3f, 2f,
2f, 3f, 4f, 3f,
1f, 2f, 3f, 4f);
assert !c.is_identity();
assert c.is_symmetric();
assert !c.is_diagonal();

View file

@ -11,10 +11,10 @@ fn test_Quat() {
// let f1 = 1.5f;
// let f2 = 0.5f;
assert a == Quat(1f, 2f, 3f, 4f);
assert a == Quat::new(1f, 2f, 3f, 4f);
assert Quat::zero() == Quat(0f, 0f, 0f, 0f);
assert Quat::identity() == Quat(1f, 0f, 0f, 0f);
assert Quat::zero() == Quat::new(0f, 0f, 0f, 0f);
assert Quat::identity() == Quat::new(1f, 0f, 0f, 0f);
assert a.w == 1f;
assert a.x == 2f;

View file

@ -12,26 +12,26 @@ fn test_Vec2() {
let f1 = 1.5f;
let f2 = 0.5f;
assert Vec2::zero() == Vec2(0f, 0f);
assert Vec2::unit_x() == Vec2(1f, 0f);
assert Vec2::unit_y() == Vec2(0f, 1f);
assert Vec2::identity() == Vec2(1f, 1f);
assert Vec2::zero() == Vec2::new(0f, 0f);
assert Vec2::unit_x() == Vec2::new(1f, 0f);
assert Vec2::unit_y() == Vec2::new(0f, 1f);
assert Vec2::identity() == Vec2::new(1f, 1f);
assert a.x == 1f;
assert a.y == 2f;
assert a[0] == 1f;
assert a[1] == 2f;
assert -a == Vec2(-1f, -2f);
assert a.neg() == Vec2(-1f, -2f);
assert -a == Vec2::new(-1f, -2f);
assert a.neg() == Vec2::new(-1f, -2f);
assert a.add_t(f1) == Vec2( 2.5f, 3.5f);
assert a.sub_t(f1) == Vec2(-0.5f, 0.5f);
assert a.mul_t(f1) == Vec2( 1.5f, 3.0f);
assert a.div_t(f2) == Vec2( 2.0f, 4.0f);
assert a.add_t(f1) == Vec2::new( 2.5f, 3.5f);
assert a.sub_t(f1) == Vec2::new(-0.5f, 0.5f);
assert a.mul_t(f1) == Vec2::new( 1.5f, 3.0f);
assert a.div_t(f2) == Vec2::new( 2.0f, 4.0f);
assert a.add_v(&b) == Vec2( 4f, 6f);
assert a.sub_v(&b) == Vec2(-2f, -2f);
assert a.add_v(&b) == Vec2::new( 4f, 6f);
assert a.sub_v(&b) == Vec2::new(-2f, -2f);
// exact_eq
// fuzzy_eq
@ -40,14 +40,14 @@ fn test_Vec2() {
assert a.magnitude2().fuzzy_eq(&5f);
assert a.magnitude().fuzzy_eq(&2.236068f);
let c = Vec2(-2.0f, -1.0f);
let d = Vec2( 1.0f, 0.0f);
let c = Vec2::new(-2.0f, -1.0f);
let d = Vec2::new( 1.0f, 0.0f);
let f3 = 0.75f;
assert c.lerp(&d, f3) == Vec2(0.250f, -0.250f);
assert c.abs() == Vec2( 2.0f, 1.0f);
assert c.min(&d) == Vec2(-2.0f, -1.0f);
assert c.max(&d) == Vec2( 1.0f, 0.0f);
assert c.lerp(&d, f3) == Vec2::new(0.250f, -0.250f);
assert c.abs() == Vec2::new( 2.0f, 1.0f);
assert c.min(&d) == Vec2::new(-2.0f, -1.0f);
assert c.max(&d) == Vec2::new( 1.0f, 0.0f);
}
#[test]
@ -59,13 +59,13 @@ fn test_Vec3() {
let f1 = 1.5f;
let f2 = 0.5f;
assert a == Vec3(1f, 2f, 3f);
assert a == Vec3::new(1f, 2f, 3f);
assert Vec3::zero() == Vec3(0f, 0f, 0f);
assert Vec3::unit_x() == Vec3(1f, 0f, 0f);
assert Vec3::unit_y() == Vec3(0f, 1f, 0f);
assert Vec3::unit_z() == Vec3(0f, 0f, 1f);
assert Vec3::identity() == Vec3(1f, 1f, 1f);
assert Vec3::zero() == Vec3::new(0f, 0f, 0f);
assert Vec3::unit_x() == Vec3::new(1f, 0f, 0f);
assert Vec3::unit_y() == Vec3::new(0f, 1f, 0f);
assert Vec3::unit_z() == Vec3::new(0f, 0f, 1f);
assert Vec3::identity() == Vec3::new(1f, 1f, 1f);
assert a.x == 1f;
assert a.y == 2f;
@ -74,18 +74,18 @@ fn test_Vec3() {
assert a[1] == 2f;
assert a[2] == 3f;
assert a.cross(&b) == Vec3(-3f, 6f, -3f);
assert a.cross(&b) == Vec3::new(-3f, 6f, -3f);
assert -a == Vec3(-1f, -2f, -3f);
assert a.neg() == Vec3(-1f, -2f, -3f);
assert -a == Vec3::new(-1f, -2f, -3f);
assert a.neg() == Vec3::new(-1f, -2f, -3f);
assert a.add_t(f1) == Vec3( 2.5f, 3.5f, 4.5f);
assert a.sub_t(f1) == Vec3(-0.5f, 0.5f, 1.5f);
assert a.mul_t(f1) == Vec3( 1.5f, 3.0f, 4.5f);
assert a.div_t(f2) == Vec3( 2.0f, 4.0f, 6.0f);
assert a.add_t(f1) == Vec3::new( 2.5f, 3.5f, 4.5f);
assert a.sub_t(f1) == Vec3::new(-0.5f, 0.5f, 1.5f);
assert a.mul_t(f1) == Vec3::new( 1.5f, 3.0f, 4.5f);
assert a.div_t(f2) == Vec3::new( 2.0f, 4.0f, 6.0f);
assert a.add_v(&b) == Vec3( 5f, 7f, 9f);
assert a.sub_v(&b) == Vec3(-3f, -3f, -3f);
assert a.add_v(&b) == Vec3::new( 5f, 7f, 9f);
assert a.sub_v(&b) == Vec3::new(-3f, -3f, -3f);
// exact_eq
// fuzzy_eq
@ -94,14 +94,14 @@ fn test_Vec3() {
assert a.magnitude2().fuzzy_eq(&14f);
assert a.magnitude().fuzzy_eq(&3.74165738677f);
let c = Vec3(-2.0f, -1.0f, 1.0f);
let d = Vec3( 1.0f, 0.0f, 0.5f);
let c = Vec3::new(-2.0f, -1.0f, 1.0f);
let d = Vec3::new( 1.0f, 0.0f, 0.5f);
let f3 = 0.75f;
assert c.lerp(&d, f3) == Vec3(0.250f, -0.250f, 0.625f);
assert c.abs() == Vec3( 2.0f, 1.0f, 1.0f);
assert c.min(&d) == Vec3(-2.0f, -1.0f, 0.5f);
assert c.max(&d) == Vec3( 1.0f, 0.0f, 1.0f);
assert c.lerp(&d, f3) == Vec3::new(0.250f, -0.250f, 0.625f);
assert c.abs() == Vec3::new( 2.0f, 1.0f, 1.0f);
assert c.min(&d) == Vec3::new(-2.0f, -1.0f, 0.5f);
assert c.max(&d) == Vec3::new( 1.0f, 0.0f, 1.0f);
}
#[test]
@ -113,14 +113,14 @@ fn test_Vec4() {
let f1 = 1.5f;
let f2 = 0.5f;
assert a == Vec4(1f, 2f, 3f, 4f);
assert a == Vec4::new(1f, 2f, 3f, 4f);
assert Vec4::zero() == Vec4(0f, 0f, 0f, 0f);
assert Vec4::unit_x() == Vec4(1f, 0f, 0f, 0f);
assert Vec4::unit_y() == Vec4(0f, 1f, 0f, 0f);
assert Vec4::unit_z() == Vec4(0f, 0f, 1f, 0f);
assert Vec4::unit_w() == Vec4(0f, 0f, 0f, 1f);
assert Vec4::identity() == Vec4(1f, 1f, 1f, 1f);
assert Vec4::zero() == Vec4::new(0f, 0f, 0f, 0f);
assert Vec4::unit_x() == Vec4::new(1f, 0f, 0f, 0f);
assert Vec4::unit_y() == Vec4::new(0f, 1f, 0f, 0f);
assert Vec4::unit_z() == Vec4::new(0f, 0f, 1f, 0f);
assert Vec4::unit_w() == Vec4::new(0f, 0f, 0f, 1f);
assert Vec4::identity() == Vec4::new(1f, 1f, 1f, 1f);
assert a.x == 1f;
assert a.y == 2f;
@ -131,16 +131,16 @@ fn test_Vec4() {
assert a[2] == 3f;
assert a[3] == 4f;
assert -a == Vec4(-1f, -2f, -3f, -4f);
assert a.neg() == Vec4(-1f, -2f, -3f, -4f);
assert -a == Vec4::new(-1f, -2f, -3f, -4f);
assert a.neg() == Vec4::new(-1f, -2f, -3f, -4f);
assert a.add_t(f1) == Vec4( 2.5f, 3.5f, 4.5f, 5.5f);
assert a.sub_t(f1) == Vec4(-0.5f, 0.5f, 1.5f, 2.5f);
assert a.mul_t(f1) == Vec4( 1.5f, 3.0f, 4.5f, 6.0f);
assert a.div_t(f2) == Vec4( 2.0f, 4.0f, 6.0f, 8.0f);
assert a.add_t(f1) == Vec4::new( 2.5f, 3.5f, 4.5f, 5.5f);
assert a.sub_t(f1) == Vec4::new(-0.5f, 0.5f, 1.5f, 2.5f);
assert a.mul_t(f1) == Vec4::new( 1.5f, 3.0f, 4.5f, 6.0f);
assert a.div_t(f2) == Vec4::new( 2.0f, 4.0f, 6.0f, 8.0f);
assert a.add_v(&b) == Vec4( 6f, 8f, 10f, 12f);
assert a.sub_v(&b) == Vec4(-4f, -4f, -4f, -4f);
assert a.add_v(&b) == Vec4::new( 6f, 8f, 10f, 12f);
assert a.sub_v(&b) == Vec4::new(-4f, -4f, -4f, -4f);
assert a.dot(&b) == 70f;
@ -151,12 +151,12 @@ fn test_Vec4() {
assert a.magnitude2().fuzzy_eq(&30f);
assert a.magnitude().fuzzy_eq(&5.477226f);
let c = Vec4(-2.0f, -1.0f, 1.0f, 2.0f);
let d = Vec4( 1.0f, 0.0f, 0.5f, 1.0f);
let c = Vec4::new(-2.0f, -1.0f, 1.0f, 2.0f);
let d = Vec4::new( 1.0f, 0.0f, 0.5f, 1.0f);
let f3 = 0.75f;
assert c.lerp(&d, f3) == Vec4(0.250f, -0.250f, 0.625f, 1.250f);
assert c.abs() == Vec4( 2.0f, 1.0f, 1.0f, 2.0f);
assert c.min(&d) == Vec4(-2.0f, -1.0f, 0.5f, 1.0f);
assert c.max(&d) == Vec4( 1.0f, 0.0f, 1.0f, 2.0f);
assert c.lerp(&d, f3) == Vec4::new(0.250f, -0.250f, 0.625f, 1.250f);
assert c.abs() == Vec4::new( 2.0f, 1.0f, 1.0f, 2.0f);
assert c.min(&d) == Vec4::new(-2.0f, -1.0f, 0.5f, 1.0f);
assert c.max(&d) == Vec4::new( 1.0f, 0.0f, 1.0f, 2.0f);
}

View file

@ -42,16 +42,13 @@ pub trait Vector3<T> {
//
pub struct Vec2<T> { x: T, y: T }
//
// Constructor
//
#[inline(always)]
pub pure fn Vec2<T:Copy>(x: T, y: T) -> Vec2<T> {
Vec2 { x: move x, y: move y }
}
pub mod Vec2 {
#[inline(always)]
pub pure fn new<T:Copy>(x: T, y: T) -> Vec2<T> {
Vec2 { x: x, y: y }
}
#[inline(always)] pub pure fn zero <T: Num>() -> Vec2<T> { Vec2 { x: from_int(0), y: from_int(0) } }
#[inline(always)] pub pure fn unit_x <T: Num>() -> Vec2<T> { Vec2 { x: from_int(1), y: from_int(0) } }
#[inline(always)] pub pure fn unit_y <T: Num>() -> Vec2<T> { Vec2 { x: from_int(0), y: from_int(1) } }
@ -64,38 +61,38 @@ pub impl<T:Copy Num Sqrt> Vec2<T>: Vector<T> {
#[inline(always)]
pure fn add_t(value: T) -> Vec2<T> {
Vec2(self[0] + value,
self[1] + value)
Vec2::new(self[0] + value,
self[1] + value)
}
#[inline(always)]
pure fn sub_t(value: T) -> Vec2<T> {
Vec2(self[0] - value,
self[1] - value)
Vec2::new(self[0] - value,
self[1] - value)
}
#[inline(always)]
pure fn mul_t(value: T) -> Vec2<T> {
Vec2(self[0] * value,
self[1] * value)
Vec2::new(self[0] * value,
self[1] * value)
}
#[inline(always)]
pure fn div_t(value: T) -> Vec2<T> {
Vec2(self[0] / value,
self[1] / value)
Vec2::new(self[0] / value,
self[1] / value)
}
#[inline(always)]
pure fn add_v(other: &Vec2<T>) -> Vec2<T> {
Vec2(self[0] + other[0],
self[1] + other[1])
Vec2::new(self[0] + other[0],
self[1] + other[1])
}
#[inline(always)]
pure fn sub_v(other: &Vec2<T>) -> Vec2<T> {
Vec2(self[0] - other[0],
self[1] - other[1])
Vec2::new(self[0] - other[0],
self[1] - other[1])
}
#[inline(always)]
@ -141,29 +138,29 @@ pub impl<T:Copy> Vec2<T>: Index<uint, T> {
pub impl<T:Copy MinMax> Vec2<T>: MinMax {
#[inline(always)]
pure fn min(other: &Vec2<T>) -> Vec2<T> {
Vec2(min(&self[0], &other[0]),
min(&self[1], &other[1]))
Vec2::new(min(&self[0], &other[0]),
min(&self[1], &other[1]))
}
#[inline(always)]
pure fn max(other: &Vec2<T>) -> Vec2<T> {
Vec2(max(&self[0], &other[0]),
max(&self[1], &other[1]))
Vec2::new(max(&self[0], &other[0]),
max(&self[1], &other[1]))
}
}
pub impl<T:Copy Abs> Vec2<T>: Abs {
#[inline(always)]
pure fn abs() -> Vec2<T> {
Vec2(abs(&self[0]),
abs(&self[1]))
Vec2::new(abs(&self[0]),
abs(&self[1]))
}
}
pub impl<T:Copy Neg<T>> Vec2<T>: Neg<Vec2<T>> {
#[inline(always)]
pure fn neg() -> Vec2<T> {
Vec2(-self[0], -self[1])
Vec2::new(-self[0], -self[1])
}
}
@ -213,15 +210,13 @@ pub impl<T:Copy> Vec2<T>: ToPtr<T> {
//
pub struct Vec3<T> { x: T, y: T, z: T }
//
// Constructor
//
#[inline(always)]
pub pure fn Vec3<T:Copy>(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: move x, y: move y, z: move z }
}
pub mod Vec3 {
#[inline(always)]
pub pure fn new<T:Copy>(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
#[inline(always)] pub pure fn zero <T: Num>() -> Vec3<T> { Vec3 { x: from_int(0), y: from_int(0), z: from_int(0) } }
#[inline(always)] pub pure fn unit_x <T: Num>() -> Vec3<T> { Vec3 { x: from_int(1), y: from_int(0), z: from_int(0) } }
#[inline(always)] pub pure fn unit_y <T: Num>() -> Vec3<T> { Vec3 { x: from_int(0), y: from_int(1), z: from_int(0) } }
@ -232,9 +227,9 @@ pub mod Vec3 {
pub impl<T:Copy Num> Vec3<T>: Vector3<T> {
#[inline(always)]
fn cross(other: &Vec3<T>) -> Vec3<T> {
Vec3((self[1] * other[2]) - (self[2] * other[1]),
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
Vec3::new((self[1] * other[2]) - (self[2] * other[1]),
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
}
}
@ -244,44 +239,44 @@ pub impl<T:Copy Num Sqrt> Vec3<T>: Vector<T> {
#[inline(always)]
pure fn add_t(value: T) -> Vec3<T> {
Vec3(self[0] + value,
self[1] + value,
self[2] + value)
Vec3::new(self[0] + value,
self[1] + value,
self[2] + value)
}
#[inline(always)]
pure fn sub_t(value: T) -> Vec3<T> {
Vec3(self[0] - value,
self[1] - value,
self[2] - value)
Vec3::new(self[0] - value,
self[1] - value,
self[2] - value)
}
#[inline(always)]
pure fn mul_t(value: T) -> Vec3<T> {
Vec3(self[0] * value,
self[1] * value,
self[2] * value)
Vec3::new(self[0] * value,
self[1] * value,
self[2] * value)
}
#[inline(always)]
pure fn div_t(value: T) -> Vec3<T> {
Vec3(self[0] / value,
self[1] / value,
self[2] / value)
Vec3::new(self[0] / value,
self[1] / value,
self[2] / value)
}
#[inline(always)]
pure fn add_v(other: &Vec3<T>) -> Vec3<T>{
Vec3(self[0] + other[0],
self[1] + other[1],
self[2] + other[2])
Vec3::new(self[0] + other[0],
self[1] + other[1],
self[2] + other[2])
}
#[inline(always)]
pure fn sub_v(other: &Vec3<T>) -> Vec3<T>{
Vec3(self[0] - other[0],
self[1] - other[1],
self[2] - other[2])
Vec3::new(self[0] - other[0],
self[1] - other[1],
self[2] - other[2])
}
#[inline(always)]
@ -329,32 +324,32 @@ pub impl<T:Copy> Vec3<T>: Index<uint, T> {
pub impl<T:Copy MinMax> Vec3<T>: MinMax {
#[inline(always)]
pure fn min(other: &Vec3<T>) -> Vec3<T> {
Vec3(min(&self[0], &other[0]),
min(&self[1], &other[1]),
min(&self[2], &other[2]))
Vec3::new(min(&self[0], &other[0]),
min(&self[1], &other[1]),
min(&self[2], &other[2]))
}
#[inline(always)]
pure fn max(other: &Vec3<T>) -> Vec3<T> {
Vec3(max(&self[0], &other[0]),
max(&self[1], &other[1]),
max(&self[2], &other[2]))
Vec3::new(max(&self[0], &other[0]),
max(&self[1], &other[1]),
max(&self[2], &other[2]))
}
}
pub impl<T:Copy Abs> Vec3<T>: Abs {
#[inline(always)]
pure fn abs() -> Vec3<T> {
Vec3(abs(&self[0]),
abs(&self[1]),
abs(&self[2]))
Vec3::new(abs(&self[0]),
abs(&self[1]),
abs(&self[2]))
}
}
pub impl<T:Copy Neg<T>> Vec3<T>: Neg<Vec3<T>> {
#[inline(always)]
pure fn neg() -> Vec3<T> {
Vec3(-self[0], -self[1], -self[2])
Vec3::new(-self[0], -self[1], -self[2])
}
}
@ -407,6 +402,12 @@ pub impl<T:Copy> Vec3<T>: ToPtr<T> {
pub struct Vec4<T> { x: T, y: T, z: T, w: T }
pub mod Vec4 {
#[inline(always)]
pub pure fn new<T:Copy>(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: x, y: y, z: z, w: w }
}
#[inline(always)] pub pure fn zero <T: Num>() -> Vec4<T> { Vec4 { x: from_int(0), y: from_int(0), z: from_int(0), w: from_int(0) } }
#[inline(always)] pub pure fn unit_x <T: Num>() -> Vec4<T> { Vec4 { x: from_int(1), y: from_int(0), z: from_int(0), w: from_int(0) } }
#[inline(always)] pub pure fn unit_y <T: Num>() -> Vec4<T> { Vec4 { x: from_int(0), y: from_int(1), z: from_int(0), w: from_int(0) } }
@ -415,64 +416,56 @@ pub mod Vec4 {
#[inline(always)] pub pure fn identity <T: Num>() -> Vec4<T> { Vec4 { x: from_int(1), y: from_int(1), z: from_int(1), w: from_int(1) } }
}
//
// Constructor
//
#[inline(always)]
pub pure fn Vec4<T:Copy>(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: move x, y: move y, z: move z, w: move w }
}
pub impl<T:Copy Num Sqrt> Vec4<T>: Vector<T> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
#[inline(always)]
pure fn add_t(value: T) -> Vec4<T> {
Vec4(self[0] + value,
self[1] + value,
self[2] + value,
self[3] + value)
Vec4::new(self[0] + value,
self[1] + value,
self[2] + value,
self[3] + value)
}
#[inline(always)]
pure fn sub_t(value: T) -> Vec4<T> {
Vec4(self[0] - value,
self[1] - value,
self[2] - value,
self[3] - value)
Vec4::new(self[0] - value,
self[1] - value,
self[2] - value,
self[3] - value)
}
#[inline(always)]
pure fn mul_t(value: T) -> Vec4<T> {
Vec4(self[0] * value,
self[1] * value,
self[2] * value,
self[3] * value)
Vec4::new(self[0] * value,
self[1] * value,
self[2] * value,
self[3] * value)
}
#[inline(always)]
pure fn div_t(value: T) -> Vec4<T> {
Vec4(self[0] / value,
self[1] / value,
self[2] / value,
self[3] / value)
Vec4::new(self[0] / value,
self[1] / value,
self[2] / value,
self[3] / value)
}
#[inline(always)]
pure fn add_v(other: &Vec4<T>) -> Vec4<T> {
Vec4(self[0] + other[0],
self[1] + other[1],
self[2] + other[2],
self[3] + other[3])
Vec4::new(self[0] + other[0],
self[1] + other[1],
self[2] + other[2],
self[3] + other[3])
}
#[inline(always)]
pure fn sub_v(other: &Vec4<T>) -> Vec4<T> {
Vec4(self[0] - other[0],
self[1] - other[1],
self[2] - other[2],
self[3] - other[3])
Vec4::new(self[0] - other[0],
self[1] - other[1],
self[2] - other[2],
self[3] - other[3])
}
#[inline(always)]
@ -523,35 +516,35 @@ pub impl<T:Copy> Vec4<T>: Index<uint, T> {
pub impl<T:Copy MinMax> Vec4<T>: MinMax {
#[inline(always)]
pure fn min(other: &Vec4<T>) -> Vec4<T> {
Vec4(min(&self[0], &other[0]),
min(&self[1], &other[1]),
min(&self[2], &other[2]),
min(&self[3], &other[3]))
Vec4::new(min(&self[0], &other[0]),
min(&self[1], &other[1]),
min(&self[2], &other[2]),
min(&self[3], &other[3]))
}
#[inline(always)]
pure fn max(other: &Vec4<T>) -> Vec4<T> {
Vec4(max(&self[0], &other[0]),
max(&self[1], &other[1]),
max(&self[2], &other[2]),
max(&self[3], &other[3]))
Vec4::new(max(&self[0], &other[0]),
max(&self[1], &other[1]),
max(&self[2], &other[2]),
max(&self[3], &other[3]))
}
}
pub impl<T:Copy Abs> Vec4<T>: Abs {
#[inline(always)]
pure fn abs() -> Vec4<T> {
Vec4(abs(&self[0]),
abs(&self[1]),
abs(&self[2]),
abs(&self[3]))
Vec4::new(abs(&self[0]),
abs(&self[1]),
abs(&self[2]),
abs(&self[3]))
}
}
pub impl<T:Copy Neg<T>> Vec4<T>: Neg<Vec4<T>> {
#[inline(always)]
pure fn neg() -> Vec4<T> {
Vec4(-self[0], -self[1], -self[2], -self[3])
Vec4::new(-self[0], -self[1], -self[2], -self[3])
}
}