BRL-CAD
rand.h
Go to the documentation of this file.
1 /* R A N D . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 /*----------------------------------------------------------------------*/
22 /** @addtogroup bn_rnt
23  *
24  * @brief
25  * A supply of fast pseudo-random numbers from table in bn/rand.c.
26  * The values are in the open interval (i.e. exclusive) of 0.0 to 1.0
27  * range with a period of 4096.
28  *
29  * @par Usage:
30  @code
31  unsigned idx;
32  float f;
33 
34  BN_RANDSEED(idx, integer_seed);
35 
36  while (NEED_MORE_RAND_NUMBERS) {
37  f = BN_RANDOM(idx);
38  }
39  @endcode
40  *
41  * Note that the values from bn_rand_half() become all 0.0 when the
42  * benchmark flag is set (bn_rand_halftab is set to all 0's). The
43  * numbers from bn_rand_table do not change, because the procedural
44  * noise would cease to exist.
45  */
46 /** @{ */
47 /** @file rand.h */
48 
49 /* TODO - investigate whether it makes sense to incorporate the T258 code from
50  * ARL-TR-7928 Section 9.1 by Joseph C Collins into libbn
51  * (https://apps.dtic.mil/sti/pdfs/AD1024837.pdf) */
52 
53 #ifndef BN_RAND_H
54 #define BN_RAND_H
55 
56 #include "common.h"
57 #include "bn/defines.h"
58 #include "vmath.h"
59 
60 __BEGIN_DECLS
61 
62 #define BN_RAND_TABSIZE 4096
63 #define BN_RAND_TABMASK 0xfff
64 #define BN_RANDSEED(_i, _seed) _i = ((unsigned)_seed) % BN_RAND_TABSIZE
65 
66 /**
67  * This is our table of random numbers. Rather than calling drand48()
68  * or random() or rand() we just pick numbers out of this table. This
69  * table has 4096 unique entries with floating point values ranging
70  * from the open interval (i.e. exclusive) 0.0 to 1.0 range.
71  *
72  * There are convenience macros for access in the bn.h header.
73  */
74 BN_EXPORT extern const float bn_rand_table[BN_RAND_TABSIZE];
75 
76 /** BN_RANDOM always gives numbers between the open interval 0.0 to 1.0 */
77 #define BN_RANDOM(_i) bn_rand_table[ _i = (_i+1) % BN_RAND_TABSIZE ]
78 
79 /** BN_RANDHALF always gives numbers between the open interval -0.5 and 0.5 */
80 #define BN_RANDHALF(_i) (bn_rand_table[ _i = (_i+1) % BN_RAND_TABSIZE ]-0.5)
81 #define BN_RANDHALF_INIT(_p) _p = bn_rand_table
82 
83 #define BN_RANDHALFTABSIZE 16535 /**< Powers of two give streaking */
84 BN_EXPORT extern int bn_randhalftabsize;
85 
86 /**
87  * The actual table of random floating point numbers with values in
88  * the closed interval (i.e. inclusive) -0.5 to +0.5 range.
89  *
90  * For benchmarking purposes, this table is zeroed.
91  */
92 BN_EXPORT extern float bn_rand_halftab[BN_RANDHALFTABSIZE];
93 
94 /**
95  * random numbers between the closed interval -0.5 to 0.5 inclusive,
96  * except when benchmark flag is set, when this becomes a constant 0.0
97  *
98  * @param _p float pointer type initialized by bn_rand_init()
99  *
100  */
101 #define bn_rand_half(_p) \
102  ((++(_p) >= &bn_rand_halftab[bn_randhalftabsize] || \
103  (_p) < bn_rand_halftab) ? \
104  *((_p) = bn_rand_halftab) : *(_p))
105 
106 /**
107  * initialize the seed for the large random number table (halftab)
108  *
109  * @param _p float pointer to be initialized, used for bn_rand0to1()
110  * and bn_rand_half()
111  * @param _seed Integer SEED for offset in the table.
112  *
113  */
114 #define bn_rand_init(_p, _seed) \
115  (_p) = &bn_rand_halftab[ \
116  (int)(\
117  (bn_rand_halftab[(_seed)%bn_randhalftabsize] + 0.5) * \
118  (bn_randhalftabsize-1)) ]
119 
120 /**
121  * random numbers in the closed interval 0.0 to 1.0 range (inclusive)
122  * except when benchmarking, when this is always 0.5
123  *
124  * @param _q float pointer type initialized by bn_rand_init()
125  *
126  */
127 #define bn_rand0to1(_q) (bn_rand_half(_q)+0.5)
128 
129 #define BN_SINTABSIZE 2048
130 
131 #define bn_tab_sin(_a) (((_a) > 0) ? \
132  (bn_sin_table[(int)((0.5+ (_a)*(BN_SINTABSIZE / M_2PI)))&(BN_SINTABSIZE-1)]) :\
133  (-bn_sin_table[(int)((0.5- (_a)*(BN_SINTABSIZE / M_2PI)))&(BN_SINTABSIZE-1)]))
134 
135 /**
136  * table of floating point sine values in the closed (i.e. inclusive)
137  * interval -1.0 to 1.0 range.
138  */
139 BN_EXPORT extern const float bn_sin_table[BN_SINTABSIZE];
140 
141 /**
142  *@brief
143  * For benchmarking purposes, make the random number table predictable.
144  * Setting to all zeros keeps dithered values at their original values.
145  */
146 BN_EXPORT extern void bn_mathtab_constant(void);
147 
148 /**
149  * @brief
150  * Generate a sample point on a sphere per Marsaglia (1972).
151  *
152  * Note that bn_sph_sample and its internal routines do not initialize the
153  * randmt seed - the user should call bn_randmt_seed in their code if a
154  * variable seed is required.
155  */
156 BN_EXPORT extern void bn_rand_sph_sample(point_t sample, const point_t center, const fastf_t radius);
157 
158 
159 __END_DECLS
160 
161 #endif /* BN_RAND_H */
162 /** @} */
163 /*
164  * Local Variables:
165  * mode: C
166  * tab-width: 8
167  * indent-tabs-mode: t
168  * c-file-style: "stroustrup"
169  * End:
170  * ex: shiftwidth=4 tabstop=8
171  */
Header file for the BRL-CAD common definitions.
void bn_mathtab_constant(void)
For benchmarking purposes, make the random number table predictable. Setting to all zeros keeps dithe...
#define BN_RANDHALFTABSIZE
Definition: rand.h:84
float bn_rand_halftab[BN_RANDHALFTABSIZE]
const float bn_sin_table[BN_SINTABSIZE]
const float bn_rand_table[BN_RAND_TABSIZE]
int bn_randhalftabsize
void bn_rand_sph_sample(point_t sample, const point_t center, const fastf_t radius)
Generate a sample point on a sphere per Marsaglia (1972).
#define BN_SINTABSIZE
Definition: rand.h:130
#define BN_RAND_TABSIZE
Definition: rand.h:63
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:334
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition: vmath.h:355
fundamental vector, matrix, quaternion math macros