nalgebra/geometry/
scale_construction.rs

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