NGL  6.5
The NCCA Graphics Library
Random.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Rob Bateman / Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITH ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this progra_m. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "Random.h"
19 #include <cstdlib> // for time
20 #include <ctime>
21 
22 //----------------------------------------------------------------------------------------------------------------------
25 //----------------------------------------------------------------------------------------------------------------------
26 namespace ngl
27 {
28  constexpr auto RandomFloat="RandomFloat";
29  constexpr auto RandomPositiveFloat="RandomPositiveFloat";
30 
31 //----------------------------------------------------------------------------------------------------------------------
33 {
34  m_generator.seed(static_cast<unsigned int>(std::time(0)));
35 }
36 
37 
38 //----------------------------------------------------------------------------------------------------------------------
39 void Random::setSeed(unsigned int _value)
40 {
41  m_generator.seed(_value);
42 }
43 
44 
45 //----------------------------------------------------------------------------------------------------------------------
47 {
48  // we have two default generators built in
49 
50  // first create a simple uniform real distrib
51  std::uniform_real_distribution<Real> MinusPlusOneFloatDistrib(-1.0f, 1.0f);
52  m_floatGenerators[RandomFloat] =MinusPlusOneFloatDistrib;
53  // same for below but using 0-1 for distrib
54  std::uniform_real_distribution<Real> ZeroOneFloatDistrib(0.0, 1.0);
55  m_floatGenerators[RandomPositiveFloat] =ZeroOneFloatDistrib;
56 }
57 
58 
59 //----------------------------------------------------------------------------------------------------------------------
61 {
62  // grab a function pointer based on the _name from the map
63  //auto func=m_floatGenerators[_name];
64  auto func = m_floatGenerators.find(_name);
65  // see if we got anything we can use
66  if(func != m_floatGenerators.end())
67  {
68  // if it exists execute the function and return the value
69  return func->second(m_generator);
70  }
71  else
72  {
73  // otherwise we return the safest possible value 0
74  return 0;
75  }
76 }
77 
78 
79 //----------------------------------------------------------------------------------------------------------------------
81 {
82  // get our positive gen function and assign valus to a colour (alpha =1)
84  return Colour(gen(m_generator),gen(m_generator),gen(m_generator));
85 }
86 
87 //----------------------------------------------------------------------------------------------------------------------
89 {
90  // get our positive gen function and assign valus to a colour with rand alpha
91 
93  return Colour(gen(m_generator),gen(m_generator),gen(m_generator),gen(m_generator));
94 }
95 
96 //----------------------------------------------------------------------------------------------------------------------
98 {
100  return Vec4(gen(m_generator),gen(m_generator),gen(m_generator),0.0f);
101 }
102 
103 //----------------------------------------------------------------------------------------------------------------------
105 {
106  auto gen=m_floatGenerators[RandomFloat];
107  Vec4 v(gen(m_generator),gen(m_generator),gen(m_generator),0.0f);
108  v.normalize();
109  return v;
110 }
111 
112 
113 //----------------------------------------------------------------------------------------------------------------------
115 {
116  auto gen=m_floatGenerators[RandomFloat];
117  return Vec3(gen(m_generator),gen(m_generator),gen(m_generator));
118 }
119 
120 //----------------------------------------------------------------------------------------------------------------------
122 {
123  auto gen=m_floatGenerators[RandomFloat];
124  Vec3 v(gen(m_generator),gen(m_generator),gen(m_generator));
125  v.normalize();
126  return v;
127 }
128 
129 //----------------------------------------------------------------------------------------------------------------------
131 {
132  auto gen=m_floatGenerators[RandomFloat];
133  return Vec2(gen(m_generator),gen(m_generator));
134 }
135 
136 //----------------------------------------------------------------------------------------------------------------------
138 {
139  auto gen=m_floatGenerators[RandomFloat];
140  Vec2 v(gen(m_generator),gen(m_generator));
141  v.normalize();
142  return v;
143 }
144 
145 
146 
147 
148 Vec3 Random::getRandomPoint( Real _xRange, Real _yRange, Real _zRange )
149 {
150  auto gen=m_floatGenerators[RandomFloat];
151  return Vec3(gen(m_generator)*_xRange,gen(m_generator)*_yRange,gen(m_generator)*_zRange);
152 
153 }
154 
155 
156 //----------------------------------------------------------------------------------------------------------------------
158 {
159  auto gen=m_floatGenerators[RandomFloat];
160  return gen(m_generator)*_mult;
161 }
162 
163 //----------------------------------------------------------------------------------------------------------------------
165 {
167  return gen(m_generator)*_mult;
168 }
169 /*
170 //----------------------------------------------------------------------------------------------------------------------
171 void Random::addGenerator( const std::string &_name,RANDDIST _distribution, Real _min, Real _max, Real _prob)
172 {
175  if(_distribution==uniform_smallint)
176  {
177  boost::uniform_smallint<int> distrib(_min, _max);
178  boost::variate_generator<ENGINE &, boost::uniform_smallint<int> >gen(m_generator,distrib);
179  m_floatGenerators[_name] =gen;
180  }
181 
182  else if(_distribution==uniform_int)
183  {
184  boost::uniform_int<int> distrib(_min, _max);
185  boost::variate_generator<ENGINE &, boost::uniform_int<int> >gen(m_generator,distrib);
186  m_floatGenerators[_name] =gen;
187  }
188  else if(_distribution==uniform_real)
189  {
190  boost::uniform_real<Real> distrib(_min, _max);
191  boost::variate_generator<ENGINE &, boost::uniform_real<Real> >gen(m_generator,distrib);
192  m_floatGenerators[_name] =gen;
193  }
194  else if(_distribution==bernoulli_distribution)
195  {
196  boost::bernoulli_distribution<Real> distrib(_min);
197 
198  boost::variate_generator<ENGINE &, boost::bernoulli_distribution<Real> >gen(m_generator,distrib);
199  m_floatGenerators[_name] =gen;
200  }
201 
202  else if(_distribution==binomial_distribution)
203  {
204  boost::binomial_distribution<> distrib(int(_min),_max);
205 
206  boost::variate_generator<ENGINE &, boost::binomial_distribution<> >gen(m_generator,distrib);
207  m_floatGenerators[_name] =gen;
208  }
209  else if(_distribution==cauchy_distribution)
210  {
211  boost::cauchy_distribution<Real> distrib(_min,_max);
212 
213  boost::variate_generator<ENGINE &, boost::cauchy_distribution<Real> >gen(m_generator,distrib);
214  m_floatGenerators[_name] =gen;
215  }
216  else if(_distribution==gamma_distribution)
217  {
218  boost::gamma_distribution<Real> distrib(_min);
219 
220  boost::variate_generator<ENGINE &, boost::gamma_distribution<Real> >gen(m_generator,distrib);
221  m_floatGenerators[_name] =gen;
222  }
223  else if(_distribution==poisson_distribution)
224  {
225  boost::poisson_distribution<int> distrib(_min);
226 
227  boost::variate_generator<ENGINE &, boost::poisson_distribution<int> >gen(m_generator,distrib);
228  m_floatGenerators[_name] =gen;
229  }
230 
231  else if(_distribution==geometric_distribution)
232  {
233  boost::geometric_distribution<int> distrib(_min);
234 
235  boost::variate_generator<ENGINE &, boost::geometric_distribution<int> >gen(m_generator,distrib);
236  m_floatGenerators[_name] =gen;
237  }
238 
239  else if(_distribution==triangle_distribution)
240  {
241  boost::triangle_distribution<Real> distrib(_min,_prob,_max);
242 
243  boost::variate_generator<ENGINE &, boost::triangle_distribution<Real> >gen(m_generator,distrib);
244  m_floatGenerators[_name] =gen;
245  }
246 
247  else if(_distribution==exponential_distribution)
248  {
249  boost::exponential_distribution<Real> distrib(_min);
250 
251  boost::variate_generator<ENGINE &, boost::exponential_distribution<Real> >gen(m_generator,distrib);
252  m_floatGenerators[_name] =gen;
253  }
254 
255  else if(_distribution==normal_distribution)
256  {
257  boost::normal_distribution<Real> distrib(_min,_max);
258 
259  boost::variate_generator<ENGINE &, boost::normal_distribution<Real> >gen(m_generator,distrib);
260  m_floatGenerators[_name] =gen;
261  }
262 
263  else if(_distribution==lognormal_distribution)
264  {
265  boost::lognormal_distribution<Real> distrib(_min,_max);
266 
267  boost::variate_generator<ENGINE &, boost::lognormal_distribution<Real> >gen(m_generator,distrib);
268  m_floatGenerators[_name] =gen;
269  }
270 
271 }
272 //----------------------------------------------------------------------------------------------------------------------
273 */
274 } // end of namespace
275 
276 //----------------------------------------------------------------------------------------------------------------------
277 
278 
simple Vec2 encapsulates a 3 float object like glsl Vec2 but not maths use the Vec2 class for maths a...
Definition: Vec2.h:49
Vec4 & normalize() noexcept
Normalize the vector using .
Definition: Vec4.cpp:99
simple class to hold colour information and set the basic opengl colour state. also has overloaded me...
Definition: Colour.h:40
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
constexpr auto RandomFloat
Definition: Random.cpp:28
void setSeed()
set the seed using std::time(NULL)
Definition: Random.cpp:32
Vec4 getRandomVec4()
get a random vector with componets ranged from +/- 1
Definition: Random.cpp:97
Vec2 getRandomNormalizedVec2()
get a random vector with componets ranged from +/- 1 and Normalized
Definition: Random.cpp:137
Colour getRandomColour()
return a random colour with rgb components clamped between 0-1
Definition: Random.cpp:80
Colour getRandomColourAndAlpha()
return a random colour with rgba components clamped between 0-1
Definition: Random.cpp:88
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
Real randomPositiveNumber(Real _mult=1)
a replacement for the old ReandomPosNum this is basically a convinience function
Definition: Random.cpp:164
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
Vec4 getRandomNormalizedVec4()
get a random vector with componets ranged from +/- 1 and Normalized
Definition: Random.cpp:104
friend class Vec4
Definition: Random.h:73
const GLdouble * v
Definition: glew.h:1394
Vec3 getRandomPoint(Real _xRange=1.0, Real _yRange=1.0, Real _zRange=1.0)
get a random point in 3D space defaults to +/- 1 else user defined range
Definition: Random.cpp:148
void normalize() noexcept
Normalize the vector using .
Definition: Vec2.cpp:164
constexpr auto RandomPositiveFloat
Definition: Random.cpp:29
std::unordered_map< std::string, std::uniform_real_distribution< Real > > m_floatGenerators
our map to hold the generator data basically we are going to hold a name / function pair...
Definition: Random.h:178
Real getFloatFromGeneratorName(const std::string &_name)
gets a pre-generated Real value for a genetator
Definition: Random.cpp:60
std::mt19937 m_generator
the generator for rng class where all funcs and distribs are added. all we need to do is replace this...
Definition: Random.h:166
Real randomNumber(Real _mult=1)
a replacement for the old RandomNumber func this is basically a convinience function ...
Definition: Random.cpp:157
friend class Colour
Definition: Random.h:72
Vec2 getRandomVec2()
get a random vector with componets ranged from +/- 1
Definition: Random.cpp:130
an encapsulation of the std::random classes
Random()
ctor hidden in protected as we are a singleton class
Definition: Random.cpp:46
Vec3 getRandomVec3()
get a random vector with componets ranged from +/- 1
Definition: Random.cpp:114
void normalize() noexcept
Normalize the vector using .
Definition: Vec3.cpp:206
GLsizei const GLchar *const * string
Definition: glew.h:1847
Vec3 getRandomNormalizedVec3()
get a random vector with componets ranged from +/- 1 and Normalized
Definition: Random.cpp:121
GLclampf f
Definition: glew.h:3511