From 3874cccde8bea8366e27fee1bdf018c4ce826552 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 21 Nov 2012 18:35:26 +1000 Subject: [PATCH] Further refine the organisation of the matrix traits --- src/mat.rs | 120 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 48 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index ec8f96c..4fc55b6 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -44,15 +44,24 @@ pub trait Matrix4: Matrix { // 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() -> bool; +} + /// A 2 x 2 square matrix -pub trait Matrix2x2: Matrix2 { +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: Matrix3 { +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, @@ -60,7 +69,8 @@ pub trait Matrix3x3: Matrix3 { } /// A 4 x 4 square matrix -pub trait Matrix4x4: Matrix4 { +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, @@ -83,7 +93,8 @@ pub trait NumericMatrix: Matrix, Neg { /// /// A square matrix with numeric elements /// -pub trait NumericMatrixNxN: NumericMatrix { +pub trait NumericMatrixNxN: MatrixNxN, + NumericMatrix { static pure fn identity() -> self; pure fn mul_m(other: &self) -> self; @@ -100,19 +111,26 @@ pub trait NumericMatrixNxN: NumericMatrix { } /// A 2 x 2 square matrix with numeric elements -pub trait NumericMatrix2x2: NumericMatrixNxN> { +pub trait NumericMatrix2x2: Matrix2x2, + NumericMatrixNxN { + // static pure fn from_value(value: T) -> self; + pure fn to_Mat3() -> Mat3; pure fn to_Mat4() -> Mat4; } /// A 3 x 3 square matrix with numeric elements -pub trait NumericMatrix3x3: NumericMatrixNxN> { +pub trait NumericMatrix3x3: Matrix3x3, + NumericMatrixNxN { + // static pure fn from_value(value: T) -> self; + pure fn to_Mat4() -> Mat4; } /// A 4 x 4 square matrix with numeric elements -pub trait NumericMatrix4x4: NumericMatrixNxN> { - +pub trait NumericMatrix4x4: Matrix4x4, + NumericMatrixNxN { + // static pure fn from_value(value: T) -> self; } @@ -186,6 +204,14 @@ pub impl Mat2: Matrix, Vec2> { } } +pub impl Mat2: MatrixNxN> { + #[inline(always)] + pure fn is_symmetric() -> 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 { @@ -263,12 +289,6 @@ pub impl Mat2: NumericMatrixNxN> { self.default_eq(&NumericMatrixNxN::identity()) } - #[inline(always)] - pure fn is_symmetric() -> bool { - self[0][1].default_eq(&self[1][0]) && - self[1][0].default_eq(&self[0][1]) - } - #[inline(always)] pure fn is_diagonal() -> bool { let _0 = cast(0); @@ -288,7 +308,7 @@ pub impl Mat2: NumericMatrixNxN> { } } -pub impl Mat2: NumericMatrix2x2 { +pub impl Mat2: NumericMatrix2x2> { #[inline(always)] pure fn to_Mat3() -> Mat3 { Mat3::from_Mat2(&self) @@ -413,6 +433,20 @@ pub impl Mat3: Matrix, Vec3> { } } +pub impl Mat3: MatrixNxN> { + #[inline(always)] + pure fn is_symmetric() -> 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 { @@ -500,18 +534,6 @@ pub impl Mat3: NumericMatrixNxN> { self.default_eq(&NumericMatrixNxN::identity()) } - #[inline(always)] - pure fn is_symmetric() -> 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_diagonal() -> bool { let _0 = cast(0); @@ -537,7 +559,7 @@ pub impl Mat3: NumericMatrixNxN> { } } -pub impl Mat3: NumericMatrix3x3 { +pub impl Mat3: NumericMatrix3x3> { #[inline(always)] pure fn to_Mat4() -> Mat4 { Mat4::from_Mat3(&self) @@ -718,6 +740,27 @@ pub impl Mat4: Matrix, Vec4> { } } +pub impl Mat4: MatrixNxN> { + #[inline(always)] + pure fn is_symmetric() -> 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 { @@ -869,25 +912,6 @@ pub impl Mat4: NumericMatrixNxN 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_diagonal() -> bool { let _0 = cast(0); @@ -920,7 +944,7 @@ pub impl Mat4: NumericMatrixNxN Mat4: NumericMatrix4x4 { +pub impl Mat4: NumericMatrix4x4> { }