Loading...
Searching...
No Matches
RandomNumbers.h
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Ioan Sucan, Jonathan Gammell */
36
37#ifndef OMPL_UTIL_RANDOM_NUMBERS_
38#define OMPL_UTIL_RANDOM_NUMBERS_
39
40#include <memory>
41#include <random>
42#include <cassert>
43#include <cstdint>
44#include <algorithm>
45
46#include "ompl/util/ProlateHyperspheroid.h"
47
48namespace ompl
49{
56 class RNG
57 {
58 public:
60 RNG();
61
63 RNG(std::uint_fast32_t localSeed);
64
66 double uniform01()
67 {
68 return uniDist_(generator_);
69 }
70
72 double uniformReal(double lower_bound, double upper_bound)
73 {
74 assert(lower_bound <= upper_bound);
75 return (upper_bound - lower_bound) * uniDist_(generator_) + lower_bound;
76 }
77
79 int uniformInt(int lower_bound, int upper_bound)
80 {
81 auto r = (int)floor(uniformReal((double)lower_bound, (double)(upper_bound) + 1.0));
82 return (r > upper_bound) ? upper_bound : r;
83 }
84
87 {
88 return uniDist_(generator_) <= 0.5;
89 }
90
92 double gaussian01()
93 {
94 return normalDist_(generator_);
95 }
96
98 double gaussian(double mean, double stddev)
99 {
100 return normalDist_(generator_) * stddev + mean;
101 }
102
109 double halfNormalReal(double r_min, double r_max, double focus = 3.0);
110
114 int halfNormalInt(int r_min, int r_max, double focus = 3.0);
115
118 void quaternion(double value[4]);
119
122 void eulerRPY(double value[3]);
123
126 static void setSeed(std::uint_fast32_t seed);
127
131 static std::uint_fast32_t getSeed();
132
135 void setLocalSeed(std::uint_fast32_t localSeed);
136
141 std::uint_fast32_t getLocalSeed() const
142 {
143 return localSeed_;
144 }
145
148 void uniformNormalVector(std::vector<double> &v);
149
153 void uniformInBall(double r, std::vector<double> &v);
154
163 void uniformProlateHyperspheroidSurface(const std::shared_ptr<const ProlateHyperspheroid> &phsPtr,
164 double value[]);
165
174 void uniformProlateHyperspheroid(const std::shared_ptr<const ProlateHyperspheroid> &phsPtr, double value[]);
175
177 template <class RandomAccessIterator>
178 void shuffle(RandomAccessIterator first, RandomAccessIterator last)
179 {
180 std::shuffle(first, last, generator_);
181 }
182
183 private:
186 class SphericalData;
187
189 std::uint_fast32_t localSeed_;
190 std::mt19937 generator_;
191 std::uniform_real_distribution<> uniDist_{0, 1};
192 std::normal_distribution<> normalDist_{0, 1};
193 // A structure holding boost::uniform_on_sphere distributions and the associated boost::variate_generators for
194 // various dimension
195 std::shared_ptr<SphericalData> sphericalDataPtr_;
196 };
197} // namespace ompl
198
199#endif
void quaternion(double value[4])
Uniform random unit quaternion sampling. The computed value has the order (x,y,z,w)....
double gaussian(double mean, double stddev)
Generate a random real using a normal distribution with given mean and variance.
int halfNormalInt(int r_min, int r_max, double focus=3.0)
Generate a random integer using a half-normal distribution. The value is within specified bounds ([r_...
void eulerRPY(double value[3])
Uniform random sampling of Euler roll-pitch-yaw angles, each in the range (-pi, pi]....
void setLocalSeed(std::uint_fast32_t localSeed)
Set the seed used for the instance of a RNG. Use this function to ensure that an instance of an RNG g...
static void setSeed(std::uint_fast32_t seed)
Set the seed used to generate the seeds of each RNG instance. Use this function to ensure the same se...
void uniformNormalVector(std::vector< double > &v)
Uniform random sampling of a unit-length vector. I.e., the surface of an n-ball. The return variable ...
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound).
void shuffle(RandomAccessIterator first, RandomAccessIterator last)
randomly rearrange elements in the range [first, last)
void uniformInBall(double r, std::vector< double > &v)
Uniform random sampling of the content of an n-ball, with a radius appropriately distributed between ...
double gaussian01()
Generate a random real using a normal distribution with mean 0 and variance 1.
void uniformProlateHyperspheroid(const std::shared_ptr< const ProlateHyperspheroid > &phsPtr, double value[])
Uniform random sampling of a prolate hyperspheroid, a special symmetric type of n-dimensional ellipse...
RNG()
Constructor. Always sets a different random seed.
static std::uint_fast32_t getSeed()
Get the seed used to generate the seeds of each RNG instance. Passing the returned value to setSeed()...
double halfNormalReal(double r_min, double r_max, double focus=3.0)
Generate a random real using a half-normal distribution. The value is within specified bounds [r_min,...
void uniformProlateHyperspheroidSurface(const std::shared_ptr< const ProlateHyperspheroid > &phsPtr, double value[])
Uniform random sampling of the surface of a prolate hyperspheroid, a special symmetric type of n-dime...
bool uniformBool()
Generate a random boolean.
int uniformInt(int lower_bound, int upper_bound)
Generate a random integer within given bounds: [lower_bound, upper_bound].
double uniform01()
Generate a random real between 0 and 1.
std::uint_fast32_t getLocalSeed() const
Get the seed used for the instance of a RNG. Passing the returned value to the setInstanceSeed() of a...
Main namespace. Contains everything in this library.