diff --git a/src/funs/relational.rs b/src/funs/relational.rs deleted file mode 100644 index 64616bc..0000000 --- a/src/funs/relational.rs +++ /dev/null @@ -1,218 +0,0 @@ -/** - * Vector Relational Functions - * - * This module corresponds to Section 8.7 of the [GLSL 4.30.6 specification] - * (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf). - */ - -use core::cmp::{Eq, Ord}; -use vec::{Vector, Vec2, Vec3, Vec4}; - -pub trait RelVector { - pure fn less_than(&self, other: &self) -> BVec; - pure fn less_than_equal(&self, other: &self) -> BVec; - pure fn greater_than(&self, other: &self) -> BVec; - pure fn greater_than_equal(&self, other: &self) -> BVec; - pure fn equal(&self, other: &self) -> BVec; - pure fn not_equal(&self, other: &self) -> BVec; -} - -#[inline(always)] pub pure fn less_than , BV>(x: &T, y: &T) -> BV { x.less_than(y) } -#[inline(always)] pub pure fn less_than_equal , BV>(x: &T, y: &T) -> BV { x.less_than_equal(y) } -#[inline(always)] pub pure fn greater_than , BV>(x: &T, y: &T) -> BV { x.greater_than(y) } -#[inline(always)] pub pure fn greater_than_equal, BV>(x: &T, y: &T) -> BV { x.greater_than_equal(y) } -#[inline(always)] pub pure fn equal , BV>(x: &T, y: &T) -> BV { x.equal(y) } -#[inline(always)] pub pure fn not_equal , BV>(x: &T, y: &T) -> BV { x.not_equal(y) } - -pub impl Vec2: RelVector> { - #[inline(always)] - pure fn less_than(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] < other[0], - self[1] < other[1]) - } - - #[inline(always)] - pure fn less_than_equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] <= other[0], - self[1] <= other[1]) - } - - #[inline(always)] - pure fn greater_than(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] > other[0], - self[1] > other[1]) - } - - #[inline(always)] - pure fn greater_than_equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] >= other[0], - self[1] >= other[1]) - } - - #[inline(always)] - pure fn equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] == other[0], - self[1] == other[1]) - } - - #[inline(always)] - pure fn not_equal(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] != other[0], - self[1] != other[1]) - } -} - -pub impl Vec3: RelVector> { - #[inline(always)] - pure fn less_than(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] < other[0], - self[1] < other[1], - self[2] < other[2]) - } - - #[inline(always)] - pure fn less_than_equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] <= other[0], - self[1] <= other[1], - self[2] <= other[2]) - } - - #[inline(always)] - pure fn greater_than(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] > other[0], - self[1] > other[1], - self[2] > other[2]) - } - - #[inline(always)] - pure fn greater_than_equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] >= other[0], - self[1] >= other[1], - self[2] >= other[2]) - } - - #[inline(always)] - pure fn equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] == other[0], - self[1] == other[1], - self[2] == other[2]) - } - - #[inline(always)] - pure fn not_equal(&self, other: &Vec3) -> Vec3 { - Vec3::new(self[0] != other[0], - self[1] != other[1], - self[2] != other[2]) - } -} - -pub impl Vec4: RelVector> { - #[inline(always)] - pure fn less_than(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] < other[0], - self[1] < other[1], - self[2] < other[2], - self[3] < other[3]) - } - - #[inline(always)] - pure fn less_than_equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] <= other[0], - self[1] <= other[1], - self[2] <= other[2], - self[3] <= other[3]) - } - - #[inline(always)] - pure fn greater_than(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] > other[0], - self[1] > other[1], - self[2] > other[2], - self[3] > other[3]) - } - - #[inline(always)] - pure fn greater_than_equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] >= other[0], - self[1] >= other[1], - self[2] >= other[2], - self[3] >= other[3]) - } - - #[inline(always)] - pure fn equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] == other[0], - self[1] == other[1], - self[2] == other[2], - self[3] == other[3]) - } - - #[inline(always)] - pure fn not_equal(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] != other[0], - self[1] != other[1], - self[2] != other[2], - self[3] != other[3]) - } -} - -pub trait BooleanVector: Vector { - pure fn any(&self) -> bool; - pure fn all(&self) -> bool; - pure fn not(&self) -> self; -} - -#[inline(always)] pub pure fn any(x: &T) -> bool { x.any() } -#[inline(always)] pub pure fn all(x: &T) -> bool { x.all() } -#[inline(always)] pub pure fn not(x: &T) -> T { x.not() } - -pub impl Vec2: BooleanVector { - #[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 { - Vec2::new(!self[0], !self[1]) - } -} - -pub impl Vec3: BooleanVector { - #[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 { - Vec3::new(!self[0], !self[1], !self[2]) - } -} - -pub impl Vec4: BooleanVector { - #[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 { - Vec4::new(!self[0], !self[1], !self[2], !self[3]) - } -} \ No newline at end of file diff --git a/src/funs/test/test_relational.rs b/src/funs/test/test_relational.rs deleted file mode 100644 index fa6d758..0000000 --- a/src/funs/test/test_relational.rs +++ /dev/null @@ -1,72 +0,0 @@ -use vec::*; -use relational::*; - -#[test] -fn test_boolv2() { - let tf = Vec2::new(true, false); - let ff = Vec2::new(false, false); - let tt = Vec2::new(true, true); - - assert tf.any() == true; - assert tf.all() == false; - assert tf.not() == Vec2::new(false, true); - - assert ff.any() == false; - assert ff.all() == false; - assert ff.not() == Vec2::new(true, true); - - assert tt.any() == true; - assert tt.all() == true; - assert tt.not() == Vec2::new(false, false); -} - -#[test] -fn test_boolv3() { - let tft = Vec3::new(true, false, true); - let fff = Vec3::new(false, false, false); - let ttt = Vec3::new(true, true, true); - - assert tft.any() == true; - assert tft.all() == false; - assert tft.not() == Vec3::new(false, true, false); - - assert fff.any() == false; - assert fff.all() == false; - assert fff.not() == Vec3::new(true, true, true); - - assert ttt.any() == true; - assert ttt.all() == true; - assert ttt.not() == Vec3::new(false, false, false); -} - -#[test] -fn test_boolv4() { - let tftf = Vec4::new(true, false, true, false); - let ffff = Vec4::new(false, false, false, false); - let tttt = Vec4::new(true, true, true, true); - - assert tftf.any() == true; - assert tftf.all() == false; - assert tftf.not() == Vec4::new(false, true, false, true); - - assert ffff.any() == false; - assert ffff.all() == false; - assert ffff.not() == Vec4::new(true, true, true, true); - - assert tttt.any() == true; - assert tttt.all() == true; - assert tttt.not() == Vec4::new(false, false, false, false); -} - -#[test] -fn test_boolv_fns() { - // let tf = Vec2::new(true, false); - // let ftf = Vec3::new(false, true, false); - // let tftf = Vec4::new(true, false, true, false); - - // FIXME: These tests won't compile! D: - - // assert any(&tf) == true; - // assert all(&ftf) == false; - // assert not(&tftf) == Vec4::new(false, true, false, true); -} \ No newline at end of file diff --git a/src/gltypes.rs b/src/gltypes.rs index bd1db5a..e1d310b 100644 --- a/src/gltypes.rs +++ b/src/gltypes.rs @@ -24,7 +24,7 @@ use core::sys::size_of; use angle::{Angle, Radians, Degrees, Rotation, Euler}; use color::color::{RGB, RGBA, HSV, HSVA}; -use mat::{NumericMatrix, NumericMatrixNxN, Mat2, Mat3, Mat4}; +use mat::{Matrix, Mat2, Mat3, Mat4}; use vec::{Vector, NumericVector, Vec2, Vec3, Vec4}; use quat::{/*Quaternion, */Quat}; @@ -40,10 +40,6 @@ pub type dvec2 = Vec2; /// a two-component double-precision flo pub type dvec3 = Vec3; /// a three-component double-precision floating-point vector pub type dvec4 = Vec4; /// a four-component double-precision floating-point vector -pub type bvec2 = Vec2; /// a two-component Boolean vector -pub type bvec3 = Vec3; /// a three-component Boolean vector -pub type bvec4 = Vec4; /// a four-component Boolean vector - pub type ivec2 = Vec2; /// a two-component signed integer vector pub type ivec3 = Vec3; /// a three-component signed integer vector pub type ivec4 = Vec4; /// a four-component signed integer vector @@ -117,37 +113,6 @@ pub impl dvec4 { } -pub impl bvec2 { - #[inline(always)] static pure fn new(x: bool, y: bool) -> bvec2 { Vec2::new(x, y) } - #[inline(always)] static pure fn from_value(v: bool) -> bvec2 { Vector::from_value(v) } - // #[inline(always)] static pure fn identity() -> bvec2 { NumericVector::identity() } - // #[inline(always)] static pure fn zero() -> bvec2 { NumericVector::zero() } - - #[inline(always)] static pure fn dim() -> uint { 2 } - #[inline(always)] static pure fn size_of() -> uint { size_of::() } -} - -pub impl bvec3 { - #[inline(always)] static pure fn new(x: bool, y: bool, z: bool) -> bvec3 { Vec3::new(x, y, z) } - #[inline(always)] static pure fn from_value(v: bool) -> bvec3 { Vector::from_value(v) } - // #[inline(always)] static pure fn identity() -> bvec3 { NumericVector::identity() } - // #[inline(always)] static pure fn zero() -> bvec3 { NumericVector::zero() } - - #[inline(always)] static pure fn dim() -> uint { 3 } - #[inline(always)] static pure fn size_of() -> uint { size_of::() } -} - -pub impl bvec4 { - #[inline(always)] static pure fn new(x: bool, y: bool, z: bool, w: bool) -> bvec4 { Vec4::new(x, y, z, w) } - #[inline(always)] static pure fn from_value(v: bool) -> bvec4 { Vector::from_value(v) } - // #[inline(always)] static pure fn identity() -> bvec4 { NumericVector::identity() } - // #[inline(always)] static pure fn zero() -> bvec4 { NumericVector::zero() } - - #[inline(always)] static pure fn dim() -> uint { 4 } - #[inline(always)] static pure fn size_of() -> uint { size_of::() } -} - - pub impl ivec2 { #[inline(always)] static pure fn new(x: i32, y: i32) -> ivec2 { Vec2::new(x, y) } #[inline(always)] static pure fn from_value(v: i32) -> ivec2 { Vector::from_value(v) } @@ -213,248 +178,142 @@ pub impl uvec4 { // Matrix aliases, corresponding to Section 4.1.6 of the [GLSL 4.30.6 specification] // (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf). -pub type mat2 = mat2x2; /// a 2×2 single-precision floating-point matrix -pub type mat3 = mat3x3; /// a 3×3 single-precision floating-point matrix -pub type mat4 = mat4x4; /// a 4×4 single-precision floating-point matrix -pub type mat2x2 = Mat2; /// same as a `mat2` -// pub type mat2x3 = /// a single-precision floating-point matrix with 2 columns and 3 rows -// pub type mat2x4 = /// a single-precision floating-point matrix with 2 columns and 4 rows -// pub type mat3x2 = /// a single-precision floating-point matrix with 3 columns and 2 rows -pub type mat3x3 = Mat3; /// same as a `mat3` -// pub type mat3x4 = /// a single-precision floating-point matrix with 3 columns and 4 rows -// pub type mat4x2 = /// a single-precision floating-point matrix with 4 columns and 2 rows -// pub type mat4x3 = /// a single-precision floating-point matrix with 4 columns and 3 rows -pub type mat4x4 = Mat4; /// same as a `mat4` +pub type mat2 = Mat2; /// a 2×2 single-precision floating-point matrix +pub type mat3 = Mat3; /// a 3×3 single-precision floating-point matrix +pub type mat4 = Mat4; /// a 4×4 single-precision floating-point matrix -pub type dmat2 = dmat2x2; /// a 2×2 double-precision floating-point matrix -pub type dmat3 = dmat3x3; /// a 3×3 double-precision floating-point matrix -pub type dmat4 = dmat4x4; /// a 4×4 double-precision floating-point matrix -pub type dmat2x2 = Mat2; /// same as a `dmat2` -// pub type dmat2x3 = /// a double-precision floating-point matrix with 2 columns and 3 rows -// pub type dmat2x4 = /// a double-precision floating-point matrix with 2 columns and 4 rows -// pub type dmat3x2 = /// a double-precision floating-point matrix with 3 columns and 2 rows -pub type dmat3x3 = Mat3; /// same as a `dmat3` -// pub type dmat3x4 = /// a double-precision floating-point matrix with 3 columns and 4 rows -// pub type dmat4x2 = /// a double-precision floating-point matrix with 4 columns and 2 rows -// pub type dmat4x3 = /// a double-precision floating-point matrix with 4 columns and 3 rows -pub type dmat4x4 = Mat4; /// same as a `dmat4` +pub type dmat2 = Mat2; /// a 2×2 double-precision floating-point matrix +pub type dmat3 = Mat3; /// a 3×3 double-precision floating-point matrix +pub type dmat4 = Mat4; /// a 4×4 double-precision floating-point matrix // Matrix method wrappers pub impl mat2 { #[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c1r0: f32, c1r1: f32) - -> mat2 { mat2x2::new(c0r0, c0r1, c1r0, c1r1) } + -> mat2 { Mat2::new(c0r0, c0r1, c1r0, c1r1) } #[inline(always)] static pure fn from_cols(c0: vec2, c1: vec2) - -> mat2 { mat2x2::from_cols(move c0, move c1) } - #[inline(always)] static pure fn from_value(v: f32) -> mat2 { mat2x2::from_value(v) } - #[inline(always)] static pure fn identity() -> mat2 { mat2x2::identity() } - #[inline(always)] static pure fn zero() -> mat2 { mat2x2::zero() } - - #[inline(always)] static pure fn dim() -> uint { mat2x2::dim() } - #[inline(always)] static pure fn rows() -> uint { mat2x2::rows() } - #[inline(always)] static pure fn cols() -> uint { mat2x2::cols() } - #[inline(always)] static pure fn size_of() -> uint { mat2x2::size_of() } -} - -pub 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 { mat3x3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } - #[inline(always)] static pure fn from_cols(c0: vec3, c1: vec3, c2: vec3) - -> mat3 { mat3x3::from_cols(move c0, move c1, move c2) } - #[inline(always)] static pure fn from_value(v: f32) -> mat3 { mat3x3::from_value(v) } - #[inline(always)] static pure fn identity() -> mat3 { mat3x3::identity() } - #[inline(always)] static pure fn zero() -> mat3 { mat3x3::zero() } - - #[inline(always)] static pure fn dim() -> uint { mat3x3::dim() } - #[inline(always)] static pure fn rows() -> uint { mat3x3::rows() } - #[inline(always)] static pure fn cols() -> uint { mat3x3::cols() } - #[inline(always)] static pure fn size_of() -> uint { mat3x3::size_of() } -} - -pub 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 { mat4x4::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 { mat4x4::from_cols(move c0, move c1, move c2, move c3) } - #[inline(always)] static pure fn from_value(v: f32) -> mat4 { mat4x4::from_value(v) } - #[inline(always)] static pure fn identity() -> mat4 { mat4x4::identity() } - #[inline(always)] static pure fn zero() -> mat4 { mat4x4::zero() } - - #[inline(always)] static pure fn dim() -> uint { mat4x4::dim() } - #[inline(always)] static pure fn rows() -> uint { mat4x4::rows() } - #[inline(always)] static pure fn cols() -> uint { mat4x4::cols() } - #[inline(always)] static pure fn size_of() -> uint { mat4x4::size_of() } -} - -pub impl mat2x2 { - #[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c1r0: f32, c1r1: f32) - -> mat2x2 { Mat2::new(c0r0, c0r1, c1r0, c1r1) } - #[inline(always)] static pure fn from_cols(c0: vec2, c1: vec2) - -> mat2x2 { Mat2::from_cols(move c0, move c1) } - #[inline(always)] static pure fn from_value(v: f32) -> mat2x2 { Mat2::from_value(v) } + -> mat2 { Mat2::from_cols(move c0, move c1) } + #[inline(always)] static pure fn from_value(v: f32) -> mat2 { Mat2::from_value(v) } // FIXME: there's something wrong with static functions here! - // #[inline(always)] static pure fn identity() -> mat2x2 { NumericMatrixNxN::identity() } - // #[inline(always)] static pure fn zero() -> mat2x2 { NumericMatrix::zero() } + // #[inline(always)] static pure fn identity() -> mat2 { NumericMatrixNxN::identity() } + // #[inline(always)] static pure fn zero() -> mat2 { NumericMatrix::zero() } // FIXME: An interim solution to the issues with static functions - #[inline(always)] static pure fn identity() -> mat2x2 { Mat2::identity() } - #[inline(always)] static pure fn zero() -> mat2x2 { Mat2::zero() } + #[inline(always)] static pure fn identity() -> mat2 { Mat2::identity() } + #[inline(always)] static pure fn zero() -> mat2 { Mat2::zero() } #[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::() } + #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl mat3x3 { +pub 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) - -> mat3x3 { Mat3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } + -> mat3 { Mat3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } #[inline(always)] static pure fn from_cols(c0: vec3, c1: vec3, c2: vec3) - -> mat3x3 { Mat3::from_cols(move c0, move c1, move c2) } - #[inline(always)] static pure fn from_value(v: f32) -> mat3x3 { Mat3::from_value(v) } + -> mat3 { Mat3::from_cols(move c0, move c1, move c2) } + #[inline(always)] static pure fn from_value(v: f32) -> mat3 { Mat3::from_value(v) } // FIXME: there's something wrong with static functions here! - // #[inline(always)] static pure fn identity() -> mat3x3 { NumericMatrixNxN::identity() } - // #[inline(always)] static pure fn zero() -> mat3x3 { NumericMatrix::zero() } + // #[inline(always)] static pure fn identity() -> mat3 { NumericMatrixNxN::identity() } + // #[inline(always)] static pure fn zero() -> mat3 { NumericMatrix::zero() } // FIXME: An interim solution to the issues with static functions - #[inline(always)] static pure fn identity() -> mat3x3 { Mat3::identity() } - #[inline(always)] static pure fn zero() -> mat3x3 { Mat3::zero() } + #[inline(always)] static pure fn identity() -> mat3 { Mat3::identity() } + #[inline(always)] static pure fn zero() -> mat3 { Mat3::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 } - #[inline(always)] static pure fn size_of() -> uint { size_of::() } + #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl mat4x4 { +pub 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) - -> mat4x4 { Mat4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) } + -> mat4 { Mat4::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) - -> mat4x4 { Mat4::from_cols(move c0, move c1, move c2, move c3) } - #[inline(always)] static pure fn from_value(v: f32) -> mat4x4 { Mat4::from_value(v) } + -> mat4 { Mat4::from_cols(move c0, move c1, move c2, move c3) } + #[inline(always)] static pure fn from_value(v: f32) -> mat4 { Mat4::from_value(v) } // FIXME: there's something wrong with static functions here! - // #[inline(always)] static pure fn identity() -> mat4x4 { NumericMatrixNxN::identity() } - // #[inline(always)] static pure fn zero() -> mat4x4 { NumericMatrix::zero() } + // #[inline(always)] static pure fn identity() -> mat4 { NumericMatrixNxN::identity() } + // #[inline(always)] static pure fn zero() -> mat4 { NumericMatrix::zero() } // FIXME: An interim solution to the issues with static functions - #[inline(always)] static pure fn identity() -> mat4x4 { Mat4::identity() } - #[inline(always)] static pure fn zero() -> mat4x4 { Mat4::zero() } + #[inline(always)] static pure fn identity() -> mat4 { Mat4::identity() } + #[inline(always)] static pure fn zero() -> mat4 { Mat4::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::() } + #[inline(always)] static pure fn size_of() -> uint { size_of::() } } pub impl dmat2 { #[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c1r0: f64, c1r1: f64) - -> dmat2 { dmat2x2::new(c0r0, c0r1, c1r0, c1r1) } + -> dmat2 { Mat2::new(c0r0, c0r1, c1r0, c1r1) } #[inline(always)] static pure fn from_cols(c0: dvec2, c1: dvec2) - -> dmat2 { dmat2x2::from_cols(move c0, move c1) } - #[inline(always)] static pure fn from_value(v: f64) -> dmat2 { dmat2x2::from_value(v) } - #[inline(always)] static pure fn identity() -> dmat2 { dmat2x2::identity() } - #[inline(always)] static pure fn zero() -> dmat2 { dmat2x2::zero() } - - #[inline(always)] static pure fn dim() -> uint { dmat2x2::dim() } - #[inline(always)] static pure fn rows() -> uint { dmat2x2::rows() } - #[inline(always)] static pure fn cols() -> uint { dmat2x2::cols() } - #[inline(always)] static pure fn size_of() -> uint { dmat2x2::size_of() } -} - -pub 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 { dmat3x3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } - #[inline(always)] static pure fn from_cols(c0: dvec3, c1: dvec3, c2: dvec3) - -> dmat3 { dmat3x3::from_cols(move c0, move c1, move c2) } - #[inline(always)] static pure fn identity() -> dmat3 { dmat3x3::identity() } - #[inline(always)] static pure fn zero() -> dmat3 { dmat3x3::zero() } - - #[inline(always)] static pure fn dim() -> uint { dmat3x3::dim() } - #[inline(always)] static pure fn rows() -> uint { dmat3x3::rows() } - #[inline(always)] static pure fn cols() -> uint { dmat3x3::cols() } - #[inline(always)] static pure fn size_of() -> uint { dmat3x3::size_of() } -} - -pub 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 { dmat4x4::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 { dmat4x4::from_cols(move c0, move c1, move c2, move c3) } - #[inline(always)] static pure fn identity() -> dmat4 { dmat4x4::identity() } - #[inline(always)] static pure fn zero() -> dmat4 { dmat4x4::zero() } - - #[inline(always)] static pure fn dim() -> uint { dmat4x4::dim() } - #[inline(always)] static pure fn rows() -> uint { dmat4x4::rows() } - #[inline(always)] static pure fn cols() -> uint { dmat4x4::cols() } - #[inline(always)] static pure fn size_of() -> uint { dmat4x4::size_of() } -} - -pub impl dmat2x2 { - #[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c1r0: f64, c1r1: f64) - -> dmat2x2 { Mat2::new(c0r0, c0r1, c1r0, c1r1) } - #[inline(always)] static pure fn from_cols(c0: dvec2, c1: dvec2) - -> dmat2x2 { Mat2::from_cols(move c0, move c1) } - #[inline(always)] static pure fn from_value(v: f64) -> dmat2x2 { Mat2::from_value(v) } + -> dmat2 { Mat2::from_cols(move c0, move c1) } + #[inline(always)] static pure fn from_value(v: f64) -> dmat2 { Mat2::from_value(v) } // FIXME: there's something wrong with static functions here! - // #[inline(always)] static pure fn identity() -> dmat2x2 { NumericMatrixNxN::identity() } - // #[inline(always)] static pure fn zero() -> dmat2x2 { NumericMatrix::zero() } + // #[inline(always)] static pure fn identity() -> dmat2 { NumericMatrixNxN::identity() } + // #[inline(always)] static pure fn zero() -> dmat2 { NumericMatrix::zero() } // FIXME: An interim solution to the issues with static functions - #[inline(always)] static pure fn identity() -> dmat2x2 { Mat2::identity() } - #[inline(always)] static pure fn zero() -> dmat2x2 { Mat2::zero() } + #[inline(always)] static pure fn identity() -> dmat2 { Mat2::identity() } + #[inline(always)] static pure fn zero() -> dmat2 { Mat2::zero() } #[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::() } + #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dmat3x3 { +pub 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) - -> dmat3x3 { Mat3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } + -> dmat3 { Mat3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) } #[inline(always)] static pure fn from_cols(c0: dvec3, c1: dvec3, c2: dvec3) - -> dmat3x3 { Mat3::from_cols(move c0, move c1, move c2) } - #[inline(always)] static pure fn from_value(v: f64) -> dmat3x3 { Mat3::from_value(v) } + -> dmat3 { Mat3::from_cols(move c0, move c1, move c2) } + #[inline(always)] static pure fn from_value(v: f64) -> dmat3 { Mat3::from_value(v) } // FIXME: there's something wrong with static functions here! - // #[inline(always)] static pure fn identity() -> dmat3x3 { NumericMatrixNxN::identity() } - // #[inline(always)] static pure fn zero() -> dmat3x3 { NumericMatrix::zero() } + // #[inline(always)] static pure fn identity() -> dmat3 { NumericMatrixNxN::identity() } + // #[inline(always)] static pure fn zero() -> dmat3 { NumericMatrix::zero() } // FIXME: An interim solution to the issues with static functions - #[inline(always)] static pure fn identity() -> dmat3x3 { Mat3::identity() } - #[inline(always)] static pure fn zero() -> dmat3x3 { Mat3::zero() } + #[inline(always)] static pure fn identity() -> dmat3 { Mat3::identity() } + #[inline(always)] static pure fn zero() -> dmat3 { Mat3::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 } - #[inline(always)] static pure fn size_of() -> uint { size_of::() } + #[inline(always)] static pure fn size_of() -> uint { size_of::() } } -pub impl dmat4x4 { +pub 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) - -> dmat4x4 { Mat4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) } + -> dmat4 { Mat4::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) - -> dmat4x4 { Mat4::from_cols(move c0, move c1, move c2, move c3) } - #[inline(always)] static pure fn from_value(v: f64) -> dmat4x4 { Mat4::from_value(v) } + -> dmat4 { Mat4::from_cols(move c0, move c1, move c2, move c3) } + #[inline(always)] static pure fn from_value(v: f64) -> dmat4 { Mat4::from_value(v) } // FIXME: there's something wrong with static functions here! - // #[inline(always)] static pure fn identity() -> dmat4x4 { NumericMatrixNxN::identity() } - // #[inline(always)] static pure fn zero() -> dmat4x4 { NumericMatrix::zero() } + // #[inline(always)] static pure fn identity() -> dmat4 { NumericMatrixNxN::identity() } + // #[inline(always)] static pure fn zero() -> dmat4 { NumericMatrix::zero() } // FIXME: An interim solution to the issues with static functions - #[inline(always)] static pure fn identity() -> dmat4x4 { Mat4::identity() } - #[inline(always)] static pure fn zero() -> dmat4x4 { Mat4::zero() } + #[inline(always)] static pure fn identity() -> dmat4 { Mat4::identity() } + #[inline(always)] static pure fn zero() -> dmat4 { Mat4::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::() } + #[inline(always)] static pure fn size_of() -> uint { size_of::() } } diff --git a/src/lmath.rc b/src/lmath.rc index 746b9af..2439e38 100644 --- a/src/lmath.rc +++ b/src/lmath.rc @@ -53,8 +53,6 @@ pub mod funs { pub mod exponential; #[path = "funs/projection.rs"] pub mod projection; - #[path = "funs/relational.rs"] - pub mod relational; #[path = "funs/triganomic.rs"] pub mod triganomic; @@ -62,8 +60,6 @@ pub mod funs { mod test { #[path = "funs/test/test_common.rs"] mod test_common; - #[path = "funs/test/test_relational.rs"] - mod test_relational; } } diff --git a/src/mat.rs b/src/mat.rs index a431465..b1d00ac 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -14,89 +14,20 @@ use num::default_eq::DefaultEq; use quat::{Quat, ToQuat}; use vec::{NumericVector, Vec2, Vec3, Vec4}; -/// -/// The base Matrix trait -/// -pub trait Matrix: Dimensional, ToPtr, Eq, DefaultEq { - static pure fn rows() -> uint; - static pure fn cols() -> uint; +/** + * An N x N Matrix + */ +pub trait Matrix: Dimensional, ToPtr, Eq, DefaultEq, Neg { + pure fn col(&self, i: uint) -> V; + pure fn row(&self, i: uint) -> V; - pure fn col(&self, i: uint) -> Col; - pure fn row(&self, i: uint) -> Row; -} - -/// A 2 x N matrix -pub trait Matrix2: Matrix { -// /// Construct the matrix from two column vectors -// static pure fn from_cols(c0: Col, c1: Col) -> self; -} - -/// A 3 x N matrix -pub trait Matrix3: Matrix { -// /// Construct the matrix from three column vectors -// static pure fn from_cols(c0: Col, c1: Col, c2: Col) -> self; -} - -/// A 4 x N matrix -pub trait Matrix4: Matrix { -// /// Construct the matrix from four column vectors -// static pure fn from_cols(c0: Col, c1: Col, c2: Col, c3: Col) -> self; -} - -/// -/// A square matrix -/// -pub trait MatrixNxN: Matrix { - pure fn is_symmetric(&self) -> bool; -} - -/// A 2 x 2 square matrix -pub trait Matrix2x2: MatrixNxN, - Matrix2 { -// /// Construct the matrix from a column major series of elements -// static pure fn new(c0r0: T, c0r1: T, -// c1r0: T, c1r1: T) -> self; -} - -/// A 3 x 3 square matrix -pub trait Matrix3x3: MatrixNxN, - Matrix3 { -// /// Construct the matrix from a column major series of elements -// static pure fn new(c0r0: T, c0r1: T, c0r2: T, -// c1r0: T, c1r1: T, c1r2: T, -// c2r0: T, c2r1: T, c2r2: T) -> self; -} - -/// A 4 x 4 square matrix -pub trait Matrix4x4: MatrixNxN, - Matrix4 { -// /// Construct the matrix from a column major series of elements -// static pure fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T, -// 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; -} - -/// -/// A matrix with numeric elements -/// -pub trait NumericMatrix: Matrix, Neg { + static pure fn identity() -> self; static pure fn zero() -> self; pure fn mul_t(&self, value: T) -> self; - pure fn mul_v(&self, other: &Col) -> Col; + pure fn mul_v(&self, other: &V) -> V; pure fn add_m(&self, other: &self) -> self; pure fn sub_m(&self, other: &self) -> self; -} - -/// -/// A square matrix with numeric elements -/// -pub trait NumericMatrixNxN: MatrixNxN, - NumericMatrix { - static pure fn identity() -> self; - - // static pure fn from_value(value: T) -> self; pure fn mul_m(&self, other: &self) -> self; pure fn dot(&self, other: &self) -> T; @@ -110,26 +41,23 @@ pub trait NumericMatrixNxN: MatrixNxN, pure fn is_identity(&self) -> bool; pure fn is_diagonal(&self) -> bool; pure fn is_rotated(&self) -> bool; + pure fn is_symmetric(&self) -> bool; pure fn is_invertible(&self) -> bool; } /// A 2 x 2 square matrix with numeric elements -pub trait NumericMatrix2x2: Matrix2x2, - NumericMatrixNxN { - +pub trait Matrix2: Matrix { pure fn to_mat3(&self) -> Mat3; pure fn to_mat4(&self) -> Mat4; } /// A 3 x 3 square matrix with numeric elements -pub trait NumericMatrix3x3: Matrix3x3, - NumericMatrixNxN { +pub trait Matrix3: Matrix { pure fn to_mat4(&self) -> Mat4; } /// A 4 x 4 square matrix with numeric elements -pub trait NumericMatrix4x4: Matrix4x4, - NumericMatrixNxN { +pub trait Matrix4: Matrix { } @@ -137,9 +65,9 @@ pub trait NumericMatrix4x4: Matrix4x4, -// -// Mat2: A 2x2, column major matrix -// +/** + * A 2 x 2 column major matrix + */ pub struct Mat2 { x: Vec2, y: Vec2 } pub impl Mat2 { @@ -181,13 +109,7 @@ pub impl Mat2 { } } -pub impl Mat2: Matrix, Vec2> { - #[inline(always)] - static pure fn cols() -> uint { 2 } - - #[inline(always)] - static pure fn rows() -> uint { 2 } - +pub impl Mat2: Matrix> { #[inline(always)] pure fn col(&self, i: uint) -> Vec2 { self[i] } @@ -196,42 +118,15 @@ pub impl Mat2: Matrix, Vec2> { Vec2::new(self[0][i], self[1][i]) } -} - -pub impl Mat2: Dimensional> { - #[inline(always)] - static pure fn dim() -> uint { 2 } #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - -pub impl Mat2: Index> { - #[inline(always)] - pure fn index(i: uint) -> Vec2 { - unsafe { do buf_as_slice( - transmute::<*Mat2, *Vec2>( - to_unsafe_ptr(&self)), 2) |slice| { slice[i] } - } + static pure fn identity() -> Mat2 { + let _0 = cast(0); + let _1 = cast(1); + Mat2::new(_1, _0, + _0, _1) } -} - -pub impl Mat2: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - self[0].to_ptr() - } -} - -pub impl Mat2: MatrixNxN> { - #[inline(always)] - pure fn is_symmetric(&self) -> bool { - self[0][1].default_eq(&self[1][0]) && - self[1][0].default_eq(&self[0][1]) - } -} - -pub impl Mat2: NumericMatrix, Vec2> { + #[inline(always)] static pure fn zero() -> Mat2 { let _0 = cast(0); @@ -262,23 +157,6 @@ pub impl Mat2: NumericMatrix, Vec2 Mat2: Neg> { - #[inline(always)] - pure fn neg(&self) -> Mat2 { - Mat2::from_cols(-self[0], -self[1]) - } -} - -pub impl Mat2: NumericMatrixNxN> { - #[inline(always)] - static pure fn identity() -> Mat2 { - let _0 = cast(0); - let _1 = cast(1); - Mat2::new(_1, _0, - _0, _1) - } #[inline(always)] pure fn mul_m(&self, other: &Mat2) -> Mat2 { @@ -318,7 +196,7 @@ pub impl Mat2: NumericMatrixNxN> { #[inline(always)] pure fn is_identity(&self) -> bool { - // self.default_eq(&NumericMatrixNxN::identity()) // FIXME: there's something wrong with static functions here! + // self.default_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! self.default_eq(&Mat2::identity()) } @@ -331,9 +209,15 @@ pub impl Mat2: NumericMatrixNxN> { #[inline(always)] pure fn is_rotated(&self) -> bool { - // !self.default_eq(&NumericMatrixNxN::identity()) // FIXME: there's something wrong with static functions here! + // !self.default_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! !self.default_eq(&Mat2::identity()) } + + #[inline(always)] + pure fn is_symmetric(&self) -> bool { + self[0][1].default_eq(&self[1][0]) && + self[1][0].default_eq(&self[0][1]) + } #[inline(always)] pure fn is_invertible(&self) -> bool { @@ -342,7 +226,7 @@ pub impl Mat2: NumericMatrixNxN> { } } -pub impl Mat2: NumericMatrix2x2> { +pub impl Mat2: Matrix2> { #[inline(always)] pure fn to_mat3(&self) -> Mat3 { Mat3::from_Mat2(self) @@ -354,6 +238,38 @@ pub impl Mat2: NumericMatrix2x2> { } } +pub impl Mat2: Dimensional> { + #[inline(always)] + static pure fn dim() -> uint { 2 } + + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } +} + +pub impl Mat2: Index> { + #[inline(always)] + pure fn index(i: uint) -> Vec2 { + unsafe { do buf_as_slice( + transmute::<*Mat2, *Vec2>( + to_unsafe_ptr(&self)), 2) |slice| { slice[i] } + } + } +} + +pub impl Mat2: ToPtr { + #[inline(always)] + pure fn to_ptr(&self) -> *T { + self[0].to_ptr() + } +} + +pub impl Mat2: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat2 { + Mat2::from_cols(-self[0], -self[1]) + } +} + pub impl Mat2: Eq { #[inline(always)] pure fn eq(&self, other: &Mat2) -> bool { @@ -388,9 +304,9 @@ pub impl Mat2: DefaultEq { -// -// Mat3: A 3x3, column major matrix -// +/** + * A 3 x 3 column major matrix + */ pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } pub impl Mat3 { @@ -447,13 +363,7 @@ pub impl Mat3 { } } -pub impl Mat3: Matrix, Vec3> { - #[inline(always)] - static pure fn cols() -> uint { 3 } - - #[inline(always)] - static pure fn rows() -> uint { 3 } - +pub impl Mat3: Matrix> { #[inline(always)] pure fn col(&self, i: uint) -> Vec3 { self[i] } @@ -463,48 +373,16 @@ pub impl Mat3: Matrix, Vec3> { self[1][i], self[2][i]) } -} - -pub impl Mat3: Dimensional> { - #[inline(always)] - static pure fn dim() -> uint { 3 } #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - -pub impl Mat3: Index> { - #[inline(always)] - pure fn index(i: uint) -> Vec3 { - unsafe { do buf_as_slice( - transmute::<*Mat3, *Vec3>( - to_unsafe_ptr(&self)), 3) |slice| { slice[i] } - } + static pure fn identity() -> Mat3 { + let _0 = cast(0); + let _1 = cast(1); + Mat3::new(_1, _0, _0, + _0, _1, _0, + _0, _0, _1) } -} - -pub impl Mat3: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - self[0].to_ptr() - } -} - -pub impl Mat3: MatrixNxN> { - #[inline(always)] - pure fn is_symmetric(&self) -> bool { - self[0][1].default_eq(&self[1][0]) && - self[0][2].default_eq(&self[2][0]) && - - self[1][0].default_eq(&self[0][1]) && - self[1][2].default_eq(&self[2][1]) && - - self[2][0].default_eq(&self[0][2]) && - self[2][1].default_eq(&self[1][2]) - } -} - -pub impl Mat3: NumericMatrix, Vec3> { + #[inline(always)] static pure fn zero() -> Mat3 { let _0 = cast(0); @@ -540,24 +418,6 @@ pub impl Mat3: NumericMatrix, Vec3 Mat3: Neg> { - #[inline(always)] - pure fn neg(&self) -> Mat3 { - Mat3::from_cols(-self[0], -self[1], -self[2]) - } -} - -pub impl Mat3: NumericMatrixNxN> { - #[inline(always)] - static pure fn identity() -> Mat3 { - let _0 = cast(0); - let _1 = cast(1); - Mat3::new(_1, _0, _0, - _0, _1, _0, - _0, _0, _1) - } #[inline(always)] pure fn mul_m(&self, other: &Mat3) -> Mat3 { @@ -601,7 +461,7 @@ pub impl Mat3: NumericMatrixNxN> { #[inline(always)] pure fn is_identity(&self) -> bool { - // self.default_eq(&NumericMatrixNxN::identity()) // FIXME: there's something wrong with static functions here! + // self.default_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! self.default_eq(&Mat3::identity()) } @@ -620,9 +480,21 @@ pub impl Mat3: NumericMatrixNxN> { #[inline(always)] pure fn is_rotated(&self) -> bool { - // !self.default_eq(&NumericMatrixNxN::identity()) // FIXME: there's something wrong with static functions here! + // !self.default_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! !self.default_eq(&Mat3::identity()) } + + #[inline(always)] + pure fn is_symmetric(&self) -> bool { + self[0][1].default_eq(&self[1][0]) && + self[0][2].default_eq(&self[2][0]) && + + self[1][0].default_eq(&self[0][1]) && + self[1][2].default_eq(&self[2][1]) && + + self[2][0].default_eq(&self[0][2]) && + self[2][1].default_eq(&self[1][2]) + } #[inline(always)] pure fn is_invertible(&self) -> bool { @@ -631,7 +503,7 @@ pub impl Mat3: NumericMatrixNxN> { } } -pub impl Mat3: NumericMatrix3x3> { +pub impl Mat3: Matrix3> { #[inline(always)] pure fn to_mat4(&self) -> Mat4 { Mat4::from_Mat3(self) @@ -681,6 +553,38 @@ pub impl Mat3: ToQuat { } } +pub impl Mat3: Dimensional> { + #[inline(always)] + static pure fn dim() -> uint { 3 } + + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } +} + +pub impl Mat3: Index> { + #[inline(always)] + pure fn index(i: uint) -> Vec3 { + unsafe { do buf_as_slice( + transmute::<*Mat3, *Vec3>( + to_unsafe_ptr(&self)), 3) |slice| { slice[i] } + } + } +} + +pub impl Mat3: ToPtr { + #[inline(always)] + pure fn to_ptr(&self) -> *T { + self[0].to_ptr() + } +} + +pub impl Mat3: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat3 { + Mat3::from_cols(-self[0], -self[1], -self[2]) + } +} + pub impl Mat3: Eq { #[inline(always)] pure fn eq(&self, other: &Mat3) -> bool { @@ -718,9 +622,9 @@ pub impl Mat3: DefaultEq { -// -// Mat4: A 4x4, column major matrix -// +/** + * A 4 x 4 column major matrix + */ pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } pub impl Mat4 { @@ -794,13 +698,7 @@ pub impl Mat4 { } } -pub impl Mat4: Matrix, Vec4> { - #[inline(always)] - static pure fn cols() -> uint { 4 } - - #[inline(always)] - static pure fn rows() -> uint { 4 } - +pub impl Mat4: Matrix> { #[inline(always)] pure fn col(&self, i: uint) -> Vec4 { self[i] } @@ -811,55 +709,7 @@ pub impl Mat4: Matrix, Vec4> { self[2][i], self[3][i]) } -} - -pub impl Mat4: Dimensional> { - #[inline(always)] - static pure fn dim() -> uint { 4 } - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - -pub impl Mat4: Index> { - #[inline(always)] - pure fn index(i: uint) -> Vec4 { - unsafe { do buf_as_slice( - transmute::<*Mat4, *Vec4>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } - } - } -} - -pub impl Mat4: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - self[0].to_ptr() - } -} - -pub impl Mat4: MatrixNxN> { - #[inline(always)] - pure fn is_symmetric(&self) -> bool { - self[0][1].default_eq(&self[1][0]) && - self[0][2].default_eq(&self[2][0]) && - self[0][3].default_eq(&self[3][0]) && - - self[1][0].default_eq(&self[0][1]) && - self[1][2].default_eq(&self[2][1]) && - self[1][3].default_eq(&self[3][1]) && - - self[2][0].default_eq(&self[0][2]) && - self[2][1].default_eq(&self[1][2]) && - self[2][3].default_eq(&self[3][2]) && - - self[3][0].default_eq(&self[0][3]) && - self[3][1].default_eq(&self[1][3]) && - self[3][2].default_eq(&self[2][3]) - } -} - -pub impl Mat4: NumericMatrix, Vec4> { #[inline(always)] static pure fn zero() -> Mat4 { let _0 = cast(0); @@ -900,16 +750,7 @@ pub impl Mat4: NumericMatrix, Vec4 Mat4: Neg> { - #[inline(always)] - pure fn neg(&self) -> Mat4 { - Mat4::from_cols(-self[0], -self[1], -self[2], -self[3]) - } -} - -pub impl Mat4: NumericMatrixNxN> { + #[inline(always)] static pure fn identity() -> Mat4 { let _0 = cast(0); @@ -964,7 +805,7 @@ pub impl Mat4: NumericMatrixNxN = NumericMatrixNxN::identity(); // FIXME: there's something wrong with static functions here! + // let mut inv: Mat4 = Matrix::identity(); // FIXME: there's something wrong with static functions here! let mut inv = Mat4::identity(); // Find largest pivot column j among rows j..3 @@ -1021,7 +862,7 @@ pub impl Mat4: NumericMatrixNxN bool { - // self.default_eq(&NumericMatrixNxN::identity()) // FIXME: there's something wrong with static functions here! + // self.default_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! self.default_eq(&Mat4::identity()) } @@ -1047,9 +888,28 @@ pub impl Mat4: NumericMatrixNxN bool { - // !self.default_eq(&NumericMatrixNxN::identity()) // FIXME: there's something wrong with static functions here! + // !self.default_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! !self.default_eq(&Mat4::identity()) } + + #[inline(always)] + pure fn is_symmetric(&self) -> bool { + self[0][1].default_eq(&self[1][0]) && + self[0][2].default_eq(&self[2][0]) && + self[0][3].default_eq(&self[3][0]) && + + self[1][0].default_eq(&self[0][1]) && + self[1][2].default_eq(&self[2][1]) && + self[1][3].default_eq(&self[3][1]) && + + self[2][0].default_eq(&self[0][2]) && + self[2][1].default_eq(&self[1][2]) && + self[2][3].default_eq(&self[3][2]) && + + self[3][0].default_eq(&self[0][3]) && + self[3][1].default_eq(&self[1][3]) && + self[3][2].default_eq(&self[2][3]) + } #[inline(always)] pure fn is_invertible(&self) -> bool { @@ -1058,8 +918,39 @@ pub impl Mat4: NumericMatrixNxN Mat4: NumericMatrix4x4> { +pub impl Mat4: Matrix4> { +} + +pub impl Mat4: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat4 { + Mat4::from_cols(-self[0], -self[1], -self[2], -self[3]) + } +} + +pub impl Mat4: Dimensional> { + #[inline(always)] + static pure fn dim() -> uint { 4 } + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } +} + +pub impl Mat4: Index> { + #[inline(always)] + pure fn index(i: uint) -> Vec4 { + unsafe { do buf_as_slice( + transmute::<*Mat4, *Vec4>( + to_unsafe_ptr(&self)), 4) |slice| { slice[i] } + } + } +} + +pub impl Mat4: ToPtr { + #[inline(always)] + pure fn to_ptr(&self) -> *T { + self[0].to_ptr() + } } pub impl Mat4: Eq { diff --git a/src/test/test_gltypes.rs b/src/test/test_gltypes.rs index 5383745..4c40cff 100644 --- a/src/test/test_gltypes.rs +++ b/src/test/test_gltypes.rs @@ -34,13 +34,6 @@ fn test_vec() { assert dvec3::size_of() == dvec3::dim() * 8; assert dvec4::size_of() == dvec4::dim() * 8; - assert bvec2::dim() == 2; - assert bvec3::dim() == 3; - assert bvec4::dim() == 4; - assert bvec2::size_of() == bvec2::dim() * 1; - assert bvec3::size_of() == bvec3::dim() * 1; - assert bvec4::size_of() == bvec4::dim() * 1; - assert ivec2::identity() == ivec2::from_value(1i32); assert ivec3::identity() == ivec3::from_value(1i32); assert ivec4::identity() == ivec4::from_value(1i32); @@ -97,127 +90,86 @@ fn test_vec() { } #[test] -fn test_mat_nxn() { - assert mat2x2::identity() == mat2x2::new(1f32, 0f32, - 0f32, 1f32); - assert mat2x2::identity() == mat2x2::from_cols(vec2::new(1f32, 0f32), - vec2::new(0f32, 1f32)); - assert mat3x3::identity() == mat3x3::new(1f32, 0f32, 0f32, - 0f32, 1f32, 0f32, - 0f32, 0f32, 1f32); - assert mat3x3::identity() == mat3x3::from_cols(vec3::new(1f32, 0f32, 0f32), - vec3::new(0f32, 1f32, 0f32), - vec3::new(0f32, 0f32, 1f32)); - assert mat4x4::identity() == mat4x4::new(1f32, 0f32, 0f32, 0f32, - 0f32, 1f32, 0f32, 0f32, - 0f32, 0f32, 1f32, 0f32, - 0f32, 0f32, 0f32, 1f32); - assert mat4x4::identity() == mat4x4::from_cols(vec4::new(1f32, 0f32, 0f32, 0f32), - vec4::new(0f32, 1f32, 0f32, 0f32), - vec4::new(0f32, 0f32, 1f32, 0f32), - vec4::new(0f32, 0f32, 0f32, 1f32)); - assert mat2x2::zero() == mat2x2::from_cols(vec2::zero(), - vec2::zero()); - assert mat3x3::zero() == mat3x3::from_cols(vec3::zero(), - vec3::zero(), - vec3::zero()); - assert mat4x4::zero() == mat4x4::from_cols(vec4::zero(), - vec4::zero(), - vec4::zero(), - vec4::zero()); - assert mat2x2::dim() == 2; - assert mat3x3::dim() == 3; - assert mat4x4::dim() == 4; - assert mat2x2::rows() == 2; - assert mat3x3::rows() == 3; - assert mat4x4::rows() == 4; - assert mat2x2::cols() == 2; - assert mat3x3::cols() == 3; - assert mat4x4::cols() == 4; - assert mat2x2::size_of() == mat2x2::rows() * mat2x2::cols() * 4; - assert mat3x3::size_of() == mat3x3::rows() * mat3x3::cols() * 4; - assert mat4x4::size_of() == mat4x4::rows() * mat4x4::cols() * 4; +fn test_mat() { + assert mat2::identity() == mat2::new(1f32, 0f32, + 0f32, 1f32); + assert mat2::identity() == mat2::from_cols(vec2::new(1f32, 0f32), + vec2::new(0f32, 1f32)); + assert mat3::identity() == mat3::new(1f32, 0f32, 0f32, + 0f32, 1f32, 0f32, + 0f32, 0f32, 1f32); + assert mat3::identity() == mat3::from_cols(vec3::new(1f32, 0f32, 0f32), + vec3::new(0f32, 1f32, 0f32), + vec3::new(0f32, 0f32, 1f32)); + assert mat4::identity() == mat4::new(1f32, 0f32, 0f32, 0f32, + 0f32, 1f32, 0f32, 0f32, + 0f32, 0f32, 1f32, 0f32, + 0f32, 0f32, 0f32, 1f32); + assert mat4::identity() == mat4::from_cols(vec4::new(1f32, 0f32, 0f32, 0f32), + vec4::new(0f32, 1f32, 0f32, 0f32), + vec4::new(0f32, 0f32, 1f32, 0f32), + vec4::new(0f32, 0f32, 0f32, 1f32)); + assert mat2::zero() == mat2::from_cols(vec2::zero(), + vec2::zero()); + assert mat3::zero() == mat3::from_cols(vec3::zero(), + vec3::zero(), + vec3::zero()); + assert mat4::zero() == mat4::from_cols(vec4::zero(), + vec4::zero(), + vec4::zero(), + vec4::zero()); + assert mat2::dim() == 2; + assert mat3::dim() == 3; + assert mat4::dim() == 4; + assert mat2::rows() == 2; + assert mat3::rows() == 3; + assert mat4::rows() == 4; + assert mat2::cols() == 2; + assert mat3::cols() == 3; + assert mat4::cols() == 4; + assert mat2::size_of() == mat2::rows() * mat2::cols() * 4; + assert mat3::size_of() == mat3::rows() * mat3::cols() * 4; + assert mat4::size_of() == mat4::rows() * mat4::cols() * 4; - assert dmat2x2::identity() == dmat2x2::new(1f64, 0f64, - 0f64, 1f64); - assert dmat2x2::identity() == dmat2x2::from_cols(dvec2::new(1f64, 0f64), - dvec2::new(0f64, 1f64)); - assert dmat3x3::identity() == dmat3x3::new(1f64, 0f64, 0f64, - 0f64, 1f64, 0f64, - 0f64, 0f64, 1f64); - assert dmat3x3::identity() == dmat3x3::from_cols(dvec3::new(1f64, 0f64, 0f64), - dvec3::new(0f64, 1f64, 0f64), - dvec3::new(0f64, 0f64, 1f64)); - assert dmat4x4::identity() == dmat4x4::new(1f64, 0f64, 0f64, 0f64, - 0f64, 1f64, 0f64, 0f64, - 0f64, 0f64, 1f64, 0f64, - 0f64, 0f64, 0f64, 1f64); - assert dmat4x4::identity() == dmat4x4::from_cols(dvec4::new(1f64, 0f64, 0f64, 0f64), - dvec4::new(0f64, 1f64, 0f64, 0f64), - dvec4::new(0f64, 0f64, 1f64, 0f64), - dvec4::new(0f64, 0f64, 0f64, 1f64)); - assert dmat2x2::zero() == dmat2x2::from_cols(dvec2::zero(), - dvec2::zero()); - assert dmat3x3::zero() == dmat3x3::from_cols(dvec3::zero(), - dvec3::zero(), - dvec3::zero()); - assert dmat4x4::zero() == dmat4x4::from_cols(dvec4::zero(), - dvec4::zero(), - dvec4::zero(), - dvec4::zero()); - assert dmat2x2::dim() == 2; - assert dmat3x3::dim() == 3; - assert dmat4x4::dim() == 4; - assert dmat2x2::rows() == 2; - assert dmat3x3::rows() == 3; - assert dmat4x4::rows() == 4; - assert dmat2x2::cols() == 2; - assert dmat3x3::cols() == 3; - assert dmat4x4::cols() == 4; - assert dmat2x2::size_of() == dmat2x2::rows() * dmat2x2::cols() * 8; - assert dmat3x3::size_of() == dmat3x3::rows() * dmat3x3::cols() * 8; - assert dmat4x4::size_of() == dmat4x4::rows() * dmat4x4::cols() * 8; -} - -#[test] -fn test_mat_n() { - assert mat2::identity() == mat2x2::identity(); - assert mat3::identity() == mat3x3::identity(); - assert mat4::identity() == mat4x4::identity(); - assert mat2::zero() == mat2x2::zero(); - assert mat3::zero() == mat3x3::zero(); - assert mat4::zero() == mat4x4::zero(); - assert mat2::dim() == 2; - assert mat3::dim() == 3; - assert mat4::dim() == 4; - assert mat2::rows() == 2; - assert mat3::rows() == 3; - assert mat4::rows() == 4; - assert mat2::cols() == 2; - assert mat3::cols() == 3; - assert mat4::cols() == 4; - assert mat2::size_of() == mat2::rows() * mat2::cols() * 4; - assert mat3::size_of() == mat3::rows() * mat3::cols() * 4; - assert mat4::size_of() == mat4::rows() * mat4::cols() * 4; - - assert dmat2::identity() == dmat2x2::identity(); - assert dmat3::identity() == dmat3x3::identity(); - assert dmat4::identity() == dmat4x4::identity(); - assert dmat2::zero() == dmat2x2::zero(); - assert dmat3::zero() == dmat3x3::zero(); - assert dmat4::zero() == dmat4x4::zero(); - assert dmat2::dim() == 2; - assert dmat3::dim() == 3; - assert dmat4::dim() == 4; - assert dmat2::rows() == 2; - assert dmat3::rows() == 3; - assert dmat4::rows() == 4; - assert dmat2::cols() == 2; - assert dmat3::cols() == 3; - assert dmat4::cols() == 4; - assert dmat2::size_of() == dmat2::rows() * dmat2::cols() * 8; - assert dmat3::size_of() == dmat3::rows() * dmat3::cols() * 8; - assert dmat4::size_of() == dmat4::rows() * dmat4::cols() * 8; + assert dmat2::identity() == dmat2::new(1f64, 0f64, + 0f64, 1f64); + assert dmat2::identity() == dmat2::from_cols(dvec2::new(1f64, 0f64), + dvec2::new(0f64, 1f64)); + assert dmat3::identity() == dmat3::new(1f64, 0f64, 0f64, + 0f64, 1f64, 0f64, + 0f64, 0f64, 1f64); + assert dmat3::identity() == dmat3::from_cols(dvec3::new(1f64, 0f64, 0f64), + dvec3::new(0f64, 1f64, 0f64), + dvec3::new(0f64, 0f64, 1f64)); + assert dmat4::identity() == dmat4::new(1f64, 0f64, 0f64, 0f64, + 0f64, 1f64, 0f64, 0f64, + 0f64, 0f64, 1f64, 0f64, + 0f64, 0f64, 0f64, 1f64); + assert dmat4::identity() == dmat4::from_cols(dvec4::new(1f64, 0f64, 0f64, 0f64), + dvec4::new(0f64, 1f64, 0f64, 0f64), + dvec4::new(0f64, 0f64, 1f64, 0f64), + dvec4::new(0f64, 0f64, 0f64, 1f64)); + assert dmat2::zero() == dmat2::from_cols(dvec2::zero(), + dvec2::zero()); + assert dmat3::zero() == dmat3::from_cols(dvec3::zero(), + dvec3::zero(), + dvec3::zero()); + assert dmat4::zero() == dmat4::from_cols(dvec4::zero(), + dvec4::zero(), + dvec4::zero(), + dvec4::zero()); + assert dmat2::dim() == 2; + assert dmat3::dim() == 3; + assert dmat4::dim() == 4; + assert dmat2::rows() == 2; + assert dmat3::rows() == 3; + assert dmat4::rows() == 4; + assert dmat2::cols() == 2; + assert dmat3::cols() == 3; + assert dmat4::cols() == 4; + assert dmat2::size_of() == dmat2::rows() * dmat2::cols() * 8; + assert dmat3::size_of() == dmat3::rows() * dmat3::cols() * 8; + assert dmat4::size_of() == dmat4::rows() * dmat4::cols() * 8; } #[test]