1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 #include "BLI_compiler_attrs.h"
23 #include "BLI_sys_types.h"
24 
25 /** \file
26  * \ingroup bli
27  * \brief Random number functions.
28  */
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* RNG is an abstract random number generator type that avoids using globals.
35  * Always use this instead of the global RNG unless you have a good reason,
36  * the global RNG is not thread safe and will not give repeatable results.
37  */
38 struct RNG;
39 typedef struct RNG RNG;
40 
41 struct RNG_THREAD_ARRAY;
42 typedef struct RNG_THREAD_ARRAY RNG_THREAD_ARRAY;
43 
44 struct RNG *BLI_rng_new(unsigned int seed);
45 struct RNG *BLI_rng_new_srandom(unsigned int seed);
46 struct RNG *BLI_rng_copy(struct RNG *rng) ATTR_NONNULL(1);
47 void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1);
48 
49 void BLI_rng_seed(struct RNG *rng, unsigned int seed) ATTR_NONNULL(1);
50 void BLI_rng_srandom(struct RNG *rng, unsigned int seed) ATTR_NONNULL(1);
51 void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len) ATTR_NONNULL(1, 2);
52 int BLI_rng_get_int(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
53 unsigned int BLI_rng_get_uint(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
54 double BLI_rng_get_double(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
55 float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
56 void BLI_rng_get_float_unit_v2(struct RNG *rng, float v[2]) ATTR_NONNULL(1, 2);
57 void BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]) ATTR_NONNULL(1, 2);
58 void BLI_rng_get_tri_sample_float_v2(struct RNG *rng,
59                                      const float v1[2],
60                                      const float v2[2],
61                                      const float v3[2],
62                                      float r_pt[2]) ATTR_NONNULL();
63 void BLI_rng_shuffle_array(struct RNG *rng,
64                            void *data,
65                            unsigned int elem_size_i,
66                            unsigned int elem_tot) ATTR_NONNULL(1, 2);
67 
68 /** Note that skipping is as slow as generating n numbers! */
69 void BLI_rng_skip(struct RNG *rng, int n) ATTR_NONNULL(1);
70 
71 /* fill an array with random numbers */
72 void BLI_array_frand(float *ar, int count, unsigned int seed);
73 
74 /** Return a pseudo-random (hash) float from an integer value */
75 float BLI_hash_frand(unsigned int seed) ATTR_WARN_UNUSED_RESULT;
76 
77 /**
78  * Shuffle an array randomly using the given seed contents.
79  * This routine does not use nor modify the state of the BLI random number generator.
80  */
81 void BLI_array_randomize(void *data,
82                          unsigned int elem_size,
83                          unsigned int elem_tot,
84                          unsigned int seed);
85 
86 /** Better seed for the random number generator, using noise.c hash[] */
87 /** Allows up to BLENDER_MAX_THREADS threads to address */
88 void BLI_thread_srandom(int thread, unsigned int seed);
89 
90 /** Return a pseudo-random number N where 0<=N<(2^31) */
91 /** Allows up to BLENDER_MAX_THREADS threads to address */
92 int BLI_thread_rand(int thread) ATTR_WARN_UNUSED_RESULT;
93 
94 /** Return a pseudo-random number N where 0.0f<=N<1.0f */
95 /** Allows up to BLENDER_MAX_THREADS threads to address */
96 float BLI_thread_frand(int thread) ATTR_WARN_UNUSED_RESULT;
97 
98 /** array versions for thread safe random generation */
99 RNG_THREAD_ARRAY *BLI_rng_threaded_new(void);
100 void BLI_rng_threaded_free(struct RNG_THREAD_ARRAY *rngarr) ATTR_NONNULL(1);
101 int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread) ATTR_WARN_UNUSED_RESULT;
102 
103 /* Low-discrepancy sequences. */
104 
105 /** Return the _n_th number of the given low-discrepancy sequence. */
106 void BLI_halton_1d(unsigned int prime, double offset, int n, double *r);
107 void BLI_halton_2d(const unsigned int prime[2], double offset[2], int n, double *r);
108 void BLI_halton_3d(const unsigned int prime[3], double offset[3], int n, double *r);
109 void BLI_hammersley_1d(unsigned int n, double *r);
110 
111 /** Return the whole low-discrepancy sequence up to _n_. */
112 void BLI_halton_2d_sequence(const unsigned int prime[2], double offset[2], int n, double *r);
113 void BLI_hammersley_2d_sequence(unsigned int n, double *r);
114 
115 #ifdef __cplusplus
116 }
117 #endif
118