cgv
random.h
1 #pragma once
2 #include <ctime>
3 #include <algorithm>
4 #include "functions.h"
5 #include <cgv/math/vec.h>
6 #include <cgv/math/mat.h>
7 #include <cgv/math/eig.h>
8 #include <cgv/math/diag_mat.h>
9 #include <cgv/math/perm_mat.h>
10 #include <cgv/math/up_tri_mat.h>
11 #include <cgv/math/low_tri_mat.h>
12 #include <cgv/math/quat.h>
13 #include <set>
14 //#include <cgv/math/constants.h>
15 
16 namespace cgv {
17  namespace math {
18 
19 
25 struct random
26 {
27 private:
28  unsigned long long u,v,w;
29  double storedval;
30 public:
32  void set_seed(unsigned long long seed)
33  {
34  w = 1;
35  v = 4101842887655102017LL;
36  u = seed^v;
37  unsigned long long ull;
38  uniform(ull);
39  v = u; uniform(ull);
40  w = v; uniform(ull);
41  }
42 
44  random() : storedval(0.0)
45  {
46  unsigned long long seed = clock();
47  set_seed(seed);
48  }
49 
51  random(unsigned long long seed) : storedval(0.0)
52  {
53  set_seed(seed);
54  }
55 
57  void uniform(unsigned long long& rv)
58  {
59  u = u*2862933555777941757LL + 7046029254386353087LL;
60  v ^= v >> 17; v ^= v <<31; v ^= v >> 8;
61  w = 4294957665U*(w & 0xffffffff) + (w >> 32);
62  unsigned long long x = u ^(u << 21); x ^= x >>35; x^=x << 4;
63  rv= (x+v)^w;
64  }
65 
67  void uniform(unsigned int& rv)
68  {
69  unsigned long long v;
70  uniform(v);
71  rv= (unsigned int) v;
72  }
73 
75  void uniform(int& rv)
76  {
77  unsigned long long v;
78  uniform(v);
79  rv = (int) v;
80  }
81 
83  void uniform(long& rv)
84  {
85  unsigned long long v;
86  uniform(v);
87  rv = (long) v;
88  }
89 
91  void uniform(long long& rv)
92  {
93  unsigned long long v;
94  uniform(v);
95  rv = (long long) v;
96  }
97 
99  void uniform(bool& rv)
100  {
101  unsigned long long v;
102  uniform(v);
103  rv= v%2 == 0;
104  }
105 
107  void uniform(const unsigned min,const unsigned max, unsigned& rv)
108  {
109  uniform(rv);
110  rv =rv%(max-min+1)+min;
111  }
112 
114  void uniform(const int min,const int max, int& rv)
115  {
116  uniform(rv);
117  rv =(int)(rv%(unsigned)(max-min+1))+min;
118  }
119 
120 
122  void uniform(double& rv)
123  {
124  unsigned long long v;
125  uniform(v);
126  rv= 5.42101086242752217E-20 * v;
127  }
128 
130  void uniform(const double min,const double max, double &rv)
131  {
132  double v;
133  uniform(v);
134  rv= (max-min)*v + min;
135  }
136 
138  void uniform(float & rv)
139  {
140  double v;
141  uniform(v);
142  rv =(float) v;
143  }
144 
146  void uniform(const float min,const float max, float& rv)
147  {
148  float f;
149  uniform(f);
150  rv= (max-min)*f + min;
151  }
152 
154  void uniform( vec<float>& rv)
155  {
156  for(unsigned i = 0; i < rv.size();i++)
157  uniform(rv(i));
158  }
159 
161  void uniform(const float min,const float max, vec<float>& rv)
162  {
163  for(unsigned i = 0; i < rv.size();i++)
164  uniform(min,max,rv(i));
165 
166  }
167 
168 
171  {
172  for(unsigned i = 0; i < rv.size();i++)
173  uniform(rv(i));
174  }
175 
177  void uniform(const double min,const double max, vec<double>& rv)
178  {
179  for(unsigned i = 0; i < rv.size();i++)
180  uniform(min,max,rv(i));
181  }
182 
184  void uniform(mat<float>& rv)
185  {
186  for(unsigned i = 0; i < rv.nrows(); i++)
187  for(unsigned j = 0; j < rv.ncols(); j++)
188  uniform(rv(i,j));
189  }
190 
192  void uniform(const float min,const float max, mat<float>& rv)
193  {
194  for(unsigned i = 0; i < rv.nrows(); i++)
195  for(unsigned j = 0; j < rv.ncols(); j++)
196  uniform(min,max,rv(i,j));
197  }
198 
200  void uniform(perm_mat& rv)
201  {
202  unsigned j;
203  for(unsigned i = 0; i < rv.size();i++)
204  {
205  uniform(j);
206  j=(i+j)%rv.size();
207  rv.swap(i,j);
208  }
209  std::random_shuffle(&(rv(0)),&(rv(rv.nrows())));
210  }
211 
213  void uniform(up_tri_mat<float>& rv)
214  {
215  for(unsigned i = 0; i < rv.nrows(); i++)
216  for(unsigned j = i; j < rv.ncols(); j++)
217  uniform(rv(i,j));
218  }
219 
220 
221 
223  void uniform(const float min, const float max,up_tri_mat<float>& rv)
224  {
225  for(unsigned i = 0; i < rv.nrows(); i++)
226  for(unsigned j = i; j < rv.ncols(); j++)
227  uniform(min,max,rv(i,j));
228  }
229 
231  void uniform(up_tri_mat<double>& rv)
232  {
233  for(unsigned i = 0; i < rv.nrows(); i++)
234  for(unsigned j = i; j < rv.ncols(); j++)
235  uniform(rv(i,j));
236  }
237 
239  void uniform(const double min, const double max,up_tri_mat<double>& rv)
240  {
241  for(unsigned i = 0; i < rv.nrows(); i++)
242  for(unsigned j = i; j < rv.ncols(); j++)
243  uniform(min,max,rv(i,j));
244  }
245 
247  void uniform( low_tri_mat<float>& rv)
248  {
249  for(unsigned i = 0; i < rv.nrows(); i++)
250  for(unsigned j = 0; j <= i; j++)
251  uniform(rv(i,j));
252  }
253 
255  void uniform(const float min,const float max, low_tri_mat<float>& rv)
256  {
257  for(unsigned i = 0; i < rv.nrows(); i++)
258  for(unsigned j = 0; j <= i; j++)
259  uniform(min,max,rv(i,j));
260  }
261 
263  void uniform( low_tri_mat<double>& rv)
264  {
265  for(unsigned j = 0; j < rv.ncols(); j++)
266  for(unsigned i = 0; i < j; i++)
267  uniform(rv(i,j));
268  }
269 
271  void uniform(const double min,const double max, low_tri_mat<double>& rv)
272  {
273  for(unsigned j = 0; j < rv.ncols(); j++)
274  for(unsigned i = 0; i < j; i++)
275  uniform(min,max,rv(i,j));
276  }
277 
280  {
281  for(unsigned i = 0; i < rv.nrows(); i++)
282  uniform(rv(i));
283  }
284 
286  void uniform(const float min,const float max, diag_mat<float>& rv)
287  {
288  for(unsigned i = 0; i < rv.nrows(); i++)
289  uniform(min,max,rv(i));
290  }
291 
294  {
295  for(unsigned i = 0; i < rv.nrows(); i++)
296  uniform(rv(i));
297  }
298 
300  void uniform(const double min,const double max, diag_mat<double>& rv)
301  {
302  for(unsigned i = 0; i < rv.nrows(); i++)
303  uniform(min,max,rv(i));
304  }
305 
306 
309  {
310  for(unsigned i = 0; i < rv.nrows(); i++)
311  for(unsigned j = 0; j < rv.ncols(); j++)
312  uniform(rv(i,j));
313  }
314 
316  void uniform(const double min,const double max, mat<double>& rv)
317  {
318  for(unsigned i = 0; i < rv.nrows(); i++)
319  for(unsigned j = 0; j < rv.ncols(); j++)
320  uniform(min,max,rv(i,j));
321  }
322 
323 
324 
326  void normal(double& rv)
327  {
328  double v1,v2,rsq,fac;
329  if(storedval == 0.)
330  {
331  do{
332  uniform(v1);
333  uniform(v2);
334 
335  v1 = 2.0 * v1-1.0;
336  v2 = 2.0 * v2-1.0;
337  rsq = v1*v1+v2*v2;
338  }while(rsq >= 1.0 || rsq == 0.0);
339  fac = sqrt(-2.0*std::log(rsq)/rsq);
340  storedval = v1*fac;
341  rv= v2*fac;
342 
343  }else
344  {
345  fac = storedval;
346  storedval = 0.;
347  rv= fac;
348  }
349  }
350 
352  void normal(float& rv)
353  {
354  double d;
355  normal(d);
356  rv = (float)d;
357  }
358 
360  void normal(const double mu, const double sigma, double &rv)
361  {
362  double d;
363  normal(d);
364  rv =mu + sigma * d;
365  }
366 
368  void normal(const float mu, const float sigma, float& rv)
369  { float f;
370  normal(f);
371  rv= mu + sigma * f;
372  }
373 
376  {
377 
378  for(unsigned i = 0;i < v.size(); i++)
379  normal(v(i));
380  v.normalize();
381  }
382 
383 
386  {
387  for(unsigned i = 0;i < v.size();i++)
388  normal(v(i));
389  v.normalize();
390  }
391 
394  {
395  float x[3];
396  uniform(x[0]);
397  uniform(x[1]);
398  uniform(x[2]);
399  float a, b, c, d, s;
400  float z, r, theta, omega;
401 
402 
403  z = x[0];
404  r = sqrt( 1 - z * z );
405  theta = 2.0f * 3.14159f * x[1];
406  omega = 3.14159f * x[2];
407 
408 
409  s = sin( omega );
410  a = cos( omega );
411  b = s * cos( theta ) * r;
412  c = s * sin( theta ) * r;
413  d = s * z;
414 
415 
416  q.set(b,c,d,a);
417  }
418 
419 
423  {
424  q.resize(4);
425  double x[3];
426  uniform(x[0]);
427  uniform(x[1]);
428  uniform(x[2]);
429  double a, b, c, d, s;
430  double z, r, theta, omega;
431 
432 
433  z = x[0];
434  r = sqrt( 1 - z * z );
435  theta = 2.0 * 3.14159 * x[1];
436  omega = 3.14159 * x[2];
437 
438 
439  s = sin( omega );
440  a = cos( omega );
441  b = s * cos( theta ) * r;
442  c = s * sin( theta ) * r;
443  d = s * z;
444 
445 
446  q.set(b,c,d,a);
447  }
448 
449 
452  {
453  if(m.nrows() == 3 && m.ncols() == 3)
454  {
455  vec<float> q(4);
457  m = quat_2_mat_33(q);
458  return;
459  }
460  if(m.nrows() == 4 && m.ncols() == 4)
461  {
462  vec<float> q(4);
464  m = quat_2_mat_44(q);
465  return;
466  }
467  assert(false);
468 
469  }
470 
473  {
474  if(m.nrows() == 3 && m.ncols() == 3)
475  {
476  vec<double> q(4);
478  m = quat_2_mat_33(q);
479  return;
480  }
481  if(m.nrows() == 4 && m.ncols() == 4)
482  {
483  vec<double> q(4);
485  m = quat_2_mat_44(q);
486  return;
487  }
488  assert(false);
489  }
490 
491 
494  {
495  if (p.size() == 0)
496  p.resize(3);
497  for (unsigned i=0; i<p.size(); ++i)
498  uniform(p(i));
499  }
500 
502  void uniform_point_in_box(const vec<double>& minp,const vec<double>& maxp, vec<double>& p)
503  {
504  assert(minp.size() == maxp.size());
505  if (p.size() != minp.size())
506  p.resize(minp.size());
507  for (unsigned i=0; i<p.size(); ++i)
508  uniform(minp(i),maxp(i),p(i));
509  }
510 
513  {
515  double r;
516  uniform(r);
517  r = pow(r, 1.0/p.size());
518  p *= r;
519  }
522  {
523  double u,v;
524  uniform(u);
525  uniform(v);
526  if(u+v > 1.0)
527  {
528  u=1.0-u;
529  v=1.0-v;
530  }
531  p= u*p1 + v*p2 +(1.0-u-v)*p3;
532  }
533 
535  void uniform_point_on_sphere(const vec<double>& center,const double& radius,
536  vec<double>& p)
537  {
538  for(unsigned i = 0;i < p.size();i++)
539  normal(p(i));
540  p.normalize();
541  p=center+radius*p;
542  }
543 
544 
546  void uniform_nchoosek(unsigned n, unsigned k, vec<unsigned>& indices)
547  {
548  std::set<unsigned> s;
549  unsigned v;
550  while(s.size() < k)
551  {
552  uniform(0,n-1,v);
553  s.insert(v);
554  }
555 
556  indices.resize(k);
557  unsigned i=0;
558  for(std::set<unsigned>::iterator it =s.begin(); it != s.end();it++,i++)
559  indices(i)=*it;
560 
561  }
562 
563 
564 
565 
566 };
567 
568 
569 }
570 }
571 
cgv::math::random::random
random(unsigned long long seed)
constructor initializes random generator with given seed
Definition: random.h:51
cgv::math::random::uniform
void uniform(up_tri_mat< double > &rv)
generates a pseudo random double precision upper triangular matrix with uniformly distribute componen...
Definition: random.h:231
cgv::math::random::uniform
void uniform(const double min, const double max, up_tri_mat< double > &rv)
generates a pseudo random double precision upper triangular matrix with uniformly distribute componen...
Definition: random.h:239
cgv::math::random::uniform
void uniform(low_tri_mat< double > &rv)
generates a pseudo random double precision lower triangular matrix with uniformly distribute componen...
Definition: random.h:263
cgv::math::random::uniform
void uniform(const float min, const float max, float &rv)
generates a pseudo random single-precision floating point number uniformly distributed between 0 and ...
Definition: random.h:146
cgv::math::quat_2_mat_44
mat< T > quat_2_mat_44(const vec< T > &q)
convert unit quaternion to 4x4 rotation matrix
Definition: quat.h:167
cgv::math::vec::resize
void resize(unsigned dim)
resize the vector
Definition: vec.h:557
cgv::math::random::uniform
void uniform(long long &rv)
generates a 64bit pseudo random signed integer
Definition: random.h:91
cgv::math::random::uniform
void uniform(perm_mat &rv)
generates a pseudo random permutation matrix
Definition: random.h:200
cgv::math::random::uniform
void uniform(const unsigned min, const unsigned max, unsigned &rv)
generates a pseudo random integer between min and max
Definition: random.h:107
cgv::math::random::uniform
void uniform(diag_mat< double > &rv)
generates a pseudo random double precision diagonal matrix with uniformly distribute components betwe...
Definition: random.h:293
cgv::math::random::uniform_orientation
void uniform_orientation(mat< float > &m)
generates a single precision random orientation represented as a rotation matrix
Definition: random.h:451
cgv::math::perm_mat::nrows
unsigned nrows() const
number of rows
Definition: perm_mat.h:40
cgv::math::vec::set
void set(const T &c0, const T &c1)
set entries of a 2d vector
Definition: vec.h:206
cgv::math::random::uniform_point_in_box
void uniform_point_in_box(const vec< double > &minp, const vec< double > &maxp, vec< double > &p)
creates an uniform distributed random point in box minp..maxp
Definition: random.h:502
cgv::math::perm_mat
Definition: perm_mat.h:13
cgv::math::random::uniform
void uniform(long &rv)
generates a 32bit pseudo random signed integer
Definition: random.h:83
cgv::math::random::uniform
void uniform(const float min, const float max, up_tri_mat< float > &rv)
generates a pseudo random single precision upper triangular matrix with uniformly distribute componen...
Definition: random.h:223
cgv::math::random::uniform
void uniform(const double min, const double max, vec< double > &rv)
generates a pseudo random double precision vector with uniformly distribute components between min an...
Definition: random.h:177
cgv::math::random::uniform
void uniform(const float min, const float max, mat< float > &rv)
generates a pseudo random single precision full matrix with uniformly distribute components between m...
Definition: random.h:192
cgv::math::mat
Definition: mat.h:14
cgv::math::vec
A column vector class.
Definition: fvec.h:13
cgv::math::random::uniform_quat_orientation
void uniform_quat_orientation(vec< double > &q)
Definition: random.h:422
cgv::math::vec::size
unsigned size() const
number of elements
Definition: vec.h:97
cgv::math::random::uniform_point_in_triangle
void uniform_point_in_triangle(const vec< double > &p1, const vec< double > &p2, const vec< double > &p3, vec< double > p)
creates an uniform distributed random point in triangle p1,p2,p3
Definition: random.h:521
cgv::math::random::uniform
void uniform(double &rv)
generates a pseudo random double-precision floating point number uniformly distributed between 0 and ...
Definition: random.h:122
cgv::math::random::uniform
void uniform(unsigned long long &rv)
generates 64bit pseudo random integer
Definition: random.h:57
cgv::math::random::uniform_quat_orientation
void uniform_quat_orientation(vec< float > &q)
generates a single precision random orientation represented as a unit quaternion
Definition: random.h:393
cgv::math::random::uniform
void uniform(const double min, const double max, low_tri_mat< double > &rv)
generates a pseudo random double precision lower triangular matrix with uniformly distribute componen...
Definition: random.h:271
cgv::math::random::uniform
void uniform(float &rv)
generates a pseudo random single-precision floating point number uniformly distributed between 0 and ...
Definition: random.h:138
cgv::math::vec::normalize
void normalize()
normalize the vector using the L2-Norm
Definition: vec.h:649
cgv::math::perm_mat::size
unsigned size() const
number of stored elements
Definition: perm_mat.h:34
cgv::math::random
Definition: random.h:26
cgv::math::random::uniform_point_in_unit_box
void uniform_point_in_unit_box(vec< double > &p)
creates an uniform distributed random point in unit box (0,0,...,0)..(1,1,...,1); if dim(p)=0,...
Definition: random.h:493
cgv::math::random::uniform_nchoosek
void uniform_nchoosek(unsigned n, unsigned k, vec< unsigned > &indices)
creates a vector of k unique indices drawn from 0 to n-1
Definition: random.h:546
cgv::math::random::uniform
void uniform(const double min, const double max, mat< double > &rv)
generates a pseudo random double full matrix with uniformly distribute components between min and max
Definition: random.h:316
cgv::math::random::uniform
void uniform(const float min, const float max, vec< float > &rv)
generates a pseudo random single precision vector with uniformly distribute components between min an...
Definition: random.h:161
cgv::math::diag_mat
Definition: diag_mat.h:16
cgv::math::random::uniform
void uniform(diag_mat< float > &rv)
generates a pseudo random single precision diagonal matrix with uniformly distribute components betwe...
Definition: random.h:279
cgv::math::random::uniform
void uniform(const double min, const double max, double &rv)
generates a pseudo random double-precision floating point number uniformly distributed between min an...
Definition: random.h:130
cgv::math::random::uniform
void uniform(const float min, const float max, low_tri_mat< float > &rv)
generates a pseudo random single precision lower triangular matrix with uniformly distribute componen...
Definition: random.h:255
cgv::math::random::uniform
void uniform(bool &rv)
generates a pseudo random boolean
Definition: random.h:99
cgv::math::random::uniform
void uniform(vec< float > &rv)
generates a pseudo random single precision vector with uniformly distribute components between 0 and ...
Definition: random.h:154
cgv::math::random::set_seed
void set_seed(unsigned long long seed)
set a new seed
Definition: random.h:32
cgv::math::random::uniform
void uniform(const int min, const int max, int &rv)
generates a pseudo random integer between min and max
Definition: random.h:114
cgv::math::random::normal
void normal(const double mu, const double sigma, double &rv)
generates a normal deviate double-precision floating point value with mu and sigma
Definition: random.h:360
cgv::math::random::uniform_orientation
void uniform_orientation(mat< double > &m)
generates a double precision random orientation represented as a rotation matrix
Definition: random.h:472
cgv::math::random::uniform
void uniform(up_tri_mat< float > &rv)
generates a pseudo random single precision upper triangular matrix with uniformly distribute componen...
Definition: random.h:213
cgv::math::quat_2_mat_33
mat< T > quat_2_mat_33(const vec< T > &q)
convert unit quaternion to 3x3 rotation matrix
Definition: quat.h:140
cgv::math::random::uniform_point_in_unit_ball
void uniform_point_in_unit_ball(vec< double > &p)
creates an uniform distributed random point in unit sphere
Definition: random.h:512
cgv::math::random::uniform
void uniform(low_tri_mat< float > &rv)
generates a pseudo random single precision lower triangular matrix with uniformly distribute componen...
Definition: random.h:247
cgv::math::mat::nrows
unsigned nrows() const
number of rows
Definition: mat.h:541
cgv::math::random::uniform
void uniform(unsigned int &rv)
generates a 32bit pseudo random unsigned integer
Definition: random.h:67
cgv::math::random::normal
void normal(const float mu, const float sigma, float &rv)
generates a normal deviate single-precision floating point value with mu and sigma
Definition: random.h:368
cgv::math::random::uniform
void uniform(const double min, const double max, diag_mat< double > &rv)
generates a pseudo random double precision diagonal matrix with uniformly distribute components betwe...
Definition: random.h:300
cgv::math::random::uniform
void uniform(mat< float > &rv)
generates a pseudo random single precision full matrix with uniformly distribute components between 0...
Definition: random.h:184
cgv::math::diag_mat::nrows
unsigned nrows() const
number of rows
Definition: diag_mat.h:63
cgv::math::random::uniform
void uniform(const float min, const float max, diag_mat< float > &rv)
generates a pseudo random single precision diagonal matrix with uniformly distribute components betwe...
Definition: random.h:286
cgv::math::random::normal
void normal(double &rv)
generates a normal deviate double-precision floating point value with mu = 0 and sigma = 1
Definition: random.h:326
cgv::math::random::uniform
void uniform(vec< double > &rv)
generates a pseudo random double precision vector with uniformly distribute components between 0 and ...
Definition: random.h:170
cgv::math::random::random
random()
standard constructor uses system time as random seed
Definition: random.h:44
cgv::math::random::uniform
void uniform(mat< double > &rv)
generates a pseudo random double full matrix with uniformly distribute components between 0 and 1
Definition: random.h:308
cgv::math::random::uniform
void uniform(int &rv)
generates a 32bit pseudo random signed integer
Definition: random.h:75
cgv::math::random::uniform_direction
void uniform_direction(vec< double > &v)
generates a double precision random direction (uniformly distributed position on the unit sphere)
Definition: random.h:385
cgv::math::random::normal
void normal(float &rv)
generates a normal deviate single-precision floating point value with mu = 0 and sigma = 1
Definition: random.h:352
cgv::math::random::uniform_point_on_sphere
void uniform_point_on_sphere(const vec< double > &center, const double &radius, vec< double > &p)
creates an uniform distributed random point on the surface of a sphere with given center and radius
Definition: random.h:535
cgv::math::mat::ncols
unsigned ncols() const
number of columns
Definition: mat.h:547
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::math::random::uniform_direction
void uniform_direction(vec< float > &v)
generates a single precision random direction (uniformly distributed position on the unit sphere)
Definition: random.h:375