nalgebra/geometry/
scale_construction.rs1#[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 #[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 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 #[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
89macro_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);