point_construction.rs - source
Description: Source of the Rust file `/Users/erlendbasso/.cargo/registry/src/github.com-1ecc6299db9ec823/nalgebra-0.32.1/src/geometry/point_construction.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268

    #[cfg(feature = "arbitrary")]
    use quickcheck::{Arbitrary, Gen};

    use num::{Bounded, One, Zero};
    #[cfg(feature = "rand-no-std")]
    use rand::{
        distributions::{Distribution, Standard},
        Rng,
    };

    use crate::base::allocator::Allocator;
    use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
    use crate::base::{DefaultAllocator, Scalar};
    use crate::{
        Const, DimName, OPoint, OVector, Point1, Point2, Point3, Point4, Point5, Point6, Vector1,
        Vector2, Vector3, Vector4, Vector5, Vector6,
    };
    use simba::scalar::{ClosedDiv, SupersetOf};

    use crate::geometry::Point;

    impl<T: Scalar + Zero, D: DimName> Default for OPoint<T, D>
    where
        DefaultAllocator: Allocator<T, D>,
    {
        fn default() -> Self {
            Self::origin()
        }
    }

    /// # Other construction methods
    impl<T: Scalar, D: DimName> OPoint<T, D>
    where
        DefaultAllocator: Allocator<T, D>,
    {
        /// Creates a new point with all coordinates equal to zero.
        ///
        /// # Example
        ///
        /// ```
        /// # use nalgebra::{Point2, Point3};
        /// // This works in any dimension.
        /// // The explicit crate::<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);
        /// ```
        #[inline]
        pub fn origin() -> Self
        where
            T: Zero,
        {
            Self::from(OVector::from_element(T::zero()))
        }

        /// Creates a new point from a slice.
        ///
        /// # 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));
        /// ```
        #[inline]
        pub fn from_slice(components: &[T]) -> Self {
            Self::from(OVector::from_row_slice(components))
        }

        /// 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.
        ///
        /// # 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)));
        /// ```
        #[inline]
        pub fn from_homogeneous(v: OVector<T, DimNameSum<D, U1>>) -> Option<Self>
        where
            T: Scalar + Zero + One + ClosedDiv,
            D: DimNameAdd<U1>,
            DefaultAllocator: Allocator<T, DimNameSum<D, U1>>,
        {
            if !v[D::dim()].is_zero() {
                let coords = v.generic_view((0, 0), (D::name(), Const::<1>)) / v[D::dim()].clone();
                Some(Self::from(coords))
            } else {
                None
            }
        }

        /// 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));
        /// ```
        pub fn cast<To: Scalar>(self) -> OPoint<To, D>
        where
            OPoint<To, D>: SupersetOf<Self>,
            DefaultAllocator: Allocator<To, D>,
        {
            crate::convert(self)
        }
    }

    /*
     *
     * Traits that build points.
     *
     */
    impl<T: Scalar + Bounded, D: DimName> Bounded for OPoint<T, D>
    where
        DefaultAllocator: Allocator<T, D>,
    {
        #[inline]
        fn max_value() -> Self {
            Self::from(OVector::max_value())
        }

        #[inline]
        fn min_value() -> Self {
            Self::from(OVector::min_value())
        }
    }

    #[cfg(feature = "rand-no-std")]
    impl<T: Scalar, D: DimName> Distribution<OPoint<T, D>> for Standard
    where
        Standard: Distribution<T>,
        DefaultAllocator: Allocator<T, D>,
    {
        /// Generate a `Point` where each coordinate is an independent variate from `[0, 1)`.
        #[inline]
        fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> OPoint<T, D> {
            OPoint::from(rng.gen::<OVector<T, D>>())
        }
    }

    #[cfg(feature = "arbitrary")]
    impl<T: Scalar + Arbitrary + Send, D: DimName> Arbitrary for OPoint<T, D>
    where
        <DefaultAllocator as Allocator<T, D>>::Buffer: Send,
        DefaultAllocator: Allocator<T, D>,
    {
        #[inline]
        fn arbitrary(g: &mut Gen) -> Self {
            Self::from(OVector::arbitrary(g))
        }
    }

    /*
     *
     * Small points construction from components.
     *
     */
    // 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
    impl<T: Scalar> Point1<T> {
        /// Initializes this point from its components.
        ///
        /// # Example
        ///
        /// ```
        /// # use nalgebra::Point1;
        /// let p = Point1::new(1.0);
        /// assert_eq!(p.x, 1.0);
        /// ```
        #[inline]
        #[cfg(not(feature = "cuda"))]
        pub const fn new(x: T) -> Self {
            Point {
                coords: Vector1::new(x),
            }
        }

        /// Initializes this point from its components.
        ///
        /// # Example
        ///
        /// ```
        /// # use nalgebra::Point1;
        /// let p = Point1::new(1.0);
        /// assert_eq!(p.x, 1.0);
        /// ```
        #[inline]
        #[cfg(feature = "cuda")]
        pub fn new(x: T) -> Self {
            Point {
                coords: Vector1::new(x),
            }
        }
    }
    macro_rules! componentwise_constructors_impl(
        ($($doc: expr; $Point: ident, $Vector: ident, $($args: ident:$irow: expr),*);* $(;)*) => {$(
            impl<T: Scalar> $Point<T> {
                #[doc = "Initializes this point from its components."]
                #[doc = "# Example\n```"]
                #[doc = $doc]
                #[doc = "```"]
                #[inline]
                #[cfg(not(feature = "cuda"))]
                pub const fn new($($args: T),*) -> Self {
                    Point { coords: $Vector::new($($args),*) }
                }

                // TODO: always let new be const once CUDA updates its supported
                //       nightly version to something more recent.
                #[doc = "Initializes this point from its components."]
                #[doc = "# Example\n```"]
                #[doc = $doc]
                #[doc = "```"]
                #[inline]
                #[cfg(feature = "cuda")]
                pub fn new($($args: T),*) -> Self {
                    Point { coords: $Vector::new($($args),*) }
                }
            }
        )*}
    );

    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;
    );