#[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen};
use num::{Bounded, One, Zero};
use rand::distributions::{Distribution, Standard};
use rand::Rng;
use crate::base::allocator::Allocator;
use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1};
use crate::base::{DefaultAllocator, Scalar, VectorN};
use crate::{
Point1, Point2, Point3, Point4, Point5, Point6, Vector1, Vector2, Vector3, Vector4, Vector5,
Vector6,
};
use simba::scalar::ClosedDiv;
use crate::geometry::Point;
impl<N: Scalar, D: DimName> Point<N, D>
where
DefaultAllocator: Allocator<N, D>,
{
#[inline]
pub unsafe fn new_uninitialized() -> Self {
Self::from(VectorN::new_uninitialized())
}
#[inline]
pub fn origin() -> Self
where
N: Zero,
{
Self::from(VectorN::from_element(N::zero()))
}
#[inline]
pub fn from_slice(components: &[N]) -> Self {
Self::from(VectorN::from_row_slice(components))
}
#[inline]
pub fn from_homogeneous(v: VectorN<N, DimNameSum<D, U1>>) -> Option<Self>
where
N: Scalar + Zero + One + ClosedDiv,
D: DimNameAdd<U1>,
DefaultAllocator: Allocator<N, DimNameSum<D, U1>>,
{
if !v[D::dim()].is_zero() {
let coords = v.fixed_slice::<D, U1>(0, 0) / v[D::dim()].inlined_clone();
Some(Self::from(coords))
} else {
None
}
}
}
impl<N: Scalar + Bounded, D: DimName> Bounded for Point<N, D>
where
DefaultAllocator: Allocator<N, D>,
{
#[inline]
fn max_value() -> Self {
Self::from(VectorN::max_value())
}
#[inline]
fn min_value() -> Self {
Self::from(VectorN::min_value())
}
}
impl<N: Scalar, D: DimName> Distribution<Point<N, D>> for Standard
where
DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N>,
{
#[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point<N, D> {
Point::from(rng.gen::<VectorN<N, D>>())
}
}
#[cfg(feature = "arbitrary")]
impl<N: Scalar + Arbitrary + Send, D: DimName> Arbitrary for Point<N, D>
where
DefaultAllocator: Allocator<N, D>,
<DefaultAllocator as Allocator<N, D>>::Buffer: Send,
{
#[inline]
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Self::from(VectorN::arbitrary(g))
}
}
impl<N: Scalar> Point1<N> {
#[inline]
pub fn new(x: N) -> Self {
Vector1::new(x).into()
}
}
macro_rules! componentwise_constructors_impl(
($($doc: expr; $Point: ident, $Vector: ident, $($args: ident:$irow: expr),*);* $(;)*) => {$(
impl<N: Scalar> $Point<N> {
#[doc = "Initializes this point from its components."]
#[doc = "# Example\n```"]
#[doc = $doc]
#[doc = "```"]
#[inline]
pub fn new($($args: N),*) -> Self {
$Vector::new($($args),*).into()
}
}
)*}
);
componentwise_constructors_impl!(
"# use nalgebra::Point2;\nlet p = Point2::new(1.0, 2.0);\nassert!(p.x == 1.0 && p.y == 2.0);";
Point2, Vector2, x:0, y:1;
"# use nalgebra::Point3;\nlet p = Point3::new(1.0, 2.0, 3.0);\nassert!(p.x == 1.0 && p.y == 2.0 && p.z == 3.0);";
Point3, Vector3, x:0, y:1, z:2;
"# use nalgebra::Point4;\nlet p = Point4::new(1.0, 2.0, 3.0, 4.0);\nassert!(p.x == 1.0 && p.y == 2.0 && p.z == 3.0 && p.w == 4.0);";
Point4, Vector4, x:0, y:1, z:2, w:3;
"# use nalgebra::Point5;\nlet p = Point5::new(1.0, 2.0, 3.0, 4.0, 5.0);\nassert!(p.x == 1.0 && p.y == 2.0 && p.z == 3.0 && p.w == 4.0 && p.a == 5.0);";
Point5, Vector5, x:0, y:1, z:2, w:3, a:4;
"# use nalgebra::Point6;\nlet p = Point6::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);\nassert!(p.x == 1.0 && p.y == 2.0 && p.z == 3.0 && p.w == 4.0 && p.a == 5.0 && p.b == 6.0);";
Point6, Vector6, x:0, y:1, z:2, w:3, a:4, b:5;
);
macro_rules! from_array_impl(
($($Point: ident, $len: expr);*) => {$(
impl <N: Scalar> From<[N; $len]> for $Point<N> {
fn from(coords: [N; $len]) -> Self {
Self {
coords: coords.into()
}
}
}
)*}
);
from_array_impl!(Point1, 1; Point2, 2; Point3, 3; Point4, 4; Point5, 5; Point6, 6);