2016-12-05 05:44:42 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
|
|
|
|
use quickcheck::{Arbitrary, Gen};
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use num::{Bounded, One, Zero};
|
2018-05-23 05:58:14 +08:00
|
|
|
|
use rand::distributions::{Distribution, Standard};
|
|
|
|
|
use rand::Rng;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
use alga::general::ClosedDiv;
|
2018-05-19 23:15:15 +08:00
|
|
|
|
use base::allocator::Allocator;
|
|
|
|
|
use base::dimension::{DimName, DimNameAdd, DimNameSum, U1, U2, U3, U4, U5, U6};
|
2018-05-23 05:58:14 +08:00
|
|
|
|
use base::{DefaultAllocator, Scalar, VectorN};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use geometry::Point;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar, D: DimName> Point<N, D>
|
2018-10-22 13:00:10 +08:00
|
|
|
|
where DefaultAllocator: Allocator<N, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Creates a new point with uninitialized coordinates.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn new_uninitialized() -> Self {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
Self::from(VectorN::new_uninitialized())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new point with all coordinates equal to zero.
|
2018-10-24 03:59:10 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Point2, Point3};
|
|
|
|
|
/// // This works in any dimension.
|
|
|
|
|
/// // The explicit ::<f32> type annotation may not always be needed,
|
|
|
|
|
/// // depending on the context of type inference.
|
|
|
|
|
/// let pt = Point2::<f32>::origin();
|
|
|
|
|
/// assert!(pt.x == 0.0 && pt.y == 0.0);
|
|
|
|
|
///
|
|
|
|
|
/// let pt = Point3::<f32>::origin();
|
|
|
|
|
/// assert!(pt.x == 0.0 && pt.y == 0.0 && pt.z == 0.0);
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn origin() -> Self
|
2018-10-22 13:00:10 +08:00
|
|
|
|
where N: Zero {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
Self::from(VectorN::from_element(N::zero()))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 16:45:42 +08:00
|
|
|
|
/// Creates a new point from a slice.
|
2018-10-24 03:59:10 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Point2, Point3};
|
|
|
|
|
/// let data = [ 1.0, 2.0, 3.0 ];
|
|
|
|
|
///
|
|
|
|
|
/// let pt = Point2::from_slice(&data[..2]);
|
|
|
|
|
/// assert_eq!(pt, Point2::new(1.0, 2.0));
|
|
|
|
|
///
|
|
|
|
|
/// let pt = Point3::from_slice(&data);
|
|
|
|
|
/// assert_eq!(pt, Point3::new(1.0, 2.0, 3.0));
|
|
|
|
|
/// ```
|
2018-10-13 16:45:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_slice(components: &[N]) -> Self {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
Self::from(VectorN::from_row_slice(components))
|
2018-10-13 16:45:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Creates a new point from its homogeneous vector representation.
|
|
|
|
|
///
|
|
|
|
|
/// In practice, this builds a D-dimensional points with the same first D component as `v`
|
|
|
|
|
/// divided by the last component of `v`. Returns `None` if this divisor is zero.
|
2018-10-24 03:59:10 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Point2, Point3, Vector3, Vector4};
|
|
|
|
|
///
|
|
|
|
|
/// let coords = Vector4::new(1.0, 2.0, 3.0, 1.0);
|
|
|
|
|
/// let pt = Point3::from_homogeneous(coords);
|
|
|
|
|
/// assert_eq!(pt, Some(Point3::new(1.0, 2.0, 3.0)));
|
|
|
|
|
///
|
|
|
|
|
/// // All component of the result will be divided by the
|
|
|
|
|
/// // last component of the vector, here 2.0.
|
|
|
|
|
/// let coords = Vector4::new(1.0, 2.0, 3.0, 2.0);
|
|
|
|
|
/// let pt = Point3::from_homogeneous(coords);
|
|
|
|
|
/// assert_eq!(pt, Some(Point3::new(0.5, 1.0, 1.5)));
|
|
|
|
|
///
|
|
|
|
|
/// // Fails because the last component is zero.
|
|
|
|
|
/// let coords = Vector4::new(1.0, 2.0, 3.0, 0.0);
|
|
|
|
|
/// let pt = Point3::from_homogeneous(coords);
|
|
|
|
|
/// assert!(pt.is_none());
|
|
|
|
|
///
|
|
|
|
|
/// // Works also in other dimensions.
|
|
|
|
|
/// let coords = Vector3::new(1.0, 2.0, 1.0);
|
|
|
|
|
/// let pt = Point2::from_homogeneous(coords);
|
|
|
|
|
/// assert_eq!(pt, Some(Point2::new(1.0, 2.0)));
|
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn from_homogeneous(v: VectorN<N, DimNameSum<D, U1>>) -> Option<Self>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
N: Scalar + Zero + One + ClosedDiv,
|
|
|
|
|
D: DimNameAdd<U1>,
|
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
if !v[D::dim()].is_zero() {
|
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
|
|
|
|
Some(Self::from(coords))
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
2018-09-24 12:48:42 +08:00
|
|
|
|
* Traits that build points.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
*
|
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar + Bounded, D: DimName> Bounded for Point<N, D>
|
2018-10-22 13:00:10 +08:00
|
|
|
|
where DefaultAllocator: Allocator<N, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn max_value() -> Self {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
Self::from(VectorN::max_value())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn min_value() -> Self {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
Self::from(VectorN::min_value())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 05:58:14 +08:00
|
|
|
|
impl<N: Scalar, D: DimName> Distribution<Point<N, D>> for Standard
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, D>,
|
2018-05-23 05:58:14 +08:00
|
|
|
|
Standard: Distribution<N>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2018-05-23 05:58:14 +08:00
|
|
|
|
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point<N, D> {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
Point::from(rng.gen::<VectorN<N, D>>())
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar + Arbitrary + Send, D: DimName> Arbitrary for Point<N, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, D>,
|
|
|
|
|
<DefaultAllocator as Allocator<N, D>>::Buffer: Send,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
|
Self::from(VectorN::arbitrary(g))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Small points construction from components.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
macro_rules! componentwise_constructors_impl(
|
2018-10-27 17:00:11 +08:00
|
|
|
|
($($doc: expr; $D: ty, $($args: ident:$irow: expr),*);* $(;)*) => {$(
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar> Point<N, $D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, $D> {
|
2018-10-28 14:33:39 +08:00
|
|
|
|
#[doc = "Initializes this point from its components."]
|
2018-10-27 17:00:11 +08:00
|
|
|
|
#[doc = "# Example\n```"]
|
|
|
|
|
#[doc = $doc]
|
|
|
|
|
#[doc = "```"]
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2019-02-17 05:29:41 +08:00
|
|
|
|
pub fn new($($args: N),*) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
unsafe {
|
|
|
|
|
let mut res = Self::new_uninitialized();
|
|
|
|
|
$( *res.get_unchecked_mut($irow) = $args; )*
|
|
|
|
|
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)*}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
componentwise_constructors_impl!(
|
2018-10-27 17:00:11 +08:00
|
|
|
|
"# use nalgebra::Point1;\nlet p = Point1::new(1.0);\nassert!(p.x == 1.0);";
|
2016-12-05 05:44:42 +08:00
|
|
|
|
U1, x:0;
|
2018-10-27 17:00:11 +08:00
|
|
|
|
"# use nalgebra::Point2;\nlet p = Point2::new(1.0, 2.0);\nassert!(p.x == 1.0 && p.y == 2.0);";
|
2016-12-05 05:44:42 +08:00
|
|
|
|
U2, x:0, y:1;
|
2018-10-27 17:00:11 +08:00
|
|
|
|
"# 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);";
|
2016-12-05 05:44:42 +08:00
|
|
|
|
U3, x:0, y:1, z:2;
|
2018-10-27 17:00:11 +08:00
|
|
|
|
"# 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);";
|
2016-12-05 05:44:42 +08:00
|
|
|
|
U4, x:0, y:1, z:2, w:3;
|
2018-10-27 17:00:11 +08:00
|
|
|
|
"# 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);";
|
2016-12-05 05:44:42 +08:00
|
|
|
|
U5, x:0, y:1, z:2, w:3, a:4;
|
2018-10-27 17:00:11 +08:00
|
|
|
|
"# 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);";
|
2016-12-05 05:44:42 +08:00
|
|
|
|
U6, x:0, y:1, z:2, w:3, a:4, b:5;
|
|
|
|
|
);
|
2018-04-01 03:42:22 +08:00
|
|
|
|
|
|
|
|
|
macro_rules! from_array_impl(
|
|
|
|
|
($($D: ty, $len: expr);*) => {$(
|
2018-05-27 18:25:58 +08:00
|
|
|
|
impl <N: Scalar> From<[N; $len]> for Point<N, $D> {
|
|
|
|
|
fn from (coords: [N; $len]) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
|
Self {
|
2018-10-24 02:47:42 +08:00
|
|
|
|
coords: coords.into()
|
|
|
|
|
}
|
2018-04-01 03:42:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)*}
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-27 18:25:58 +08:00
|
|
|
|
from_array_impl!(U1, 1; U2, 2; U3, 3; U4, 4; U5, 5; U6, 6);
|