From a4c2bacc46a87bd2b552f0585bb3b642585584a3 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Wed, 21 Nov 2012 16:27:44 -0500 Subject: [PATCH 1/2] Added trace function. --- src/mat.rs | 17 +++++++++++++++-- src/test/test_mat.rs | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index 0d4112f..6fcd9ac 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -100,6 +100,7 @@ pub trait NumericMatrixNxN: MatrixNxN, pure fn mul_m(other: &self) -> self; pure fn det() -> T; + pure fn trace() -> T; pure fn invert() -> Option; pure fn transpose() -> self; @@ -266,6 +267,10 @@ pub impl Mat2: NumericMatrixNxN> { self[0][0] * self[1][1] - self[1][0] * self[0][1] } + pure fn trace() -> T { + self[0][0] + self[1][1] + } + #[inline(always)] pure fn invert() -> Option> { let _0 = cast(0); @@ -508,6 +513,10 @@ pub impl Mat3: NumericMatrixNxN> { self.col(0).dot(&self.col(1).cross(&self.col(2))) } + pure fn trace() -> T { + self[0][0] + self[1][1] + self[2][2] + } + // #[inline(always)] pure fn invert() -> Option> { let d = self.det(); @@ -566,14 +575,14 @@ pub impl Mat3: NumericMatrix3x3> { } } -pub impl Mat3: ToQuat { +pub impl Mat3: ToQuat { pure fn to_Quat() -> 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: float; let w: float, x: float, y: float, z: float; - let trace: float = cast(self[0][0] + self[1][1] + self[2][2]); + let trace: float = cast(self.trace()); if trace >= cast(0) { s = (trace + 1f).sqrt(); @@ -843,6 +852,10 @@ pub impl Mat4: NumericMatrixNxN T { + self[0][0] + self[1][1] + self[2][2] + self[3][3] + } + pure fn invert() -> Option> { let d = self.det(); let _0 = cast(0); diff --git a/src/test/test_mat.rs b/src/test/test_mat.rs index 36b4d23..1892618 100644 --- a/src/test/test_mat.rs +++ b/src/test/test_mat.rs @@ -31,6 +31,7 @@ fn test_Mat2() { assert a.col(1) == Vec2::new(2f, 4f); assert a.det() == -2f; + assert a.trace() == 5f; assert a.neg() == Mat2::new(-1f, -3f, -2f, -4f); @@ -135,6 +136,7 @@ fn test_Mat3() { assert a.col(2) == Vec3::new(3f, 6f, 9f); assert a.det() == 0f; + assert a.trace() == 15f; assert a.neg() == Mat3::new(-1f, -4f, -7f, -2f, -5f, -8f, @@ -269,6 +271,7 @@ fn test_Mat4() { assert a.col(3) == Vec4::new(4f, 8f, 12f, 16f); assert a.det() == 0f; + assert a.trace() == 34f; assert a.neg() == Mat4::new(-1f, -5f, -9f, -13f, -2f, -6f, -10f, -14f, From ad27c97d76bda5132724e06e3e57a70bee6ed211 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Wed, 21 Nov 2012 18:24:43 -0500 Subject: [PATCH 2/2] Added standard inner (dot) product for matricies. --- src/mat.rs | 13 +++++++++++++ src/test/test_mat.rs | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/mat.rs b/src/mat.rs index 6fcd9ac..644cf35 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -98,6 +98,7 @@ pub trait NumericMatrixNxN: MatrixNxN, static pure fn identity() -> self; pure fn mul_m(other: &self) -> self; + pure fn dot(other: &self) -> T; pure fn det() -> T; pure fn trace() -> T; @@ -262,6 +263,10 @@ pub impl Mat2: NumericMatrixNxN> { Mat2::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1))) } + + pure fn dot(other: &Mat2) -> T { + other.transpose().mul_m(&self).trace() + } pure fn det() -> T { self[0][0] * self[1][1] - self[1][0] * self[0][1] @@ -509,6 +514,10 @@ pub impl Mat3: NumericMatrixNxN> { self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2))) } + pure fn dot(other: &Mat3) -> T { + other.transpose().mul_m(&self).trace() + } + pure fn det() -> T { self.col(0).dot(&self.col(1).cross(&self.col(2))) } @@ -837,6 +846,10 @@ pub impl Mat4: NumericMatrixNxN) -> T { + other.transpose().mul_m(&self).trace() + } + pure fn det() -> T { self[0][0]*Mat3::new(self[1][1], self[2][1], self[3][1], self[1][2], self[2][2], self[3][2], diff --git a/src/test/test_mat.rs b/src/test/test_mat.rs index 1892618..9a1abae 100644 --- a/src/test/test_mat.rs +++ b/src/test/test_mat.rs @@ -47,6 +47,7 @@ fn test_Mat2() { -1f, -1f); assert a.mul_m(&b) == Mat2::new(10.0, 22.0, 13.0, 29.0); + assert a.dot(&b) == 40f; assert a.transpose() == Mat2::new(1f, 2f, 3f, 4f); @@ -157,6 +158,7 @@ fn test_Mat3() { assert a.mul_m(&b) == Mat3::new(36f, 81f, 126f, 42f, 96f, 150f, 48f, 111f, 174f); + assert a.dot(&b) == 330f; assert a.transpose() == Mat3::new(1f, 2f, 3f, 4f, 5f, 6f, @@ -297,6 +299,7 @@ fn test_Mat4() { 110f, 254f, 398f, 542f, 120f, 280f, 440f, 600f, 130f, 306f, 482f, 658f); + assert a.dot(&b) == 1632f; assert a.transpose() == Mat4::new( 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f,