From 130885d83a51f13f1040ad4520f8c09844f92022 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Sat, 22 Sep 2018 20:16:16 +0200 Subject: [PATCH] Add conversion functions between square matrices. --- nalgebra-glm/src/gtc/type_ptr.rs | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/nalgebra-glm/src/gtc/type_ptr.rs b/nalgebra-glm/src/gtc/type_ptr.rs index 1d236f51..4cf23f04 100644 --- a/nalgebra-glm/src/gtc/type_ptr.rs +++ b/nalgebra-glm/src/gtc/type_ptr.rs @@ -65,6 +65,69 @@ pub fn make_mat4x4(ptr: &[N]) -> Mat { Matrix4::from_column_slice(ptr) } +/// Converts a 2x2 matrix to a 3x3 matrix. +pub fn mat2_to_mat3(m: &Mat) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + Matrix3::new( + m.m11, m.m12, _0, + m.m21, m.m22, _0, + _0, _0, _1 + ) +} + +/// Converts a 3x3 matrix to a 2x2 matrix. +pub fn mat3_to_mat2(m: &Mat) -> Mat { + Matrix2::new( + m.m11, m.m12, + m.m21, m.m22 + ) +} + +/// Converts a 3x3 matrix to a 4x4 matrix. +pub fn mat3_to_mat4(m: &Mat) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + Matrix4::new( + m.m11, m.m12, m.m13, _0, + m.m21, m.m22, m.m23, _0, + m.m31, m.m32, m.m33, _0, + _0, _0, _0, _1, + ) +} + +/// Converts a 4x4 matrix to a 3x3 matrix. +pub fn mat4_to_mat3(m: &Mat) -> Mat { + Matrix3::new( + m.m11, m.m12, m.m13, + m.m21, m.m22, m.m23, + m.m31, m.m32, m.m33, + ) +} + +/// Converts a 2x2 matrix to a 4x4 matrix. +pub fn mat2_to_mat4(m: &Mat) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + Matrix4::new( + m.m11, m.m12, _0, _0, + m.m21, m.m22, _0, _0, + _0, _0, _1, _0, + _0, _0, _0, _1, + ) +} + +/// Converts a 4x4 matrix to a 2x2 matrix. +pub fn mat4_to_mat2(m: &Mat) -> Mat { + Matrix2::new( + m.m11, m.m12, + m.m21, m.m22, + ) +} + /// Creates a quaternion from a slice arranged as `[x, y, z, w]`. pub fn make_quat(ptr: &[N]) -> Qua { Quaternion::from_vector(Vector4::from_column_slice(ptr))