diff --git a/src/matrix.rs b/src/matrix.rs index 510c369..d471964 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -36,6 +36,14 @@ pub trait Matrix { pure fn is_rotated() -> bool; } +// +// 2x2 Matrix +// +pub trait Matrix2 { + pure fn to_Mat3() -> Mat3; + pure fn to_Mat4() -> Mat4; +} + // // 3x3 Matrix // @@ -185,6 +193,18 @@ pub impl Mat2: Matrix> { } } +pub impl Mat2: Matrix2 { + #[inline(always)] + pure fn to_Mat3() -> Mat3 { + Mat3::from_Mat2(&self) + } + + #[inline(always)] + pure fn to_Mat4() -> Mat4 { + Mat4::from_Mat2(&self) + } +} + pub impl Mat2: Index> { #[inline(always)] pure fn index(i: uint) -> Vec2 { @@ -267,6 +287,14 @@ pub mod Mat3 { _0, _0, value) } + #[inline(always)] + pub pure fn from_Mat2(m: &Mat2) -> Mat3 { + 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() -> Mat3 { let _0 = cast(0); @@ -547,6 +575,16 @@ pub mod Mat4 { _0, _0, _0, value) } + #[inline(always)] + pub pure fn from_Mat2(m: &Mat2) -> Mat4 { + 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(m: &Mat3) -> Mat4 { let _0 = cast(0); diff --git a/src/test/test_matrix.rs b/src/test/test_matrix.rs index aa3f227..38bdacc 100644 --- a/src/test/test_matrix.rs +++ b/src/test/test_matrix.rs @@ -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,9 +100,14 @@ fn test_Mat3() { Vec3::new(2f, 5f, 8f), Vec3::new(3f, 6f, 9f)); - assert Mat3::from_value::(4f64) == Mat3::new::(4f64, 0f64, 0f64, - 0f64, 4f64, 0f64, - 0f64, 0f64, 4f64); + 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); @@ -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,17 +200,23 @@ fn test_Mat4() { Vec4::new(3f, 7f, 11f, 15f), Vec4::new(4f, 8f, 12f, 16f)); - 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_value(4f64) == Mat4::new(4f64, 0f64, 0f64, 0f64, + 0f64, 4f64, 0f64, 0f64, + 0f64, 0f64, 4f64, 0f64, + 0f64, 0f64, 0f64, 4f64); - 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 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(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[1] == Vec4::new(2f, 6f, 10f, 14f);