glam/features/
impl_rand.rs

1macro_rules! impl_vec_types {
2    ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident) => {
3        impl Distribution<$vec2> for Standard {
4            #[inline]
5            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec2 {
6                rng.gen::<[$t; 2]>().into()
7            }
8        }
9
10        impl Distribution<$vec3> for Standard {
11            #[inline]
12            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec3 {
13                rng.gen::<[$t; 3]>().into()
14            }
15        }
16
17        impl Distribution<$vec4> for Standard {
18            #[inline]
19            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec4 {
20                rng.gen::<[$t; 4]>().into()
21            }
22        }
23
24        #[test]
25        fn test_vec2_rand() {
26            use rand::{Rng, SeedableRng};
27            use rand_xoshiro::Xoshiro256Plus;
28            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
29            let a: ($t, $t) = rng1.gen();
30            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
31            let b: $vec2 = rng2.gen();
32            assert_eq!(a, b.into());
33        }
34
35        #[test]
36        fn test_vec3_rand() {
37            use rand::{Rng, SeedableRng};
38            use rand_xoshiro::Xoshiro256Plus;
39            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
40            let a: ($t, $t, $t) = rng1.gen();
41            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
42            let b: $vec3 = rng2.gen();
43            assert_eq!(a, b.into());
44        }
45
46        #[test]
47        fn test_vec4_rand() {
48            use rand::{Rng, SeedableRng};
49            use rand_xoshiro::Xoshiro256Plus;
50            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
51            let a: ($t, $t, $t, $t) = rng1.gen();
52            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
53            let b: $vec4 = rng2.gen();
54            assert_eq!(a, b.into());
55        }
56    };
57}
58
59macro_rules! impl_float_types {
60    ($t:ident, $mat2:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec2:ident, $vec3:ident, $vec4:ident) => {
61        impl_vec_types!($t, $vec2, $vec3, $vec4);
62
63        impl Distribution<$mat2> for Standard {
64            #[inline]
65            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat2 {
66                $mat2::from_cols_array(&rng.gen())
67            }
68        }
69
70        impl Distribution<$mat3> for Standard {
71            #[inline]
72            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat3 {
73                $mat3::from_cols_array(&rng.gen())
74            }
75        }
76
77        impl Distribution<$mat4> for Standard {
78            #[inline]
79            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat4 {
80                $mat4::from_cols_array(&rng.gen())
81            }
82        }
83
84        impl Distribution<$quat> for Standard {
85            #[inline]
86            fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $quat {
87                let yaw = -PI + rng.gen::<$t>() * 2.0 * PI;
88                let pitch = -PI + rng.gen::<$t>() * 2.0 * PI;
89                let roll = -PI + rng.gen::<$t>() * 2.0 * PI;
90                $quat::from_euler(crate::EulerRot::YXZ, yaw, pitch, roll)
91            }
92        }
93
94        #[test]
95        fn test_mat2_rand() {
96            use rand::{Rng, SeedableRng};
97            use rand_xoshiro::Xoshiro256Plus;
98            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
99            let a = $mat2::from_cols_array(&rng1.gen::<[$t; 4]>());
100            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
101            let b = rng2.gen::<$mat2>();
102            assert_eq!(a, b);
103        }
104
105        #[test]
106        fn test_mat3_rand() {
107            use rand::{Rng, SeedableRng};
108            use rand_xoshiro::Xoshiro256Plus;
109            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
110            let a = $mat3::from_cols_array(&rng1.gen::<[$t; 9]>());
111            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
112            let b = rng2.gen::<$mat3>();
113            assert_eq!(a, b);
114        }
115
116        #[test]
117        fn test_mat4_rand() {
118            use rand::{Rng, SeedableRng};
119            use rand_xoshiro::Xoshiro256Plus;
120            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
121            let a = $mat4::from_cols_array(&rng1.gen::<[$t; 16]>());
122            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
123            let b = rng2.gen::<$mat4>();
124            assert_eq!(a, b);
125        }
126
127        #[test]
128        fn test_quat_rand() {
129            use rand::{Rng, SeedableRng};
130            use rand_xoshiro::Xoshiro256Plus;
131            let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
132            let a: $quat = rng1.gen();
133            assert!(a.is_normalized());
134            let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
135            let b: $quat = rng2.gen();
136            assert_eq!(a, b);
137        }
138    };
139}
140
141mod f32 {
142    use crate::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
143    use core::f32::consts::PI;
144    use rand::{
145        distributions::{Distribution, Standard},
146        Rng,
147    };
148
149    impl_float_types!(f32, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4);
150
151    impl Distribution<Vec3A> for Standard {
152        #[inline]
153        fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec3A {
154            rng.gen::<[f32; 3]>().into()
155        }
156    }
157
158    #[test]
159    fn test_vec3a_rand() {
160        use rand::{Rng, SeedableRng};
161        use rand_xoshiro::Xoshiro256Plus;
162        let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
163        let a: (f32, f32, f32) = rng1.gen();
164        let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
165        let b: Vec3A = rng2.gen();
166        assert_eq!(a, b.into());
167    }
168}
169
170mod f64 {
171    use crate::{DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4};
172    use core::f64::consts::PI;
173    use rand::{
174        distributions::{Distribution, Standard},
175        Rng,
176    };
177
178    impl_float_types!(f64, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4);
179}
180
181mod i16 {
182    use crate::{I16Vec2, I16Vec3, I16Vec4};
183    use rand::{
184        distributions::{Distribution, Standard},
185        Rng,
186    };
187
188    impl_vec_types!(i16, I16Vec2, I16Vec3, I16Vec4);
189}
190
191mod i32 {
192    use crate::{IVec2, IVec3, IVec4};
193    use rand::{
194        distributions::{Distribution, Standard},
195        Rng,
196    };
197
198    impl_vec_types!(i32, IVec2, IVec3, IVec4);
199}
200
201mod i64 {
202    use crate::{I64Vec2, I64Vec3, I64Vec4};
203    use rand::{
204        distributions::{Distribution, Standard},
205        Rng,
206    };
207
208    impl_vec_types!(i64, I64Vec2, I64Vec3, I64Vec4);
209}
210
211mod u16 {
212    use crate::{U16Vec2, U16Vec3, U16Vec4};
213    use rand::{
214        distributions::{Distribution, Standard},
215        Rng,
216    };
217
218    impl_vec_types!(u16, U16Vec2, U16Vec3, U16Vec4);
219}
220
221mod u32 {
222    use crate::{UVec2, UVec3, UVec4};
223    use rand::{
224        distributions::{Distribution, Standard},
225        Rng,
226    };
227
228    impl_vec_types!(u32, UVec2, UVec3, UVec4);
229}
230
231mod u64 {
232    use crate::{U64Vec2, U64Vec3, U64Vec4};
233    use rand::{
234        distributions::{Distribution, Standard},
235        Rng,
236    };
237
238    impl_vec_types!(u64, U64Vec2, U64Vec3, U64Vec4);
239}