1#[cfg(feature = "arbitrary")]
2use quickcheck::{Arbitrary, Gen};
3
4use num::{Bounded, One, Zero};
5#[cfg(feature = "rand-no-std")]
6use rand::{
7    Rng,
8    distr::{Distribution, StandardUniform},
9};
10
11use crate::base::allocator::Allocator;
12use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
13use crate::base::{DefaultAllocator, Scalar};
14use crate::{
15    Const, DimName, OPoint, OVector, Point1, Point2, Point3, Point4, Point5, Point6, Vector1,
16    Vector2, Vector3, Vector4, Vector5, Vector6,
17};
18use simba::scalar::{ClosedDivAssign, SupersetOf};
19
20use crate::geometry::Point;
21
22impl<T: Scalar + Zero, D: DimName> Default for OPoint<T, D>
23where
24    DefaultAllocator: Allocator<D>,
25{
26    fn default() -> Self {
27        Self::origin()
28    }
29}
30
31impl<T: Scalar, D: DimName> OPoint<T, D>
33where
34    DefaultAllocator: Allocator<D>,
35{
36    #[inline]
52    pub fn origin() -> Self
53    where
54        T: Zero,
55    {
56        Self::from(OVector::from_element(T::zero()))
57    }
58
59    #[inline]
74    pub fn from_slice(components: &[T]) -> Self {
75        Self::from(OVector::from_row_slice(components))
76    }
77
78    #[inline]
109    pub fn from_homogeneous(v: OVector<T, DimNameSum<D, U1>>) -> Option<Self>
110    where
111        T: Scalar + Zero + One + ClosedDivAssign,
112        D: DimNameAdd<U1>,
113        DefaultAllocator: Allocator<DimNameSum<D, U1>>,
114    {
115        if !v[D::DIM].is_zero() {
116            let coords = v.generic_view((0, 0), (D::name(), Const::<1>)) / v[D::DIM].clone();
117            Some(Self::from(coords))
118        } else {
119            None
120        }
121    }
122
123    pub fn cast<To: Scalar>(self) -> OPoint<To, D>
133    where
134        OPoint<To, D>: SupersetOf<Self>,
135        DefaultAllocator: Allocator<D>,
136    {
137        crate::convert(self)
138    }
139}
140
141impl<T: Scalar + Bounded, D: DimName> Bounded for OPoint<T, D>
147where
148    DefaultAllocator: Allocator<D>,
149{
150    #[inline]
151    fn max_value() -> Self {
152        Self::from(OVector::max_value())
153    }
154
155    #[inline]
156    fn min_value() -> Self {
157        Self::from(OVector::min_value())
158    }
159}
160
161#[cfg(feature = "rand-no-std")]
162impl<T: Scalar, D: DimName> Distribution<OPoint<T, D>> for StandardUniform
163where
164    StandardUniform: Distribution<T>,
165    DefaultAllocator: Allocator<D>,
166{
167    #[inline]
169    fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> OPoint<T, D> {
170        OPoint::from(rng.random::<OVector<T, D>>())
171    }
172}
173
174#[cfg(feature = "arbitrary")]
175impl<T: Scalar + Arbitrary + Send, D: DimName> Arbitrary for OPoint<T, D>
176where
177    <DefaultAllocator as Allocator<D>>::Buffer<T>: Send,
178    DefaultAllocator: Allocator<D>,
179{
180    #[inline]
181    fn arbitrary(g: &mut Gen) -> Self {
182        Self::from(OVector::arbitrary(g))
183    }
184}
185
186impl<T: Scalar> Point1<T> {
195    #[inline]
205    pub const fn new(x: T) -> Self {
206        Point {
207            coords: Vector1::new(x),
208        }
209    }
210}
211macro_rules! componentwise_constructors_impl(
212    ($($doc: expr_2021; $Point: ident, $Vector: ident, $($args: ident:$irow: expr_2021),*);* $(;)*) => {$(
213        impl<T: Scalar> $Point<T> {
214            #[doc = "Initializes this point from its components."]
215            #[doc = "# Example\n```"]
216            #[doc = $doc]
217            #[doc = "```"]
218            #[inline]
219            pub const fn new($($args: T),*) -> Self {
220                Point { coords: $Vector::new($($args),*) }
221            }
222        }
223    )*}
224);
225
226componentwise_constructors_impl!(
227    "# use nalgebra::Point2;\nlet p = Point2::new(1.0, 2.0);\nassert!(p.x == 1.0 && p.y == 2.0);";
228    Point2, Vector2, x:0, y:1;
229    "# 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);";
230    Point3, Vector3, x:0, y:1, z:2;
231    "# 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);";
232    Point4, Vector4, x:0, y:1, z:2, w:3;
233    "# 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);";
234    Point5, Vector5, x:0, y:1, z:2, w:3, a:4;
235    "# 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);";
236    Point6, Vector6, x:0, y:1, z:2, w:3, a:4, b:5;
237);