Robowflex  v0.1
Making MoveIt Easy
random.cpp
Go to the documentation of this file.
1 /* Author: Constantinos Chamzas */
2 
5 
6 using namespace robowflex;
7 
8 namespace
9 {
10  static std::mt19937 GENERATOR; ///< Random engine generator.
11  static std::uniform_real_distribution<> UNIDIST{0, 1}; ///< Uniform distribution.
12  static std::normal_distribution<> NORMALDIST{0, 1}; ///< Normal distribution.
13 } // namespace
14 
15 void RNG::setSeed(unsigned int seed)
16 {
17  GENERATOR.seed(seed);
18 }
19 
21 {
22  return UNIDIST(GENERATOR);
23 }
24 
25 double RNG::uniformReal(double lower_bound, double upper_bound)
26 {
27  assert(lower_bound <= upper_bound);
28  return (upper_bound - lower_bound) * uniform01() + lower_bound;
29 }
30 
31 int RNG::uniformInt(int lower_bound, int upper_bound)
32 {
33  auto r = (int)floor(uniformReal((double)lower_bound, (double)(upper_bound)));
34  return (r > upper_bound - 1) ? upper_bound - 1 : r;
35 }
36 
38 {
39  return uniform01() <= 0.5;
40 }
41 
43 {
44  return NORMALDIST(GENERATOR);
45 }
46 
47 double RNG::gaussian(double mean, double stddev)
48 {
49  return gaussian01() * stddev + mean;
50 }
51 
52 double RNG::gaussian(double stddev)
53 {
54  return gaussian01() * stddev;
55 }
56 
57 Eigen::Vector3d RNG::uniformRPY(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
58 {
59  const double phi_min = std::max(-constants::pi, lbound[0]);
60  const double chi_min = std::max(-constants::half_pi, lbound[1]);
61  const double psi_min = std::max(-constants::pi, lbound[2]);
62 
63  const double phi_max = std::min(constants::pi, ubound[0]);
64  const double chi_max = std::min(constants::half_pi, ubound[1]);
65  const double psi_max = std::min(constants::pi, ubound[2]);
66 
67  Eigen::Vector3d v;
68  // From Effective Sampling and Distance Metrics for 3D Rigid Body Path Planning, 2004
69  v[0] = uniformReal(phi_min, phi_max);
70  v[1] = acos(uniformReal(cos(chi_min + constants::half_pi), cos(chi_max + constants::half_pi))) -
72  v[2] = uniformReal(psi_min, psi_max);
73 
74  return v;
75 }
76 
77 Eigen::Vector3d RNG::uniformRPY(const Eigen::Vector3d &bounds)
78 {
79  return uniformRPY(-bounds, bounds);
80 }
81 
82 Eigen::Vector3d RNG::unifromRPY()
83 {
84  return uniformRPY(Eigen::Vector3d{-constants::pi, -constants::half_pi, -constants::pi},
86 }
87 
88 Eigen::Vector3d RNG::uniformVec(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
89 {
90  Eigen::Vector3d vec;
91  vec[0] = uniformReal(lbound[0], ubound[0]);
92  vec[1] = uniformReal(lbound[1], ubound[1]);
93  vec[2] = uniformReal(lbound[2], ubound[2]);
94 
95  return vec;
96 }
97 
98 Eigen::Vector3d RNG::uniformVec(const Eigen::Vector3d &bounds)
99 {
100  return uniformVec(-bounds, bounds);
101 }
102 
103 Eigen::Vector3d RNG::gaussianVec(const Eigen::Vector3d &mean, const Eigen::Vector3d &stddev)
104 {
105  Eigen::Vector3d vec;
106  vec[0] = gaussian(mean[0], stddev[0]);
107  vec[1] = gaussian(mean[1], stddev[1]);
108  vec[2] = gaussian(mean[2], stddev[2]);
109  return vec;
110 }
111 
112 Eigen::Vector3d RNG::gaussianVec(const Eigen::Vector3d &stddev)
113 {
114  Eigen::Vector3d vec;
115  vec[0] = gaussian(stddev[0]);
116  vec[1] = gaussian(stddev[1]);
117  vec[2] = gaussian(stddev[2]);
118 
119  return vec;
120 }
T max(T... args)
T min(T... args)
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
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
static const double half_pi
Definition: constants.h:22
static const double pi
Definition: constants.h:21
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25