From 35058611f3975974647b22c4f6d90b868b64a766 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 1 Nov 2012 17:41:42 +1000 Subject: [PATCH] Rename and move constructors --- src/matrix.rs | 366 +++++++++++++++++------------------- src/projection.rs | 8 +- src/quaternion.rs | 70 +++---- src/test/test_matrix.rs | 236 +++++++++++------------ src/test/test_quaternion.rs | 6 +- src/test/test_vector.rs | 120 ++++++------ src/vector.rs | 217 +++++++++++---------- 7 files changed, 492 insertions(+), 531 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 3f5be45..836a570 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -59,41 +59,33 @@ pub trait Matrix4 { // pub struct Mat2 { data:[Vec2 * 2] } -// -// Mat2 Constructor -// -#[inline(always)] -pub pure fn Mat2(m00: T, m01: T, - m10: T, m11: T) -> Mat2 { - Mat2 { data: [ Vec2(m00, m01), - Vec2(m10, m11) ] } -} - -// -// Construct Mat2 from column vectors -// -#[inline(always)] -pub pure fn Mat2_v(col0: &Vec2, col1: &Vec2) -> Mat2 { - Mat2 { data: [ *move col0, *move col1 ] } -} - pub mod Mat2 { + #[inline(always)] - pub pure fn zero() -> Mat2 { - Mat2 { data: [ Vec2::zero(), - Vec2::zero() ] } + pub pure fn new(m00: T, m01: T, + m10: T, m11: T) -> Mat2 { + Mat2::from_cols(&Vec2::new(m00, m01), + &Vec2::new(m10, m11)) + } + + #[inline(always)] + pub pure fn from_cols(col0: &Vec2, col1: &Vec2) -> Mat2 { + Mat2 { data: [ *col0, *col1 ] } } #[inline(always)] - pub pure fn identity() -> Mat2 { + pub pure fn zero() -> Mat2 { + Mat2 { data: [ Vec2::zero(), + Vec2::zero() ] } + } + + #[inline(always)] + pub pure fn identity() -> Mat2 { Mat2 { data: [ Vec2::unit_x(), Vec2::unit_y() ] } } } -// -// Matrix2x2 Implementation -// pub impl Mat2: Matrix> { #[inline(always)] pure fn rows() -> uint { 2 } @@ -106,8 +98,8 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn row(i: uint) -> Vec2 { - Vec2(self[0][i], - self[1][i]) + Vec2::new(self[0][i], + self[1][i]) } #[inline(always)] @@ -117,35 +109,35 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn mul_t(value: T) -> Mat2 { - 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) -> Vec2 { - 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) -> Mat2 { - 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) -> Mat2 { - 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) -> Mat2 { - 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 Mat2: Matrix> { #[inline(always)] pure fn transpose() -> Mat2 { - 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 Mat2: Index> { pub impl> Mat2: Neg> { #[inline(always)] pure fn neg() -> Mat2 { - Mat2_v(&-self[0], &-self[1]) + Mat2::from_cols(&-self[0], &-self[1]) } } @@ -234,45 +226,37 @@ pub impl Mat2: FuzzyEq { // pub struct Mat3 { data:[Vec3 * 3] } -// -// Mat3 Constructor -// -#[inline(always)] -pub pure fn Mat3(m00:T, m01:T, m02:T, - m10:T, m11:T, m12:T, - m20:T, m21:T, m22:T) -> Mat3 { - 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(col0: &Vec3, col1: &Vec3, col2: &Vec3) -> Mat3 { - Mat3 { data: [ *move col0, *move col1, *move col2 ] } -} - pub mod Mat3 { + #[inline(always)] - pub pure fn zero() -> Mat3 { - Mat3 { data: [ Vec3::zero(), - Vec3::zero(), - Vec3::zero() ] } + pub pure fn new(m00:T, m01:T, m02:T, + m10:T, m11:T, m12:T, + m20:T, m21:T, m22:T) -> Mat3 { + Mat3::from_cols(&Vec3::new(m00, m01, m02), + &Vec3::new(m10, m11, m12), + &Vec3::new(m20, m21, m22)) } #[inline(always)] - pub pure fn identity() -> Mat3 { + pub pure fn from_cols(col0: &Vec3, col1: &Vec3, col2: &Vec3) -> Mat3 { + Mat3 { data: [ *col0, *col1, *col2 ] } + } + + #[inline(always)] + pub pure fn zero() -> Mat3 { + Mat3 { data: [ Vec3::zero(), + Vec3::zero(), + Vec3::zero() ] } + } + + #[inline(always)] + pub pure fn identity() -> Mat3 { Mat3 { data: [ Vec3::unit_x(), Vec3::unit_y(), Vec3::unit_z() ] } } } -// -// Matrix3x3 Implementation -// pub impl Mat3: Matrix> { #[inline(always)] pure fn rows() -> uint { 3 } @@ -285,9 +269,9 @@ pub impl Mat3: Matrix> { #[inline(always)] pure fn row(i: uint) -> Vec3 { - 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 Mat3: Matrix> { #[inline(always)] pure fn mul_t(value: T) -> Mat3 { - 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) -> Vec3 { - 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) -> Mat3 { - 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) -> Mat3 { - 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) -> Mat3 { - 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 Mat3: Matrix> { #[inline(always)] pure fn transpose() -> Mat3 { - 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 Mat3: Matrix> { pub impl Mat3: Matrix3 { #[inline(always)] pure fn scale(vec: &Vec3) -> Mat3 { - 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 { - 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 Mat3: ToQuat { z = (self[0][1] - self[1][0]).cast::() * 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 Mat3: Index> { pub impl> Mat3: Neg> { #[inline(always)] pure fn neg() -> Mat3 { - Mat3_v(&-self[0], &-self[1], &-self[2]) + Mat3::from_cols(&-self[0], &-self[1], &-self[2]) } } @@ -506,39 +490,34 @@ pub impl Mat3: ToPtr { // pub struct Mat4 { data:[Vec4 * 4] } -// -// Mat4 Constructor -// -#[inline(always)] -pub pure fn Mat4(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 { - 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(col0: &Vec4, col1: &Vec4, col2: &Vec4, col3: &Vec4) -> Mat4 { - Mat4 { data: [ *move col0, *move col1, *move col2, *move col3 ] } -} - pub mod Mat4 { + #[inline(always)] - pub pure fn zero() -> Mat4 { - Mat4 { data: [ Vec4::zero(), - Vec4::zero(), - Vec4::zero(), - Vec4::zero() ] } + pub pure fn new(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 { + 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() -> Mat4 { + pub pure fn from_cols(col0: &Vec4, col1: &Vec4, col2: &Vec4, col3: &Vec4) -> Mat4 { + Mat4 { data: [ *col0, *col1, *col2, *col3 ] } + } + + #[inline(always)] + pub pure fn zero() -> Mat4 { + Mat4 { data: [ Vec4::zero(), + Vec4::zero(), + Vec4::zero(), + Vec4::zero() ] } + } + + #[inline(always)] + pub pure fn identity() -> Mat4 { Mat4 { data: [ Vec4::unit_x(), Vec4::unit_y(), Vec4::unit_z(), @@ -546,9 +525,6 @@ pub mod Mat4 { } } -// -// Matrix4x4 Implementation -// pub impl Mat4: Matrix> { #[inline(always)] pure fn rows() -> uint { 4 } @@ -561,10 +537,10 @@ pub impl Mat4: Matrix> { #[inline(always)] pure fn row(i: uint) -> Vec4 { - 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 Mat4: Matrix> { #[inline(always)] pure fn mul_t(value: T) -> Mat4 { - 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) -> Vec4 { - 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) -> Mat4 { - 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) -> Mat4 { - 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) -> Mat4 { - 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 Mat4: Matrix> { #[inline(always)] pure fn transpose() -> Mat4 { - 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 Mat4: Matrix> { pub impl Mat4: Matrix4 { #[inline(always)] pure fn scale(vec: &Vec3) -> Mat4 { - 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) -> Mat4 { - 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 Mat4: Index> { pub impl> Mat4: Neg> { #[inline(always)] pure fn neg() -> Mat4 { - Mat4_v(&-self[0], &-self[1], &-self[2], &-self[3]) + Mat4::from_cols(&-self[0], &-self[1], &-self[2], &-self[3]) } } diff --git a/src/projection.rs b/src/projection.rs index d8421ae..2aded23 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -43,8 +43,8 @@ pure fn frustum>(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); } \ No newline at end of file diff --git a/src/quaternion.rs b/src/quaternion.rs index 08933bb..ed201e5 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -42,74 +42,66 @@ pub trait ToQuat { -// -// Quat struct definition -// pub struct Quat { w: T, x: T, y: T, z: T } -// -// Quat Constructor -// -#[inline(always)] -pub pure fn Quat(w: T, x: T, y: T, z: T) -> Quat { - Quat { w: move w, x: move x, y: move y, z: move z } -} - pub mod Quat { + + #[inline(always)] + pub pure fn new(w: T, x: T, y: T, z: T) -> Quat { + Quat { w: w, x: x, y: y, z: z } + } + #[inline(always)] pub pure fn zero () -> Quat { Quat { w: from_int(0), x: from_int(0), y: from_int(0), z: from_int(0) } } #[inline(always)] pub pure fn identity () -> Quat { Quat { w: from_int(1), x: from_int(0), y: from_int(0), z: from_int(0) } } } -// -// Quaternion Implementation -// pub impl Quat: Quaternion { #[inline(always)] pure fn dim() -> uint { 4 } #[inline(always)] pure fn mul_t(value: T) -> Quat { - 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 { - 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) -> Quat { - 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) -> Quat { - 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) -> Quat { - 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 { - 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 Quat: Quaternion { 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 Quat: Index { pub impl> Quat: Neg> { #[inline(always)] pure fn neg() -> Quat { - Quat(-self[0], -self[1], -self[2], -self[3]) + Quat::new(-self[0], -self[1], -self[2], -self[3]) } } diff --git a/src/test/test_matrix.rs b/src/test/test_matrix.rs index bf21c6e..0856706 100644 --- a/src/test/test_matrix.rs +++ b/src/test/test_matrix.rs @@ -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(); diff --git a/src/test/test_quaternion.rs b/src/test/test_quaternion.rs index da0244e..bb6d5d2 100644 --- a/src/test/test_quaternion.rs +++ b/src/test/test_quaternion.rs @@ -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; diff --git a/src/test/test_vector.rs b/src/test/test_vector.rs index 373d725..dc1d7a3 100644 --- a/src/test/test_vector.rs +++ b/src/test/test_vector.rs @@ -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); } \ No newline at end of file diff --git a/src/vector.rs b/src/vector.rs index b4b324f..0f1057e 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -42,16 +42,13 @@ pub trait Vector3 { // pub struct Vec2 { x: T, y: T } - -// -// Constructor -// -#[inline(always)] -pub pure fn Vec2(x: T, y: T) -> Vec2 { - Vec2 { x: move x, y: move y } -} - pub mod Vec2 { + + #[inline(always)] + pub pure fn new(x: T, y: T) -> Vec2 { + Vec2 { x: x, y: y } + } + #[inline(always)] pub pure fn zero () -> Vec2 { Vec2 { x: from_int(0), y: from_int(0) } } #[inline(always)] pub pure fn unit_x () -> Vec2 { Vec2 { x: from_int(1), y: from_int(0) } } #[inline(always)] pub pure fn unit_y () -> Vec2 { Vec2 { x: from_int(0), y: from_int(1) } } @@ -64,38 +61,38 @@ pub impl Vec2: Vector { #[inline(always)] pure fn add_t(value: T) -> Vec2 { - Vec2(self[0] + value, - self[1] + value) + Vec2::new(self[0] + value, + self[1] + value) } #[inline(always)] pure fn sub_t(value: T) -> Vec2 { - Vec2(self[0] - value, - self[1] - value) + Vec2::new(self[0] - value, + self[1] - value) } #[inline(always)] pure fn mul_t(value: T) -> Vec2 { - Vec2(self[0] * value, - self[1] * value) + Vec2::new(self[0] * value, + self[1] * value) } #[inline(always)] pure fn div_t(value: T) -> Vec2 { - Vec2(self[0] / value, - self[1] / value) + Vec2::new(self[0] / value, + self[1] / value) } #[inline(always)] pure fn add_v(other: &Vec2) -> Vec2 { - 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) -> Vec2 { - 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 Vec2: Index { pub impl Vec2: MinMax { #[inline(always)] pure fn min(other: &Vec2) -> Vec2 { - 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) -> Vec2 { - 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 Vec2: Abs { #[inline(always)] pure fn abs() -> Vec2 { - Vec2(abs(&self[0]), - abs(&self[1])) + Vec2::new(abs(&self[0]), + abs(&self[1])) } } pub impl> Vec2: Neg> { #[inline(always)] pure fn neg() -> Vec2 { - Vec2(-self[0], -self[1]) + Vec2::new(-self[0], -self[1]) } } @@ -213,15 +210,13 @@ pub impl Vec2: ToPtr { // pub struct Vec3 { x: T, y: T, z: T } -// -// Constructor -// -#[inline(always)] -pub pure fn Vec3(x: T, y: T, z: T) -> Vec3 { - Vec3 { x: move x, y: move y, z: move z } -} - pub mod Vec3 { + + #[inline(always)] + pub pure fn new(x: T, y: T, z: T) -> Vec3 { + Vec3 { x: x, y: y, z: z } + } + #[inline(always)] pub pure fn zero () -> Vec3 { Vec3 { x: from_int(0), y: from_int(0), z: from_int(0) } } #[inline(always)] pub pure fn unit_x () -> Vec3 { Vec3 { x: from_int(1), y: from_int(0), z: from_int(0) } } #[inline(always)] pub pure fn unit_y () -> Vec3 { Vec3 { x: from_int(0), y: from_int(1), z: from_int(0) } } @@ -232,9 +227,9 @@ pub mod Vec3 { pub impl Vec3: Vector3 { #[inline(always)] fn cross(other: &Vec3) -> Vec3 { - 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 Vec3: Vector { #[inline(always)] pure fn add_t(value: T) -> Vec3 { - 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 { - 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 { - 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 { - 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) -> Vec3{ - 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) -> Vec3{ - 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 Vec3: Index { pub impl Vec3: MinMax { #[inline(always)] pure fn min(other: &Vec3) -> Vec3 { - 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) -> Vec3 { - 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 Vec3: Abs { #[inline(always)] pure fn abs() -> Vec3 { - Vec3(abs(&self[0]), - abs(&self[1]), - abs(&self[2])) + Vec3::new(abs(&self[0]), + abs(&self[1]), + abs(&self[2])) } } pub impl> Vec3: Neg> { #[inline(always)] pure fn neg() -> Vec3 { - Vec3(-self[0], -self[1], -self[2]) + Vec3::new(-self[0], -self[1], -self[2]) } } @@ -407,6 +402,12 @@ pub impl Vec3: ToPtr { pub struct Vec4 { x: T, y: T, z: T, w: T } pub mod Vec4 { + + #[inline(always)] + pub pure fn new(x: T, y: T, z: T, w: T) -> Vec4 { + Vec4 { x: x, y: y, z: z, w: w } + } + #[inline(always)] pub pure fn zero () -> Vec4 { Vec4 { x: from_int(0), y: from_int(0), z: from_int(0), w: from_int(0) } } #[inline(always)] pub pure fn unit_x () -> Vec4 { Vec4 { x: from_int(1), y: from_int(0), z: from_int(0), w: from_int(0) } } #[inline(always)] pub pure fn unit_y () -> Vec4 { 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 () -> Vec4 { Vec4 { x: from_int(1), y: from_int(1), z: from_int(1), w: from_int(1) } } } -// -// Constructor -// -#[inline(always)] -pub pure fn Vec4(x: T, y: T, z: T, w: T) -> Vec4 { - Vec4 { x: move x, y: move y, z: move z, w: move w } -} - pub impl Vec4: Vector { #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] pure fn add_t(value: T) -> Vec4 { - 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 { - 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 { - 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 { - 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) -> Vec4 { - 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) -> Vec4 { - 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 Vec4: Index { pub impl Vec4: MinMax { #[inline(always)] pure fn min(other: &Vec4) -> Vec4 { - 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) -> Vec4 { - 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 Vec4: Abs { #[inline(always)] pure fn abs() -> Vec4 { - 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> Vec4: Neg> { #[inline(always)] pure fn neg() -> Vec4 { - Vec4(-self[0], -self[1], -self[2], -self[3]) + Vec4::new(-self[0], -self[1], -self[2], -self[3]) } }