Add more matrix constructors and corresponding tests

This commit is contained in:
Brendan Zabarauskas 2012-11-04 16:27:41 +10:00
parent f3183dd0f1
commit b245244b7d
2 changed files with 73 additions and 15 deletions

View file

@ -36,6 +36,14 @@ pub trait Matrix<T, V> {
pure fn is_rotated() -> bool;
}
//
// 2x2 Matrix
//
pub trait Matrix2<T> {
pure fn to_Mat3() -> Mat3<T>;
pure fn to_Mat4() -> Mat4<T>;
}
//
// 3x3 Matrix
//
@ -185,6 +193,18 @@ pub impl<T:Copy Num NumCast Sqrt FuzzyEq> Mat2<T>: Matrix<T, Vec2<T>> {
}
}
pub impl<T:Copy Num NumCast Sqrt FuzzyEq> Mat2<T>: Matrix2<T> {
#[inline(always)]
pure fn to_Mat3() -> Mat3<T> {
Mat3::from_Mat2(&self)
}
#[inline(always)]
pure fn to_Mat4() -> Mat4<T> {
Mat4::from_Mat2(&self)
}
}
pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
#[inline(always)]
pure fn index(i: uint) -> Vec2<T> {
@ -267,6 +287,14 @@ pub mod Mat3 {
_0, _0, value)
}
#[inline(always)]
pub pure fn from_Mat2<T:Copy NumCast>(m: &Mat2<T>) -> Mat3<T> {
let _0 = cast(0);
Mat3::new(m[0][0], m[0][1], _0,
m[1][0], m[1][1], _0,
_0, _0, cast(1))
}
#[inline(always)]
pub pure fn zero<T:Copy NumCast>() -> Mat3<T> {
let _0 = cast(0);
@ -547,6 +575,16 @@ pub mod Mat4 {
_0, _0, _0, value)
}
#[inline(always)]
pub pure fn from_Mat2<T:Copy NumCast>(m: &Mat2<T>) -> Mat4<T> {
let _0 = cast(0);
let _1 = cast(1);
Mat4::new(m[0][0], m[0][1], _0, _0,
m[1][0], m[1][1], _0, _0,
_0, _0, _1, _0,
_0, _0, _0, _1)
}
#[inline(always)]
pub pure fn from_Mat3<T:Copy NumCast>(m: &Mat3<T>) -> Mat4<T> {
let _0 = cast(0);

View file

@ -70,6 +70,15 @@ fn test_Mat2() {
assert c.is_rotated();
assert Mat2::from_value(6f).is_diagonal();
assert a.to_Mat3() == Mat3::new(1f, 3f, 0f,
2f, 4f, 0f,
0f, 0f, 1f);
assert a.to_Mat4() == Mat4::new(1f, 3f, 0f, 0f,
2f, 4f, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f);
}
#[test]
@ -91,10 +100,15 @@ fn test_Mat3() {
Vec3::new(2f, 5f, 8f),
Vec3::new(3f, 6f, 9f));
assert Mat3::from_value::<f64>(4f64) == Mat3::new::<f64>(4f64, 0f64, 0f64,
assert Mat3::from_value(4f64) == Mat3::new(4f64, 0f64, 0f64,
0f64, 4f64, 0f64,
0f64, 0f64, 4f64);
assert Mat3::from_Mat2(&Mat2::new(1f32, 3f32,
2f32, 4f32)) == Mat3::new(1f32, 3f32, 0f32,
2f32, 4f32, 0f32,
0f32, 0f32, 1f32);
assert a[0] == Vec3::new(1f, 4f, 7f);
assert a[1] == Vec3::new(2f, 5f, 8f);
assert a[2] == Vec3::new(3f, 6f, 9f);
@ -153,13 +167,13 @@ fn test_Mat3() {
assert !c.is_diagonal();
assert c.is_rotated();
assert Mat3::from_value(6f).is_diagonal();
assert a.to_Mat4() == Mat4::new(1f, 4f, 7f, 0f,
2f, 5f, 8f, 0f,
3f, 6f, 9f, 0f,
0f, 0f, 0f, 1f);
assert Mat3::from_value(6f).is_diagonal();
// to_Quaternion
}
@ -186,14 +200,20 @@ fn test_Mat4() {
Vec4::new(3f, 7f, 11f, 15f),
Vec4::new(4f, 8f, 12f, 16f));
assert Mat4::from_value::<f64>(4f64) == Mat4::new::<f64>(4f64, 0f64, 0f64, 0f64,
assert Mat4::from_value(4f64) == Mat4::new(4f64, 0f64, 0f64, 0f64,
0f64, 4f64, 0f64, 0f64,
0f64, 0f64, 4f64, 0f64,
0f64, 0f64, 0f64, 4f64);
assert Mat4::from_Mat3::<f32>(&Mat3::new(1f32, 4f32, 7f32,
assert Mat4::from_Mat2(&Mat2::new(1f, 3f,
2f, 4f)) == Mat4::new(1f, 3f, 0f, 0f,
2f, 4f, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f);
assert Mat4::from_Mat3(&Mat3::new(1f32, 4f32, 7f32,
2f32, 5f32, 8f32,
3f32, 6f32, 9f32)) == Mat4::new::<f32>(1f32, 4f32, 7f32, 0f32,
3f32, 6f32, 9f32)) == Mat4::new(1f32, 4f32, 7f32, 0f32,
2f32, 5f32, 8f32, 0f32,
3f32, 6f32, 9f32, 0f32,
0f32, 0f32, 0f32, 1f32);