nalgebra/geometry/
translation_construction.rs

1#[cfg(feature = "arbitrary")]
2use crate::base::storage::Owned;
3#[cfg(feature = "arbitrary")]
4use quickcheck::{Arbitrary, Gen};
5
6use num::{One, Zero};
7#[cfg(feature = "rand-no-std")]
8use rand::{
9    distributions::{Distribution, Standard},
10    Rng,
11};
12
13use simba::scalar::{ClosedAddAssign, SupersetOf};
14
15use crate::base::{SVector, Scalar};
16use crate::geometry::Translation;
17
18impl<T: Scalar + Zero, const D: usize> Default for Translation<T, D> {
19    fn default() -> Self {
20        Self::identity()
21    }
22}
23
24impl<T: Scalar, const D: usize> Translation<T, D> {
25    /// Creates a new identity translation.
26    ///
27    /// # Example
28    /// ```
29    /// # use nalgebra::{Point2, Point3, Translation2, Translation3};
30    /// let t = Translation2::identity();
31    /// let p = Point2::new(1.0, 2.0);
32    /// assert_eq!(t * p, p);
33    ///
34    /// // Works in all dimensions.
35    /// let t = Translation3::identity();
36    /// let p = Point3::new(1.0, 2.0, 3.0);
37    /// assert_eq!(t * p, p);
38    /// ```
39    #[inline]
40    pub fn identity() -> Translation<T, D>
41    where
42        T: Zero,
43    {
44        Self::from(SVector::<T, D>::from_element(T::zero()))
45    }
46
47    /// Cast the components of `self` to another type.
48    ///
49    /// # Example
50    /// ```
51    /// # use nalgebra::Translation2;
52    /// let tra = Translation2::new(1.0f64, 2.0);
53    /// let tra2 = tra.cast::<f32>();
54    /// assert_eq!(tra2, Translation2::new(1.0f32, 2.0));
55    /// ```
56    pub fn cast<To: Scalar>(self) -> Translation<To, D>
57    where
58        Translation<To, D>: SupersetOf<Self>,
59    {
60        crate::convert(self)
61    }
62}
63
64impl<T: Scalar + Zero + ClosedAddAssign, const D: usize> One for Translation<T, D> {
65    #[inline]
66    fn one() -> Self {
67        Self::identity()
68    }
69}
70
71#[cfg(feature = "rand-no-std")]
72impl<T: Scalar, const D: usize> Distribution<Translation<T, D>> for Standard
73where
74    Standard: Distribution<T>,
75{
76    /// Generate an arbitrary random variate for testing purposes.
77    #[inline]
78    fn sample<G: Rng + ?Sized>(&self, rng: &mut G) -> Translation<T, D> {
79        Translation::from(rng.gen::<SVector<T, D>>())
80    }
81}
82
83#[cfg(feature = "arbitrary")]
84impl<T: Scalar + Arbitrary + Send, const D: usize> Arbitrary for Translation<T, D>
85where
86    Owned<T, crate::Const<D>>: Send,
87{
88    #[inline]
89    fn arbitrary(rng: &mut Gen) -> Self {
90        let v: SVector<T, D> = Arbitrary::arbitrary(rng);
91        Self::from(v)
92    }
93}
94
95/*
96 *
97 * Small translation construction from components.
98 *
99 */
100macro_rules! componentwise_constructors_impl(
101    ($($doc: expr; $D: expr, $($args: ident:$irow: expr),*);* $(;)*) => {$(
102        impl<T> Translation<T, $D>
103             {
104            #[doc = "Initializes this translation from its components."]
105            #[doc = "# Example\n```"]
106            #[doc = $doc]
107            #[doc = "```"]
108            #[inline]
109            pub const fn new($($args: T),*) -> Self {
110                Self { vector: SVector::<T, $D>::new($($args),*) }
111            }
112        }
113    )*}
114);
115
116componentwise_constructors_impl!(
117    "# use nalgebra::Translation1;\nlet t = Translation1::new(1.0);\nassert!(t.vector.x == 1.0);";
118    1, x:0;
119    "# use nalgebra::Translation2;\nlet t = Translation2::new(1.0, 2.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0);";
120    2, x:0, y:1;
121    "# use nalgebra::Translation3;\nlet t = Translation3::new(1.0, 2.0, 3.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0);";
122    3, x:0, y:1, z:2;
123    "# use nalgebra::Translation4;\nlet t = Translation4::new(1.0, 2.0, 3.0, 4.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0 && t.vector.w == 4.0);";
124    4, x:0, y:1, z:2, w:3;
125    "# use nalgebra::Translation5;\nlet t = Translation5::new(1.0, 2.0, 3.0, 4.0, 5.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0 && t.vector.w == 4.0 && t.vector.a == 5.0);";
126    5, x:0, y:1, z:2, w:3, a:4;
127    "# use nalgebra::Translation6;\nlet t = Translation6::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0 && t.vector.w == 4.0 && t.vector.a == 5.0 && t.vector.b == 6.0);";
128    6, x:0, y:1, z:2, w:3, a:4, b:5;
129);