Mint matrices

This commit is contained in:
Dzmitry Malyshau 2017-07-25 12:27:18 -04:00 committed by Sébastien Crozet
parent ce2063c191
commit 6b6a0717c2
2 changed files with 83 additions and 2 deletions

View File

@ -280,8 +280,61 @@ macro_rules! impl_from_into_mint_1D(
// Implement for vectors of dimension 2 .. 4. // Implement for vectors of dimension 2 .. 4.
#[cfg(feature = "mint")] #[cfg(feature = "mint")]
impl_from_into_mint_1D!( // Column vectors. impl_from_into_mint_1D!(
U2 => Vector2[2]; U2 => Vector2[2];
U3 => Vector3[3]; U3 => Vector3[3];
U4 => Vector4[4]; U4 => Vector4[4];
); );
#[cfg(feature = "mint")]
macro_rules! impl_from_into_mint_2D(
($(($NRows: ty, $NCols: ty) => $MV:ident{ $($component:ident),* }[$SZRows: expr]);* $(;)*) => {$(
impl<N, S> From<mint::$MV<N>> for Matrix<N, $NRows, $NCols, S>
where N: Scalar,
S: OwnedStorage<N, $NRows, $NCols>,
S::Alloc: OwnedAllocator<N, $NRows, $NCols, S> {
#[inline]
fn from(m: mint::$MV<N>) -> 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<N, S> Into<mint::$MV<N>> for Matrix<N, $NRows, $NCols, S>
where N: Scalar,
S: OwnedStorage<N, $NRows, $NCols>,
S::Alloc: OwnedAllocator<N, $NRows, $NCols, S> {
#[inline]
fn into(self) -> mint::$MV<N> {
unsafe {
let mut res: mint::$MV<N> = 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];
);

View File

@ -267,8 +267,8 @@ macro_rules! mint_vector_conversion(
let mv: mint::$Vector<usize> = v.into(); let mv: mint::$Vector<usize> = v.into();
let mv_ref: &mint::$Vector<usize> = v.as_ref(); let mv_ref: &mint::$Vector<usize> = v.as_ref();
let v2 = $Vector::from(mv); let v2 = $Vector::from(mv);
let arr: [usize; $SZ] = mv.into(); let arr: [usize; $SZ] = mv.into();
for i in 0 .. $SZ { for i in 0 .. $SZ {
assert_eq!(arr[i], i); assert_eq!(arr[i], i);
} }
@ -300,3 +300,31 @@ fn mint_quaternion_conversions() {
assert_eq!(q, q2); 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<usize> = 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);
);