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
|
|
|
|
2021-07-17 12:17:56 +08:00
|
|
|
use crate::base::allocator::{Allocator, InnerAllocator};
|
2021-04-07 20:29:20 +08:00
|
|
|
use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
|
2021-04-11 17:00:38 +08:00
|
|
|
use crate::base::{Const, DefaultAllocator, Matrix, OVector, Scalar};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::geometry::Point;
|
2021-06-27 00:39:02 +08:00
|
|
|
use crate::{DimName, OPoint};
|
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-06-27 00:39:02 +08:00
|
|
|
impl<T1, T2, D: DimName> SubsetOf<OPoint<T2, D>> for OPoint<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-07-16 14:53:28 +08:00
|
|
|
T2: SupersetOf<T1>,
|
2021-06-27 00:39:02 +08:00
|
|
|
DefaultAllocator: Allocator<T1, D> + Allocator<T2, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn to_superset(&self) -> OPoint<T2, D> {
|
|
|
|
OPoint::from(self.coords.to_superset())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn is_in_subset(m: &OPoint<T2, 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]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn from_superset_unchecked(m: &OPoint<T2, 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-06-27 00:39:02 +08:00
|
|
|
impl<T1, T2, D> SubsetOf<OVector<T2, DimNameSum<D, U1>>> for OPoint<T1, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-06-27 00:39:02 +08:00
|
|
|
D: DimNameAdd<U1>,
|
2021-04-11 17:00:38 +08:00
|
|
|
T2: Scalar + Zero + One + ClosedDiv + SupersetOf<T1>,
|
2021-06-27 00:39:02 +08:00
|
|
|
DefaultAllocator: Allocator<T1, D>
|
|
|
|
+ Allocator<T2, D>
|
|
|
|
+ Allocator<T1, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<T2, DimNameSum<D, U1>>,
|
2021-04-11 17:00:38 +08:00
|
|
|
// + Allocator<T1, D>
|
|
|
|
// + Allocator<T2, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn to_superset(&self) -> OVector<T2, DimNameSum<D, U1>> {
|
|
|
|
let p: OPoint<T2, D> = self.to_superset();
|
2021-07-17 12:17:56 +08:00
|
|
|
p.into_homogeneous()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn is_in_subset(v: &OVector<T2, DimNameSum<D, U1>>) -> bool {
|
|
|
|
crate::is_convertible::<_, OVector<T1, DimNameSum<D, U1>>>(v) && !v[D::dim()].is_zero()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn from_superset_unchecked(v: &OVector<T2, DimNameSum<D, U1>>) -> Self {
|
2021-07-16 14:53:28 +08:00
|
|
|
let coords = v.generic_slice((0, 0), (D::name(), Const::<1>)) / v[D::dim()].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-07-16 14:53:28 +08:00
|
|
|
impl<T: Zero + One, D: DimName> From<OPoint<T, D>> for OVector<T, DimNameSum<D, U1>>
|
2018-10-13 16:25:34 +08:00
|
|
|
where
|
2021-06-27 00:39:02 +08:00
|
|
|
D: DimNameAdd<U1>,
|
|
|
|
DefaultAllocator: Allocator<T, DimNameSum<D, U1>> + Allocator<T, D>,
|
2018-10-13 16:25:34 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn from(t: OPoint<T, D>) -> Self {
|
2021-07-17 12:17:56 +08:00
|
|
|
t.into_homogeneous()
|
2018-10-13 16:25:34 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-24 02:47:42 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, const D: usize> From<[T; D]> for Point<T, D> {
|
2021-04-27 19:17:12 +08:00
|
|
|
#[inline]
|
|
|
|
fn from(coords: [T; D]) -> Self {
|
|
|
|
Point {
|
|
|
|
coords: coords.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 17:36:14 +08:00
|
|
|
impl<T, const D: usize> From<Point<T, D>> for [T; D] where
|
|
|
|
T: Clone,{
|
2021-04-27 19:17:12 +08:00
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(p: Point<T, D>) -> Self {
|
|
|
|
p.coords.into()
|
2021-04-27 19:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, D: DimName> From<OVector<T, D>> for OPoint<T, D>
|
2021-06-27 00:39:02 +08:00
|
|
|
where
|
2021-07-17 12:17:56 +08:00
|
|
|
DefaultAllocator: InnerAllocator<T, D>,
|
2021-06-27 00:39:02 +08:00
|
|
|
{
|
2018-10-24 02:47:42 +08:00
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn from(coords: OVector<T, D>) -> Self {
|
|
|
|
OPoint { coords }
|
2018-10-24 02:47:42 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 16:16:01 +08:00
|
|
|
|
2021-07-17 12:17:56 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Point<T::Element, D>; 2]> for Point<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
|
2021-07-17 12:17:56 +08:00
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Point<T::Element, D>; 2]) -> Self {
|
2021-07-17 12:17:56 +08:00
|
|
|
Self::from(OVector::from([
|
|
|
|
arr[0].coords.clone(),
|
|
|
|
arr[1].coords.clone(),
|
|
|
|
]))
|
2020-03-23 16:16:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 12:17:56 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Point<T::Element, D>; 4]> for Point<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
|
2021-07-17 12:17:56 +08:00
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Point<T::Element, D>; 4]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2021-07-17 12:17:56 +08:00
|
|
|
arr[0].coords.clone(),
|
|
|
|
arr[1].coords.clone(),
|
|
|
|
arr[2].coords.clone(),
|
|
|
|
arr[3].coords.clone(),
|
2020-03-23 16:16:01 +08:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 12:17:56 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Point<T::Element, D>; 8]> for Point<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
|
2021-07-17 12:17:56 +08:00
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Point<T::Element, D>; 8]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2021-07-17 12:17:56 +08:00
|
|
|
arr[0].coords.clone(),
|
|
|
|
arr[1].coords.clone(),
|
|
|
|
arr[2].coords.clone(),
|
|
|
|
arr[3].coords.clone(),
|
|
|
|
arr[4].coords.clone(),
|
|
|
|
arr[5].coords.clone(),
|
|
|
|
arr[6].coords.clone(),
|
|
|
|
arr[7].coords.clone(),
|
2020-03-23 16:16:01 +08:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 12:17:56 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Point<T::Element, D>; 16]>
|
|
|
|
for Point<T, D>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
|
2021-07-17 12:17:56 +08:00
|
|
|
T::Element: Scalar,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [Point<T::Element, D>; 16]) -> Self {
|
|
|
|
Self::from(OVector::from([
|
2021-07-17 12:17:56 +08:00
|
|
|
arr[0].coords.clone(),
|
|
|
|
arr[1].coords.clone(),
|
|
|
|
arr[2].coords.clone(),
|
|
|
|
arr[3].coords.clone(),
|
|
|
|
arr[4].coords.clone(),
|
|
|
|
arr[5].coords.clone(),
|
|
|
|
arr[6].coords.clone(),
|
|
|
|
arr[7].coords.clone(),
|
|
|
|
arr[8].coords.clone(),
|
|
|
|
arr[9].coords.clone(),
|
|
|
|
arr[10].coords.clone(),
|
|
|
|
arr[11].coords.clone(),
|
|
|
|
arr[12].coords.clone(),
|
|
|
|
arr[13].coords.clone(),
|
|
|
|
arr[14].coords.clone(),
|
|
|
|
arr[15].coords.clone(),
|
2020-03-23 16:16:01 +08:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|