Add more matrix constructors and corresponding tests
This commit is contained in:
parent
f3183dd0f1
commit
b245244b7d
2 changed files with 73 additions and 15 deletions
|
@ -36,6 +36,14 @@ pub trait Matrix<T, V> {
|
||||||
pure fn is_rotated() -> bool;
|
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
|
// 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>> {
|
pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pure fn index(i: uint) -> Vec2<T> {
|
pure fn index(i: uint) -> Vec2<T> {
|
||||||
|
@ -267,6 +287,14 @@ pub mod Mat3 {
|
||||||
_0, _0, value)
|
_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)]
|
#[inline(always)]
|
||||||
pub pure fn zero<T:Copy NumCast>() -> Mat3<T> {
|
pub pure fn zero<T:Copy NumCast>() -> Mat3<T> {
|
||||||
let _0 = cast(0);
|
let _0 = cast(0);
|
||||||
|
@ -547,6 +575,16 @@ pub mod Mat4 {
|
||||||
_0, _0, _0, value)
|
_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)]
|
#[inline(always)]
|
||||||
pub pure fn from_Mat3<T:Copy NumCast>(m: &Mat3<T>) -> Mat4<T> {
|
pub pure fn from_Mat3<T:Copy NumCast>(m: &Mat3<T>) -> Mat4<T> {
|
||||||
let _0 = cast(0);
|
let _0 = cast(0);
|
||||||
|
|
|
@ -70,6 +70,15 @@ fn test_Mat2() {
|
||||||
assert c.is_rotated();
|
assert c.is_rotated();
|
||||||
|
|
||||||
assert Mat2::from_value(6f).is_diagonal();
|
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]
|
#[test]
|
||||||
|
@ -91,9 +100,14 @@ fn test_Mat3() {
|
||||||
Vec3::new(2f, 5f, 8f),
|
Vec3::new(2f, 5f, 8f),
|
||||||
Vec3::new(3f, 6f, 9f));
|
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, 4f64, 0f64,
|
||||||
0f64, 0f64, 4f64);
|
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[0] == Vec3::new(1f, 4f, 7f);
|
||||||
assert a[1] == Vec3::new(2f, 5f, 8f);
|
assert a[1] == Vec3::new(2f, 5f, 8f);
|
||||||
|
@ -153,13 +167,13 @@ fn test_Mat3() {
|
||||||
assert !c.is_diagonal();
|
assert !c.is_diagonal();
|
||||||
assert c.is_rotated();
|
assert c.is_rotated();
|
||||||
|
|
||||||
|
assert Mat3::from_value(6f).is_diagonal();
|
||||||
|
|
||||||
assert a.to_Mat4() == Mat4::new(1f, 4f, 7f, 0f,
|
assert a.to_Mat4() == Mat4::new(1f, 4f, 7f, 0f,
|
||||||
2f, 5f, 8f, 0f,
|
2f, 5f, 8f, 0f,
|
||||||
3f, 6f, 9f, 0f,
|
3f, 6f, 9f, 0f,
|
||||||
0f, 0f, 0f, 1f);
|
0f, 0f, 0f, 1f);
|
||||||
|
|
||||||
assert Mat3::from_value(6f).is_diagonal();
|
|
||||||
|
|
||||||
// to_Quaternion
|
// to_Quaternion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,17 +200,23 @@ fn test_Mat4() {
|
||||||
Vec4::new(3f, 7f, 11f, 15f),
|
Vec4::new(3f, 7f, 11f, 15f),
|
||||||
Vec4::new(4f, 8f, 12f, 16f));
|
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, 4f64, 0f64, 0f64,
|
||||||
0f64, 0f64, 4f64, 0f64,
|
0f64, 0f64, 4f64, 0f64,
|
||||||
0f64, 0f64, 0f64, 4f64);
|
0f64, 0f64, 0f64, 4f64);
|
||||||
|
|
||||||
assert Mat4::from_Mat3::<f32>(&Mat3::new(1f32, 4f32, 7f32,
|
assert Mat4::from_Mat2(&Mat2::new(1f, 3f,
|
||||||
2f32, 5f32, 8f32,
|
2f, 4f)) == Mat4::new(1f, 3f, 0f, 0f,
|
||||||
3f32, 6f32, 9f32)) == Mat4::new::<f32>(1f32, 4f32, 7f32, 0f32,
|
2f, 4f, 0f, 0f,
|
||||||
2f32, 5f32, 8f32, 0f32,
|
0f, 0f, 1f, 0f,
|
||||||
3f32, 6f32, 9f32, 0f32,
|
0f, 0f, 0f, 1f);
|
||||||
0f32, 0f32, 0f32, 1f32);
|
|
||||||
|
assert Mat4::from_Mat3(&Mat3::new(1f32, 4f32, 7f32,
|
||||||
|
2f32, 5f32, 8f32,
|
||||||
|
3f32, 6f32, 9f32)) == Mat4::new(1f32, 4f32, 7f32, 0f32,
|
||||||
|
2f32, 5f32, 8f32, 0f32,
|
||||||
|
3f32, 6f32, 9f32, 0f32,
|
||||||
|
0f32, 0f32, 0f32, 1f32);
|
||||||
|
|
||||||
assert a[0] == Vec4::new(1f, 5f, 9f, 13f);
|
assert a[0] == Vec4::new(1f, 5f, 9f, 13f);
|
||||||
assert a[1] == Vec4::new(2f, 6f, 10f, 14f);
|
assert a[1] == Vec4::new(2f, 6f, 10f, 14f);
|
||||||
|
|
Loading…
Reference in a new issue