nalgebra/geometry/
translation_construction.rs1#[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    Rng,
10    distr::{Distribution, StandardUniform},
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    #[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    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 StandardUniform
73where
74    StandardUniform: Distribution<T>,
75{
76    #[inline]
78    fn sample<G: Rng + ?Sized>(&self, rng: &mut G) -> Translation<T, D> {
79        Translation::from(rng.random::<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
95macro_rules! componentwise_constructors_impl(
101    ($($doc: expr_2021; $D: expr_2021, $($args: ident:$irow: expr_2021),*);* $(;)*) => {$(
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);