2018-10-13 16:25:34 +08:00
|
|
|
use num::{One, Zero};
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::{ClosedDiv, SubsetOf, SupersetOf};
|
2020-04-05 23:15:43 +08:00
|
|
|
use simba::simd::PrimitiveSimdValue;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::Allocator;
|
2021-04-07 20:29:20 +08:00
|
|
|
use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
|
|
|
|
use crate::base::{Const, DefaultAllocator, Matrix, Scalar, VectorN};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::geometry::Point;
|
2021-03-06 19:20:38 +08:00
|
|
|
|
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)
|
2016-12-05 05:44:42 +08:00
|
|
|
*/
|
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N1, N2, const D: usize> SubsetOf<Point<N2, D>> for Point<N1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2019-12-17 07:09:14 +08:00
|
|
|
N1: Scalar,
|
|
|
|
N2: Scalar + SupersetOf<N1>,
|
2021-04-07 20:29:20 +08:00
|
|
|
// DefaultAllocator: Allocator<N2, D> + Allocator<N1, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
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 {
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: is there a way to reuse the `.is_in_subset` from the matrix implementation of
|
2016-12-05 05:44:42 +08:00
|
|
|
// SubsetOf?
|
|
|
|
m.iter().all(|e| e.is_in_subset())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-21 19:16:46 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N1, N2, const D: usize> SubsetOf<VectorN<N2, DimNameSum<Const<D>, U1>>> for Point<N1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1>,
|
2019-12-17 07:09:14 +08:00
|
|
|
N1: Scalar,
|
|
|
|
N2: Scalar + Zero + One + ClosedDiv + SupersetOf<N1>,
|
2021-04-07 20:29:20 +08:00
|
|
|
DefaultAllocator:
|
|
|
|
Allocator<N1, DimNameSum<Const<D>, U1>> + Allocator<N2, DimNameSum<Const<D>, U1>>,
|
|
|
|
// + Allocator<N1, D>
|
|
|
|
// + Allocator<N2, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-07 20:29:20 +08:00
|
|
|
fn to_superset(&self) -> VectorN<N2, DimNameSum<Const<D>, U1>> {
|
2017-08-03 01:37:44 +08:00
|
|
|
let p: Point<N2, D> = self.to_superset();
|
2016-12-05 05:44:42 +08:00
|
|
|
p.to_homogeneous()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-07 20:29:20 +08:00
|
|
|
fn is_in_subset(v: &VectorN<N2, DimNameSum<Const<D>, U1>>) -> bool {
|
|
|
|
crate::is_convertible::<_, VectorN<N1, DimNameSum<D, U1>>>(v) && !v[D].is_zero()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-07 20:29:20 +08:00
|
|
|
fn from_superset_unchecked(v: &VectorN<N2, DimNameSum<Const<D>, U1>>) -> Self {
|
|
|
|
let coords = v.fixed_slice::<D, U1>(0, 0) / v[D].inlined_clone();
|
2018-10-24 02:47:42 +08:00
|
|
|
Self {
|
2020-03-21 19:16:46 +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
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N: Scalar + Zero + One, const D: usize> From<Point<N, D>>
|
|
|
|
for VectorN<N, DimNameSum<Const<D>, U1>>
|
2018-10-13 16:25:34 +08:00
|
|
|
where
|
2021-04-07 20:29:20 +08:00
|
|
|
Const<D>: DimNameAdd<U1>,
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<Const<D>, U1>>,
|
2018-10-13 16:25:34 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(t: Point<N, D>) -> Self {
|
|
|
|
t.to_homogeneous()
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 02:47:42 +08:00
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N: Scalar, const D: usize> From<VectorN<N, Const<D>>> for Point<N, D>
|
|
|
|
// where
|
|
|
|
// DefaultAllocator: Allocator<N, D>,
|
2018-10-24 02:47:42 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-07 20:29:20 +08:00
|
|
|
fn from(coords: VectorN<N, Const<D>>) -> Self {
|
2020-03-21 19:16:46 +08:00
|
|
|
Point { coords }
|
2018-10-24 02:47:42 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 16:16:01 +08:00
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N: Scalar + Copy + PrimitiveSimdValue, const D: usize> From<[Point<N::Element, D>; 2]>
|
2020-03-23 16:16:01 +08:00
|
|
|
for Point<N, D>
|
|
|
|
where
|
|
|
|
N: From<[<N as simba::simd::SimdValue>::Element; 2]>,
|
|
|
|
N::Element: Scalar + Copy,
|
2021-04-07 20:29:20 +08:00
|
|
|
// DefaultAllocator: Allocator<N, D> + Allocator<N::Element, D>,
|
|
|
|
<DefaultAllocator as Allocator<N::Element, Const<D>>>::Buffer: Copy,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(arr: [Point<N::Element, D>; 2]) -> Self {
|
|
|
|
Self::from(VectorN::from([arr[0].coords, arr[1].coords]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N: Scalar + Copy + PrimitiveSimdValue, const D: usize> From<[Point<N::Element, D>; 4]>
|
2020-03-23 16:16:01 +08:00
|
|
|
for Point<N, D>
|
|
|
|
where
|
|
|
|
N: From<[<N as simba::simd::SimdValue>::Element; 4]>,
|
|
|
|
N::Element: Scalar + Copy,
|
2021-04-07 20:29:20 +08:00
|
|
|
// DefaultAllocator: Allocator<N, D> + Allocator<N::Element, D>,
|
|
|
|
<DefaultAllocator as Allocator<N::Element, Const<D>>>::Buffer: Copy,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(arr: [Point<N::Element, D>; 4]) -> Self {
|
|
|
|
Self::from(VectorN::from([
|
|
|
|
arr[0].coords,
|
|
|
|
arr[1].coords,
|
|
|
|
arr[2].coords,
|
|
|
|
arr[3].coords,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N: Scalar + Copy + PrimitiveSimdValue, const D: usize> From<[Point<N::Element, D>; 8]>
|
2020-03-23 16:16:01 +08:00
|
|
|
for Point<N, D>
|
|
|
|
where
|
|
|
|
N: From<[<N as simba::simd::SimdValue>::Element; 8]>,
|
|
|
|
N::Element: Scalar + Copy,
|
2021-04-07 20:29:20 +08:00
|
|
|
// DefaultAllocator: Allocator<N, D> + Allocator<N::Element, D>,
|
|
|
|
<DefaultAllocator as Allocator<N::Element, Const<D>>>::Buffer: Copy,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(arr: [Point<N::Element, D>; 8]) -> Self {
|
|
|
|
Self::from(VectorN::from([
|
|
|
|
arr[0].coords,
|
|
|
|
arr[1].coords,
|
|
|
|
arr[2].coords,
|
|
|
|
arr[3].coords,
|
|
|
|
arr[4].coords,
|
|
|
|
arr[5].coords,
|
|
|
|
arr[6].coords,
|
|
|
|
arr[7].coords,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:29:20 +08:00
|
|
|
impl<N: Scalar + Copy + PrimitiveSimdValue, const D: usize> From<[Point<N::Element, D>; 16]>
|
2020-03-23 16:16:01 +08:00
|
|
|
for Point<N, D>
|
|
|
|
where
|
|
|
|
N: From<[<N as simba::simd::SimdValue>::Element; 16]>,
|
|
|
|
N::Element: Scalar + Copy,
|
2021-04-07 20:29:20 +08:00
|
|
|
// DefaultAllocator: Allocator<N, D> + Allocator<N::Element, D>,
|
|
|
|
<DefaultAllocator as Allocator<N::Element, Const<D>>>::Buffer: Copy,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(arr: [Point<N::Element, D>; 16]) -> Self {
|
|
|
|
Self::from(VectorN::from([
|
|
|
|
arr[0].coords,
|
|
|
|
arr[1].coords,
|
|
|
|
arr[2].coords,
|
|
|
|
arr[3].coords,
|
|
|
|
arr[4].coords,
|
|
|
|
arr[5].coords,
|
|
|
|
arr[6].coords,
|
|
|
|
arr[7].coords,
|
|
|
|
arr[8].coords,
|
|
|
|
arr[9].coords,
|
|
|
|
arr[10].coords,
|
|
|
|
arr[11].coords,
|
|
|
|
arr[12].coords,
|
|
|
|
arr[13].coords,
|
|
|
|
arr[14].coords,
|
|
|
|
arr[15].coords,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|