From a3c4dbca95fedd0106c272847037c6923f6770df Mon Sep 17 00:00:00 2001 From: Simon Heath Date: Fri, 8 Jun 2018 12:43:39 -0400 Subject: [PATCH] Adds conversions to and from mint Point types. --- src/geometry/point_conversion.rs | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/geometry/point_conversion.rs b/src/geometry/point_conversion.rs index 582f5caf..a26cf35e 100644 --- a/src/geometry/point_conversion.rs +++ b/src/geometry/point_conversion.rs @@ -6,6 +6,16 @@ use base::dimension::{DimName, DimNameAdd, DimNameSum, U1}; use base::allocator::Allocator; 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: @@ -13,6 +23,8 @@ use geometry::Point; * * Point -> Point * Point -> Vector (homogeneous) + * + * mint::Point <-> Point */ impl SubsetOf> for Point @@ -67,3 +79,57 @@ where 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 From> for Point + where N: Scalar, + DefaultAllocator: Allocator { + #[inline] + fn from(p: mint::$PT) -> Self { + Self { + coords: VectorN::from(mint::$VT::from(p)), + } + } + } + + impl Into> for Point + where N: Scalar { + #[inline] + fn into(self) -> mint::$PT { + let mint_vec: mint::$VT = self.coords.into(); + mint::$PT::from(mint_vec) + } + } + + impl AsRef> for Point + where N: Scalar { + #[inline] + fn as_ref(&self) -> &mint::$PT { + unsafe { + mem::transmute(self.coords.data.ptr()) + } + } + } + + impl AsMut> for Point + where N: Scalar { + #[inline] + fn as_mut(&mut self) -> &mut mint::$PT { + 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]; +);