Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RandGenerator.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright and trademark 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
57 #ifndef __RAND_GENERATOR_H__
58 #define __RAND_GENERATOR_H__
59 
60 #include "CoreConfig.h"
61 #include "vector.h"
62 #include <iostream>
63 #include <vector>
64 #include "SimpleLock.h"
65 
66 // Forward Declarations
67 class TiXmlElement;
68 
69 namespace Menge {
70 
71  namespace Math {
87  MENGE_API void setDefaultGeneratorSeed( int seed );
88 
94  MENGE_API int getDefaultSeed();
95 
99  class MENGE_API FloatGenerator {
100  public:
105 
109  virtual ~FloatGenerator(){}
110 
130  virtual void print( Logger & out ) const = 0;
131 
139  virtual float getValue() const = 0;
140 
149  virtual float getValueConcurrent() const = 0;
150 
162  virtual FloatGenerator * copy() const = 0;
163  };
164 
166 
170  class MENGE_API ConstFloatGenerator : public FloatGenerator {
171  public:
177  ConstFloatGenerator( float value ):FloatGenerator(), _value(value){}
178 
184  virtual float getValue() const { return _value; }
185 
194  virtual float getValueConcurrent() const { return _value; }
195 
201  virtual void print( Logger & out ) const;
202 
210  virtual FloatGenerator * copy() const;
211 
219  friend Logger & operator<<( Logger & out, const ConstFloatGenerator & gen );
220 
221  protected:
225  float _value;
226  };
227 
229 
243  class MENGE_API NormalFloatGenerator : public FloatGenerator {
244  public:
262  NormalFloatGenerator( float mean, float stddev, float minVal, float maxVal, int seed=0 );
263 
276  void set( float mean, float stddev, float minVal, float maxVal );
277 
283  virtual float getValue() const;
284 
293  virtual float getValueConcurrent() const;
294 
300  virtual void print( Logger & out ) const;
301 
309  virtual FloatGenerator * copy() const;
310 
318  friend Logger & operator<<( Logger & out, const NormalFloatGenerator & gen );
319 
320  protected:
324  float _mean;
325 
329  float _std;
330 
336  float _min;
342  float _max;
343 
347  mutable float _second;
348 
353  mutable unsigned int _calls;
354 
358  mutable int _seed;
359 
363  mutable SimpleLock _lock;
364  };
365 
367 
372  class MENGE_API UniformFloatGenerator : public FloatGenerator {
373  public:
385  UniformFloatGenerator( float minVal, float maxVal, int seed=0 );
386 
395 
401  virtual float getValue() const;
402 
411  virtual float getValueConcurrent() const;
412 
418  virtual void print( Logger & out ) const;
419 
427  virtual FloatGenerator * copy() const;
428 
436  friend Logger & operator<<( Logger & out, const UniformFloatGenerator & gen );
437 
443  float getMin() const { return _min; }
444 
450  float getMax() const { return _min + _size; }
451 
457  float getSize() const { return _size; }
458 
459  protected:
463  float _min;
464 
468  float _size;
469 
473  mutable int _seed;
474 
478  mutable SimpleLock _lock;
479  };
480 
482 
486  class MENGE_API IntGenerator {
487  public:
492 
496  virtual ~IntGenerator() {}
497 
505  virtual int getValue() const = 0;
506 
515  virtual int getValueConcurrent() const = 0;
516 
525  virtual void print( Logger & out ) const = 0;
526 
536  virtual IntGenerator * copy() const = 0;
537  };
538 
540 
544  class MENGE_API ConstIntGenerator : public IntGenerator {
545  public:
551  ConstIntGenerator( int value ): IntGenerator(),_value(value) {}
552 
558  virtual int getValue() const { return _value; }
559 
568  virtual int getValueConcurrent() const { return _value; }
569 
575  virtual void print( Logger & out ) const;
576 
586  virtual IntGenerator * copy() const;
587 
595  friend Logger & operator<<( Logger & out, const ConstIntGenerator & gen );
596 
597 
598  protected:
602  int _value;
603  };
604 
606 
611  class MENGE_API UniformIntGenerator : public IntGenerator {
612  public:
624  UniformIntGenerator( int minVal, int maxVal, int seed=0 );
625 
632  void setRange( int minVal, int maxVal ) { _min = minVal; _size = maxVal - minVal + 1; }
633 
639  virtual int getValue() const;
640 
649  virtual int getValueConcurrent() const;
650 
656  virtual void print( Logger & out ) const;
657 
667  virtual IntGenerator * copy() const;
668 
676  friend Logger & operator<<( Logger & out, const UniformIntGenerator & gen );
677 
678  protected:
682  int _min;
683 
687  int _size;
688 
692  mutable int _seed;
693 
697  mutable SimpleLock _lock;
698  };
699 
700 
702 
706  class MENGE_API Vec2DGenerator {
707  public:
712 
716  virtual ~Vec2DGenerator(){}
717 
725  virtual Vector2 getValue() const = 0;
726 
735  virtual Vector2 getValueConcurrent() const = 0;
736 
745  virtual void print( Logger & out ) const = 0;
746 
756  virtual Vec2DGenerator * copy() const = 0;
757  };
758 
760 
764  class MENGE_API Zero2DGenerator : public Vec2DGenerator {
765  public:
771  virtual Vector2 getValue() const { return Vector2(0.f, 0.f); }
772 
781  virtual Vector2 getValueConcurrent() const { return Vector2(0.f, 0.f); }
782 
790  virtual Vec2DGenerator * copy() const { return new Zero2DGenerator(); }
791 
797  virtual void print( Logger & out ) const;
798 
806  friend Logger & operator<<( Logger & out, const Zero2DGenerator & gen );
807  };
808 
810 
814  class MENGE_API Const2DGenerator : public Vec2DGenerator {
815  public:
821  Const2DGenerator( const Vector2 & val ): Vec2DGenerator(), _value(val) {}
822 
828  virtual Vector2 getValue() const { return _value; }
829 
838  virtual Vector2 getValueConcurrent() const { return _value; }
839 
845  virtual void print( Logger & out ) const;
846 
854  virtual Vec2DGenerator * copy() const { return new Const2DGenerator( _value ); }
855 
863  friend Logger & operator<<( Logger & out, const Const2DGenerator & gen );
864 
865  protected:
870  };
871 
873 
877  class MENGE_API AABBUniformPosGenerator : public Vec2DGenerator {
878  public:
887  AABBUniformPosGenerator( const Vector2 & minPt, const Vector2 & maxPt, int seed=0 );
888 
895 
901  virtual Vector2 getValue() const;
902 
911  virtual Vector2 getValueConcurrent() const;
912 
920  virtual Vec2DGenerator * copy() const;
921 
927  virtual void print( Logger & out ) const;
928 
936  friend Logger & operator<<( Logger & out, const AABBUniformPosGenerator & gen );
937  protected:
942 
947 
951  mutable SimpleLock _lock;
952  };
953 
955 
959  class MENGE_API OBBUniformPosGenerator : public Vec2DGenerator {
960  public:
970  OBBUniformPosGenerator( const Vector2 & minPt, const Vector2 & size, float theta, int seed=0 );
971 
978 
984  virtual Vector2 getValue() const;
985 
994  virtual Vector2 getValueConcurrent() const;
995 
1001  virtual void print( Logger & out ) const;
1002 
1010  virtual Vec2DGenerator * copy() const;
1011 
1019  friend Logger & operator<<( Logger & out, const OBBUniformPosGenerator & gen );
1020 
1021  protected:
1026 
1031 
1036 
1040  float _cosTheta;
1041 
1045  float _sinTheta;
1046 
1051  };
1052 
1054 
1058  class MENGE_API WeightedInt {
1059  public:
1066  WeightedInt( int value, float weight): _val(value), _wt( weight ){}
1067 
1071  int _val;
1072 
1076  float _wt;
1077 
1085  friend Logger & operator<<( Logger & out, const WeightedInt & wInt );
1086  };
1087 
1089 
1096  class MENGE_API WeightedIntGenerator : public IntGenerator {
1097  public:
1102 
1111 
1117  virtual int getValue() const;
1118 
1127  virtual int getValueConcurrent() const;
1128 
1135  void addValue( int value, float weight );
1136 
1142  virtual void print( Logger & out ) const;
1143 
1154  virtual IntGenerator * copy() const;
1155 
1163  friend Logger & operator<<( Logger & out, const WeightedIntGenerator & gen );
1164 
1173  void finalize();
1174  protected:
1175 
1180 
1184  std::vector< WeightedInt > _pairs;
1185 
1190  };
1191 
1206  MENGE_API FloatGenerator * createFloatGenerator( TiXmlElement * node, float scale=1.f, const std::string & prefix="" );
1207 
1219  MENGE_API IntGenerator * createIntGenerator( TiXmlElement * node, const std::string & prefix="" );
1220 
1234  MENGE_API Vec2DGenerator * create2DGenerator( TiXmlElement * node, float scale=1.f );
1235 
1236 
1237  } // namespace Math
1238 } // namespace Menge
1239 #endif // __RAND_GENERATOR_H__
float getSize() const
Reports the size of the interval.
Definition: RandGenerator.h:457
SimpleLock _lock
The lock for guaranteeing threadsafe random number generation.
Definition: RandGenerator.h:363
unsigned int _calls
The number of calls to the generator. Every second call requires a new call to the random number gene...
Definition: RandGenerator.h:353
float _wt
The weight of the value.
Definition: RandGenerator.h:1076
SimpleLock _lock
The lock for guaranteeing threadsafe random number generation.
Definition: RandGenerator.h:1189
Vector2 _minPt
The anchor point of the OB.
Definition: RandGenerator.h:1035
The definition of a simple thrading lock.
Const2DGenerator(const Vector2 &val)
Constructor.
Definition: RandGenerator.h:821
std::vector< WeightedInt > _pairs
The weighted values to select from.
Definition: RandGenerator.h:1184
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
Logger & operator<<(Logger &out, const BFSM::PropertyOperand op)
Friend function for printing string versions of the PropertyOperand enum.
Definition: FSMEnumeration.cpp:43
Generates a 2D float value uniformly distributed in an oriented box (OB).
Definition: RandGenerator.h:959
Generic abstract class which generates a scalar float value.
Definition: RandGenerator.h:99
virtual Vector2 getValue() const
Return a value based on the 2D float generation rules.
Definition: RandGenerator.h:828
Sets up the proper compiler directives for platform and dll export/import.
float getMin() const
Reports the lower end of the valid range.
Definition: RandGenerator.h:443
FloatGenerator()
Constructor.
Definition: RandGenerator.h:104
int _value
The generator's constant value.
Definition: RandGenerator.h:602
An IntGenerator which returns a constant value.
Definition: RandGenerator.h:544
The definition of a simple mutex-style lock.
Definition: SimpleLock.h:62
virtual Vec2DGenerator * copy() const
Create a copy of itself.
Definition: RandGenerator.h:854
virtual float getValue() const
Return a value based on the float generation rules.
Definition: RandGenerator.h:184
UniformFloatGenerator _yRand
The random selector for the position of the return value along the height of the OB.
Definition: RandGenerator.h:1030
virtual Vector2 getValue() const
Return a value based on the 2D float generation rules.
Definition: RandGenerator.h:771
Generic abstract class which generates a 2D vector float values.
Definition: RandGenerator.h:706
UniformFloatGenerator _yRand
The random selector for the y-position of the return value.
Definition: RandGenerator.h:946
void setRange(int minVal, int maxVal)
Set the selection range.
Definition: RandGenerator.h:632
virtual ~IntGenerator()
Virtual destructor.
Definition: RandGenerator.h:496
int _min
The lower end of the valid range.
Definition: RandGenerator.h:682
float _cosTheta
The cosine of the OB's rotation.
Definition: RandGenerator.h:1040
virtual float getValueConcurrent() const
Return a value based on the float generation rules - performed in a thread-safe manner.
Definition: RandGenerator.h:194
UniformFloatGenerator _dice
A uniform float generator for "rolling the dice".
Definition: RandGenerator.h:1179
float _std
The standard deviation of the value.
Definition: RandGenerator.h:329
An html logger - writes messages to a formatted html file.
Definition: Logger.h:59
SimpleLock _lock
The lock for guaranteeing threadsafe random number generation.
Definition: RandGenerator.h:1050
UniformFloatGenerator _xRand
The random selector for the position of the return value along the width of the OB.
Definition: RandGenerator.h:1025
ConstFloatGenerator(float value)
Constructor.
Definition: RandGenerator.h:177
int _val
The value of the entry.
Definition: RandGenerator.h:1071
UniformFloatGenerator _xRand
The random selector for the x-position of the return value.
Definition: RandGenerator.h:941
virtual ~Vec2DGenerator()
Virtual destructor.
Definition: RandGenerator.h:716
The namespace for math primitives for simulation and visualization.
float _min
The lower clamped value, such that all values returned will be greater than or equal to minVal...
Definition: RandGenerator.h:336
float _max
The upper clamped value, such that all values returned will be less than or equal to maxVal...
Definition: RandGenerator.h:342
A number generator based on a weighted probability of a discrete value set.
Definition: RandGenerator.h:1096
float getMax() const
Reports the upper end of the valid range.
Definition: RandGenerator.h:450
SimpleLock _lock
The lock for guaranteeing threadsafe random number generation.
Definition: RandGenerator.h:478
virtual Vector2 getValueConcurrent() const
Return a value based on the 2D float generation rules - performed in a thread-safe manner...
Definition: RandGenerator.h:781
A 2D float generator which always returns a constant 2D vector.
Definition: RandGenerator.h:814
Generic abstract class which generates a scalar integer value.
Definition: RandGenerator.h:486
Vector2 _value
The constant value to return.
Definition: RandGenerator.h:869
float _sinTheta
The sine of the OB's rotation.
Definition: RandGenerator.h:1045
ConstIntGenerator(int value)
Constructor.
Definition: RandGenerator.h:551
virtual ~FloatGenerator()
Virtual destructor.
Definition: RandGenerator.h:109
SimpleLock _lock
The lock for guaranteeing threadsafe random number generation.
Definition: RandGenerator.h:697
Collection of Vector2, Vector3, and constants for simple inclusion.
A weighted integer value. Used with WeightedIntGenerator.
Definition: RandGenerator.h:1058
SimpleLock _lock
The lock for guaranteeing threadsafe random number generation.
Definition: RandGenerator.h:951
float _size
The size of the valid range.
Definition: RandGenerator.h:468
virtual Vector2 getValueConcurrent() const
Return a value based on the 2D float generation rules - performed in a thread-safe manner...
Definition: RandGenerator.h:838
float _value
The generator's constant value.
Definition: RandGenerator.h:225
float _mean
The mean value of the distribution.
Definition: RandGenerator.h:324
A FloatGenerator which returns a uniformly distributed value within a defined range.
Definition: RandGenerator.h:372
virtual int getValueConcurrent() const
Return a value based on the integer generation rules - performed in a thread-safe manner...
Definition: RandGenerator.h:568
virtual Vec2DGenerator * copy() const
Create a copy of itself.
Definition: RandGenerator.h:790
int _seed
A seed for the random number generator.
Definition: RandGenerator.h:358
An IntGenerator which returns a uniformly distributed value within a defined range.
Definition: RandGenerator.h:611
int _seed
A seed for the random number generator.
Definition: RandGenerator.h:473
int _seed
A seed for the random number generator.
Definition: RandGenerator.h:692
Generates a 2D float value uniformly distributed in an axis-aligned box (AAB).
Definition: RandGenerator.h:877
int _size
The size of the valid range.
Definition: RandGenerator.h:687
A FloatGenerator which returns a constant value.
Definition: RandGenerator.h:170
float _min
The lower end of the valid range.
Definition: RandGenerator.h:463
virtual int getValue() const
Return a value based on the integer generation rules.
Definition: RandGenerator.h:558
float _second
The second random number generated (see Math::r4_normalR).
Definition: RandGenerator.h:347
A FloatGenerator which returns a normally distributed value.
Definition: RandGenerator.h:243
A 2D float generator which always returns a zero.
Definition: RandGenerator.h:764
Vec2DGenerator()
Constructor.
Definition: RandGenerator.h:711
WeightedInt(int value, float weight)
Constructor.
Definition: RandGenerator.h:1066
IntGenerator()
Constructor.
Definition: RandGenerator.h:491