From 038b20033843cdfe3938d93d98e2fff5377d0be7 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 7 Sep 2013 15:04:33 +1000 Subject: [PATCH] Implement matrix to matrix conversions --- src/cgmath/matrix.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 63e719a..0d12d46 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -39,11 +39,6 @@ approx_eq!(impl Mat2) approx_eq!(impl Mat3) approx_eq!(impl Mat4) -// Conversion traits -pub trait ToMat2 { fn to_mat2(&self) -> Mat2; } -pub trait ToMat3 { fn to_mat3(&self) -> Mat3; } -pub trait ToMat4 { fn to_mat4(&self) -> Mat4; } - impl Mat2 { #[inline] pub fn new(c0r0: S, c0r1: S, @@ -571,6 +566,43 @@ for Mat4 } } +// Conversion traits +pub trait ToMat2 { fn to_mat2(&self) -> Mat2; } +pub trait ToMat3 { fn to_mat3(&self) -> Mat3; } +pub trait ToMat4 { fn to_mat4(&self) -> Mat4; } + +impl ToMat3 for Mat2 { + /// Clone the elements of a 2-dimensional matrix into the top corner of a + /// 3-dimensional identity matrix. + fn to_mat3(&self) -> Mat3 { + Mat3::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), zero(), + self.cr(1, 0).clone(), self.cr(1, 1).clone(), zero(), + zero(), zero(), one()) + } +} + +impl ToMat4 for Mat2 { + /// Clone the elements of a 2-dimensional matrix into the top corner of a + /// 4-dimensional identity matrix. + fn to_mat4(&self) -> Mat4 { + Mat4::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), zero(), zero(), + self.cr(1, 0).clone(), self.cr(1, 1).clone(), zero(), zero(), + zero(), zero(), one(), zero(), + zero(), zero(), zero(), one()) + } +} + +impl ToMat4 for Mat3 { + /// Clone the elements of a 3-dimensional matrix into the top corner of a + /// 4-dimensional identity matrix. + fn to_mat4(&self) -> Mat4 { + Mat4::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), self.cr(0, 2).clone(), zero(), + self.cr(1, 0).clone(), self.cr(1, 1).clone(), self.cr(1, 2).clone(), zero(), + self.cr(2, 0).clone(), self.cr(2, 1).clone(), self.cr(2, 2).clone(), zero(), + zero(), zero(), zero(), one()) + } +} + impl ToQuat for Mat3 { /// Convert the matrix to a quaternion fn to_quat(&self) -> Quat {