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};
|
2021-03-02 19:25:12 +08:00
|
|
|
#[cfg(feature = "rand-no-std")]
|
|
|
|
use rand::{
|
|
|
|
distributions::{Distribution, Standard},
|
|
|
|
Rng,
|
|
|
|
};
|
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};
|
2021-06-27 00:39:02 +08:00
|
|
|
use crate::base::{DefaultAllocator, Scalar};
|
2020-11-21 00:45:11 +08:00
|
|
|
use crate::{
|
2021-06-27 00:39:02 +08:00
|
|
|
Const, DimName, OPoint, OVector, Point1, Point2, Point3, Point4, Point5, Point6, Vector1,
|
|
|
|
Vector2, Vector3, Vector4, Vector5, Vector6,
|
2020-11-21 00:45:11 +08:00
|
|
|
};
|
2021-03-06 00:08:46 +08:00
|
|
|
use simba::scalar::{ClosedDiv, SupersetOf};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::geometry::Point;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2020-11-21 00:45:11 +08:00
|
|
|
/// # Other construction methods
|
2021-06-27 00:39:02 +08:00
|
|
|
impl<T: Scalar, D: DimName> OPoint<T, D>
|
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<T, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Creates a new point with uninitialized coordinates.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn new_uninitialized() -> Self {
|
2020-11-28 05:00:48 +08:00
|
|
|
Self::from(crate::unimplemented_or_uninitialized_generic!(
|
2021-06-27 00:39:02 +08:00
|
|
|
D::name(),
|
|
|
|
Const::<1>
|
2020-11-28 05:00:48 +08:00
|
|
|
))
|
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.
|
2019-03-23 21:29:07 +08:00
|
|
|
/// // The explicit crate::<f32> type annotation may not always be needed,
|
2018-10-24 03:59:10 +08:00
|
|
|
/// // 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
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: Zero,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
2021-06-27 00:39:02 +08:00
|
|
|
Self::from(OVector::from_element(T::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]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn from_slice(components: &[T]) -> Self {
|
2021-06-27 00:39:02 +08:00
|
|
|
Self::from(OVector::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
|
2020-11-21 00:45:11 +08:00
|
|
|
/// // last component of the vector, here 2.0.
|
2018-10-24 03:59:10 +08:00
|
|
|
/// 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]
|
2021-06-27 00:39:02 +08:00
|
|
|
pub fn from_homogeneous(v: OVector<T, DimNameSum<D, U1>>) -> Option<Self>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: Scalar + Zero + One + ClosedDiv,
|
2021-06-27 00:39:02 +08:00
|
|
|
D: DimNameAdd<U1>,
|
|
|
|
DefaultAllocator: Allocator<T, DimNameSum<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2021-06-27 00:39:02 +08:00
|
|
|
if !v[D::dim()].is_zero() {
|
|
|
|
let coords =
|
|
|
|
v.generic_slice((0, 0), (D::name(), Const::<1>)) / v[D::dim()].inlined_clone();
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-03-06 00:08:46 +08:00
|
|
|
|
|
|
|
/// Cast the components of `self` to another type.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::Point2;
|
|
|
|
/// let pt = Point2::new(1.0f64, 2.0);
|
|
|
|
/// let pt2 = pt.cast::<f32>();
|
|
|
|
/// assert_eq!(pt2, Point2::new(1.0f32, 2.0));
|
|
|
|
/// ```
|
2021-06-27 00:39:02 +08:00
|
|
|
pub fn cast<To: Scalar>(self) -> OPoint<To, D>
|
2021-03-06 00:08:46 +08:00
|
|
|
where
|
2021-06-27 00:39:02 +08:00
|
|
|
OPoint<To, D>: SupersetOf<Self>,
|
|
|
|
DefaultAllocator: Allocator<To, D>,
|
2021-03-06 00:08:46 +08:00
|
|
|
{
|
|
|
|
crate::convert(self)
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
2018-09-24 12:48:42 +08:00
|
|
|
* Traits that build points.
|
2016-12-05 05:44:42 +08:00
|
|
|
*
|
|
|
|
*/
|
2021-06-27 00:39:02 +08:00
|
|
|
impl<T: Scalar + Bounded, D: DimName> Bounded for OPoint<T, D>
|
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<T, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn max_value() -> Self {
|
2021-06-27 00:39:02 +08:00
|
|
|
Self::from(OVector::max_value())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn min_value() -> Self {
|
2021-06-27 00:39:02 +08:00
|
|
|
Self::from(OVector::min_value())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:25:12 +08:00
|
|
|
#[cfg(feature = "rand-no-std")]
|
2021-06-27 00:39:02 +08:00
|
|
|
impl<T: Scalar, D: DimName> Distribution<OPoint<T, D>> for Standard
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
Standard: Distribution<T>,
|
2021-06-27 00:39:02 +08:00
|
|
|
DefaultAllocator: Allocator<T, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2021-04-11 20:09:01 +08:00
|
|
|
/// Generate a `Point` where each coordinate is an independent variate from `[0, 1)`.
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> OPoint<T, D> {
|
2021-07-02 18:01:01 +08:00
|
|
|
OPoint::from(rng.gen::<OVector<T, D>>())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2021-06-27 00:39:02 +08:00
|
|
|
impl<T: Scalar + Arbitrary + Send, D: DimName> Arbitrary for OPoint<T, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-06-27 00:39:02 +08:00
|
|
|
<DefaultAllocator as Allocator<T, D>>::Buffer: Send,
|
|
|
|
DefaultAllocator: Allocator<T, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-03-01 00:52:14 +08:00
|
|
|
fn arbitrary(g: &mut Gen) -> Self {
|
2021-06-27 00:39:02 +08:00
|
|
|
Self::from(OVector::arbitrary(g))
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Small points construction from components.
|
|
|
|
*
|
|
|
|
*/
|
2020-11-21 00:45:11 +08:00
|
|
|
// NOTE: the impl for Point1 is not with the others so that we
|
|
|
|
// can add a section with the impl block comment.
|
|
|
|
/// # Construction from individual components
|
2021-06-27 00:39:02 +08:00
|
|
|
impl<T: Scalar> Point1<T> {
|
2020-11-21 00:45:11 +08:00
|
|
|
/// Initializes this point from its components.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::Point1;
|
|
|
|
/// let p = Point1::new(1.0);
|
|
|
|
/// assert_eq!(p.x, 1.0);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2021-06-27 00:39:02 +08:00
|
|
|
pub fn new(x: T) -> Self {
|
2021-04-12 16:32:17 +08:00
|
|
|
Point {
|
|
|
|
coords: Vector1::new(x),
|
|
|
|
}
|
2020-11-21 00:45:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
macro_rules! componentwise_constructors_impl(
|
2020-11-21 00:45:11 +08:00
|
|
|
($($doc: expr; $Point: ident, $Vector: ident, $($args: ident:$irow: expr),*);* $(;)*) => {$(
|
2021-06-27 00:39:02 +08:00
|
|
|
impl<T: Scalar> $Point<T> {
|
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]
|
2021-06-27 00:39:02 +08:00
|
|
|
pub fn new($($args: T),*) -> Self {
|
2021-04-12 16:32:17 +08:00
|
|
|
Point { coords: $Vector::new($($args),*) }
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
|
|
|
componentwise_constructors_impl!(
|
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);";
|
2020-11-21 00:45:11 +08:00
|
|
|
Point2, Vector2, 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);";
|
2020-11-21 00:45:11 +08:00
|
|
|
Point3, Vector3, 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);";
|
2020-11-21 00:45:11 +08:00
|
|
|
Point4, Vector4, 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);";
|
2020-11-21 00:45:11 +08:00
|
|
|
Point5, Vector5, 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);";
|
2020-11-21 00:45:11 +08:00
|
|
|
Point6, Vector6, x:0, y:1, z:2, w:3, a:4, b:5;
|
2016-12-05 05:44:42 +08:00
|
|
|
);
|