From c6c92dc3e376306b7982d20844c77d8598b893ef Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 29 Mar 2013 22:51:34 +1100 Subject: [PATCH] Merge each mutable trait with its respective immutable trait --- src/mat.rs | 129 ++++++++++++++++++------------------ src/mat2.rs | 72 ++++++++++---------- src/mat3.rs | 162 ++++++++++++++++++++++----------------------- src/mat4.rs | 186 +++++++++++++++++++++++++--------------------------- src/vec.rs | 107 ++++++++++++------------------ src/vec2.rs | 89 +++++++++++-------------- src/vec3.rs | 106 +++++++++++++----------------- src/vec4.rs | 101 +++++++++++++--------------- 8 files changed, 441 insertions(+), 511 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index 24d0a43..0e13f74 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -123,6 +123,68 @@ pub trait Matrix: Index + Eq + Neg { * The transposed matrix */ fn transpose(&self) -> Self; + + /** + * # Return value + * + * A mutable reference to the column at `i` + */ + fn col_mut(&mut self, i: uint) -> &'self mut V; + + /** + * Swap two columns of the matrix in place + */ + fn swap_cols(&mut self, a: uint, b: uint); + + /** + * Swap two rows of the matrix in place + */ + fn swap_rows(&mut self, a: uint, b: uint); + + /** + * Sets the matrix to `other` + */ + fn set(&mut self, other: &Self); + + /** + * Sets the matrix to the identity matrix + */ + fn to_identity(&mut self); + + /** + * Sets each element of the matrix to zero + */ + fn to_zero(&mut self); + + /** + * Multiplies the matrix by a scalar + */ + fn mul_self_t(&mut self, value: T); + + /** + * Add the matrix `other` to `self` + */ + fn add_self_m(&mut self, other: &Self); + + /** + * Subtract the matrix `other` from `self` + */ + fn sub_self_m(&mut self, other: &Self); + + /** + * Sets the matrix to its inverse + * + * # Failure + * + * Fails if the matrix is not invertable. Make sure you check with the + * `is_invertible` method before you attempt this! + */ + fn invert_self(&mut self); + + /** + * Sets the matrix to its transpose + */ + fn transpose_self(&mut self); /** * Check to see if the matrix is an identity matrix @@ -234,70 +296,3 @@ pub trait Matrix4: Matrix { fn from_cols(c0: V, c1: V, c2: V, c3: V) -> Self; } - -/** - * A mutable matrix - */ -pub trait MutableMatrix: Matrix { - /** - * # Return value - * - * A mutable reference to the column at `i` - */ - fn col_mut(&mut self, i: uint) -> &'self mut V; - - /** - * Swap two columns of the matrix in place - */ - fn swap_cols(&mut self, a: uint, b: uint); - - /** - * Swap two rows of the matrix in place - */ - fn swap_rows(&mut self, a: uint, b: uint); - - /** - * Sets the matrix to `other` - */ - fn set(&mut self, other: &Self); - - /** - * Sets the matrix to the identity matrix - */ - fn to_identity(&mut self); - - /** - * Sets each element of the matrix to zero - */ - fn to_zero(&mut self); - - /** - * Multiplies the matrix by a scalar - */ - fn mul_self_t(&mut self, value: T); - - /** - * Add the matrix `other` to `self` - */ - fn add_self_m(&mut self, other: &Self); - - /** - * Subtract the matrix `other` from `self` - */ - fn sub_self_m(&mut self, other: &Self); - - /** - * Sets the matrix to its inverse - * - * # Failure - * - * Fails if the matrix is not invertable. Make sure you check with the - * `is_invertible` method before you attempt this! - */ - fn invert_self(&mut self); - - /** - * Sets the matrix to its transpose - */ - fn transpose_self(&mut self); -} diff --git a/src/mat2.rs b/src/mat2.rs index 59ca63d..0e3f03f 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -12,9 +12,8 @@ use numeric::number::Number::{zero,one}; use vec::{ Vec2, Vector2, - MutableVector, + Vector, NumericVector, - MutableNumericVector, vec2, dvec2, }; @@ -26,7 +25,6 @@ use mat::{ Matrix2, Matrix3, Matrix4, - MutableMatrix, }; /** @@ -169,41 +167,7 @@ impl + Add + Sub + Mul + Div + N Matrix2::new(self[0][0], self[1][0], self[0][1], self[1][1]) } - - #[inline(always)] - fn is_identity(&self) -> bool { - self.fuzzy_eq(&Matrix::identity()) - } - - #[inline(always)] - fn is_diagonal(&self) -> bool { - self[0][1].fuzzy_eq(&zero()) && - self[1][0].fuzzy_eq(&zero()) - } - - #[inline(always)] - fn is_rotated(&self) -> bool { - !self.fuzzy_eq(&Matrix::identity()) - } - - #[inline(always)] - fn is_symmetric(&self) -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[1][0].fuzzy_eq(&self[0][1]) - } - - #[inline(always)] - fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&zero()) - } - - #[inline(always)] - fn to_ptr(&self) -> *T { - unsafe { transmute(self) } - } -} - -impl + Add + Sub + Mul + Div + Neg> MutableMatrix > for Mat2 { + #[inline(always)] fn col_mut(&mut self, i: uint) -> &'self mut Vec2 { match i { @@ -270,6 +234,38 @@ impl + Add + Sub + Mul + Div + N swap(self.x.index_mut(1), self.y.index_mut(0)); swap(self.y.index_mut(0), self.x.index_mut(1)); } + + #[inline(always)] + fn is_identity(&self) -> bool { + self.fuzzy_eq(&Matrix::identity()) + } + + #[inline(always)] + fn is_diagonal(&self) -> bool { + self[0][1].fuzzy_eq(&zero()) && + self[1][0].fuzzy_eq(&zero()) + } + + #[inline(always)] + fn is_rotated(&self) -> bool { + !self.fuzzy_eq(&Matrix::identity()) + } + + #[inline(always)] + fn is_symmetric(&self) -> bool { + self[0][1].fuzzy_eq(&self[1][0]) && + self[1][0].fuzzy_eq(&self[0][1]) + } + + #[inline(always)] + fn is_invertible(&self) -> bool { + !self.determinant().fuzzy_eq(&zero()) + } + + #[inline(always)] + fn to_ptr(&self) -> *T { + unsafe { transmute(self) } + } } impl + Add + Sub + Mul + Div + Neg> Matrix2> for Mat2 { diff --git a/src/mat3.rs b/src/mat3.rs index 4b8176a..3db476f 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -13,10 +13,9 @@ use quat::Quat; use vec::{ Vec3, Vector3, - MutableVector, + Vector, NumericVector, NumericVector3, - MutableNumericVector, EuclideanVector, vec3, dvec3, @@ -27,7 +26,6 @@ use mat::{ Matrix, Matrix3, Matrix4, - MutableMatrix, }; /** @@ -196,6 +194,84 @@ impl + Add + Sub + Mul + Div + N self[0][1], self[1][1], self[2][1], self[0][2], self[1][2], self[2][2]) } + + #[inline(always)] + fn col_mut(&mut self, i: uint) -> &'self mut Vec3 { + match i { + 0 => &mut self.x, + 1 => &mut self.y, + 2 => &mut self.z, + _ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) + } + } + + #[inline(always)] + fn swap_cols(&mut self, a: uint, b: uint) { + *self.col_mut(a) <-> *self.col_mut(b); + } + + #[inline(always)] + fn swap_rows(&mut self, a: uint, b: uint) { + self.x.swap(a, b); + self.y.swap(a, b); + self.z.swap(a, b); + } + + #[inline(always)] + fn set(&mut self, other: &Mat3) { + (*self) = (*other); + } + + #[inline(always)] + fn to_identity(&mut self) { + (*self) = Matrix::identity(); + } + + #[inline(always)] + fn to_zero(&mut self) { + (*self) = Matrix::zero(); + } + + #[inline(always)] + fn mul_self_t(&mut self, value: T) { + self.col_mut(0).mul_self_t(value); + self.col_mut(1).mul_self_t(value); + self.col_mut(2).mul_self_t(value); + } + + #[inline(always)] + fn add_self_m(&mut self, other: &Mat3) { + self.col_mut(0).add_self_v(&other[0]); + self.col_mut(1).add_self_v(&other[1]); + self.col_mut(2).add_self_v(&other[2]); + } + + #[inline(always)] + fn sub_self_m(&mut self, other: &Mat3) { + self.col_mut(0).sub_self_v(&other[0]); + self.col_mut(1).sub_self_v(&other[1]); + self.col_mut(2).sub_self_v(&other[2]); + } + + #[inline(always)] + fn invert_self(&mut self) { + match self.inverse() { + Some(m) => (*self) = m, + None => fail!(~"Couldn't invert the matrix!") + } + } + + #[inline(always)] + fn transpose_self(&mut self) { + *self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0); + *self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0); + + *self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1); + *self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1); + + *self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2); + *self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2); + } #[inline(always)] fn is_identity(&self) -> bool { @@ -469,86 +545,6 @@ impl + Add + Sub + Mul + Div + N } } -impl + Add + Sub + Mul + Div + Neg> MutableMatrix> for Mat3 { - #[inline(always)] - fn col_mut(&mut self, i: uint) -> &'self mut Vec3 { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) - } - } - - #[inline(always)] - fn swap_cols(&mut self, a: uint, b: uint) { - *self.col_mut(a) <-> *self.col_mut(b); - } - - #[inline(always)] - fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - self.z.swap(a, b); - } - - #[inline(always)] - fn set(&mut self, other: &Mat3) { - (*self) = (*other); - } - - #[inline(always)] - fn to_identity(&mut self) { - (*self) = Matrix::identity(); - } - - #[inline(always)] - fn to_zero(&mut self) { - (*self) = Matrix::zero(); - } - - #[inline(always)] - fn mul_self_t(&mut self, value: T) { - self.col_mut(0).mul_self_t(value); - self.col_mut(1).mul_self_t(value); - self.col_mut(2).mul_self_t(value); - } - - #[inline(always)] - fn add_self_m(&mut self, other: &Mat3) { - self.col_mut(0).add_self_v(&other[0]); - self.col_mut(1).add_self_v(&other[1]); - self.col_mut(2).add_self_v(&other[2]); - } - - #[inline(always)] - fn sub_self_m(&mut self, other: &Mat3) { - self.col_mut(0).sub_self_v(&other[0]); - self.col_mut(1).sub_self_v(&other[1]); - self.col_mut(2).sub_self_v(&other[2]); - } - - #[inline(always)] - fn invert_self(&mut self) { - match self.inverse() { - Some(m) => (*self) = m, - None => fail!(~"Couldn't invert the matrix!") - } - } - - #[inline(always)] - fn transpose_self(&mut self) { - *self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0); - *self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0); - - *self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1); - *self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1); - - *self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2); - *self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2); - } -} - impl Index> for Mat3 { #[inline(always)] fn index(&self, i: &uint) -> Vec3 { diff --git a/src/mat4.rs b/src/mat4.rs index a1d9748..92dfec7 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -11,9 +11,8 @@ use numeric::number::Number::{zero,one}; use vec::{ Vec4, Vector4, - MutableVector, + Vector, NumericVector, - MutableNumericVector, vec4, dvec4, }; @@ -23,7 +22,6 @@ use mat::{ Matrix, Matrix3, Matrix4, - MutableMatrix, }; /** @@ -266,6 +264,96 @@ impl + Add + Sub + Mul + Div + N 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)] + fn col_mut(&mut self, i: uint) -> &'self mut Vec4 { + match i { + 0 => &mut self.x, + 1 => &mut self.y, + 2 => &mut self.z, + 3 => &mut self.w, + _ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i)) + } + } + + #[inline(always)] + fn swap_cols(&mut self, a: uint, b: uint) { + *self.col_mut(a) <-> *self.col_mut(b); + } + + #[inline(always)] + fn swap_rows(&mut self, a: uint, b: uint) { + self.x.swap(a, b); + self.y.swap(a, b); + self.z.swap(a, b); + self.w.swap(a, b); + } + + #[inline(always)] + fn set(&mut self, other: &Mat4) { + (*self) = (*other); + } + + #[inline(always)] + fn to_identity(&mut self) { + (*self) = Matrix::identity(); + } + + #[inline(always)] + fn to_zero(&mut self) { + (*self) = Matrix::zero(); + } + + #[inline(always)] + fn mul_self_t(&mut self, value: T) { + self.col_mut(0).mul_self_t(value); + self.col_mut(1).mul_self_t(value); + self.col_mut(2).mul_self_t(value); + self.col_mut(3).mul_self_t(value); + } + + #[inline(always)] + fn add_self_m(&mut self, other: &Mat4) { + self.col_mut(0).add_self_v(&other[0]); + self.col_mut(1).add_self_v(&other[1]); + self.col_mut(2).add_self_v(&other[2]); + self.col_mut(3).add_self_v(&other[3]); + } + + #[inline(always)] + fn sub_self_m(&mut self, other: &Mat4) { + self.col_mut(0).sub_self_v(&other[0]); + self.col_mut(1).sub_self_v(&other[1]); + self.col_mut(2).sub_self_v(&other[2]); + self.col_mut(3).sub_self_v(&other[3]); + } + + #[inline(always)] + fn invert_self(&mut self) { + match self.inverse() { + Some(m) => (*self) = m, + None => fail!(~"Couldn't invert the matrix!") + } + } + + #[inline(always)] + fn transpose_self(&mut self) { + *self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0); + *self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0); + *self.col_mut(0).index_mut(3) <-> *self.col_mut(3).index_mut(0); + + *self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1); + *self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1); + *self.col_mut(1).index_mut(3) <-> *self.col_mut(3).index_mut(1); + + *self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2); + *self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2); + *self.col_mut(2).index_mut(3) <-> *self.col_mut(3).index_mut(2); + + *self.col_mut(3).index_mut(0) <-> *self.col_mut(0).index_mut(3); + *self.col_mut(3).index_mut(1) <-> *self.col_mut(1).index_mut(3); + *self.col_mut(3).index_mut(2) <-> *self.col_mut(2).index_mut(3); + } #[inline(always)] fn is_identity(&self) -> bool { @@ -393,98 +481,6 @@ impl + Add + Sub + Mul + Div + N } } -impl + Add + Sub + Mul + Div + Neg> MutableMatrix> for Mat4 { - #[inline(always)] - fn col_mut(&mut self, i: uint) -> &'self mut Vec4 { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - 3 => &mut self.w, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i)) - } - } - - #[inline(always)] - fn swap_cols(&mut self, a: uint, b: uint) { - *self.col_mut(a) <-> *self.col_mut(b); - } - - #[inline(always)] - fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - self.z.swap(a, b); - self.w.swap(a, b); - } - - #[inline(always)] - fn set(&mut self, other: &Mat4) { - (*self) = (*other); - } - - #[inline(always)] - fn to_identity(&mut self) { - (*self) = Matrix::identity(); - } - - #[inline(always)] - fn to_zero(&mut self) { - (*self) = Matrix::zero(); - } - - #[inline(always)] - fn mul_self_t(&mut self, value: T) { - self.col_mut(0).mul_self_t(value); - self.col_mut(1).mul_self_t(value); - self.col_mut(2).mul_self_t(value); - self.col_mut(3).mul_self_t(value); - } - - #[inline(always)] - fn add_self_m(&mut self, other: &Mat4) { - self.col_mut(0).add_self_v(&other[0]); - self.col_mut(1).add_self_v(&other[1]); - self.col_mut(2).add_self_v(&other[2]); - self.col_mut(3).add_self_v(&other[3]); - } - - #[inline(always)] - fn sub_self_m(&mut self, other: &Mat4) { - self.col_mut(0).sub_self_v(&other[0]); - self.col_mut(1).sub_self_v(&other[1]); - self.col_mut(2).sub_self_v(&other[2]); - self.col_mut(3).sub_self_v(&other[3]); - } - - #[inline(always)] - fn invert_self(&mut self) { - match self.inverse() { - Some(m) => (*self) = m, - None => fail!(~"Couldn't invert the matrix!") - } - } - - #[inline(always)] - fn transpose_self(&mut self) { - *self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0); - *self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0); - *self.col_mut(0).index_mut(3) <-> *self.col_mut(3).index_mut(0); - - *self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1); - *self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1); - *self.col_mut(1).index_mut(3) <-> *self.col_mut(3).index_mut(1); - - *self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2); - *self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2); - *self.col_mut(2).index_mut(3) <-> *self.col_mut(3).index_mut(2); - - *self.col_mut(3).index_mut(0) <-> *self.col_mut(0).index_mut(3); - *self.col_mut(3).index_mut(1) <-> *self.col_mut(1).index_mut(3); - *self.col_mut(3).index_mut(2) <-> *self.col_mut(2).index_mut(3); - } -} - impl + Add + Sub + Mul + Div + Neg> Neg> for Mat4 { #[inline(always)] fn neg(&self) -> Mat4 { diff --git a/src/vec.rs b/src/vec.rs index c4828c1..94a6569 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -13,7 +13,7 @@ pub use vec4::{Vec4, vec4, dvec4, bvec4, ivec4, uvec4}; * * `T` - The type of the components. This is intended to support boolean, * integer, unsigned integer, and floating point types. */ -pub trait Vector: Index + Eq { +pub trait Vector: Index + Eq { /** * Construct the vector from a single value, copying it to each component */ @@ -25,9 +25,7 @@ pub trait Vector: Index + Eq { * A pointer to the first component of the vector */ fn to_ptr(&self) -> *T; -} - -pub trait MutableVector<'self, T>: Vector { + /** * Get a mutable reference to the component at `i` */ @@ -129,54 +127,7 @@ pub trait NumericVector: Vector + Neg { * The dot product of the vector and `other` */ fn dot(&self, other: &Self) -> T; -} - -/** - * A 2-dimensional vector with numeric components - */ -pub trait NumericVector2: NumericVector { - fn unit_x() -> Self; - fn unit_y() -> Self; - - /** - * # Return value - * - * The perp dot product of the vector and `other` - */ - fn perp_dot(&self, other: &Self) -> T; -} - -/** - * A 3-dimensional vector with numeric components - */ -pub trait NumericVector3: NumericVector { - fn unit_x() -> Self; - fn unit_y() -> Self; - fn unit_z() -> Self; - - /** - * # Return value - * - * The cross product of the vector and `other` - */ - fn cross(&self, other: &Self) -> Self; -} - -/** - * A 4-dimensional vector with numeric components - */ -pub trait NumericVector4: NumericVector { - fn unit_x() -> Self; - fn unit_y() -> Self; - fn unit_z() -> Self; - fn unit_w() -> Self; -} - -/** - * A mutable vector with numeric components - */ -pub trait MutableNumericVector<'self, T>: MutableVector + - NumericVector { + /** * Negate the vector */ @@ -214,15 +165,51 @@ pub trait MutableNumericVector<'self, T>: MutableVector + } /** - * A mutable 3-dimensional vector with numeric components + * A 2-dimensional vector with numeric components */ -pub trait MutableNumericVector3<'self, T>: MutableNumericVector { +pub trait NumericVector2: NumericVector { + fn unit_x() -> Self; + fn unit_y() -> Self; + + /** + * # Return value + * + * The perp dot product of the vector and `other` + */ + fn perp_dot(&self, other: &Self) -> T; +} + +/** + * A 3-dimensional vector with numeric components + */ +pub trait NumericVector3: NumericVector { + fn unit_x() -> Self; + fn unit_y() -> Self; + fn unit_z() -> Self; + + /** + * # Return value + * + * The cross product of the vector and `other` + */ + fn cross(&self, other: &Self) -> Self; + /** * Set to the cross product of the vector and `other` */ fn cross_self(&mut self, other: &Self); } +/** + * A 4-dimensional vector with numeric components + */ +pub trait NumericVector4: NumericVector { + fn unit_x() -> Self; + fn unit_y() -> Self; + fn unit_z() -> Self; + fn unit_w() -> Self; +} + pub trait ToHomogeneous { /** * Convert to a homogenous coordinate @@ -300,17 +287,7 @@ pub trait EuclideanVector: NumericVector { * The intoperlated vector */ fn lerp(&self, other: &Self, amount: T) -> Self; -} - -/** - * A mutable Euclidean (or Affine) vector - * - * # Type parameters - * - * * `T` - The type of the components. This should be a floating point type. - */ -pub trait MutableEuclideanVector<'self, T>: MutableNumericVector + - EuclideanVector { + /** * Normalize the vector */ diff --git a/src/vec2.rs b/src/vec2.rs index fff17c1..3e76330 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -13,13 +13,10 @@ use vec::{ Vector, Vector2, Vector3, - MutableVector, NumericVector, NumericVector2, - MutableNumericVector, ToHomogeneous, EuclideanVector, - MutableEuclideanVector, EquableVector, OrdinalVector, BooleanVector, @@ -51,6 +48,20 @@ impl Vector for Vec2 { fn to_ptr(&self) -> *T { unsafe { transmute(self) } } + + #[inline(always)] + fn index_mut(&mut self, i: uint) -> &'self mut T { + match i { + 0 => &mut self.x, + 1 => &mut self.y, + _ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) + } + } + + #[inline(always)] + fn swap(&mut self, a: uint, b: uint) { + *self.index_mut(a) <-> *self.index_mut(b); + } } impl Vector2 for Vec2 { @@ -67,22 +78,6 @@ impl Index for Vec2 { } } -impl MutableVector for Vec2 { - #[inline(always)] - fn index_mut(&mut self, i: uint) -> &'self mut T { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) - } - } - - #[inline(always)] - fn swap(&mut self, a: uint, b: uint) { - *self.index_mut(a) <-> *self.index_mut(b); - } -} - impl + Sub + Mul + Div + Neg> NumericVector for Vec2 { #[inline(always)] fn identity() -> Vec2 { @@ -141,33 +136,7 @@ impl + Sub + Mul + Div + Neg> Numer self[0] * other[0] + self[1] * other[1] } -} - -impl + Sub + Mul + Div + Neg> Neg> for Vec2 { - #[inline(always)] - fn neg(&self) -> Vec2 { - Vector2::new(-self[0], -self[1]) - } -} - -impl + Sub + Mul + Div + Neg> NumericVector2 for Vec2 { - #[inline(always)] - fn unit_x() -> Vec2 { - Vector2::new(one::(), zero::()) - } - - #[inline(always)] - fn unit_y() -> Vec2 { - Vector2::new(zero::(), one::()) - } - - #[inline(always)] - fn perp_dot(&self, other: &Vec2) ->T { - (self[0] * other[1]) - (self[1] * other[0]) - } -} - -impl + Sub + Mul + Div + Neg> MutableNumericVector for Vec2 { + #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -*self.index_mut(0); @@ -211,6 +180,30 @@ impl + Sub + Mul + Div + Neg> Mutab } } +impl + Sub + Mul + Div + Neg> Neg> for Vec2 { + #[inline(always)] + fn neg(&self) -> Vec2 { + Vector2::new(-self[0], -self[1]) + } +} + +impl + Sub + Mul + Div + Neg> NumericVector2 for Vec2 { + #[inline(always)] + fn unit_x() -> Vec2 { + Vector2::new(one::(), zero::()) + } + + #[inline(always)] + fn unit_y() -> Vec2 { + Vector2::new(zero::(), one::()) + } + + #[inline(always)] + fn perp_dot(&self, other: &Vec2) ->T { + (self[0] * other[1]) - (self[1] * other[0]) + } +} + impl ToHomogeneous> for Vec2 { #[inline(always)] fn to_homogeneous(&self) -> Vec3 { @@ -258,9 +251,7 @@ impl + Sub + Mul + Div + Neg> Euclid fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { self.add_v(&other.sub_v(self).mul_t(amount)) } -} - -impl + Sub + Mul + Div + Neg> MutableEuclideanVector for Vec2 { + #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); diff --git a/src/vec3.rs b/src/vec3.rs index 1cbe0b6..7ac04c2 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -13,14 +13,10 @@ use vec::{ Vector, Vector3, Vector4, - MutableVector, NumericVector, NumericVector3, - MutableNumericVector, - MutableNumericVector3, ToHomogeneous, EuclideanVector, - MutableEuclideanVector, EquableVector, OrdinalVector, BooleanVector, @@ -53,23 +49,7 @@ impl Vector for Vec3 { fn to_ptr(&self) -> *T { unsafe { transmute(self) } } -} - -impl Vector3 for Vec3 { - #[inline(always)] - fn new(x: T, y: T, z: T) -> Vec3 { - Vec3 { x: x, y: y, z: z } - } -} - -impl Index for Vec3 { - #[inline(always)] - fn index(&self, i: &uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[*i] } } - } -} - -impl MutableVector for Vec3 { + #[inline(always)] fn index_mut(&mut self, i: uint) -> &'self mut T { match i { @@ -86,6 +66,20 @@ impl MutableVector for Vec3 { } } +impl Vector3 for Vec3 { + #[inline(always)] + fn new(x: T, y: T, z: T) -> Vec3 { + Vec3 { x: x, y: y, z: z } + } +} + +impl Index for Vec3 { + #[inline(always)] + fn index(&self, i: &uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[*i] } } + } +} + impl + Sub + Mul + Div + Neg> NumericVector for Vec3 { #[inline(always)] fn identity() -> Vec3 { @@ -152,40 +146,7 @@ impl + Sub + Mul + Div + Neg> Numer self[1] * other[1] + self[2] * other[2] } -} - -impl + Sub + Mul + Div + Neg> Neg> for Vec3 { - #[inline(always)] - fn neg(&self) -> Vec3 { - Vector3::new(-self[0], -self[1], -self[2]) - } -} - -impl + Sub + Mul + Div + Neg> NumericVector3 for Vec3 { - #[inline(always)] - fn unit_x() -> Vec3 { - Vector3::new(one::(), zero::(), zero::()) - } - - #[inline(always)] - fn unit_y() -> Vec3 { - Vector3::new(zero::(), one::(), zero::()) - } - - #[inline(always)] - fn unit_z() -> Vec3 { - Vector3::new(zero::(), zero::(), one::()) - } - - #[inline(always)] - fn cross(&self, other: &Vec3) -> Vec3 { - Vector3::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])) - } -} - -impl + Sub + Mul + Div + Neg> MutableNumericVector for Vec3 { + #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -*self.index_mut(0); @@ -236,7 +197,36 @@ impl + Sub + Mul + Div + Neg> Mutab } } -impl + Sub + Mul + Div + Neg> MutableNumericVector3 for Vec3 { +impl + Sub + Mul + Div + Neg> Neg> for Vec3 { + #[inline(always)] + fn neg(&self) -> Vec3 { + Vector3::new(-self[0], -self[1], -self[2]) + } +} + +impl + Sub + Mul + Div + Neg> NumericVector3 for Vec3 { + #[inline(always)] + fn unit_x() -> Vec3 { + Vector3::new(one::(), zero::(), zero::()) + } + + #[inline(always)] + fn unit_y() -> Vec3 { + Vector3::new(zero::(), one::(), zero::()) + } + + #[inline(always)] + fn unit_z() -> Vec3 { + Vector3::new(zero::(), zero::(), one::()) + } + + #[inline(always)] + fn cross(&self, other: &Vec3) -> Vec3 { + Vector3::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])) + } + #[inline(always)] fn cross_self(&mut self, other: &Vec3) { *self = self.cross(other); @@ -290,9 +280,7 @@ impl + Sub + Mul + Div + Neg> Euclid fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { self.add_v(&other.sub_v(self).mul_t(amount)) } -} - -impl + Sub + Mul + Div + Neg> MutableEuclideanVector for Vec3 { + #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); diff --git a/src/vec4.rs b/src/vec4.rs index 6a57081..e5497c4 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -11,13 +11,10 @@ use numeric::number::Number::{zero,one}; use vec::{ Vector, Vector4, - MutableVector, NumericVector, NumericVector4, - MutableNumericVector, ToHomogeneous, EuclideanVector, - MutableEuclideanVector, EquableVector, OrdinalVector, BooleanVector, @@ -51,23 +48,7 @@ impl Vector for Vec4 { fn to_ptr(&self) -> *T { unsafe { transmute(self) } } -} - -impl Vector4 for Vec4 { - #[inline(always)] - fn new(x: T, y: T, z: T, w: T) -> Vec4 { - Vec4 { x: x, y: y, z: z, w: w } - } -} - -impl Index for Vec4 { - #[inline(always)] - fn index(&self, i: &uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[*i] } } - } -} - -impl MutableVector for Vec4 { + #[inline(always)] fn index_mut(&mut self, i: uint) -> &'self mut T { match i { @@ -85,6 +66,20 @@ impl MutableVector for Vec4 { } } +impl Vector4 for Vec4 { + #[inline(always)] + fn new(x: T, y: T, z: T, w: T) -> Vec4 { + Vec4 { x: x, y: y, z: z, w: w } + } +} + +impl Index for Vec4 { + #[inline(always)] + fn index(&self, i: &uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[*i] } } + } +} + impl + Sub + Mul + Div + Neg> NumericVector for Vec4 { #[inline(always)] fn identity() -> Vec4 { @@ -159,38 +154,7 @@ impl + Sub + Mul + Div + Neg> Numer self[2] * other[2] + self[3] * other[3] } -} - -impl + Sub + Mul + Div + Neg> Neg> for Vec4 { - #[inline(always)] - fn neg(&self) -> Vec4 { - Vector4::new(-self[0], -self[1], -self[2], -self[3]) - } -} - -impl NumericVector4 for Vec4 { - #[inline(always)] - fn unit_x() -> Vec4 { - Vector4::new(one::(), zero::(), zero::(), zero::()) - } - - #[inline(always)] - fn unit_y() -> Vec4 { - Vector4::new(zero::(), one::(), zero::(), zero::()) - } - - #[inline(always)] - fn unit_z() -> Vec4 { - Vector4::new(zero::(), zero::(), one::(), zero::()) - } - - #[inline(always)] - fn unit_w() -> Vec4 { - Vector4::new(zero::(), zero::(), zero::(), one::()) - } -} - -impl<'self,T:Copy + Number + Add + Sub + Mul + Div + Neg> MutableNumericVector for Vec4 { + #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -*self.index_mut(0); @@ -248,6 +212,35 @@ impl<'self,T:Copy + Number + Add + Sub + Mul + Div + Neg> } } +impl + Sub + Mul + Div + Neg> Neg> for Vec4 { + #[inline(always)] + fn neg(&self) -> Vec4 { + Vector4::new(-self[0], -self[1], -self[2], -self[3]) + } +} + +impl NumericVector4 for Vec4 { + #[inline(always)] + fn unit_x() -> Vec4 { + Vector4::new(one::(), zero::(), zero::(), zero::()) + } + + #[inline(always)] + fn unit_y() -> Vec4 { + Vector4::new(zero::(), one::(), zero::(), zero::()) + } + + #[inline(always)] + fn unit_z() -> Vec4 { + Vector4::new(zero::(), zero::(), one::(), zero::()) + } + + #[inline(always)] + fn unit_w() -> Vec4 { + Vector4::new(zero::(), zero::(), zero::(), one::()) + } +} + impl + Sub + Mul + Div + Neg> EuclideanVector for Vec4 { #[inline(always)] fn length2(&self) -> T { @@ -288,9 +281,7 @@ impl + Sub + Mul + Div + Neg> Euclid fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { self.add_v(&other.sub_v(self).mul_t(amount)) } -} - -impl + Sub + Mul + Div + Neg> MutableEuclideanVector for Vec4 { + #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length();