diff --git a/src/mat.rs b/src/mat.rs index f48d832..73821ca 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -18,122 +18,122 @@ pub use mat4::{Mat4, mat4, dmat4}; * floating point type and have the same number of dimensions as the * number of rows and columns in the matrix. */ -pub trait Matrix: Index Eq Neg { +pub trait Matrix: Index + Eq + Neg { /** * # Return value * * The column vector at `i` */ pure fn col(&self, i: uint) -> V; - + /** * # Return value * * The row vector at `i` */ pure fn row(&self, i: uint) -> V; - + /** * Construct a diagonal matrix with the major diagonal set to `value` */ static pure fn from_value(value: T) -> Self; - + /** * # Return value * * The identity matrix */ static pure fn identity() -> Self; - + /** * # Return value * * A matrix with all elements set to zero */ static pure fn zero() -> Self; - + /** * # Return value * * The scalar multiplication of this matrix and `value` */ pure fn mul_t(&self, value: T) -> Self; - + /** * # Return value * * The matrix vector product of the matrix and `vec` */ pure fn mul_v(&self, vec: &V) -> V; - + /** * # Return value * * The matrix addition of the matrix and `other` */ pure fn add_m(&self, other: &Self) -> Self; - + /** * # Return value * * The difference between the matrix and `other` */ pure fn sub_m(&self, other: &Self) -> Self; - + /** * # Return value * * The matrix product of the matrix and `other` */ pure fn mul_m(&self, other: &Self) -> Self; - + /** * # Return value * * The matrix dot product of the matrix and `other` */ pure fn dot(&self, other: &Self) -> T; - + /** * # Return value * * The determinant of the matrix */ pure fn determinant(&self) -> T; - + /** * # Return value * * The sum of the main diagonal of the matrix */ pure fn trace(&self) -> T; - + /** * Returns the inverse of the matrix - * + * * # Return value * * * `Some(m)` - if the inversion was successful, where `m` is the inverted matrix * * `None` - if the inversion was unsuccessful (because the matrix was not invertable) */ pure fn inverse(&self) -> Option; - + /** * # Return value * * The transposed matrix */ pure fn transpose(&self) -> Self; - + /** * Check to see if the matrix is an identity matrix * * # Return value - * + * * `true` if the matrix is approximately equal to the identity matrix */ pure fn is_identity(&self) -> bool; - + /** * Check to see if the matrix is diagonal * @@ -143,7 +143,7 @@ pub trait Matrix: Index Eq Neg { * equal to zero. */ pure fn is_diagonal(&self) -> bool; - + /** * Check to see if the matrix is rotated * @@ -152,7 +152,7 @@ pub trait Matrix: Index Eq Neg { * `true` if the matrix is not approximately equal to the identity matrix. */ pure fn is_rotated(&self) -> bool; - + /** * Check to see if the matrix is symmetric * @@ -161,7 +161,7 @@ pub trait Matrix: Index Eq Neg { * `true` if the matrix is approximately equal to its transpose). */ pure fn is_symmetric(&self) -> bool; - + /** * Check to see if the matrix is invertable * @@ -170,7 +170,7 @@ pub trait Matrix: Index Eq Neg { * `true` if the matrix is invertable */ pure fn is_invertible(&self) -> bool; - + /** * # Return value * @@ -185,13 +185,13 @@ pub trait Matrix: Index Eq Neg { pub trait Matrix2: Matrix { static pure fn new(c0r0: T, c0r1: T, c1r0: T, c1r1: T) -> Self; - + static pure fn from_cols(c0: V, c1: V) -> Self; - + static pure fn from_angle(radians: T) -> Self; - + pure fn to_mat3(&self) -> Mat3; - + pure fn to_mat4(&self) -> Mat4; } @@ -202,25 +202,25 @@ pub trait Matrix3: Matrix { static pure fn new(c0r0:T, c0r1:T, c0r2:T, c1r0:T, c1r1:T, c1r2:T, c2r0:T, c2r1:T, c2r2:T) -> Self; - + static pure fn from_cols(c0: V, c1: V, c2: V) -> Self; - + static pure fn from_angle_x(radians: T) -> Self; - + static pure fn from_angle_y(radians: T) -> Self; - + static pure fn from_angle_z(radians: T) -> Self; - + static pure fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Self; - + static pure fn from_angle_axis(radians: T, axis: &Vec3) -> Self; - + static pure fn from_axes(x: V, y: V, z: V) -> Self; - + static pure fn look_at(dir: &Vec3, up: &Vec3) -> Self; - + pure fn to_mat4(&self) -> Mat4; - + pure fn to_quat(&self) -> Quat; } @@ -232,7 +232,7 @@ pub trait Matrix4: Matrix { c1r0: T, c1r1: T, c1r2: T, c1r3: T, c2r0: T, c2r1: T, c2r2: T, c2r3: T, c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Self; - + static pure fn from_cols(c0: V, c1: V, c2: V, c3: V) -> Self; } @@ -246,27 +246,27 @@ pub trait MutableMatrix: Matrix { * 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 + * Swap two rows of the matrix in place */ fn swap_rows(&mut self, a: uint, b: uint); - + /** - * Sets the matrix to `other` + * 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 */ @@ -276,27 +276,27 @@ pub trait MutableMatrix: Matrix { * 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 */ diff --git a/src/mat2.rs b/src/mat2.rs index 1e25a02..4605339 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -46,16 +46,16 @@ use mat::{ #[deriving_eq] pub struct Mat2 { x: Vec2, y: Vec2 } -pub impl> Matrix> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat2 { #[inline(always)] pure fn col(&self, i: uint) -> Vec2 { self[i] } - + #[inline(always)] pure fn row(&self, i: uint) -> Vec2 { Vector2::new(self[0][i], self[1][i]) } - + /** * Construct a 2 x 2 diagonal matrix with the major diagonal set to `value` * @@ -77,12 +77,12 @@ pub impl> Matrix> for Mat2 { Matrix2::new(value, zero(), zero(), value) } - + /** * Returns the multiplicative identity matrix * ~~~ * c0 c1 - * +----+----+ + * +----+----+ * r0 | 1 | 0 | * +----+----+ * r1 | 0 | 1 | @@ -94,12 +94,12 @@ pub impl> Matrix> for Mat2 { Matrix2::new( one::(), zero::(), zero::(), one::()) } - + /** * Returns the additive identity matrix * ~~~ * c0 c1 - * +----+----+ + * +----+----+ * r0 | 0 | 0 | * +----+----+ * r1 | 0 | 0 | @@ -111,31 +111,31 @@ pub impl> Matrix> for Mat2 { Matrix2::new(zero::(), zero::(), zero::(), zero::()) } - + #[inline(always)] pure fn mul_t(&self, value: T) -> Mat2 { Matrix2::from_cols(self[0].mul_t(value), self[1].mul_t(value)) } - + #[inline(always)] pure fn mul_v(&self, vec: &Vec2) -> Vec2 { Vector2::new(self.row(0).dot(vec), self.row(1).dot(vec)) } - + #[inline(always)] pure fn add_m(&self, other: &Mat2) -> Mat2 { Matrix2::from_cols(self[0].add_v(&other[0]), self[1].add_v(&other[1])) } - + #[inline(always)] pure fn sub_m(&self, other: &Mat2) -> Mat2 { Matrix2::from_cols(self[0].sub_v(&other[0]), self[1].sub_v(&other[1])) } - + #[inline(always)] pure fn mul_m(&self, other: &Mat2) -> Mat2 { Matrix2::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), @@ -145,7 +145,7 @@ pub impl> Matrix> for Mat2 { pure fn dot(&self, other: &Mat2) -> T { other.transpose().mul_m(self).trace() } - + pure fn determinant(&self) -> T { self[0][0] * self[1][1] - self[1][0] * self[0][1] } @@ -164,29 +164,29 @@ pub impl> Matrix> for Mat2 { -self[1][0]/d, self[0][0]/d)) } } - + #[inline(always)] pure fn transpose(&self) -> Mat2 { Matrix2::new(self[0][0], self[1][0], self[0][1], self[1][1]) } - + #[inline(always)] pure fn is_identity(&self) -> bool { self.fuzzy_eq(&Matrix::identity()) } - + #[inline(always)] pure fn is_diagonal(&self) -> bool { self[0][1].fuzzy_eq(&zero()) && self[1][0].fuzzy_eq(&zero()) } - + #[inline(always)] pure fn is_rotated(&self) -> bool { !self.fuzzy_eq(&Matrix::identity()) } - + #[inline(always)] pure fn is_symmetric(&self) -> bool { self[0][1].fuzzy_eq(&self[1][0]) && @@ -197,7 +197,7 @@ pub impl> Matrix> for Mat2 { pure fn is_invertible(&self) -> bool { !self.determinant().fuzzy_eq(&zero()) } - + #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -208,7 +208,7 @@ pub impl> Matrix> for Mat2 { } } -pub impl> MutableMatrix> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> MutableMatrix > for Mat2 { #[inline(always)] fn col_mut(&mut self, i: uint) -> &self/mut Vec2 { match i { @@ -217,52 +217,51 @@ pub impl> MutableMatrix> for Mat2 { _ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) } } - + #[inline(always)] fn swap_cols(&mut self, a: uint, b: uint) { - swap(self.col_mut(a), - self.col_mut(b)); + swap(&mut self.x, &mut self.y); } - + #[inline(always)] fn swap_rows(&mut self, a: uint, b: uint) { self.x.swap(a, b); self.y.swap(a, b); } - + #[inline(always)] fn set(&mut self, other: &Mat2) { (*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); + &mut self.x.mul_self_t(&value); + &mut self.y.mul_self_t(&value); } - + #[inline(always)] fn add_self_m(&mut self, other: &Mat2) { - self.col_mut(0).add_self_v(&other[0]); - self.col_mut(1).add_self_v(&other[1]); + &mut self.x.add_self_v(&other[0]); + &mut self.y.add_self_v(&other[1]); } - + #[inline(always)] fn sub_self_m(&mut self, other: &Mat2) { - self.col_mut(0).sub_self_v(&other[0]); - self.col_mut(1).sub_self_v(&other[1]); + &mut self.x.sub_self_v(&other[0]); + &mut self.y.sub_self_v(&other[1]); } - + #[inline(always)] fn invert_self(&mut self) { match self.inverse() { @@ -270,15 +269,15 @@ pub impl> MutableMatrix> for Mat2 { None => fail!(~"Couldn't invert the matrix!") } } - + #[inline(always)] fn transpose_self(&mut self) { - swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); - swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); + &mut swap(&mut self.x.index_mut(1), &mut self.y.index_mut(0)); + &mut swap(&mut self.y.index_mut(0), &mut self.x.index_mut(1)); } } -pub impl> Matrix2> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> Matrix2> for Mat2 { /** * Construct a 2 x 2 matrix * @@ -302,7 +301,7 @@ pub impl> Matrix2> for Mat2 { Matrix2::from_cols(Vector2::new::>(c0r0, c0r1), Vector2::new::>(c1r0, c1r1)) } - + /** * Construct a 2 x 2 matrix from column vectors * @@ -325,16 +324,16 @@ pub impl> Matrix2> for Mat2 { c1: Vec2) -> Mat2 { Mat2 { x: c0, y: c1 } } - + #[inline(always)] static pure fn from_angle(radians: T) -> Mat2 { let cos_theta = cos(radians); let sin_theta = sin(radians); - + Matrix2::new(cos_theta, -sin_theta, sin_theta, cos_theta) } - + /** * Returns the the matrix with an extra row and column added * ~~~ @@ -354,7 +353,7 @@ pub impl> Matrix2> for Mat2 { self[1][0], self[1][1], zero(), zero(), zero(), one()) } - + /** * Returns the the matrix with an extra two rows and columns added * ~~~ @@ -379,7 +378,7 @@ pub impl> Matrix2> for Mat2 { } } -pub impl Index> for Mat2 { +impl Index> for Mat2 { #[inline(always)] pure fn index(&self, i: uint) -> Vec2 { unsafe { do buf_as_slice( @@ -389,19 +388,19 @@ pub impl Index> for Mat2 { } } -pub impl> Neg> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> Neg> for Mat2 { #[inline(always)] pure fn neg(&self) -> Mat2 { Matrix2::from_cols(-self[0], -self[1]) } } -pub impl> FuzzyEq for Mat2 { +impl> FuzzyEq for Mat2 { #[inline(always)] pure fn fuzzy_eq(&self, other: &Mat2) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -417,36 +416,36 @@ pub type dmat2 = Mat2; // a 2×2 double-precision floating-point matrix // Static method wrappers for GLSL-style types -pub impl mat2 { +impl mat2 { #[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c1r0: f32, c1r1: f32) -> mat2 { Matrix2::new(c0r0, c0r1, c1r0, c1r1) } #[inline(always)] static pure fn from_cols(c0: vec2, c1: vec2) -> mat2 { Matrix2::from_cols(c0, c1) } #[inline(always)] static pure fn from_value(v: f32) -> mat2 { Matrix::from_value(v) } - + #[inline(always)] static pure fn identity() -> mat2 { Matrix::identity() } #[inline(always)] static pure fn zero() -> mat2 { Matrix::zero() } - + #[inline(always)] static pure fn from_angle(radians: f32) -> mat2 { Matrix2::from_angle(radians) } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn rows() -> uint { 2 } #[inline(always)] static pure fn cols() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dmat2 { +impl dmat2 { #[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c1r0: f64, c1r1: f64) -> dmat2 { Matrix2::new(c0r0, c0r1, c1r0, c1r1) } #[inline(always)] static pure fn from_cols(c0: dvec2, c1: dvec2) -> dmat2 { Matrix2::from_cols(c0, c1) } #[inline(always)] static pure fn from_value(v: f64) -> dmat2 { Matrix::from_value(v) } - + #[inline(always)] static pure fn identity() -> dmat2 { Matrix::identity() } #[inline(always)] static pure fn zero() -> dmat2 { Matrix::zero() } - + #[inline(always)] static pure fn from_angle(radians: f64) -> dmat2 { Matrix2::from_angle(radians) } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn rows() -> uint { 2 } #[inline(always)] static pure fn cols() -> uint { 2 } diff --git a/src/mat3.rs b/src/mat3.rs index d41e2af..54232da 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -48,17 +48,17 @@ use mat::{ #[deriving_eq] pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } -pub impl> Matrix> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat3 { #[inline(always)] pure fn col(&self, i: uint) -> Vec3 { self[i] } - + #[inline(always)] pure fn row(&self, i: uint) -> Vec3 { Vector3::new(self[0][i], self[1][i], self[2][i]) } - + /** * Construct a 3 x 3 diagonal matrix with the major diagonal set to `value` * @@ -83,7 +83,7 @@ pub impl> Matrix> for Mat3 { zero(), value, zero(), zero(), zero(), value) } - + /** * Returns the multiplicative identity matrix * ~~~ @@ -103,7 +103,7 @@ pub impl> Matrix> for Mat3 { zero::(), one::(), zero::(), zero::(), zero::(), one::()) } - + /** * Returns the additive identity matrix * ~~~ @@ -123,50 +123,50 @@ pub impl> Matrix> for Mat3 { zero::(), zero::(), zero::(), zero::(), zero::(), zero::()) } - + #[inline(always)] pure fn mul_t(&self, value: T) -> Mat3 { Matrix3::from_cols(self[0].mul_t(value), self[1].mul_t(value), self[2].mul_t(value)) } - + #[inline(always)] pure fn mul_v(&self, vec: &Vec3) -> Vec3 { Vector3::new(self.row(0).dot(vec), self.row(1).dot(vec), self.row(2).dot(vec)) } - + #[inline(always)] pure fn add_m(&self, other: &Mat3) -> Mat3 { Matrix3::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(&self, other: &Mat3) -> Mat3 { Matrix3::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(&self, other: &Mat3) -> Mat3 { Matrix3::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), - + self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), - + self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2))) } - + pure fn dot(&self, other: &Mat3) -> T { other.transpose().mul_m(self).trace() } @@ -191,44 +191,44 @@ pub impl> Matrix> for Mat3 { Some(m.transpose()) } } - + #[inline(always)] pure fn transpose(&self) -> Mat3 { Matrix3::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)] pure fn is_identity(&self) -> bool { self.fuzzy_eq(&Matrix::identity()) } - + #[inline(always)] pure fn is_diagonal(&self) -> bool { self[0][1].fuzzy_eq(&zero()) && self[0][2].fuzzy_eq(&zero()) && - + self[1][0].fuzzy_eq(&zero()) && self[1][2].fuzzy_eq(&zero()) && - + self[2][0].fuzzy_eq(&zero()) && self[2][1].fuzzy_eq(&zero()) } - + #[inline(always)] pure fn is_rotated(&self) -> bool { !self.fuzzy_eq(&Matrix::identity()) } - + #[inline(always)] pure fn is_symmetric(&self) -> bool { self[0][1].fuzzy_eq(&self[1][0]) && self[0][2].fuzzy_eq(&self[2][0]) && - + self[1][0].fuzzy_eq(&self[0][1]) && self[1][2].fuzzy_eq(&self[2][1]) && - + self[2][0].fuzzy_eq(&self[0][2]) && self[2][1].fuzzy_eq(&self[1][2]) } @@ -237,7 +237,7 @@ pub impl> Matrix> for Mat3 { pure fn is_invertible(&self) -> bool { !self.determinant().fuzzy_eq(&zero()) } - + #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -248,7 +248,7 @@ pub impl> Matrix> for Mat3 { } } -pub impl> Matrix3> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> Matrix3> for Mat3 { /** * Construct a 3 x 3 matrix * @@ -277,7 +277,7 @@ pub impl> Matrix3> for Mat3 { Vector3::new::>(c1r0, c1r1, c1r2), Vector3::new::>(c2r0, c2r1, c2r2)) } - + /** * Construct a 3 x 3 matrix from column vectors * @@ -304,7 +304,7 @@ pub impl> Matrix3> for Mat3 { c2: Vec3) -> Mat3 { Mat3 { x: c0, y: c1, z: c2 } } - + /** * Construct a matrix from an angular rotation around the `x` axis */ @@ -313,12 +313,12 @@ pub impl> Matrix3> for Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let cos_theta = cos(radians); let sin_theta = sin(radians); - + Matrix3::new( one(), zero(), zero(), zero(), cos_theta, sin_theta, zero(), -sin_theta, cos_theta) } - + /** * Construct a matrix from an angular rotation around the `y` axis */ @@ -327,12 +327,12 @@ pub impl> Matrix3> for Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let cos_theta = cos(radians); let sin_theta = sin(radians); - + Matrix3::new(cos_theta, zero(), -sin_theta, zero(), one(), zero(), sin_theta, zero(), cos_theta) } - + /** * Construct a matrix from an angular rotation around the `z` axis */ @@ -341,12 +341,12 @@ pub impl> Matrix3> for Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let cos_theta = cos(radians); let sin_theta = sin(radians); - + Matrix3::new( cos_theta, sin_theta, zero(), -sin_theta, cos_theta, zero(), zero(), zero(), one()) } - + /** * Construct a matrix from Euler angles * @@ -365,12 +365,12 @@ pub impl> Matrix3> for Mat3 { let sy = sin(radians_y); let cz = cos(radians_z); let sz = sin(radians_z); - + Matrix3::new( cy*cz, cy*sz, -sy, -cx*sz + sx*sy*cz, cx*cz + sx*sy*sz, sx*cy, sx*sz + cx*sy*cz, -sx*cz + cx*sy*sz, cx*cy) } - + /** * Construct a matrix from an axis and an angular rotation */ @@ -379,30 +379,30 @@ pub impl> Matrix3> for Mat3 { let c = cos(radians); let s = sin(radians); let _1_c = one::() - c; - + let x = axis.x; let y = axis.y; let z = axis.z; - + Matrix3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y, _1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x, _1_c*x*z + s*y, _1_c*y*z - s*x, _1_c*z*z + c) } - + #[inline(always)] static pure fn from_axes(x: Vec3, y: Vec3, z: Vec3) -> Mat3 { Matrix3::from_cols(x, y, z) } - + #[inline(always)] static pure fn look_at(dir: &Vec3, up: &Vec3) -> Mat3 { let dir_ = dir.normalize(); let side = dir_.cross(&up.normalize()); let up_ = side.cross(&dir_).normalize(); - + Matrix3::from_axes(up_, side, dir_) } - + /** * Returns the the matrix with an extra row and column added * ~~~ @@ -425,7 +425,7 @@ pub impl> Matrix3> for Mat3 { self[2][0], self[2][1], self[2][2], zero(), zero(), zero(), zero(), one()) } - + /** * Convert the matrix to a quaternion */ @@ -433,14 +433,14 @@ pub impl> Matrix3> for Mat3 { pure fn to_quat(&self) -> Quat { // Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's // paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf - + let mut s; let w, x, y, z; let trace = self.trace(); - + let _1: T = Number::from(1.0); let half: T = Number::from(0.5); - + if trace >= zero() { s = (_1 + trace).sqrt(); w = half * s; @@ -470,12 +470,12 @@ pub impl> Matrix3> for Mat3 { y = (self[1][2] - self[2][1]) * s; z = (self[0][1] - self[1][0]) * s; } - + Quat::new(w, x, y, z) } } -pub impl> MutableMatrix> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> MutableMatrix> for Mat3 { #[inline(always)] fn col_mut(&mut self, i: uint) -> &self/mut Vec3 { match i { @@ -485,56 +485,56 @@ pub impl> MutableMatrix> for Mat3 { _ => 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) { swap(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() { @@ -542,21 +542,21 @@ pub impl> MutableMatrix> for Mat3 { None => fail!(~"Couldn't invert the matrix!") } } - + #[inline(always)] fn transpose_self(&mut self) { swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0)); - + swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1)); - + swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2)); swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2)); } } -pub impl Index> for Mat3 { +impl Index> for Mat3 { #[inline(always)] pure fn index(&self, i: uint) -> Vec3 { unsafe { do buf_as_slice( @@ -566,19 +566,19 @@ pub impl Index> for Mat3 { } } -pub impl> Neg> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> Neg> for Mat3 { #[inline(always)] pure fn neg(&self) -> Mat3 { Matrix3::from_cols(-self[0], -self[1], -self[2]) } } -pub impl> FuzzyEq for Mat3 { +impl + Add + Sub + Mul + Div + Neg> FuzzyEq for Mat3 { #[inline(always)] pure fn fuzzy_eq(&self, other: &Mat3) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -595,16 +595,16 @@ pub type dmat3 = Mat3; // a 3×3 double-precision floating-point matrix // Static method wrappers for GLSL-style types -pub impl mat3 { +impl mat3 { #[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c0r2: f32, c1r0: f32, c1r1: f32, c1r2: f32, c2r0: f32, c2r1: f32, c2r2: f32) -> mat3 { Matrix3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } #[inline(always)] static pure fn from_cols(c0: vec3, c1: vec3, c2: vec3) -> mat3 { Matrix3::from_cols(c0, c1, c2) } #[inline(always)] static pure fn from_value(v: f32) -> mat3 { Matrix::from_value(v) } - + #[inline(always)] static pure fn identity() -> mat3 { Matrix::identity() } #[inline(always)] static pure fn zero() -> mat3 { Matrix::zero() } - + #[inline(always)] static pure fn from_angle_x(radians: f32) -> mat3 { Matrix3::from_angle_x(radians) } #[inline(always)] static pure fn from_angle_y(radians: f32) -> mat3 { Matrix3::from_angle_y(radians) } #[inline(always)] static pure fn from_angle_z(radians: f32) -> mat3 { Matrix3::from_angle_z(radians) } @@ -612,7 +612,7 @@ pub impl mat3 { #[inline(always)] static pure fn from_angle_axis(radians: f32, axis: &vec3) -> mat3 { Matrix3::from_angle_axis(radians, axis) } #[inline(always)] static pure fn from_axes(x: vec3, y: vec3, z: vec3) -> mat3 { Matrix3::from_axes(x, y, z) } #[inline(always)] static pure fn look_at(dir: &vec3, up: &vec3) -> mat3 { Matrix3::look_at(dir, up) } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn rows() -> uint { 3 } #[inline(always)] static pure fn cols() -> uint { 3 } @@ -620,16 +620,16 @@ pub impl mat3 { } -pub impl dmat3 { +impl dmat3 { #[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c0r2: f64, c1r0: f64, c1r1: f64, c1r2: f64, c2r0: f64, c2r1: f64, c2r2: f64) -> dmat3 { Matrix3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } #[inline(always)] static pure fn from_cols(c0: dvec3, c1: dvec3, c2: dvec3) -> dmat3 { Matrix3::from_cols(c0, c1, c2) } #[inline(always)] static pure fn from_value(v: f64) -> dmat3 { Matrix::from_value(v) } - + #[inline(always)] static pure fn identity() -> dmat3 { Matrix::identity() } #[inline(always)] static pure fn zero() -> dmat3 { Matrix::zero() } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn rows() -> uint { 3 } #[inline(always)] static pure fn cols() -> uint { 3 } diff --git a/src/mat4.rs b/src/mat4.rs index 7bca0e1..1de82d1 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -45,10 +45,10 @@ use mat::{ #[deriving_eq] pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } -pub impl> Matrix> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat4 { #[inline(always)] pure fn col(&self, i: uint) -> Vec4 { self[i] } - + #[inline(always)] pure fn row(&self, i: uint) -> Vec4 { Vector4::new(self[0][i], @@ -56,7 +56,7 @@ pub impl> Matrix> for Mat4 { self[2][i], self[3][i]) } - + /** * Construct a 4 x 4 diagonal matrix with the major diagonal set to `value` * @@ -84,7 +84,7 @@ pub impl> Matrix> for Mat4 { zero(), zero(), value, zero(), zero(), zero(), zero(), value) } - + /** * Returns the multiplicative identity matrix * ~~~ @@ -107,7 +107,7 @@ pub impl> Matrix> for Mat4 { zero::(), zero::(), one::(), zero::(), zero::(), zero::(), zero::(), one::()) } - + /** * Returns the additive identity matrix * ~~~ @@ -130,7 +130,7 @@ pub impl> Matrix> for Mat4 { zero::(), zero::(), zero::(), zero::(), zero::(), zero::(), zero::(), zero::()) } - + #[inline(always)] pure fn mul_t(&self, value: T) -> Mat4 { Matrix4::from_cols(self[0].mul_t(value), @@ -138,7 +138,7 @@ pub impl> Matrix> for Mat4 { self[2].mul_t(value), self[3].mul_t(value)) } - + #[inline(always)] pure fn mul_v(&self, vec: &Vec4) -> Vec4 { Vector4::new(self.row(0).dot(vec), @@ -146,7 +146,7 @@ pub impl> Matrix> for Mat4 { self.row(2).dot(vec), self.row(3).dot(vec)) } - + #[inline(always)] pure fn add_m(&self, other: &Mat4) -> Mat4 { Matrix4::from_cols(self[0].add_v(&other[0]), @@ -154,7 +154,7 @@ pub impl> Matrix> for Mat4 { self[2].add_v(&other[2]), self[3].add_v(&other[3])) } - + #[inline(always)] pure fn sub_m(&self, other: &Mat4) -> Mat4 { Matrix4::from_cols(self[0].sub_v(&other[0]), @@ -162,31 +162,31 @@ pub impl> Matrix> for Mat4 { self[2].sub_v(&other[2]), self[3].sub_v(&other[3])) } - + #[inline(always)] pure fn mul_m(&self, other: &Mat4) -> Mat4 { Matrix4::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), self.row(3).dot(&other.col(0)), - + self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), self.row(3).dot(&other.col(1)), - + self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2)), self.row(3).dot(&other.col(2)), - + self.row(0).dot(&other.col(3)), self.row(1).dot(&other.col(3)), self.row(2).dot(&other.col(3)), self.row(3).dot(&other.col(3))) - + } - + pure fn dot(&self, other: &Mat4) -> T { other.transpose().mul_m(self).trace() } @@ -204,7 +204,7 @@ pub impl> Matrix> for Mat4 { let m3: Mat3 = Matrix3::new(self[0][1], self[1][1], self[2][1], self[0][2], self[1][2], self[2][2], self[0][3], self[1][3], self[2][3]); - + self[0][0] * m0.determinant() - self[1][0] * m1.determinant() + self[2][0] * m2.determinant() - @@ -220,14 +220,14 @@ pub impl> Matrix> for Mat4 { if d.fuzzy_eq(&zero()) { None } else { - + // Gauss Jordan Elimination with partial pivoting // So take this matrix, A, augmented with the identity // and essentially reduce [A|I] - + let mut A = *self; let mut I: Mat4 = Matrix::identity(); - + for uint::range(0, 4) |j| { // Find largest element in col j let mut i1 = j; @@ -236,17 +236,17 @@ pub impl> Matrix> for Mat4 { i1 = i; } } - + unsafe { // Swap columns i1 and j in A and I to // put pivot on diagonal A.swap_cols(i1, j); I.swap_cols(i1, j); - + // Scale col j to have a unit diagonal I.col_mut(j).div_self_t(&A[j][j]); A.col_mut(j).div_self_t(&A[j][j]); - + // Eliminate off-diagonal elems in col j of A, // doing identical ops to I for uint::range(0, 4) |i| { @@ -260,7 +260,7 @@ pub impl> Matrix> for Mat4 { Some(I) } } - + #[inline(always)] pure fn transpose(&self) -> Mat4 { Matrix4::new(self[0][0], self[1][0], self[2][0], self[3][0], @@ -268,50 +268,50 @@ pub impl> Matrix> for Mat4 { 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)] pure fn is_identity(&self) -> bool { self.fuzzy_eq(&Matrix::identity()) } - + #[inline(always)] pure fn is_diagonal(&self) -> bool { self[0][1].fuzzy_eq(&zero()) && self[0][2].fuzzy_eq(&zero()) && self[0][3].fuzzy_eq(&zero()) && - + self[1][0].fuzzy_eq(&zero()) && self[1][2].fuzzy_eq(&zero()) && self[1][3].fuzzy_eq(&zero()) && - + self[2][0].fuzzy_eq(&zero()) && self[2][1].fuzzy_eq(&zero()) && self[2][3].fuzzy_eq(&zero()) && - + self[3][0].fuzzy_eq(&zero()) && self[3][1].fuzzy_eq(&zero()) && self[3][2].fuzzy_eq(&zero()) } - + #[inline(always)] pure fn is_rotated(&self) -> bool { !self.fuzzy_eq(&Matrix::identity()) } - + #[inline(always)] pure fn is_symmetric(&self) -> bool { self[0][1].fuzzy_eq(&self[1][0]) && self[0][2].fuzzy_eq(&self[2][0]) && self[0][3].fuzzy_eq(&self[3][0]) && - + self[1][0].fuzzy_eq(&self[0][1]) && self[1][2].fuzzy_eq(&self[2][1]) && self[1][3].fuzzy_eq(&self[3][1]) && - + self[2][0].fuzzy_eq(&self[0][2]) && self[2][1].fuzzy_eq(&self[1][2]) && self[2][3].fuzzy_eq(&self[3][2]) && - + self[3][0].fuzzy_eq(&self[0][3]) && self[3][1].fuzzy_eq(&self[1][3]) && self[3][2].fuzzy_eq(&self[2][3]) @@ -321,7 +321,7 @@ pub impl> Matrix> for Mat4 { pure fn is_invertible(&self) -> bool { !self.determinant().fuzzy_eq(&zero()) } - + #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -332,7 +332,7 @@ pub impl> Matrix> for Mat4 { } } -pub impl> Matrix4> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> Matrix4> for Mat4 { /** * Construct a 4 x 4 matrix * @@ -366,7 +366,7 @@ pub impl> Matrix4> for Mat4 { Vector4::new::>(c2r0, c2r1, c2r2, c2r3), Vector4::new::>(c3r0, c3r1, c3r2, c3r3)) } - + /** * Construct a 4 x 4 matrix from column vectors * @@ -399,7 +399,7 @@ pub impl> Matrix4> for Mat4 { } } -pub impl> MutableMatrix> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> MutableMatrix> for Mat4 { #[inline(always)] fn col_mut(&mut self, i: uint) -> &self/mut Vec4 { match i { @@ -410,13 +410,13 @@ pub impl> MutableMatrix> for Mat4 { _ => 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) { swap(self.col_mut(a), self.col_mut(b)); } - + #[inline(always)] fn swap_rows(&mut self, a: uint, b: uint) { self.x.swap(a, b); @@ -424,22 +424,22 @@ pub impl> MutableMatrix> for Mat4 { 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); @@ -447,7 +447,7 @@ pub impl> MutableMatrix> for Mat4 { 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]); @@ -455,7 +455,7 @@ pub impl> MutableMatrix> for Mat4 { 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]); @@ -463,7 +463,7 @@ pub impl> MutableMatrix> for Mat4 { 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() { @@ -471,35 +471,35 @@ pub impl> MutableMatrix> for Mat4 { None => fail!(~"Couldn't invert the matrix!") } } - + #[inline(always)] fn transpose_self(&mut self) { swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0)); swap(self.col_mut(0).index_mut(3), self.col_mut(3).index_mut(0)); - + swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1)); swap(self.col_mut(1).index_mut(3), self.col_mut(3).index_mut(1)); - + swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2)); swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2)); swap(self.col_mut(2).index_mut(3), self.col_mut(3).index_mut(2)); - + swap(self.col_mut(3).index_mut(0), self.col_mut(0).index_mut(3)); swap(self.col_mut(3).index_mut(1), self.col_mut(1).index_mut(3)); swap(self.col_mut(3).index_mut(2), self.col_mut(2).index_mut(3)); } } -pub impl> Neg> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> Neg> for Mat4 { #[inline(always)] pure fn neg(&self) -> Mat4 { Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3]) } } -pub impl Index> for Mat4 { +impl Index> for Mat4 { #[inline(always)] pure fn index(&self, i: uint) -> Vec4 { unsafe { do buf_as_slice( @@ -509,12 +509,12 @@ pub impl Index> for Mat4 { } } -pub impl> FuzzyEq for Mat4 { +impl> FuzzyEq for Mat4 { #[inline(always)] pure fn fuzzy_eq(&self, other: &Mat4) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -532,32 +532,32 @@ pub type dmat4 = Mat4; // a 4×4 double-precision floating-point matrix // Static method wrappers for GLSL-style types -pub impl mat4 { +impl mat4 { #[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c0r2: f32, c0r3: f32, c1r0: f32, c1r1: f32, c1r2: f32, c1r3: f32, c2r0: f32, c2r1: f32, c2r2: f32, c2r3: f32, c3r0: f32, c3r1: f32, c3r2: f32, c3r3: f32) -> mat4 { Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) } #[inline(always)] static pure fn from_cols(c0: vec4, c1: vec4, c2: vec4, c3: vec4) -> mat4 { Matrix4::from_cols(c0, c1, c2, c3) } #[inline(always)] static pure fn from_value(v: f32) -> mat4 { Matrix::from_value(v) } - + #[inline(always)] static pure fn identity() -> mat4 { Matrix::identity() } #[inline(always)] static pure fn zero() -> mat4 { Matrix::zero() } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn rows() -> uint { 4 } #[inline(always)] static pure fn cols() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dmat4 { +impl dmat4 { #[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c0r2: f64, c0r3: f64, c1r0: f64, c1r1: f64, c1r2: f64, c1r3: f64, c2r0: f64, c2r1: f64, c2r2: f64, c2r3: f64, c3r0: f64, c3r1: f64, c3r2: f64, c3r3: f64) -> dmat4 { Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) } #[inline(always)] static pure fn from_cols(c0: dvec4, c1: dvec4, c2: dvec4, c3: dvec4) -> dmat4 { Matrix4::from_cols(c0, c1, c2, c3) } #[inline(always)] static pure fn from_value(v: f64) -> dmat4 { Matrix::from_value(v) } - + #[inline(always)] static pure fn identity() -> dmat4 { Matrix::identity() } #[inline(always)] static pure fn zero() -> dmat4 { Matrix::zero() } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn rows() -> uint { 4 } #[inline(always)] static pure fn cols() -> uint { 4 } diff --git a/src/projection.rs b/src/projection.rs index c671f3d..fb420bf 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -12,10 +12,10 @@ use mat::{Mat4, Matrix4}; * can be found [here](http://www.opengl.org/wiki/GluPerspective_code). */ #[inline(always)] -pub pure fn perspective>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { +pub pure fn perspective + Add + Sub + Mul + Div + Neg>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { let ymax = near * tan(radians(fovy)); let xmax = ymax * aspectRatio; - + frustum(-xmax, xmax, -ymax, ymax, near, far) } @@ -26,31 +26,31 @@ pub pure fn perspective>(fovy: T, aspectRatio: T, near: * (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. */ #[inline(always)] -pub pure fn frustum>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { +pub pure fn frustum + Add + Sub + Mul + Div + Neg>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { let _0: T = Number::from(0); let _1: T = Number::from(1); let _2: T = Number::from(2); - + let c0r0 = (_2 * near) / (right - left); let c0r1 = _0; let c0r2 = _0; let c0r3 = _0; - + let c1r0 = _0; let c1r1 = (_2 * near) / (top - bottom); let c1r2 = _0; let c1r3 = _0; - + let c2r0 = (right + left) / (right - left); let c2r1 = (top + bottom) / (top - bottom); let c2r2 = -(far + near) / (far - near); let c2r3 = -_1; - + let c3r0 = _0; let c3r1 = _0; let c3r2 = -(_2 * far * near) / (far - near); let c3r3 = _0; - + Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, diff --git a/src/quat.rs b/src/quat.rs index 171504e..6f395b1 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -48,7 +48,7 @@ use vec::{ #[deriving_eq] pub struct Quat { s: T, v: Vec3 } -pub impl> Quat { +impl + Add + Sub + Mul + Div + Neg> Quat { /** * Construct the quaternion from one scalar component and three * imaginary components @@ -64,7 +64,7 @@ pub impl> Quat { static pure fn new(w: T, xi: T, yj: T, zk: T) -> Quat { Quat::from_sv(w, Vector3::new(xi, yj, zk)) } - + /** * Construct the quaternion from a scalar and a vector * @@ -77,7 +77,7 @@ pub impl> Quat { static pure fn from_sv(s: T, v: Vec3) -> Quat { Quat { s: s, v: v } } - + /** * # Return value * @@ -87,7 +87,7 @@ pub impl> Quat { static pure fn identity() -> Quat { Quat::new(one(), zero(), zero(), zero()) } - + /** * # Return value * @@ -97,25 +97,25 @@ pub impl> Quat { static pure fn zero() -> Quat { Quat::new(zero(), zero(), zero(), zero()) } - + #[inline(always)] static pure fn from_angle_x(radians: T) -> Quat { let _2 = Number::from(2); Quat::new(cos(radians / _2), sin(radians), zero(), zero()) } - + #[inline(always)] static pure fn from_angle_y(radians: T) -> Quat { let _2 = Number::from(2); Quat::new(cos(radians / _2), zero(), sin(radians), zero()) } - + #[inline(always)] static pure fn from_angle_z(radians: T) -> Quat { let _2 = Number::from(2); Quat::new(cos(radians / _2), zero(), zero(), sin(radians)) } - + #[inline(always)] static pure fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Quat { // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion @@ -128,27 +128,27 @@ pub impl> Quat { cos(zdiv2) * sin(xdiv2) * cos(ydiv2) + sin(zdiv2) * cos(xdiv2) * sin(ydiv2), cos(zdiv2) * cos(xdiv2) * sin(ydiv2) - sin(zdiv2) * sin(xdiv2) * cos(ydiv2)) } - + #[inline(always)] static pure fn from_angle_axis(radians: T, axis: &Vec3) -> Quat { let half = radians / Number::from(2); Quat::from_sv(cos(half), axis.mul_t(sin(half))) } - + #[inline(always)] static pure fn from_axes(x: Vec3, y: Vec3, z: Vec3) -> Quat { let m: Mat3 = Matrix3::from_axes(x, y, z); m.to_quat() } - + pure fn get_angle_axis(&self) -> (T, Vec3) { fail!(~"Not yet implemented.") } - + #[inline(always)] static pure fn look_at(dir: &Vec3, up: &Vec3) -> Quat { let m: Mat3 = Matrix3::look_at(dir, up); m.to_quat() } - + /** * # Return value * @@ -161,7 +161,7 @@ pub impl> Quat { self[2] * value, self[3] * value) } - + /** * # Return value * @@ -174,7 +174,7 @@ pub impl> Quat { self[2] / value, self[3] / value) } - + /** * # Return value * @@ -185,11 +185,11 @@ pub impl> Quat { let tmp = self.v.cross(vec).add_v(&vec.mul_t(self.s)); self.v.cross(&tmp).mul_t(Number::from(2)).add_v(vec) } - + /** * # Return value * - * The sum of this quaternion and `other` + * The sum of this quaternion and `other` */ #[inline(always)] pure fn add_q(&self, other: &Quat) -> Quat { @@ -198,11 +198,11 @@ pub impl> Quat { self[2] + other[2], self[3] + other[3]) } - + /** * # Return value * - * The sum of this quaternion and `other` + * The sum of this quaternion and `other` */ #[inline(always)] pure fn sub_q(&self, other: &Quat) -> Quat { @@ -211,7 +211,7 @@ pub impl> Quat { self[2] - other[2], self[3] - other[3]) } - + /** * # Return value * @@ -220,11 +220,11 @@ pub impl> Quat { #[inline(always)] pure fn mul_q(&self, other: &Quat) -> Quat { Quat::new(self.s * other.s - self.v.x * other.v.x - self.v.y * other.v.y - self.v.z * other.v.z, - self.s * other.v.x + self.v.x * other.s + self.v.y * other.v.z - self.v.z * other.v.y, - self.s * other.v.y + self.v.y * other.s + self.v.z * other.v.x - self.v.x * other.v.z, - self.s * other.v.z + self.v.z * other.s + self.v.x * other.v.y - self.v.y * other.v.x) + self.s * other.v.x + self.v.x * other.s + self.v.y * other.v.z - self.v.z * other.v.y, + self.s * other.v.y + self.v.y * other.s + self.v.z * other.v.x - self.v.x * other.v.z, + self.s * other.v.z + self.v.z * other.s + self.v.x * other.v.y - self.v.y * other.v.x) } - + /** * # Return value * @@ -234,7 +234,7 @@ pub impl> Quat { pure fn dot(&self, other: &Quat) -> T { self.s * other.s + self.v.dot(&other.v) } - + /** * # Return value * @@ -244,7 +244,7 @@ pub impl> Quat { pure fn conjugate(&self) -> Quat { Quat::from_sv(self.s, -self.v) } - + /** * # Return value * @@ -254,7 +254,7 @@ pub impl> Quat { pure fn inverse(&self) -> Quat { self.conjugate().div_t(self.magnitude2()) } - + /** * # Return value * @@ -266,7 +266,7 @@ pub impl> Quat { pure fn magnitude2(&self) -> T { self.s * self.s + self.v.length2() } - + /** * # Return value * @@ -282,7 +282,7 @@ pub impl> Quat { pure fn magnitude(&self) -> T { self.magnitude2().sqrt() } - + /** * # Return value * @@ -292,7 +292,7 @@ pub impl> Quat { pure fn normalize(&self) -> Quat { self.mul_t(one::()/self.magnitude()) } - + /** * Normalised linear interpolation * @@ -304,7 +304,7 @@ pub impl> Quat { pure fn nlerp(&self, other: &Quat, amount: T) -> Quat { self.mul_t(one::() - amount).add_q(&other.mul_t(amount)).normalize() } - + /** * Spherical Linear Intoperlation * @@ -329,24 +329,24 @@ pub impl> Quat { #[inline(always)] pure fn slerp(&self, other: &Quat, amount: T) -> Quat { let dot = self.dot(other); - + let dot_threshold = Number::from(0.9995); - + if dot > dot_threshold { return self.nlerp(other, amount); // if quaternions are close together use `nlerp` } else { let robust_dot = dot.clamp(-one::(), one()); // stay within the domain of acos() - + let theta_0 = acos(robust_dot); // the angle between the quaternions let theta = theta_0 * amount; // the fraction of theta specified by `amount` - + let q = other.sub_q(&self.mul_t(robust_dot)) .normalize(); - + return self.mul_t(cos(theta)).add_q(&q.mul_t(sin(theta))); } } - + /** * # Return value * @@ -360,7 +360,7 @@ pub impl> Quat { ) } } - + /** * Convert the quaternion to a 3 x 3 rotation matrix */ @@ -369,28 +369,28 @@ pub impl> Quat { let x2 = self.v.x + self.v.x; let y2 = self.v.y + self.v.y; let z2 = self.v.z + self.v.z; - + let xx2 = x2 * self.v.x; let xy2 = x2 * self.v.y; let xz2 = x2 * self.v.z; - + let yy2 = y2 * self.v.y; let yz2 = y2 * self.v.z; let zz2 = z2 * self.v.z; - + let sy2 = y2 * self.s; let sz2 = z2 * self.s; let sx2 = x2 * self.s; - + let _1: T = one(); - + Matrix3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2, xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2, xz2 + sy2, yz2 - sx2, _1 - xx2 - yy2) } } -pub impl Index for Quat { +impl Index for Quat { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice( @@ -400,19 +400,19 @@ pub impl Index for Quat { } } -pub impl> Neg> for Quat { +impl + Add + Sub + Mul + Div + Neg> Neg> for Quat { #[inline(always)] pure fn neg(&self) -> Quat { Quat::new(-self[0], -self[1], -self[2], -self[3]) } } -pub impl> FuzzyEq for Quat { +impl> FuzzyEq for Quat { #[inline(always)] pure fn fuzzy_eq(&self, other: &Quat) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Quat, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -430,12 +430,12 @@ pub type dquat = Quat; /// a double-precision floating-point qu // Static method wrappers for GLSL-style types -pub impl quat { +impl quat { #[inline(always)] static pure fn new(w: f32, xi: f32, yj: f32, zk: f32) -> quat { Quat::new(w, xi, yj, zk) } #[inline(always)] static pure fn from_sv(s: f32, v: vec3) -> quat { Quat::from_sv(s, v) } #[inline(always)] static pure fn identity() -> quat { Quat::identity() } #[inline(always)] static pure fn zero() -> quat { Quat::zero() } - + #[inline(always)] static pure fn from_angle_x(radians: f32) -> quat { Quat::from_angle_x(radians) } #[inline(always)] static pure fn from_angle_y(radians: f32) -> quat { Quat::from_angle_y(radians) } #[inline(always)] static pure fn from_angle_z(radians: f32) -> quat { Quat::from_angle_z(radians) } @@ -446,12 +446,12 @@ pub impl quat { #[inline(always)] static pure fn look_at(dir: &vec3, up: &vec3) -> quat { Quat::look_at(dir, up) } } -pub impl dquat { +impl dquat { #[inline(always)] static pure fn new(w: f64, xi: f64, yj: f64, zk: f64) -> dquat { Quat::new(w, xi, yj, zk) } #[inline(always)] static pure fn from_sv(s: f64, v: dvec3) -> dquat { Quat::from_sv(s, v) } #[inline(always)] static pure fn identity() -> dquat { Quat::identity() } #[inline(always)] static pure fn zero() -> dquat { Quat::zero() } - + #[inline(always)] static pure fn from_angle_x(radians: f64) -> dquat { Quat::from_angle_x(radians) } #[inline(always)] static pure fn from_angle_y(radians: f64) -> dquat { Quat::from_angle_y(radians) } #[inline(always)] static pure fn from_angle_z(radians: f64) -> dquat { Quat::from_angle_z(radians) } diff --git a/src/vec.rs b/src/vec.rs index 93e072b..763fe5f 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -17,12 +17,12 @@ 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 */ static pure fn from_value(value: T) -> Self; - + /** * # Return value * @@ -36,7 +36,7 @@ pub trait MutableVector: Vector { * Get a mutable reference to the component at `i` */ fn index_mut(&mut self, i: uint) -> &self/mut T; - + /** * Swap two components of the vector in place */ @@ -67,7 +67,7 @@ pub trait Vector4: Vector { /** * A vector with numeric components */ -pub trait NumericVector: Vector Neg { +pub trait NumericVector: Vector + Neg { /** * The standard basis vector * @@ -76,7 +76,7 @@ pub trait NumericVector: Vector Neg { * A vector with each component set to one */ static pure fn identity() -> Self; - + /** * The null vector * @@ -85,48 +85,48 @@ pub trait NumericVector: Vector Neg { * A vector with each component set to zero */ static pure fn zero() -> Self; - + /** * # Return value * * True if the vector is equal to zero */ pure fn is_zero(&self) -> bool; - + /** * # Return value * * The scalar multiplication of the vector and `value` */ pure fn mul_t(&self, value: T) -> Self; - + /** * # Return value * * The scalar division of the vector and `value` */ pure fn div_t(&self, value: T) -> Self; - + /** * Component-wise vector addition */ pure fn add_v(&self, other: &Self) -> Self; - + /** * Component-wise vector subtraction */ pure fn sub_v(&self, other: &Self) -> Self; - + /** * Component-wise vector multiplication */ pure fn mul_v(&self, other: &Self) -> Self; - + /** * Component-wise vector division */ pure fn div_v(&self, other: &Self) -> Self; - + /** * # Return value * @@ -141,7 +141,7 @@ pub trait NumericVector: Vector Neg { pub trait NumericVector2: NumericVector { static pure fn unit_x() -> Self; static pure fn unit_y() -> Self; - + /** * # Return value * @@ -157,7 +157,7 @@ pub trait NumericVector3: NumericVector { static pure fn unit_x() -> Self; static pure fn unit_y() -> Self; static pure fn unit_z() -> Self; - + /** * # Return value * @@ -179,38 +179,38 @@ pub trait NumericVector4: NumericVector { /** * A mutable vector with numeric components */ -pub trait MutableNumericVector: MutableVector<&self/T> +pub trait MutableNumericVector: MutableVector<&self/T> + NumericVector { /** * Negate the vector */ fn neg_self(&mut self); - + /** * Multiply the vector by a scalar */ fn mul_self_t(&mut self, value: T); - + /** * Divide the vector by a scalar */ fn div_self_t(&mut self, value: T); - + /** * Set the vector to the component-wise vector sum */ fn add_self_v(&mut self, other: &Self); - + /** * Set the vector to the component-wise vector difference */ fn sub_self_v(&mut self, other: &Self); - + /** * Set the vector to the component-wise vector product */ fn mul_self_v(&mut self, other: &Self); - + /** * Set the vector to the component-wise vector quotient */ @@ -249,7 +249,7 @@ pub trait EuclideanVector: NumericVector { * the exact length does not need to be calculated. */ pure fn length2(&self) -> T; - + /** * # Return value * @@ -262,40 +262,40 @@ pub trait EuclideanVector: NumericVector { * it is advisable to use the `length2` method instead. */ pure fn length(&self) -> T; - + /** * # Return value * * The squared distance between the vector and `other`. */ pure fn distance2(&self, other: &Self) -> T; - + /** * # Return value * * The distance between the vector and `other` */ pure fn distance(&self, other: &Self) -> T; - + /** * # Return value * * The angle between the vector and `other` in radians */ pure fn angle(&self, other: &Self) -> T; - + /** * # Return value * * The normalized vector */ pure fn normalize(&self) -> Self; - + /** * Set the length of the vector whilst preserving the direction */ pure fn normalize_to(&self, length: T) -> Self; - + /** * Linearly intoperlate between the vector and `other` * @@ -313,18 +313,18 @@ pub trait EuclideanVector: NumericVector { * * * `T` - The type of the components. This should be a floating point type. */ -pub trait MutableEuclideanVector: MutableNumericVector<&self/T> +pub trait MutableEuclideanVector: MutableNumericVector<&self/T> + EuclideanVector { /** * Normalize the vector */ fn normalize_self(&mut self); - + /** * Set the vector to a specified length whilst preserving the direction */ fn normalize_self_to(&mut self, length: T); - + /** * Linearly intoperlate the vector towards `other` */ @@ -343,17 +343,17 @@ pub trait OrdinalVector: Vector { * Component-wise compare of `self < other` */ pure fn less_than(&self, other: &Self) -> BoolVec; - + /** * Component-wise compare of `self <= other` */ pure fn less_than_equal(&self, other: &Self) -> BoolVec; - + /** * Component-wise compare of `self > other` */ pure fn greater_than(&self, other: &Self) -> BoolVec; - + /** * Component-wise compare of `self >= other` */ @@ -372,7 +372,7 @@ pub trait EquableVector: Vector { * Component-wise compare of `self == other` */ pure fn equal(&self, other: &Self) -> BoolVec; - + /** * Component-wise compare of `self != other` */ @@ -393,14 +393,14 @@ pub trait BooleanVector: Vector { * `true` if of any component is `true` */ pure fn any(&self) -> bool; - + /** * # Return value * * `true` only if all components are `true` */ pure fn all(&self) -> bool; - + /** * # Return value * @@ -412,18 +412,18 @@ pub trait BooleanVector: Vector { pub trait TrigVec: Vector { pure fn radians(&self) -> Self; pure fn degrees(&self) -> Self; - + // Triganometric functions pure fn sin(&self) -> Self; pure fn cos(&self) -> Self; pure fn tan(&self) -> Self; - + // Inverse triganometric functions pure fn asin(&self) -> Self; pure fn acos(&self) -> Self; pure fn atan(&self) -> Self; pure fn atan2(&self, other: Self) -> Self; - + // Hyperbolic triganometric functions pure fn sinh(&self) -> Self; pure fn cosh(&self) -> Self; @@ -460,7 +460,7 @@ pub trait SignedVec: Vector { pure fn is_negative(&self) -> BV; pure fn is_nonpositive(&self) -> BV; pure fn is_nonnegative(&self) -> BV; - + pure fn abs(&self) -> Self; pure fn sign(&self) -> Self; pure fn copysign(&self, other: Self) -> Self; @@ -470,7 +470,7 @@ pub trait ExtentVec: Vector { pure fn min_v(&self, other: &Self) -> Self; pure fn max_v(&self, other: &Self) -> Self; pure fn clamp_v(&self, mn: &Self, mx: &Self) -> Self; - + pure fn min_t(&self, other: T) -> Self; pure fn max_t(&self, other: T) -> Self; pure fn clamp_t(&self, mn: T, mx: T) -> Self; diff --git a/src/vec2.rs b/src/vec2.rs index 33fd66d..77cb610 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -43,12 +43,12 @@ use vec::{ #[deriving_eq] pub struct Vec2 { x: T, y: T } -pub impl Vector for Vec2 { +impl Vector for Vec2 { #[inline(always)] static pure fn from_value(value: T) -> Vec2 { Vector2::new(value, value) } - + #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -59,21 +59,21 @@ pub impl Vector for Vec2 { } } -pub impl Vector2 for Vec2 { +impl Vector2 for Vec2 { #[inline(always)] static pure fn new(x: T, y: T ) -> Vec2 { Vec2 { x: x, y: y } } } -pub impl Index for Vec2 { +impl Index for Vec2 { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } } } -pub impl MutableVector for Vec2 { +impl MutableVector for Vec2 { #[inline(always)] fn index_mut(&mut self, i: uint) -> &self/mut T { match i { @@ -82,67 +82,67 @@ pub impl MutableVector for Vec2 { _ => 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) { swap(self.index_mut(a), self.index_mut(b)); } } - -pub impl NumericVector for Vec2 { + +impl + Sub + Mul + Div + Neg> NumericVector for Vec2 { #[inline(always)] static pure fn identity() -> Vec2 { Vector2::new(one::(), one::()) } - + #[inline(always)] static pure fn zero() -> Vec2 { Vector2::new(zero::(), zero::()) } - + #[inline(always)] pure fn is_zero(&self) -> bool { self[0] == zero() && self[1] == zero() } - + #[inline(always)] pure fn mul_t(&self, value: T) -> Vec2 { Vector2::new(self[0] * value, self[1] * value) } - + #[inline(always)] pure fn div_t(&self, value: T) -> Vec2 { Vector2::new(self[0] / value, self[1] / value) } - + #[inline(always)] pure fn add_v(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] + other[0], self[1] + other[1]) } - + #[inline(always)] pure fn sub_v(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] - other[0], self[1] - other[1]) } - + #[inline(always)] pure fn mul_v(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] * other[0], self[1] * other[1]) } - + #[inline(always)] pure fn div_v(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] / other[0], self[1] / other[1]) } - + #[inline(always)] pure fn dot(&self, other: &Vec2) -> T { self[0] * other[0] + @@ -150,67 +150,67 @@ pub impl NumericVector for Vec2 { } } -pub impl Neg> for Vec2 { +impl + Sub + Mul + Div + Neg> Neg> for Vec2 { #[inline(always)] pure fn neg(&self) -> Vec2 { Vector2::new(-self[0], -self[1]) } } -pub impl NumericVector2 for Vec2 { +impl + Sub + Mul + Div + Neg> NumericVector2 for Vec2 { #[inline(always)] static pure fn unit_x() -> Vec2 { Vector2::new(one::(), zero::()) } - + #[inline(always)] static pure fn unit_y() -> Vec2 { Vector2::new(zero::(), one::()) } - + #[inline(always)] pure fn perp_dot(&self, other: &Vec2) ->T { (self[0] * other[1]) - (self[1] * other[0]) } } -pub impl MutableNumericVector<&self/T> for Vec2 { +impl + Sub + Mul + Div + Neg> MutableNumericVector<&self/T> for Vec2 { #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -*self.index_mut(0); *self.index_mut(1) = -*self.index_mut(1); } - + #[inline(always)] fn mul_self_t(&mut self, value: &T) { *self.index_mut(0) *= (*value); *self.index_mut(1) *= (*value); } - + #[inline(always)] fn div_self_t(&mut self, value: &T) { *self.index_mut(0) /= (*value); *self.index_mut(1) /= (*value); } - + #[inline(always)] fn add_self_v(&mut self, other: &Vec2) { *self.index_mut(0) += other[0]; *self.index_mut(1) += other[1]; } - + #[inline(always)] fn sub_self_v(&mut self, other: &Vec2) { *self.index_mut(0) -= other[0]; *self.index_mut(1) -= other[1]; } - + #[inline(always)] fn mul_self_v(&mut self, other: &Vec2) { *self.index_mut(0) *= other[0]; *self.index_mut(1) *= other[1]; } - + #[inline(always)] fn div_self_v(&mut self, other: &Vec2) { *self.index_mut(0) /= other[0]; @@ -218,79 +218,79 @@ pub impl MutableNumericVector<&self/T> for Vec2 { } } -pub impl ToHomogeneous> for Vec2 { +impl ToHomogeneous> for Vec2 { #[inline(always)] pure fn to_homogeneous(&self) -> Vec3 { Vector3::new(self.x, self.y, zero()) } } -pub impl EuclideanVector for Vec2 { +impl + Sub + Mul + Div + Neg> EuclideanVector for Vec2 { #[inline(always)] pure fn length2(&self) -> T { self.dot(self) } - + #[inline(always)] pure fn length(&self) -> T { self.length2().sqrt() } - + #[inline(always)] pure fn distance2(&self, other: &Vec2) -> T { other.sub_v(self).length2() } - + #[inline(always)] pure fn distance(&self, other: &Vec2) -> T { other.distance2(self).sqrt() } - + #[inline(always)] pure fn angle(&self, other: &Vec2) -> T { atan2(self.perp_dot(other), self.dot(other)) } - + #[inline(always)] pure fn normalize(&self) -> Vec2 { self.mul_t(one::()/self.length()) } - + #[inline(always)] pure fn normalize_to(&self, length: T) -> Vec2 { self.mul_t(length / self.length()) } - + #[inline(always)] pure fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { self.add_v(&other.sub_v(self).mul_t(amount)) } } -pub impl MutableEuclideanVector<&self/T> for Vec2 { +impl + Sub + Mul + Div + Neg> MutableEuclideanVector<&self/T> for Vec2 { #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); self.mul_self_t(&n); } - + #[inline(always)] fn normalize_self_to(&mut self, length: &T) { let n = length / self.length(); self.mul_self_t(&n); } - + fn lerp_self(&mut self, other: &Vec2, amount: &T) { self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); } } -pub impl> FuzzyEq for Vec2 { +impl> FuzzyEq for Vec2 { #[inline(always)] pure fn fuzzy_eq(&self, other: &Vec2) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -298,25 +298,25 @@ pub impl> FuzzyEq for Vec2 { } } -pub impl OrdinalVector> for Vec2 { +impl OrdinalVector> for Vec2 { #[inline(always)] pure fn less_than(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] < other[0], self[1] < other[1]) } - + #[inline(always)] pure fn less_than_equal(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] <= other[0], self[1] <= other[1]) } - + #[inline(always)] pure fn greater_than(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] > other[0], self[1] > other[1]) } - + #[inline(always)] pure fn greater_than_equal(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] >= other[0], @@ -324,13 +324,13 @@ pub impl OrdinalVector> for Vec2 { } } -pub impl EquableVector> for Vec2 { +impl EquableVector> for Vec2 { #[inline(always)] pure fn equal(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] == other[0], self[1] == other[1]) } - + #[inline(always)] pure fn not_equal(&self, other: &Vec2) -> Vec2 { Vector2::new(self[0] != other[0], @@ -338,19 +338,19 @@ pub impl EquableVector> for Vec2 { } } -pub impl BooleanVector for Vec2 { +impl BooleanVector for Vec2 { #[inline(always)] pure fn any(&self) -> bool { self[0] || self[1] } - + #[inline(always)] pure fn all(&self) -> bool { self[0] && self[1] } - + #[inline(always)] - pure fn not(&self) -> Vec2 { + pure fn not(&self) -> Vec2 { Vector2::new(!self[0], !self[1]) } } @@ -366,62 +366,62 @@ pub type uvec2 = Vec2; // a two-component unsigned integer vector // Static method wrappers for GLSL-style types -pub impl vec2 { +impl vec2 { #[inline(always)] static pure fn new(x: f32, y: f32) -> vec2 { Vector2::new(x, y) } #[inline(always)] static pure fn from_value(v: f32) -> vec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> vec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> vec2 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> vec2 { NumericVector2::unit_x() } #[inline(always)] static pure fn unit_y() -> vec2 { NumericVector2::unit_y() } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dvec2 { +impl dvec2 { #[inline(always)] static pure fn new(x: f64, y: f64) -> dvec2 { Vector2::new(x, y) } #[inline(always)] static pure fn from_value(v: f64) -> dvec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> dvec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> dvec2 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> dvec2 { NumericVector2::unit_x() } #[inline(always)] static pure fn unit_y() -> dvec2 { NumericVector2::unit_y() } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl bvec2 { +impl bvec2 { #[inline(always)] static pure fn new(x: bool, y: bool) -> bvec2 { Vector2::new(x, y) } #[inline(always)] static pure fn from_value(v: bool) -> bvec2 { Vector::from_value(v) } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl ivec2 { +impl ivec2 { #[inline(always)] static pure fn new(x: i32, y: i32) -> ivec2 { Vector2::new(x, y) } #[inline(always)] static pure fn from_value(v: i32) -> ivec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> ivec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec2 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> ivec2 { NumericVector2::unit_x() } #[inline(always)] static pure fn unit_y() -> ivec2 { NumericVector2::unit_y() } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl uvec2 { +impl uvec2 { #[inline(always)] static pure fn new(x: u32, y: u32) -> uvec2 { Vector2::new(x, y) } #[inline(always)] static pure fn from_value(v: u32) -> uvec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> uvec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec2 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> uvec2 { NumericVector2::unit_x() } #[inline(always)] static pure fn unit_y() -> uvec2 { NumericVector2::unit_y() } - + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } diff --git a/src/vec3.rs b/src/vec3.rs index c4fa7ed..b8a0afe 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -45,12 +45,12 @@ use vec::{ #[deriving_eq] pub struct Vec3 { x: T, y: T, z: T } -pub impl Vector for Vec3 { +impl Vector for Vec3 { #[inline(always)] static pure fn from_value(value: T) -> Vec3 { Vector3::new(value, value, value) } - + #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -61,21 +61,21 @@ pub impl Vector for Vec3 { } } -pub impl Vector3 for Vec3 { +impl Vector3 for Vec3 { #[inline(always)] static pure fn new(x: T, y: T, z: T) -> Vec3 { Vec3 { x: x, y: y, z: z } } } -pub impl Index for Vec3 { +impl Index for Vec3 { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } -pub impl MutableVector for Vec3 { +impl MutableVector for Vec3 { #[inline(always)] fn index_mut(&mut self, i: uint) -> &self/mut T { match i { @@ -85,7 +85,7 @@ pub impl MutableVector for Vec3 { _ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) } } - + #[inline(always)] fn swap(&mut self, a: uint, b: uint) { swap(self.index_mut(a), @@ -93,66 +93,66 @@ pub impl MutableVector for Vec3 { } } -pub impl NumericVector for Vec3 { +impl + Sub + Mul + Div + Neg> NumericVector for Vec3 { #[inline(always)] static pure fn identity() -> Vec3 { Vector3::new(one::(), one::(), one::()) } - + #[inline(always)] static pure fn zero() -> Vec3 { Vector3::new(zero::(), zero::(), zero::()) } - + #[inline(always)] pure fn is_zero(&self) -> bool { self[0] == zero() && self[1] == zero() && self[2] == zero() } - + #[inline(always)] pure fn mul_t(&self, value: T) -> Vec3 { Vector3::new(self[0] * value, self[1] * value, self[2] * value) } - + #[inline(always)] pure fn div_t(&self, value: T) -> Vec3 { Vector3::new(self[0] / value, self[1] / value, self[2] / value) } - + #[inline(always)] pure fn add_v(&self, other: &Vec3) -> Vec3{ Vector3::new(self[0] + other[0], self[1] + other[1], self[2] + other[2]) } - + #[inline(always)] pure fn sub_v(&self, other: &Vec3) -> Vec3{ Vector3::new(self[0] - other[0], self[1] - other[1], self[2] - other[2]) } - + #[inline(always)] pure fn mul_v(&self, other: &Vec3) -> Vec3{ Vector3::new(self[0] * other[0], self[1] * other[1], self[2] * other[2]) } - + #[inline(always)] pure fn div_v(&self, other: &Vec3) -> Vec3{ Vector3::new(self[0] / other[0], self[1] / other[1], self[2] / other[2]) } - + #[inline(always)] pure fn dot(&self, other: &Vec3) -> T { self[0] * other[0] + @@ -161,29 +161,29 @@ pub impl NumericVector for Vec3 { } } -pub impl Neg> for Vec3 { +impl + Sub + Mul + Div + Neg> Neg> for Vec3 { #[inline(always)] pure fn neg(&self) -> Vec3 { Vector3::new(-self[0], -self[1], -self[2]) } } -pub impl NumericVector3 for Vec3 { +impl + Sub + Mul + Div + Neg> NumericVector3 for Vec3 { #[inline(always)] static pure fn unit_x() -> Vec3 { Vector3::new(one::(), zero::(), zero::()) } - + #[inline(always)] static pure fn unit_y() -> Vec3 { Vector3::new(zero::(), one::(), zero::()) } - + #[inline(always)] static pure fn unit_z() -> Vec3 { Vector3::new(zero::(), zero::(), one::()) } - + #[inline(always)] pure fn cross(&self, other: &Vec3) -> Vec3 { Vector3::new((self[1] * other[2]) - (self[2] * other[1]), @@ -192,49 +192,49 @@ pub impl NumericVector3 for Vec3 { } } -pub impl MutableNumericVector<&self/T> for Vec3 { +impl + Sub + Mul + Div + Neg> MutableNumericVector<&self/T> for Vec3 { #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -*self.index_mut(0); *self.index_mut(1) = -*self.index_mut(1); *self.index_mut(2) = -*self.index_mut(2); } - + #[inline(always)] fn mul_self_t(&mut self, value: &T) { *self.index_mut(0) *= (*value); *self.index_mut(1) *= (*value); *self.index_mut(2) *= (*value); } - + #[inline(always)] fn div_self_t(&mut self, value: &T) { *self.index_mut(0) /= (*value); *self.index_mut(1) /= (*value); *self.index_mut(2) /= (*value); } - + #[inline(always)] fn add_self_v(&mut self, other: &Vec3) { *self.index_mut(0) += other[0]; *self.index_mut(1) += other[1]; *self.index_mut(2) += other[2]; } - + #[inline(always)] fn sub_self_v(&mut self, other: &Vec3) { *self.index_mut(0) -= other[0]; *self.index_mut(1) -= other[1]; *self.index_mut(2) -= other[2]; } - + #[inline(always)] fn mul_self_v(&mut self, other: &Vec3) { *self.index_mut(0) *= other[0]; *self.index_mut(1) *= other[1]; *self.index_mut(2) *= other[2]; } - + #[inline(always)] fn div_self_v(&mut self, other: &Vec3) { *self.index_mut(0) /= other[0]; @@ -243,86 +243,86 @@ pub impl MutableNumericVector<&self/T> for Vec3 { } } -pub impl MutableNumericVector3<&self/T> for Vec3 { +impl + Sub + Mul + Div + Neg> MutableNumericVector3<&self/T> for Vec3 { #[inline(always)] fn cross_self(&mut self, other: &Vec3) { *self = self.cross(other); } } -pub impl ToHomogeneous> for Vec3 { +impl + Sub + Mul + Div + Neg> ToHomogeneous> for Vec3 { #[inline(always)] pure fn to_homogeneous(&self) -> Vec4 { Vector4::new(self.x, self.y, self.z, zero()) } } -pub impl EuclideanVector for Vec3 { +impl + Sub + Mul + Div + Neg> EuclideanVector for Vec3 { #[inline(always)] pure fn length2(&self) -> T { self.dot(self) } - + #[inline(always)] pure fn length(&self) -> T { self.length2().sqrt() } - + #[inline(always)] pure fn distance2(&self, other: &Vec3) -> T { other.sub_v(self).length2() } - + #[inline(always)] pure fn distance(&self, other: &Vec3) -> T { other.distance2(self).sqrt() } - + #[inline(always)] pure fn angle(&self, other: &Vec3) -> T { atan2(self.cross(other).length(), self.dot(other)) } - + #[inline(always)] pure fn normalize(&self) -> Vec3 { self.mul_t(one::()/self.length()) } - + #[inline(always)] pure fn normalize_to(&self, length: T) -> Vec3 { self.mul_t(length / self.length()) } - + #[inline(always)] pure fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { self.add_v(&other.sub_v(self).mul_t(amount)) } } -pub impl MutableEuclideanVector<&self/T> for Vec3 { +impl + Sub + Mul + Div + Neg> MutableEuclideanVector<&self/T> for Vec3 { #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); self.mul_self_t(&n); } - + #[inline(always)] fn normalize_self_to(&mut self, length: &T) { let n = length / self.length(); self.mul_self_t(&n); } - + fn lerp_self(&mut self, other: &Vec3, amount: &T) { self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); } } -pub impl> FuzzyEq for Vec3 { +impl> FuzzyEq for Vec3 { #[inline(always)] pure fn fuzzy_eq(&self, other: &Vec3) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -331,28 +331,28 @@ pub impl> FuzzyEq for Vec3 { } } -pub impl OrdinalVector> for Vec3 { +impl OrdinalVector> for Vec3 { #[inline(always)] pure fn less_than(&self, other: &Vec3) -> Vec3 { Vector3::new(self[0] < other[0], self[1] < other[1], self[2] < other[2]) } - + #[inline(always)] pure fn less_than_equal(&self, other: &Vec3) -> Vec3 { Vector3::new(self[0] <= other[0], self[1] <= other[1], self[2] <= other[2]) } - + #[inline(always)] pure fn greater_than(&self, other: &Vec3) -> Vec3 { Vector3::new(self[0] > other[0], self[1] > other[1], self[2] > other[2]) } - + #[inline(always)] pure fn greater_than_equal(&self, other: &Vec3) -> Vec3 { Vector3::new(self[0] >= other[0], @@ -361,14 +361,14 @@ pub impl OrdinalVector> for Vec3 { } } -pub impl EquableVector> for Vec3 { +impl EquableVector> for Vec3 { #[inline(always)] pure fn equal(&self, other: &Vec3) -> Vec3 { Vector3::new(self[0] == other[0], self[1] == other[1], self[2] == other[2]) } - + #[inline(always)] pure fn not_equal(&self, other: &Vec3) -> Vec3 { Vector3::new(self[0] != other[0], @@ -377,19 +377,19 @@ pub impl EquableVector> for Vec3 { } } -pub impl BooleanVector for Vec3 { +impl BooleanVector for Vec3 { #[inline(always)] pure fn any(&self) -> bool { self[0] || self[1] || self[2] } - + #[inline(always)] pure fn all(&self) -> bool { self[0] && self[1] && self[2] } - + #[inline(always)] - pure fn not(&self) -> Vec3 { + pure fn not(&self) -> Vec3 { Vector3::new(!self[0], !self[1], !self[2]) } } @@ -405,66 +405,66 @@ pub type uvec3 = Vec3; // a three-component unsigned integer vector // Static method wrappers for GLSL-style types -pub impl vec3 { +impl vec3 { #[inline(always)] static pure fn new(x: f32, y: f32, z: f32) -> vec3 { Vector3::new(x, y, z) } #[inline(always)] static pure fn from_value(v: f32) -> vec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> vec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> vec3 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> vec3 { NumericVector3::unit_x() } #[inline(always)] static pure fn unit_y() -> vec3 { NumericVector3::unit_y() } #[inline(always)] static pure fn unit_z() -> vec3 { NumericVector3::unit_z() } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dvec3 { +impl dvec3 { #[inline(always)] static pure fn new(x: f64, y: f64, z: f64) -> dvec3 { Vector3::new(x, y, z) } #[inline(always)] static pure fn from_value(v: f64) -> dvec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> dvec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> dvec3 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> dvec3 { NumericVector3::unit_x() } #[inline(always)] static pure fn unit_y() -> dvec3 { NumericVector3::unit_y() } #[inline(always)] static pure fn unit_z() -> dvec3 { NumericVector3::unit_z() } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl bvec3 { +impl bvec3 { #[inline(always)] static pure fn new(x: bool, y: bool, z: bool) -> bvec3 { Vector3::new(x, y, z) } #[inline(always)] static pure fn from_value(v: bool) -> bvec3 { Vector::from_value(v) } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl ivec3 { +impl ivec3 { #[inline(always)] static pure fn new(x: i32, y: i32, z: i32) -> ivec3 { Vector3::new(x, y, z) } #[inline(always)] static pure fn from_value(v: i32) -> ivec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> ivec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec3 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> ivec3 { NumericVector3::unit_x() } #[inline(always)] static pure fn unit_y() -> ivec3 { NumericVector3::unit_y() } #[inline(always)] static pure fn unit_z() -> ivec3 { NumericVector3::unit_z() } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl uvec3 { +impl uvec3 { #[inline(always)] static pure fn new(x: u32, y: u32, z: u32) -> uvec3 { Vector3::new(x, y, z) } #[inline(always)] static pure fn from_value(v: u32) -> uvec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> uvec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec3 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> uvec3 { NumericVector3::unit_x() } #[inline(always)] static pure fn unit_y() -> uvec3 { NumericVector3::unit_y() } #[inline(always)] static pure fn unit_z() -> uvec3 { NumericVector3::unit_z() } - + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } diff --git a/src/vec4.rs b/src/vec4.rs index 55c1a58..0459996 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -43,12 +43,12 @@ use vec::{ #[deriving_eq] pub struct Vec4 { x: T, y: T, z: T, w: T } -pub impl Vector for Vec4 { +impl Vector for Vec4 { #[inline(always)] static pure fn from_value(value: T) -> Vec4 { Vector4::new(value, value, value, value) } - + #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -59,21 +59,21 @@ pub impl Vector for Vec4 { } } -pub impl Vector4 for Vec4 { +impl Vector4 for Vec4 { #[inline(always)] static pure fn new(x: T, y: T, z: T, w: T) -> Vec4 { Vec4 { x: x, y: y, z: z, w: w } } } -pub impl Index for Vec4 { +impl Index for Vec4 { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } -pub impl MutableVector for Vec4 { +impl MutableVector for Vec4 { #[inline(always)] fn index_mut(&mut self, i: uint) -> &self/mut T { match i { @@ -84,7 +84,7 @@ pub impl MutableVector for Vec4 { _ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i)) } } - + #[inline(always)] fn swap(&mut self, a: uint, b: uint) { swap(self.index_mut(a), @@ -92,17 +92,17 @@ pub impl MutableVector for Vec4 { } } -pub impl NumericVector for Vec4 { +impl + Sub + Mul + Div + Neg> NumericVector for Vec4 { #[inline(always)] static pure fn identity() -> Vec4 { Vector4::new(one::(), one::(), one::(), one::()) } - + #[inline(always)] static pure fn zero() -> Vec4 { Vector4::new(zero::(), zero::(), zero::(), zero::()) } - + #[inline(always)] pure fn is_zero(&self) -> bool { self[0] == zero() && @@ -110,7 +110,7 @@ pub impl NumericVector for Vec4 { self[2] == zero() && self[3] == zero() } - + #[inline(always)] pure fn mul_t(&self, value: T) -> Vec4 { Vector4::new(self[0] * value, @@ -118,7 +118,7 @@ pub impl NumericVector for Vec4 { self[2] * value, self[3] * value) } - + #[inline(always)] pure fn div_t(&self, value: T) -> Vec4 { Vector4::new(self[0] / value, @@ -126,7 +126,7 @@ pub impl NumericVector for Vec4 { self[2] / value, self[3] / value) } - + #[inline(always)] pure fn add_v(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] + other[0], @@ -134,7 +134,7 @@ pub impl NumericVector for Vec4 { self[2] + other[2], self[3] + other[3]) } - + #[inline(always)] pure fn sub_v(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] - other[0], @@ -142,7 +142,7 @@ pub impl NumericVector for Vec4 { self[2] - other[2], self[3] - other[3]) } - + #[inline(always)] pure fn mul_v(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] * other[0], @@ -150,7 +150,7 @@ pub impl NumericVector for Vec4 { self[2] * other[2], self[3] * other[3]) } - + #[inline(always)] pure fn div_v(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] / other[0], @@ -158,7 +158,7 @@ pub impl NumericVector for Vec4 { self[2] / other[2], self[3] / other[3]) } - + #[inline(always)] pure fn dot(&self, other: &Vec4) -> T { self[0] * other[0] + @@ -168,36 +168,36 @@ pub impl NumericVector for Vec4 { } } -pub impl Neg> for Vec4 { +impl + Sub + Mul + Div + Neg> Neg> for Vec4 { #[inline(always)] pure fn neg(&self) -> Vec4 { Vector4::new(-self[0], -self[1], -self[2], -self[3]) } } -pub impl NumericVector4 for Vec4 { +impl NumericVector4 for Vec4 { #[inline(always)] static pure fn unit_x() -> Vec4 { Vector4::new(one::(), zero::(), zero::(), zero::()) } - + #[inline(always)] static pure fn unit_y() -> Vec4 { Vector4::new(zero::(), one::(), zero::(), zero::()) } - + #[inline(always)] static pure fn unit_z() -> Vec4 { Vector4::new(zero::(), zero::(), one::(), zero::()) } - + #[inline(always)] static pure fn unit_w() -> Vec4 { Vector4::new(zero::(), zero::(), zero::(), one::()) } } -pub impl MutableNumericVector<&self/T> for Vec4 { +impl + Sub + Mul + Div + Neg> MutableNumericVector<&self/T> for Vec4 { #[inline(always)] fn neg_self(&mut self) { *self.index_mut(0) = -*self.index_mut(0); @@ -205,7 +205,7 @@ pub impl MutableNumericVector<&self/T> for Vec4 { *self.index_mut(2) = -*self.index_mut(2); *self.index_mut(3) = -*self.index_mut(3); } - + #[inline(always)] fn mul_self_t(&mut self, value: &T) { *self.index_mut(0) *= (*value); @@ -213,7 +213,7 @@ pub impl MutableNumericVector<&self/T> for Vec4 { *self.index_mut(2) *= (*value); *self.index_mut(3) *= (*value); } - + #[inline(always)] fn div_self_t(&mut self, value: &T) { *self.index_mut(0) /= (*value); @@ -221,7 +221,7 @@ pub impl MutableNumericVector<&self/T> for Vec4 { *self.index_mut(2) /= (*value); *self.index_mut(3) /= (*value); } - + #[inline(always)] fn add_self_v(&mut self, other: &Vec4) { *self.index_mut(0) += other[0]; @@ -229,7 +229,7 @@ pub impl MutableNumericVector<&self/T> for Vec4 { *self.index_mut(2) += other[2]; *self.index_mut(3) += other[3]; } - + #[inline(always)] fn sub_self_v(&mut self, other: &Vec4) { *self.index_mut(0) -= other[0]; @@ -237,7 +237,7 @@ pub impl MutableNumericVector<&self/T> for Vec4 { *self.index_mut(2) -= other[2]; *self.index_mut(3) -= other[3]; } - + #[inline(always)] fn mul_self_v(&mut self, other: &Vec4) { *self.index_mut(0) *= other[0]; @@ -245,7 +245,7 @@ pub impl MutableNumericVector<&self/T> for Vec4 { *self.index_mut(2) *= other[2]; *self.index_mut(3) *= other[3]; } - + #[inline(always)] fn div_self_v(&mut self, other: &Vec4) { *self.index_mut(0) /= other[0]; @@ -255,72 +255,72 @@ pub impl MutableNumericVector<&self/T> for Vec4 { } } -pub impl EuclideanVector for Vec4 { +impl + Sub + Mul + Div + Neg> EuclideanVector for Vec4 { #[inline(always)] pure fn length2(&self) -> T { self.dot(self) } - + #[inline(always)] pure fn length(&self) -> T { self.length2().sqrt() } - + #[inline(always)] pure fn distance2(&self, other: &Vec4) -> T { other.sub_v(self).length2() } - + #[inline(always)] pure fn distance(&self, other: &Vec4) -> T { other.distance2(self).sqrt() } - + #[inline(always)] pure fn angle(&self, other: &Vec4) -> T { acos(self.dot(other) / (self.length() * other.length())) } - + #[inline(always)] pure fn normalize(&self) -> Vec4 { self.mul_t(one::()/self.length()) } - + #[inline(always)] pure fn normalize_to(&self, length: T) -> Vec4 { self.mul_t(length / self.length()) } - + #[inline(always)] pure fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { self.add_v(&other.sub_v(self).mul_t(amount)) } } -pub impl MutableEuclideanVector<&self/T> for Vec4 { +impl + Sub + Mul + Div + Neg> MutableEuclideanVector<&self/T> for Vec4 { #[inline(always)] fn normalize_self(&mut self) { let n = one::() / self.length(); self.mul_self_t(&n); } - + #[inline(always)] fn normalize_self_to(&mut self, length: &T) { let n = length / self.length(); self.mul_self_t(&n); } - + fn lerp_self(&mut self, other: &Vec4, amount: &T) { self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); } } -pub impl> FuzzyEq for Vec4 { +impl> FuzzyEq for Vec4 { #[inline(always)] pure fn fuzzy_eq(&self, other: &Vec4) -> bool { self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) } - + #[inline(always)] pure fn fuzzy_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { self[0].fuzzy_eq_eps(&other[0], epsilon) && @@ -330,7 +330,7 @@ pub impl> FuzzyEq for Vec4 { } } -pub impl OrdinalVector> for Vec4 { +impl OrdinalVector> for Vec4 { #[inline(always)] pure fn less_than(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] < other[0], @@ -338,7 +338,7 @@ pub impl OrdinalVector> for Vec4 { self[2] < other[2], self[3] < other[3]) } - + #[inline(always)] pure fn less_than_equal(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] <= other[0], @@ -346,7 +346,7 @@ pub impl OrdinalVector> for Vec4 { self[2] <= other[2], self[3] <= other[3]) } - + #[inline(always)] pure fn greater_than(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] > other[0], @@ -354,7 +354,7 @@ pub impl OrdinalVector> for Vec4 { self[2] > other[2], self[3] > other[3]) } - + #[inline(always)] pure fn greater_than_equal(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] >= other[0], @@ -364,7 +364,7 @@ pub impl OrdinalVector> for Vec4 { } } -pub impl EquableVector> for Vec4 { +impl EquableVector> for Vec4 { #[inline(always)] pure fn equal(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] == other[0], @@ -372,7 +372,7 @@ pub impl EquableVector> for Vec4 { self[2] == other[2], self[3] == other[3]) } - + #[inline(always)] pure fn not_equal(&self, other: &Vec4) -> Vec4 { Vector4::new(self[0] != other[0], @@ -382,19 +382,19 @@ pub impl EquableVector> for Vec4 { } } -pub impl BooleanVector for Vec4 { +impl BooleanVector for Vec4 { #[inline(always)] pure fn any(&self) -> bool { self[0] || self[1] || self[2] || self[3] } - + #[inline(always)] pure fn all(&self) -> bool { self[0] && self[1] && self[2] && self[3] } - + #[inline(always)] - pure fn not(&self) -> Vec4 { + pure fn not(&self) -> Vec4 { Vector4::new(!self[0], !self[1], !self[2], !self[3]) } } @@ -410,71 +410,71 @@ pub type uvec4 = Vec4; // a four-component unsigned integer vector // Static method wrappers for GLSL-style types -pub impl vec4 { +impl vec4 { #[inline(always)] static pure fn new(x: f32, y: f32, z: f32, w: f32) -> vec4 { Vector4::new(x, y, z, w) } #[inline(always)] static pure fn from_value(v: f32) -> vec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> vec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> vec4 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> vec4 { NumericVector4::unit_x() } #[inline(always)] static pure fn unit_y() -> vec4 { NumericVector4::unit_y() } #[inline(always)] static pure fn unit_z() -> vec4 { NumericVector4::unit_z() } #[inline(always)] static pure fn unit_w() -> vec4 { NumericVector4::unit_w() } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dvec4 { +impl dvec4 { #[inline(always)] static pure fn new(x: f64, y: f64, z: f64, w: f64) -> dvec4 { Vector4::new(x, y, z, w) } #[inline(always)] static pure fn from_value(v: f64) -> dvec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> dvec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> dvec4 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> dvec4 { NumericVector4::unit_x() } #[inline(always)] static pure fn unit_y() -> dvec4 { NumericVector4::unit_y() } #[inline(always)] static pure fn unit_z() -> dvec4 { NumericVector4::unit_z() } #[inline(always)] static pure fn unit_w() -> dvec4 { NumericVector4::unit_w() } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } - - -pub impl bvec4 { + + +impl bvec4 { #[inline(always)] static pure fn new(x: bool, y: bool, z: bool, w: bool) -> bvec4 { Vector4::new(x, y, z, w) } #[inline(always)] static pure fn from_value(v: bool) -> bvec4 { Vector::from_value(v) } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl ivec4 { +impl ivec4 { #[inline(always)] static pure fn new(x: i32, y: i32, z: i32, w: i32) -> ivec4 { Vector4::new(x, y, z, w) } #[inline(always)] static pure fn from_value(v: i32) -> ivec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> ivec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec4 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> ivec4 { NumericVector4::unit_x() } #[inline(always)] static pure fn unit_y() -> ivec4 { NumericVector4::unit_y() } #[inline(always)] static pure fn unit_z() -> ivec4 { NumericVector4::unit_z() } #[inline(always)] static pure fn unit_w() -> ivec4 { NumericVector4::unit_w() } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl uvec4 { +impl uvec4 { #[inline(always)] static pure fn new(x: u32, y: u32, z: u32, w: u32) -> uvec4 { Vector4::new(x, y, z, w) } #[inline(always)] static pure fn from_value(v: u32) -> uvec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> uvec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec4 { NumericVector::zero() } - + #[inline(always)] static pure fn unit_x() -> uvec4 { NumericVector4::unit_x() } #[inline(always)] static pure fn unit_y() -> uvec4 { NumericVector4::unit_y() } #[inline(always)] static pure fn unit_z() -> uvec4 { NumericVector4::unit_z() } #[inline(always)] static pure fn unit_w() -> uvec4 { NumericVector4::unit_w() } - + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } }