1 #![allow(dead_code)]
2 use core::f32;
3 use glam::{Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
4
5 pub struct PCG32 {
6 state: u64,
7 inc: u64,
8 }
9
10 impl Default for PCG32 {
default() -> Self11 fn default() -> Self {
12 PCG32::seed(0x853c49e6748fea9b, 0xda3e39cb94b95bdb)
13 }
14 }
15
16 impl PCG32 {
seed(initstate: u64, initseq: u64) -> Self17 pub fn seed(initstate: u64, initseq: u64) -> Self {
18 let mut rng = PCG32 {
19 state: 0,
20 inc: (initseq << 1) | 1,
21 };
22 rng.next_u32();
23 rng.state = rng.state.wrapping_add(initstate);
24 rng.next_u32();
25 rng
26 }
27
next_u32(&mut self) -> u3228 pub fn next_u32(&mut self) -> u32 {
29 let oldstate = self.state;
30 self.state = oldstate
31 .wrapping_mul(6364136223846793005)
32 .wrapping_add(self.inc | 1);
33 let xorshifted = ((oldstate >> 18) ^ oldstate) >> 27;
34 let rot = oldstate >> 59;
35 ((xorshifted >> rot) | (xorshifted << (rot.wrapping_neg() & 31))) as u32
36 }
37
next_f32(&mut self) -> f3238 pub fn next_f32(&mut self) -> f32 {
39 (self.next_u32() & 0xffffff) as f32 / 16777216.0
40 }
41 }
42
random_vec2(rng: &mut PCG32) -> Vec243 pub fn random_vec2(rng: &mut PCG32) -> Vec2 {
44 Vec2::new(rng.next_f32(), rng.next_f32())
45 }
46
random_vec3(rng: &mut PCG32) -> Vec347 pub fn random_vec3(rng: &mut PCG32) -> Vec3 {
48 Vec3::new(rng.next_f32(), rng.next_f32(), rng.next_f32())
49 }
50
random_vec3a(rng: &mut PCG32) -> Vec3A51 pub fn random_vec3a(rng: &mut PCG32) -> Vec3A {
52 Vec3A::new(rng.next_f32(), rng.next_f32(), rng.next_f32())
53 }
54
random_vec4(rng: &mut PCG32) -> Vec455 pub fn random_vec4(rng: &mut PCG32) -> Vec4 {
56 Vec4::new(
57 rng.next_f32(),
58 rng.next_f32(),
59 rng.next_f32(),
60 rng.next_f32(),
61 )
62 }
63
random_nonzero_vec2(rng: &mut PCG32) -> Vec264 pub fn random_nonzero_vec2(rng: &mut PCG32) -> Vec2 {
65 loop {
66 let v = random_vec2(rng);
67 if v.length_squared() > 0.01 {
68 return v;
69 }
70 }
71 }
72
random_nonzero_vec3(rng: &mut PCG32) -> Vec373 pub fn random_nonzero_vec3(rng: &mut PCG32) -> Vec3 {
74 loop {
75 let v = random_vec3(rng);
76 if v.length_squared() > 0.01 {
77 return v;
78 }
79 }
80 }
81
random_f32(rng: &mut PCG32) -> f3282 pub fn random_f32(rng: &mut PCG32) -> f32 {
83 rng.next_f32()
84 }
85
random_radians(rng: &mut PCG32) -> f3286 pub fn random_radians(rng: &mut PCG32) -> f32 {
87 -f32::consts::PI + rng.next_f32() * 2.0 * f32::consts::PI
88 }
89
random_quat(rng: &mut PCG32) -> Quat90 pub fn random_quat(rng: &mut PCG32) -> Quat {
91 let yaw = random_radians(rng);
92 let pitch = random_radians(rng);
93 let roll = random_radians(rng);
94 Quat::from_euler(glam::EulerRot::YXZ, yaw, pitch, roll)
95 }
96
random_mat2(rng: &mut PCG32) -> Mat297 pub fn random_mat2(rng: &mut PCG32) -> Mat2 {
98 Mat2::from_cols(random_vec2(rng), random_vec2(rng))
99 }
100
random_mat3(rng: &mut PCG32) -> Mat3101 pub fn random_mat3(rng: &mut PCG32) -> Mat3 {
102 Mat3::from_cols(random_vec3(rng), random_vec3(rng), random_vec3(rng))
103 }
104
random_srt_mat3(rng: &mut PCG32) -> Mat3105 pub fn random_srt_mat3(rng: &mut PCG32) -> Mat3 {
106 Mat3::from_scale_angle_translation(
107 random_nonzero_vec2(rng),
108 random_radians(rng),
109 random_vec2(rng),
110 )
111 }
112
random_mat3a(rng: &mut PCG32) -> Mat3A113 pub fn random_mat3a(rng: &mut PCG32) -> Mat3A {
114 Mat3A::from_cols(random_vec3a(rng), random_vec3a(rng), random_vec3a(rng))
115 }
116
random_srt_mat3a(rng: &mut PCG32) -> Mat3A117 pub fn random_srt_mat3a(rng: &mut PCG32) -> Mat3A {
118 Mat3A::from_scale_angle_translation(
119 random_nonzero_vec2(rng),
120 random_radians(rng),
121 random_vec2(rng),
122 )
123 }
124
random_srt_mat4(rng: &mut PCG32) -> Mat4125 pub fn random_srt_mat4(rng: &mut PCG32) -> Mat4 {
126 Mat4::from_scale_rotation_translation(
127 random_nonzero_vec3(rng),
128 random_quat(rng),
129 random_vec3(rng),
130 )
131 }
132