Blender V5.0
rand.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12#include <ctime>
13#include <random>
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_bitmap.h"
18#include "BLI_compiler_compat.h"
19#include "BLI_math_vector.h"
20#include "BLI_noise.h"
21#include "BLI_rand.h"
22#include "BLI_rand.hh"
23#include "BLI_sys_types.h"
24#include "BLI_threads.h"
25
26#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
27
28#define hash BLI_noise_hash_uchar_512
29
33struct RNG {
35
36 MEM_CXX_CLASS_ALLOC_FUNCS("RNG")
37};
38
40{
41 RNG *rng = new RNG();
42 rng->rng.seed(seed);
43 return rng;
44}
45
47{
48 RNG *rng = new RNG();
49 rng->rng.seed_random(seed);
50 return rng;
51}
52
53void BLI_rng_free(RNG *rng)
54{
55 delete rng;
56}
57
59{
60 rng->rng.seed(seed);
61}
62
64{
65 rng->rng.seed_random(seed);
66}
67
68void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len)
69{
70 rng->rng.get_bytes(blender::MutableSpan(bytes, int64_t(bytes_len)));
71}
72
74{
75 return rng->rng.get_int32();
76}
77
79{
80 return rng->rng.get_uint32();
81}
82
84{
85 return rng->rng.get_double();
86}
87
89{
90 return rng->rng.get_float();
91}
92
94 RNG *rng, const float v1[2], const float v2[2], const float v3[2], float r_pt[2])
95{
96 copy_v2_v2(r_pt, rng->rng.get_triangle_sample(v1, v2, v3));
97}
98
99void BLI_rng_shuffle_array(RNG *rng, void *data, uint elem_size_i, uint elem_num)
100{
101 if (elem_num <= 1) {
102 return;
103 }
104
105 const uint elem_size = elem_size_i;
106 uint i = elem_num;
107 void *temp = malloc(elem_size);
108
109 while (i--) {
110 const uint j = BLI_rng_get_uint(rng) % elem_num;
111 if (i != j) {
112 void *iElem = (uchar *)data + i * elem_size_i;
113 void *jElem = (uchar *)data + j * elem_size_i;
114 memcpy(temp, iElem, elem_size);
115 memcpy(iElem, jElem, elem_size);
116 memcpy(jElem, temp, elem_size);
117 }
118 }
119
120 free(temp);
121}
122
123void BLI_rng_shuffle_bitmap(RNG *rng, BLI_bitmap *bitmap, uint bits_num)
124{
125 if (bits_num <= 1) {
126 return;
127 }
128
129 uint i = bits_num;
130 while (i--) {
131 const uint j = BLI_rng_get_uint(rng) % bits_num;
132 if (i != j) {
133 const bool i_bit = BLI_BITMAP_TEST(bitmap, i);
134 const bool j_bit = BLI_BITMAP_TEST(bitmap, j);
135 BLI_BITMAP_SET(bitmap, i, j_bit);
136 BLI_BITMAP_SET(bitmap, j, i_bit);
137 }
138 }
139}
140
141void BLI_rng_skip(RNG *rng, int n)
142{
143 rng->rng.skip(uint(n));
144}
145
146/***/
147
149{
150 RNG rng;
151
152 BLI_rng_srandom(&rng, seed);
153 return BLI_rng_get_float(&rng);
154}
155
156void BLI_array_randomize(void *data, uint elem_size, uint elem_num, uint seed)
157{
158 RNG rng;
159
160 BLI_rng_seed(&rng, seed);
161 BLI_rng_shuffle_array(&rng, data, elem_size, elem_num);
162}
163
165{
166 RNG rng;
167
168 BLI_rng_seed(&rng, seed);
169 BLI_rng_shuffle_bitmap(&rng, bitmap, bits_num);
170}
171
172/* ********* for threaded random ************** */
173
175 std::array<RNG, BLENDER_MAX_THREADS> rng_tab;
176};
177
179{
180 uint i;
181 RNG_THREAD_ARRAY *rngarr = MEM_new<RNG_THREAD_ARRAY>("random_array");
182
183 for (i = 0; i < BLENDER_MAX_THREADS; i++) {
184 BLI_rng_srandom(&rngarr->rng_tab[i], uint(clock()));
185 }
186
187 return rngarr;
188}
189
191{
192 MEM_delete(rngarr);
193}
194
196{
197 return BLI_rng_get_int(&rngarr->rng_tab[size_t(thread)]);
198}
199
200/* ********* Low-discrepancy sequences ************** */
201
202/* incremental halton sequence generator, from:
203 * "Instant Radiosity", Keller A. */
204BLI_INLINE double halton_ex(double invprimes, double *offset)
205{
206 double e = fabs((1.0 - *offset) - 1e-10);
207
208 if (invprimes >= e) {
209 double lasth;
210 double h = invprimes;
211
212 do {
213 lasth = h;
214 h *= invprimes;
215 } while (h >= e);
216
217 *offset += ((lasth + h) - 1.0);
218 }
219 else {
220 *offset += invprimes;
221 }
222
223 return *offset;
224}
225
226void BLI_halton_1d(uint prime, double offset, int n, double *r)
227{
228 const double invprime = 1.0 / double(prime);
229
230 *r = 0.0;
231
232 for (int s = 0; s < n; s++) {
233 *r = halton_ex(invprime, &offset);
234 }
235}
236
237void BLI_halton_2d(const uint prime[2], double offset[2], int n, double *r)
238{
239 const double invprimes[2] = {1.0 / double(prime[0]), 1.0 / double(prime[1])};
240
241 r[0] = r[1] = 0.0;
242
243 for (int s = 0; s < n; s++) {
244 for (int i = 0; i < 2; i++) {
245 r[i] = halton_ex(invprimes[i], &offset[i]);
246 }
247 }
248}
249
250void BLI_halton_3d(const uint prime[3], double offset[3], int n, double *r)
251{
252 const double invprimes[3] = {
253 1.0 / double(prime[0]), 1.0 / double(prime[1]), 1.0 / double(prime[2])};
254
255 r[0] = r[1] = r[2] = 0.0;
256
257 for (int s = 0; s < n; s++) {
258 for (int i = 0; i < 3; i++) {
259 r[i] = halton_ex(invprimes[i], &offset[i]);
260 }
261 }
262}
263
264/* From "Sampling with Hammersley and Halton Points" TT Wong
265 * Appendix: Source Code 1 */
267{
268 double u = 0;
269
270 /* This reverse the bit-wise representation
271 * around the decimal point. */
272 for (double p = 0.5; n; p *= 0.5, n >>= 1) {
273 if (n & 1) {
274 u += p;
275 }
276 }
277
278 return u;
279}
280
281void BLI_hammersley_1d(uint n, double *r)
282{
283 *r = radical_inverse(n);
284}
285
286namespace blender {
287
289{
290 std::random_device rd;
291 std::mt19937 e{rd()};
292 std::uniform_int_distribution<uint32_t> dist;
293 const uint32_t seed = dist(e);
295}
296
298{
299 this->seed(seed + hash[seed & 255]);
300 seed = this->get_uint32();
301 this->seed(seed + hash[seed & 255]);
302 seed = this->get_uint32();
303 this->seed(seed + hash[seed & 255]);
304}
305
307{
308 /* Support for negative values can be added when necessary. */
309 BLI_assert(x >= 0.0f);
310 const float round_up_probability = fractf(x);
311 const bool round_up = round_up_probability > this->get_float();
312 return int(x) + int(round_up);
313}
314
316{
317 float a = float(M_PI * 2.0) * this->get_float();
318 return {cosf(a), sinf(a)};
319}
320
322{
323 float z = (2.0f * this->get_float()) - 1.0f;
324 float r = 1.0f - z * z;
325 if (r > 0.0f) {
326 float a = float(M_PI * 2.0) * this->get_float();
327 r = sqrtf(r);
328 float x = r * cosf(a);
329 float y = r * sinf(a);
330 return {x, y, z};
331 }
332 return {0.0f, 0.0f, 1.0f};
333}
334
336{
337 float u = this->get_float();
338 float v = this->get_float();
339
340 if (u + v > 1.0f) {
341 u = 1.0f - u;
342 v = 1.0f - v;
343 }
344
345 float2 side_u = v2 - v1;
346 float2 side_v = v3 - v1;
347
348 float2 sample = v1;
349 sample += side_u * u;
350 sample += side_v * v;
351 return sample;
352}
353
355{
356 float u = this->get_float();
357 float v = this->get_float();
358
359 if (u + v > 1.0f) {
360 u = 1.0f - u;
361 v = 1.0f - v;
362 }
363
364 float3 side_u = v2 - v1;
365 float3 side_v = v3 - v1;
366
367 float3 sample = v1;
368 sample += side_u * u;
369 sample += side_v * v;
370 return sample;
371}
372
374{
375 constexpr int64_t mask_bytes = 2;
376 constexpr int64_t rand_stride = int64_t(sizeof(x_)) - mask_bytes;
377
378 int64_t last_len = 0;
379 int64_t trim_len = r_bytes.size();
380
381 if (trim_len > rand_stride) {
382 last_len = trim_len % rand_stride;
383 trim_len = trim_len - last_len;
384 }
385 else {
386 trim_len = 0;
387 last_len = r_bytes.size();
388 }
389
390 const char *data_src = (const char *)&x_;
391 int64_t i = 0;
392 while (i != trim_len) {
393 BLI_assert(i < trim_len);
394 /* NOTE: this is endianness-sensitive.
395 * Big Endian needs to iterate in reverse, with a `mask_bytes - 1` offset. */
396 for (int64_t j = 0; j != rand_stride; j++) {
397 r_bytes[i++] = data_src[j];
398 }
399 this->step();
400 }
401 if (last_len) {
402 for (int64_t j = 0; j != last_len; j++) {
403 r_bytes[i++] = data_src[j];
404 }
405 }
406}
407
408} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.h:61
#define BLI_BITMAP_SET(_bitmap, _index, _set)
Definition BLI_bitmap.h:99
unsigned int BLI_bitmap
Definition BLI_bitmap.h:13
#define BLI_INLINE
void BLI_kdtree_nd_ free(KDTree *tree)
#define M_PI
MINLINE void copy_v2_v2(float r[2], const float a[2])
Random number functions.
struct RNG RNG
Definition BLI_rand.h:21
unsigned char uchar
unsigned int uint
#define BLENDER_MAX_THREADS
Definition BLI_threads.h:16
Read Guarded memory(de)allocation.
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
long long int int64_t
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
static unsigned long seed
Definition btSoftBody.h:39
constexpr int64_t size() const
Definition BLI_span.hh:493
void get_bytes(MutableSpan< char > r_bytes)
Definition rand.cc:373
int round_probabilistic(float x)
Definition rand.cc:306
RandomNumberGenerator(uint32_t seed=0)
Definition BLI_rand.hh:21
void seed_random(uint32_t seed)
Definition rand.cc:297
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition rand.cc:354
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition rand.cc:335
void seed(uint32_t seed)
Definition BLI_rand.hh:35
static RandomNumberGenerator from_random_seed()
Definition rand.cc:288
nullptr float
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline float2 fabs(const float2 a)
VecBase< float, 2 > float2
VecBase< float, 3 > float3
#define hash
Definition noise_c.cc:154
#define sqrtf
#define sinf
#define fractf
#define cosf
void BLI_rng_threaded_free(RNG_THREAD_ARRAY *rngarr)
Definition rand.cc:190
uint BLI_rng_get_uint(RNG *rng)
Definition rand.cc:78
float BLI_rng_get_float(RNG *rng)
Definition rand.cc:88
void BLI_halton_3d(const uint prime[3], double offset[3], int n, double *r)
Definition rand.cc:250
void BLI_rng_free(RNG *rng)
Definition rand.cc:53
void BLI_rng_srandom(RNG *rng, uint seed)
Definition rand.cc:63
void BLI_rng_shuffle_array(RNG *rng, void *data, uint elem_size_i, uint elem_num)
Definition rand.cc:99
void BLI_rng_shuffle_bitmap(RNG *rng, BLI_bitmap *bitmap, uint bits_num)
Definition rand.cc:123
void BLI_halton_2d(const uint prime[2], double offset[2], int n, double *r)
Definition rand.cc:237
int BLI_rng_get_int(RNG *rng)
Definition rand.cc:73
BLI_INLINE double radical_inverse(uint n)
Definition rand.cc:266
void BLI_hammersley_1d(uint n, double *r)
Definition rand.cc:281
void BLI_rng_seed(RNG *rng, uint seed)
Definition rand.cc:58
int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread)
Definition rand.cc:195
void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len)
Definition rand.cc:68
double BLI_rng_get_double(RNG *rng)
Definition rand.cc:83
void BLI_rng_skip(RNG *rng, int n)
Definition rand.cc:141
RNG * BLI_rng_new_srandom(uint seed)
Definition rand.cc:46
void BLI_halton_1d(uint prime, double offset, int n, double *r)
Definition rand.cc:226
RNG_THREAD_ARRAY * BLI_rng_threaded_new()
Definition rand.cc:178
void BLI_array_randomize(void *data, uint elem_size, uint elem_num, uint seed)
Definition rand.cc:156
BLI_INLINE double halton_ex(double invprimes, double *offset)
Definition rand.cc:204
RNG * BLI_rng_new(uint seed)
Definition rand.cc:39
void BLI_rng_get_tri_sample_float_v2(RNG *rng, const float v1[2], const float v2[2], const float v3[2], float r_pt[2])
Definition rand.cc:93
float BLI_hash_frand(uint seed)
Definition rand.cc:148
void BLI_bitmap_randomize(BLI_bitmap *bitmap, uint bits_num, uint seed)
Definition rand.cc:164
std::array< RNG, BLENDER_MAX_THREADS > rng_tab
Definition rand.cc:175
Definition rand.cc:33
blender::RandomNumberGenerator rng
Definition rand.cc:34
i
Definition text_draw.cc:230
ccl_device_inline size_t round_up(const size_t x, const size_t multiple)
Definition types_base.h:57