Added trace function.

This commit is contained in:
Luqman Aden 2012-11-21 16:27:44 -05:00
parent 118fcdf7e3
commit a4c2bacc46
2 changed files with 18 additions and 2 deletions

View file

@ -100,6 +100,7 @@ pub trait NumericMatrixNxN<T, ColRow>: MatrixNxN<T, ColRow>,
pure fn mul_m(other: &self) -> self;
pure fn det() -> T;
pure fn trace() -> T;
pure fn invert() -> Option<self>;
pure fn transpose() -> self;
@ -266,6 +267,10 @@ pub impl<T:Copy Num NumCast DefaultEq> Mat2<T>: NumericMatrixNxN<T, Vec2<T>> {
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<Mat2<T>> {
let _0 = cast(0);
@ -508,6 +513,10 @@ pub impl<T:Copy Num NumCast DefaultEq> Mat3<T>: NumericMatrixNxN<T, Vec3<T>> {
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<Mat3<T>> {
let d = self.det();
@ -566,14 +575,14 @@ pub impl<T:Copy NumCast> Mat3<T>: NumericMatrix3x3<T, Vec3<T>> {
}
}
pub impl<T:Copy Num NumCast Ord> Mat3<T>: ToQuat<T> {
pub impl<T:Copy Num NumCast Ord DefaultEq> Mat3<T>: ToQuat<T> {
pure fn to_Quat() -> Quat<T> {
// 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<T:Copy Num NumCast DefaultEq Signed Ord> Mat4<T>: NumericMatrixNxN<T, V
self[0][3], self[1][3], self[2][3]).det()
}
pure fn trace() -> T {
self[0][0] + self[1][1] + self[2][2] + self[3][3]
}
pure fn invert() -> Option<Mat4<T>> {
let d = self.det();
let _0 = cast(0);

View file

@ -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,