Switch matrix zero and identity functions to static methods
This commit is contained in:
parent
a06d2cff54
commit
0482e268ae
2 changed files with 32 additions and 78 deletions
65
src/mat.rs
65
src/mat.rs
|
@ -94,21 +94,6 @@ pub mod Mat2 {
|
|||
Mat2::new(value, _0,
|
||||
_0, value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn zero<T:Copy NumCast>() -> Mat2<T> {
|
||||
let _0 = cast(0);
|
||||
Mat2::new(_0, _0,
|
||||
_0, _0)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn identity<T:Copy NumCast>() -> Mat2<T> {
|
||||
let _0 = cast(0);
|
||||
let _1 = cast(1);
|
||||
Mat2::new(_1, _0,
|
||||
_0, _1)
|
||||
}
|
||||
}
|
||||
|
||||
pub impl<T:Copy> Mat2<T>: Matrix<T, Vec2<T>, Vec2<T>> {
|
||||
|
@ -224,7 +209,7 @@ pub impl<T:Copy Num NumCast DefaultEq> Mat2<T>: NumericMatrix_NxN<T, Vec2<T>> {
|
|||
|
||||
#[inline(always)]
|
||||
pure fn is_identity() -> bool {
|
||||
self.default_eq(&Mat2::identity())
|
||||
self.default_eq(&NumericMatrix_NxN::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -242,7 +227,7 @@ pub impl<T:Copy Num NumCast DefaultEq> Mat2<T>: NumericMatrix_NxN<T, Vec2<T>> {
|
|||
|
||||
#[inline(always)]
|
||||
pure fn is_rotated() -> bool {
|
||||
!self.default_eq(&Mat2::identity())
|
||||
!self.default_eq(&NumericMatrix_NxN::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -336,23 +321,6 @@ pub mod Mat3 {
|
|||
m[1][0], m[1][1], _0,
|
||||
_0, _0, _1)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn zero<T:Copy NumCast>() -> Mat3<T> {
|
||||
let _0 = cast(0);
|
||||
Mat3::new(_0, _0, _0,
|
||||
_0, _0, _0,
|
||||
_0, _0, _0)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn identity<T:Copy NumCast>() -> Mat3<T> {
|
||||
let _0 = cast(0);
|
||||
let _1 = cast(1);
|
||||
Mat3::new(_1, _0, _0,
|
||||
_0, _1, _0,
|
||||
_0, _0, _1)
|
||||
}
|
||||
}
|
||||
|
||||
pub impl<T:Copy> Mat3<T>: Matrix<T, Vec3<T>, Vec3<T>> {
|
||||
|
@ -479,7 +447,7 @@ pub impl<T:Copy Num NumCast DefaultEq> Mat3<T>: NumericMatrix_NxN<T, Vec3<T>> {
|
|||
|
||||
#[inline(always)]
|
||||
pure fn is_identity() -> bool {
|
||||
self.default_eq(&Mat3::identity())
|
||||
self.default_eq(&NumericMatrix_NxN::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -509,7 +477,7 @@ pub impl<T:Copy Num NumCast DefaultEq> Mat3<T>: NumericMatrix_NxN<T, Vec3<T>> {
|
|||
|
||||
#[inline(always)]
|
||||
pure fn is_rotated() -> bool {
|
||||
!self.default_eq(&Mat3::identity())
|
||||
!self.default_eq(&NumericMatrix_NxN::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -658,25 +626,6 @@ pub mod Mat4 {
|
|||
m[2][0], m[2][1], m[2][2], _0,
|
||||
_0, _0, _0, _1)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn zero<T:Copy NumCast>() -> Mat4<T> {
|
||||
let _0 = cast(0);
|
||||
Mat4::new(_0, _0, _0, _0,
|
||||
_0, _0, _0, _0,
|
||||
_0, _0, _0, _0,
|
||||
_0, _0, _0, _0)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn identity<T:Copy NumCast>() -> Mat4<T> {
|
||||
let _0 = cast(0);
|
||||
let _1 = cast(1);
|
||||
Mat4::new(_1, _0, _0, _0,
|
||||
_0, _1, _0, _0,
|
||||
_0, _0, _1, _0,
|
||||
_0, _0, _0, _1)
|
||||
}
|
||||
}
|
||||
|
||||
pub impl<T:Copy> Mat4<T>: Matrix<T, Vec4<T>, Vec4<T>> {
|
||||
|
@ -812,7 +761,7 @@ pub impl<T:Copy Num NumCast DefaultEq Signed Ord> Mat4<T>: NumericMatrix_NxN<T,
|
|||
// Gauss Jordan Elimination with partial pivoting
|
||||
|
||||
let mut a = self.transpose();
|
||||
let mut inv = Mat4::identity::<T>();
|
||||
let mut inv: Mat4<T> = NumericMatrix_NxN::identity();
|
||||
|
||||
// Find largest pivot column j among rows j..3
|
||||
for uint::range(0, 4) |j| {
|
||||
|
@ -868,7 +817,7 @@ pub impl<T:Copy Num NumCast DefaultEq Signed Ord> Mat4<T>: NumericMatrix_NxN<T,
|
|||
|
||||
#[inline(always)]
|
||||
pure fn is_identity() -> bool {
|
||||
self.default_eq(&Mat4::identity())
|
||||
self.default_eq(&NumericMatrix_NxN::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -912,7 +861,7 @@ pub impl<T:Copy Num NumCast DefaultEq Signed Ord> Mat4<T>: NumericMatrix_NxN<T,
|
|||
|
||||
#[inline(always)]
|
||||
pure fn is_rotated() -> bool {
|
||||
!self.default_eq(&Mat4::identity())
|
||||
!self.default_eq(&NumericMatrix_NxN::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
|
@ -60,11 +60,13 @@ fn test_Mat2() {
|
|||
// fuzzy_eq
|
||||
// eq
|
||||
|
||||
assert Mat2::identity::<float>().is_identity();
|
||||
assert Mat2::identity::<float>().is_symmetric();
|
||||
assert Mat2::identity::<float>().is_diagonal();
|
||||
assert !Mat2::identity::<float>().is_rotated();
|
||||
assert Mat2::identity::<float>().is_invertible();
|
||||
let ident: Mat2<float> = NumericMatrix_NxN::identity();
|
||||
|
||||
assert ident.is_identity();
|
||||
assert ident.is_symmetric();
|
||||
assert ident.is_diagonal();
|
||||
assert !ident.is_rotated();
|
||||
assert ident.is_invertible();
|
||||
|
||||
assert !a.is_identity();
|
||||
assert !a.is_symmetric();
|
||||
|
@ -159,9 +161,6 @@ fn test_Mat3() {
|
|||
7f, 8f, 9f);
|
||||
|
||||
assert a.invert().is_none();
|
||||
|
||||
assert option::unwrap(Mat3::identity::<float>().invert())
|
||||
== Mat3::identity::<float>();
|
||||
|
||||
assert option::unwrap(Mat3::new(2f, 4f, 6f,
|
||||
0f, 2f, 4f,
|
||||
|
@ -169,15 +168,20 @@ fn test_Mat3() {
|
|||
== Mat3::new(0.5f, -1f, 1f,
|
||||
0f, 0.5f, -2f,
|
||||
0f, 0f, 1f);
|
||||
|
||||
let ident: Mat3<float> = NumericMatrix_NxN::identity();
|
||||
|
||||
assert option::unwrap(ident.invert()) == ident;
|
||||
|
||||
// exact_eq
|
||||
// fuzzy_eq
|
||||
// eq
|
||||
|
||||
assert Mat3::identity::<float>().is_identity();
|
||||
assert Mat3::identity::<float>().is_symmetric();
|
||||
assert Mat3::identity::<float>().is_diagonal();
|
||||
assert !Mat3::identity::<float>().is_rotated();
|
||||
assert Mat3::identity::<float>().is_invertible();
|
||||
assert ident.is_identity();
|
||||
assert ident.is_symmetric();
|
||||
assert ident.is_diagonal();
|
||||
assert !ident.is_rotated();
|
||||
assert ident.is_invertible();
|
||||
|
||||
assert !a.is_identity();
|
||||
assert !a.is_symmetric();
|
||||
|
@ -301,19 +305,20 @@ fn test_Mat4() {
|
|||
-4f, 8f, -4f, 0f,
|
||||
4f, -8f, 4f, 8f,
|
||||
-3f, 4f, 1f, -8f).mul_t(0.125f);
|
||||
|
||||
let ident: Mat4<float> = NumericMatrix_NxN::identity();
|
||||
|
||||
assert option::unwrap(Mat4::identity::<float>().invert())
|
||||
== Mat4::identity::<float>();
|
||||
assert option::unwrap(ident.invert()) == ident;
|
||||
|
||||
// exact_eq
|
||||
// fuzzy_eq
|
||||
// eq
|
||||
|
||||
assert Mat4::identity::<float>().is_identity();
|
||||
assert Mat4::identity::<float>().is_symmetric();
|
||||
assert Mat4::identity::<float>().is_diagonal();
|
||||
assert !Mat4::identity::<float>().is_rotated();
|
||||
assert Mat4::identity::<float>().is_invertible();
|
||||
assert ident.is_identity();
|
||||
assert ident.is_symmetric();
|
||||
assert ident.is_diagonal();
|
||||
assert !ident.is_rotated();
|
||||
assert ident.is_invertible();
|
||||
|
||||
assert !a.is_identity();
|
||||
assert !a.is_symmetric();
|
||||
|
|
Loading…
Reference in a new issue