2018-02-02 19:26:35 +08:00
|
|
|
use alga::general::{ClosedDiv, SubsetOf, SupersetOf};
|
2018-10-13 16:25:34 +08:00
|
|
|
use num::{One, Zero};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::Allocator;
|
|
|
|
use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1};
|
|
|
|
use crate::base::{DefaultAllocator, Matrix, Scalar, VectorN};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-10-13 16:25:34 +08:00
|
|
|
#[cfg(feature = "mint")]
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::dimension::{U2, U3};
|
2018-10-13 16:25:34 +08:00
|
|
|
#[cfg(feature = "mint")]
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::storage::{Storage, StorageMut};
|
|
|
|
use crate::geometry::Point;
|
2018-06-09 00:43:39 +08:00
|
|
|
#[cfg(feature = "mint")]
|
|
|
|
use mint;
|
|
|
|
#[cfg(feature = "mint")]
|
|
|
|
use std::convert::{AsMut, AsRef, From, Into};
|
2016-12-05 05:44:42 +08:00
|
|
|
/*
|
|
|
|
* This file provides the following conversions:
|
|
|
|
* =============================================
|
|
|
|
*
|
2017-08-03 01:37:44 +08:00
|
|
|
* Point -> Point
|
|
|
|
* Point -> Vector (homogeneous)
|
2018-10-13 16:25:34 +08:00
|
|
|
*
|
2018-06-09 00:43:39 +08:00
|
|
|
* mint::Point <-> Point
|
2016-12-05 05:44:42 +08:00
|
|
|
*/
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, D> SubsetOf<Point<N2, D>> for Point<N1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
D: DimName,
|
|
|
|
N1: Scalar,
|
|
|
|
N2: Scalar + SupersetOf<N1>,
|
|
|
|
DefaultAllocator: Allocator<N2, D> + Allocator<N1, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> Point<N2, D> {
|
2018-10-24 02:47:42 +08:00
|
|
|
Point::from(self.coords.to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(m: &Point<N2, D>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
// FIXME: is there a way to reuse the `.is_in_subset` from the matrix implementation of
|
|
|
|
// SubsetOf?
|
|
|
|
m.iter().all(|e| e.is_in_subset())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(m: &Point<N2, D>) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
Self::from(Matrix::from_superset_unchecked(&m.coords))
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N1, N2, D> SubsetOf<VectorN<N2, DimNameSum<D, U1>>> for Point<N1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
D: DimNameAdd<U1>,
|
|
|
|
N1: Scalar,
|
|
|
|
N2: Scalar + Zero + One + ClosedDiv + SupersetOf<N1>,
|
|
|
|
DefaultAllocator: Allocator<N1, D>
|
|
|
|
+ Allocator<N1, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N2, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N2, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn to_superset(&self) -> VectorN<N2, DimNameSum<D, U1>> {
|
|
|
|
let p: Point<N2, D> = self.to_superset();
|
2016-12-05 05:44:42 +08:00
|
|
|
p.to_homogeneous()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn is_in_subset(v: &VectorN<N2, DimNameSum<D, U1>>) -> bool {
|
2019-03-23 21:29:07 +08:00
|
|
|
crate::is_convertible::<_, VectorN<N1, DimNameSum<D, U1>>>(v) && !v[D::dim()].is_zero()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
unsafe fn from_superset_unchecked(v: &VectorN<N2, DimNameSum<D, U1>>) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
let coords = v.fixed_slice::<D, U1>(0, 0) / v[D::dim()];
|
2018-10-24 02:47:42 +08:00
|
|
|
Self {
|
2019-03-23 21:29:07 +08:00
|
|
|
coords: crate::convert_unchecked(coords)
|
2018-10-24 02:47:42 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-09 00:43:39 +08:00
|
|
|
|
|
|
|
#[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>
|
2018-06-09 06:07:25 +08:00
|
|
|
where N: Scalar {
|
2018-06-09 00:43:39 +08:00
|
|
|
#[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 {
|
2018-06-09 02:21:04 +08:00
|
|
|
&*(self.coords.data.ptr() as *const mint::$PT<N>)
|
2018-06-09 00:43:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> AsMut<mint::$PT<N>> for Point<N, $NRows>
|
|
|
|
where N: Scalar {
|
|
|
|
#[inline]
|
|
|
|
fn as_mut(&mut self) -> &mut mint::$PT<N> {
|
|
|
|
unsafe {
|
2018-06-09 02:21:04 +08:00
|
|
|
&mut *(self.coords.data.ptr_mut() as *mut mint::$PT<N>)
|
2018-06-09 00:43:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Implement for points of dimension 2, 3.
|
|
|
|
#[cfg(feature = "mint")]
|
|
|
|
impl_from_into_mint_1D!(
|
|
|
|
U2 => Point2, Vector2[2];
|
|
|
|
U3 => Point3, Vector3[3];
|
|
|
|
);
|
2018-10-13 16:25:34 +08:00
|
|
|
|
|
|
|
impl<N: Scalar + Zero + One, D: DimName> From<Point<N, D>> for VectorN<N, DimNameSum<D, U1>>
|
|
|
|
where
|
|
|
|
D: DimNameAdd<U1>,
|
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, DimNameSum<D, U1>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(t: Point<N, D>) -> Self {
|
|
|
|
t.to_homogeneous()
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 02:47:42 +08:00
|
|
|
|
|
|
|
impl<N: Scalar, D: DimName> From<VectorN<N, D>> for Point<N, D>
|
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(coords: VectorN<N, D>) -> Self {
|
|
|
|
Point {
|
|
|
|
coords
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|