Adds conversions to and from mint Point types.

This commit is contained in:
Simon Heath 2018-06-08 12:43:39 -04:00 committed by Sébastien Crozet
parent 4b3a157513
commit a3c4dbca95

View File

@ -6,6 +6,16 @@ use base::dimension::{DimName, DimNameAdd, DimNameSum, U1};
use base::allocator::Allocator; use base::allocator::Allocator;
use geometry::Point; use geometry::Point;
#[cfg(feature = "mint")]
use mint;
#[cfg(feature = "mint")]
use base::dimension::{U2, U3};
#[cfg(feature = "mint")]
use std::convert::{AsMut, AsRef, From, Into};
#[cfg(feature = "mint")]
use base::storage::{Storage, StorageMut};
#[cfg(feature = "mint")]
use std::mem;
/* /*
* This file provides the following conversions: * This file provides the following conversions:
@ -13,6 +23,8 @@ use geometry::Point;
* *
* Point -> Point * Point -> Point
* Point -> Vector (homogeneous) * Point -> Vector (homogeneous)
*
* mint::Point <-> Point
*/ */
impl<N1, N2, D> SubsetOf<Point<N2, D>> for Point<N1, D> impl<N1, N2, D> SubsetOf<Point<N2, D>> for Point<N1, D>
@ -67,3 +79,57 @@ where
Self::from_coordinates(::convert_unchecked(coords)) Self::from_coordinates(::convert_unchecked(coords))
} }
} }
#[cfg(feature = "mint")]
macro_rules! impl_from_into_mint_1D(
($($NRows: ident => $PT:ident, $VT:ident [$SZ: expr]);* $(;)*) => {$(
impl<N> From<mint::$PT<N>> for Point<N, $NRows>
where N: Scalar,
DefaultAllocator: Allocator<N, $NRows> {
#[inline]
fn from(p: mint::$PT<N>) -> Self {
Self {
coords: VectorN::from(mint::$VT::from(p)),
}
}
}
impl<N> Into<mint::$PT<N>> for Point<N, $NRows>
where N: Scalar {
#[inline]
fn into(self) -> mint::$PT<N> {
let mint_vec: mint::$VT<N> = self.coords.into();
mint::$PT::from(mint_vec)
}
}
impl<N> AsRef<mint::$PT<N>> for Point<N, $NRows>
where N: Scalar {
#[inline]
fn as_ref(&self) -> &mint::$PT<N> {
unsafe {
mem::transmute(self.coords.data.ptr())
}
}
}
impl<N> AsMut<mint::$PT<N>> for Point<N, $NRows>
where N: Scalar {
#[inline]
fn as_mut(&mut self) -> &mut mint::$PT<N> {
unsafe {
mem::transmute(self.coords.data.ptr_mut())
}
}
}
)*}
);
// Implement for points of dimension 2, 3.
#[cfg(feature = "mint")]
impl_from_into_mint_1D!(
U2 => Point2, Vector2[2];
U3 => Point3, Vector3[3];
);