From 6b6a0717c25743de4cd2ed94b73d318dca3edeb1 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 25 Jul 2017 12:27:18 -0400 Subject: [PATCH] Mint matrices --- src/core/conversion.rs | 55 +++++++++++++++++++++++++++++++++++++++- tests/core/conversion.rs | 30 +++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/core/conversion.rs b/src/core/conversion.rs index 247a7ee5..34486c4a 100644 --- a/src/core/conversion.rs +++ b/src/core/conversion.rs @@ -280,8 +280,61 @@ macro_rules! impl_from_into_mint_1D( // Implement for vectors of dimension 2 .. 4. #[cfg(feature = "mint")] -impl_from_into_mint_1D!( // Column vectors. +impl_from_into_mint_1D!( U2 => Vector2[2]; U3 => Vector3[3]; U4 => Vector4[4]; ); + +#[cfg(feature = "mint")] +macro_rules! impl_from_into_mint_2D( + ($(($NRows: ty, $NCols: ty) => $MV:ident{ $($component:ident),* }[$SZRows: expr]);* $(;)*) => {$( + impl From> for Matrix + where N: Scalar, + S: OwnedStorage, + S::Alloc: OwnedAllocator { + #[inline] + fn from(m: mint::$MV) -> Self { + unsafe { + let mut res = Self::new_uninitialized(); + let mut ptr = res.data.ptr_mut(); + $( + ptr::copy_nonoverlapping(&m.$component.x, ptr, $SZRows); + ptr = ptr.offset($SZRows); + )* + let _ = ptr; + res + } + } + } + + impl Into> for Matrix + where N: Scalar, + S: OwnedStorage, + S::Alloc: OwnedAllocator { + #[inline] + fn into(self) -> mint::$MV { + unsafe { + let mut res: mint::$MV = mem::uninitialized(); + let mut ptr = self.data.ptr(); + $( + ptr::copy_nonoverlapping(ptr, &mut res.$component.x, $SZRows); + ptr = ptr.offset($SZRows); + )* + let _ = ptr; + res + } + } + } + )*} +); + +// Implement for matrices with shape 2x2 .. 4x4. +#[cfg(feature = "mint")] +impl_from_into_mint_2D!( + (U2, U2) => ColumnMatrix2{x, y}[2]; + (U3, U2) => ColumnMatrix3x2{x, y}[3]; + (U3, U3) => ColumnMatrix3{x, y, z}[3]; + (U4, U3) => ColumnMatrix4x3{x, y, z}[4]; + (U4, U4) => ColumnMatrix4{x, y, z, w}[4]; +); diff --git a/tests/core/conversion.rs b/tests/core/conversion.rs index ca6d23cf..355dcb14 100644 --- a/tests/core/conversion.rs +++ b/tests/core/conversion.rs @@ -267,8 +267,8 @@ macro_rules! mint_vector_conversion( let mv: mint::$Vector = v.into(); let mv_ref: &mint::$Vector = v.as_ref(); let v2 = $Vector::from(mv); - let arr: [usize; $SZ] = mv.into(); + for i in 0 .. $SZ { assert_eq!(arr[i], i); } @@ -300,3 +300,31 @@ fn mint_quaternion_conversions() { assert_eq!(q, q2); } + +macro_rules! mint_matrix_conversion( + ($($mint_matrix_conversion_i_j: ident, $Matrix: ident, $Mint: ident, ($NRows: expr, $NCols: expr));* $(;)*) => {$( + #[test] + fn $mint_matrix_conversion_i_j() { + let m = $Matrix::from_fn(|i, j| i * 10 + j); + let mm: mint::$Mint = m.into(); + let m2 = $Matrix::from(mm); + let arr: [[usize; $NRows]; $NCols] = mm.into(); + + for i in 0 .. $NRows { + for j in 0 .. $NCols { + assert_eq!(arr[j][i], i * 10 + j); + } + } + + assert_eq!(m, m2); + } + )*} +); + +mint_matrix_conversion!( + mint_matrix_conversion_2_2, Matrix2, ColumnMatrix2, (2, 2); + mint_matrix_conversion_3_2, Matrix3x2, ColumnMatrix3x2, (3, 2); + mint_matrix_conversion_3_3, Matrix3, ColumnMatrix3, (3, 3); + mint_matrix_conversion_4_3, Matrix4x3, ColumnMatrix4x3, (4, 3); + mint_matrix_conversion_4_4, Matrix4, ColumnMatrix4, (4, 4); +);