diff --git a/src/matrix.rs b/src/matrix.rs index 629ed27..e09f534 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -39,19 +39,24 @@ type dmat3x3 = Mat3; /// same as a `dmat3` // type dmat4x3 = /// a double-precision floating-point matrix with 4 columns and 3 rows type dmat4x4 = Mat4; /// same as a `dmat4` -// -// NxN Matrix -// -pub trait Matrix { + +pub trait Matrix { pure fn rows() -> uint; pure fn cols() -> uint; pure fn is_col_major() -> bool; + pure fn is_square() -> bool; - pure fn col(i: uint) -> V; - pure fn row(i: uint) -> V; + pure fn col(i: uint) -> ColV; + pure fn row(i: uint) -> RowV; pure fn mul_t(value: T) -> self; - pure fn mul_v(other: &V) -> V; + pure fn mul_v(other: &ColV) -> ColV; +} + +// +// NxN Matrix +// +pub trait SquareMatrix { pure fn add_m(other: &self) -> self; pure fn sub_m(other: &self) -> self; pure fn mul_m(other: &self) -> self; @@ -135,7 +140,7 @@ pub mod Mat2 { } } -pub impl Mat2: Matrix> { +pub impl Mat2: Matrix, Vec2> { #[inline(always)] pure fn rows() -> uint { 2 } @@ -145,6 +150,9 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn is_col_major() -> bool { true } + #[inline(always)] + pure fn is_square() -> bool { true } + #[inline(always)] pure fn col(i: uint) -> Vec2 { self[i] } @@ -165,7 +173,9 @@ pub impl Mat2: Matrix> { Vec2::new(self[0][0] * other[0] + self[1][0] * other[1], self[0][1] * other[0] + self[1][1] * other[1]) } - +} + +pub impl Mat2: SquareMatrix> { #[inline(always)] pure fn add_m(other: &Mat2) -> Mat2 { Mat2::from_cols(self[0].add_v(&other[0]), @@ -342,7 +352,7 @@ pub mod Mat3 { } } -pub impl Mat3: Matrix> { +pub impl Mat3: Matrix, Vec3> { #[inline(always)] pure fn rows() -> uint { 3 } @@ -352,6 +362,9 @@ pub impl Mat3: Matrix> { #[inline(always)] pure fn is_col_major() -> bool { true } + #[inline(always)] + pure fn is_square() -> bool { true } + #[inline(always)] pure fn col(i: uint) -> Vec3 { self[i] } @@ -375,7 +388,9 @@ pub impl Mat3: Matrix> { self[0][1] * other[0] + self[1][1] * other[1] + self[2][1] * other[2], self[0][2] * other[0] + self[1][2] * other[1] + self[2][2] * other[2]) } - +} + +pub impl Mat3: SquareMatrix> { #[inline(always)] pure fn add_m(other: &Mat3) -> Mat3 { Mat3::from_cols(self[0].add_v(&other[0]), @@ -637,7 +652,7 @@ pub mod Mat4 { } } -pub impl Mat4: Matrix> { +pub impl Mat4: Matrix, Vec4> { #[inline(always)] pure fn rows() -> uint { 4 } @@ -647,6 +662,9 @@ pub impl Mat4: Matrix> { #[inline(always)] pure fn is_col_major() -> bool { true } + #[inline(always)] + pure fn is_square() -> bool { true } + #[inline(always)] pure fn col(i: uint) -> Vec4 { self[i] } @@ -673,7 +691,9 @@ pub impl Mat4: Matrix> { self[0][2] * other[0] + self[1][2] * other[1] + self[2][2] * other[2] + self[3][2] * other[3], self[0][3] * other[0] + self[1][3] * other[1] + self[2][3] * other[2] + self[3][3] * other[3]) } - +} + +pub impl Mat4: SquareMatrix> { #[inline(always)] pure fn add_m(other: &Mat4) -> Mat4 { Mat4::from_cols(self[0].add_v(&other[0]),