Robowflex  v0.1
Making MoveIt Easy
random.h
Go to the documentation of this file.
1 /* Author: Constantinos Chamzas */
2 
3 #ifndef ROBOWFLEX_RANDOM_
4 #define ROBOWFLEX_RANDOM_
5 
6 #include <random>
8 
9 #include <Eigen/Core>
10 
11 namespace robowflex
12 {
13  /** \brief Collection of methods relating to random sampling
14  */
15  namespace RNG
16  {
17  /** \brief Set the random seed of the underlying generator.
18  * \param[in] seed Seed to set in generator.
19  */
20  void setSeed(unsigned int seed);
21 
22  /** \brief Generate a random real in [0,1).
23  * \return Sampled number.
24  */
25  double uniform01();
26 
27  /** \brief Generate a random real within given bounds: [\a lower_bound, \a upper_bound)
28  * \param[in] lower_bound Lower bound of uniform distribution.
29  * \param[in] upper_bound Upper bound of uniform distribution.
30  * \return Sampled number.
31  */
32  double uniformReal(double lower_bound, double upper_bound);
33 
34  /** \brief Generate a random integer within given bounds: [\a lower_bound, \a upper_bound)
35  * \param[in] lower_bound Lower bound of uniform distribution.
36  * \param[in] upper_bound Upper bound of uniform distribution.
37  * \return Sampled number.
38  */
39  int uniformInt(int lower_bound, int upper_bound);
40 
41  /** \brief Generate a random boolean.
42  * \return Sampled number.
43  */
44  bool uniformBool();
45 
46  /** Generate a random real using a normal distribution with mean 0 and variance 1
47  * \return Sampled number.
48  */
49  double gaussian01();
50 
51  /** \brief Generate a random real using a normal distribution with given \a mean and \e standard
52  * deviation.
53  * \param[in] mean Mean of the normal distribution.
54  * \param[in] stddev Standard deviation of the normal distribution.
55  * \return Sampled number.
56  */
57  double gaussian(double mean, double stddev);
58 
59  /** \brief Generate a random real using a normal distribution with zero mean and given \e standard
60  * deviation.
61  * \param[in] stddev Standard deviation of the normal distribution.
62  * \return Sampled number.
63  */
64  double gaussian(double stddev);
65 
66  /** \brief Uniform random sampling of Euler roll-pitch-yaw angles within lower bound \a lbound and
67  * upper bound \a ubound computed value has the order (roll, pitch, yaw).
68  * \param[in] lbound Lower bound for roll pitch yaw.
69  * \param[in] ubound Upper bound for roll pitch yaw.
70  * \return roll-pitch-yaw vector.
71  */
72  Eigen::Vector3d uniformRPY(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound);
73 
74  /** \brief Uniform random sampling of Euler roll-pitch-yaw angles within lower bound \a lbound and
75  * upper bound \a ubound computed value has the order (roll, pitch, yaw).
76  * \param[in] bounds [-bounds, bounds] is lower and upper bound is respectively.
77  * \return roll-pitch-yaw vector.
78  */
79  Eigen::Vector3d uniformRPY(const Eigen::Vector3d &bounds);
80 
81  /** \brief Uniform random sampling of Euler roll-pitch-yaw angles, roll, yaw in range [-pi, pi) and
82  * pitch in range[-pi/2, pi/2) computed value has the order (roll, pitch, yaw).
83  * \return roll-pitch-yaw vector.
84  */
85  Eigen::Vector3d unifromRPY();
86 
87  /** \brief Generate a uniform real vector within given bounds: [\a lower_bound, \a upper_bound)
88  * \param[in] lbound Lower bound vector of uniform distribution.
89  * \param[in] ubound Upper bound vector of uniform distribution.
90  * \return Sampled vector.
91  */
92  Eigen::Vector3d uniformVec(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound);
93 
94  /** \brief Generate a uniform real vector within given bounds: [\a -bounds, \a bounds)
95  * \param[in] bounds Upper and (negative) lower bound vector of uniform distribution.
96  * \return Sampled vector.
97  */
98  Eigen::Vector3d uniformVec(const Eigen::Vector3d &bounds);
99 
100  /** \brief Generate a random real vector using a normal distribution with given \a mean and \e
101  * standard deviation
102  * \param[in] mean Mean vector of the normal distribution.
103  * \param[in] stddev Standard deviation vector (diagonal covariance) of the normal distribution.
104  * \return Sampled vector.
105  */
106  Eigen::Vector3d gaussianVec(const Eigen::Vector3d &mean, const Eigen::Vector3d &stddev);
107 
108  /** \brief Generate a random real vector using a normal distribution with \e mean zero and \e
109  * standard deviation.
110  * \param[in] stddev Standard deviation vector (diagonal covariance) of the normal distribution.
111  * \return Sampled vector.
112  */
113  Eigen::Vector3d gaussianVec(const Eigen::Vector3d &stddev);
114 
115  /** \brief Choose a random element between \a start and \a end.
116  * \param[in] start Start iterator.
117  * \param[in] end End iterator.
118  * \return Chosen element.
119  */
120  template <typename Iter>
121  Iter uniformSample(Iter start, Iter end)
122  {
123  std::advance(start, uniformInt(0, std::distance(start, end)));
124  return start;
125  };
126 
127  /** \brief Choose a random element from a vector.
128  * \param[in] vector Vector to sample from.
129  * \return Chosen element.
130  */
131  template <typename Type>
133  {
134  return *uniformSample(vector.begin(), vector.end());
135  }
136  } // namespace RNG
137 
138 } // namespace robowflex
139 
140 #endif
T advance(T... args)
T begin(T... args)
T distance(T... args)
T end(T... args)
Type
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:25
double uniform01()
Generate a random real in [0,1).
Definition: random.cpp:20
double gaussian(double mean, double stddev)
Generate a random real using a normal distribution with given mean and standard deviation.
Definition: random.cpp:47
bool uniformBool()
Generate a random boolean.
Definition: random.cpp:37
Eigen::Vector3d uniformRPY(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubou...
Definition: random.cpp:57
Iter uniformSample(Iter start, Iter end)
Choose a random element between start and end.
Definition: random.h:121
Eigen::Vector3d unifromRPY()
Uniform random sampling of Euler roll-pitch-yaw angles, roll, yaw in range [-pi, pi) and pitch in ran...
Definition: random.cpp:82
int uniformInt(int lower_bound, int upper_bound)
Generate a random integer within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:31
void setSeed(unsigned int seed)
Set the random seed of the underlying generator.
Definition: random.cpp:15
double gaussian01()
Definition: random.cpp:42
Eigen::Vector3d uniformVec(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
Generate a uniform real vector within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:88
Eigen::Vector3d gaussianVec(const Eigen::Vector3d &mean, const Eigen::Vector3d &stddev)
Generate a random real vector using a normal distribution with given mean and standard deviation.
Definition: random.cpp:103
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25