Point Cloud Library (PCL)  1.14.0-dev
random.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <random>
43 
44 #include <pcl/pcl_macros.h>
45 
46 namespace pcl
47 {
48  namespace common
49  {
50  /// uniform distribution dummy struct
51  template <typename T, typename T2=void> struct uniform_distribution;
52  /// uniform distribution int specialized
53  template <typename T>
54  struct uniform_distribution<T, std::enable_if_t<std::is_integral<T>::value>>
55  {
56  using type = std::uniform_int_distribution<T>;
57  };
58  /// uniform distribution float specialized
59  template <typename T>
60  struct uniform_distribution<T, std::enable_if_t<std::is_floating_point<T>::value>>
61  {
62  using type = std::uniform_real_distribution<T>;
63  };
64  /// normal distribution
65  template<typename T>
67  {
68  using type = std::normal_distribution<T>;
69  };
70 
71  /** \brief UniformGenerator class generates a random number from range [min, max] at each run picked
72  * according to a uniform distribution i.e each number within [min, max] has almost the same
73  * probability of being drawn.
74  *
75  * \author Nizar Sallem
76  */
77  template<typename T>
79  {
80  public:
81  struct Parameters
82  {
83  Parameters (T _min = 0, T _max = 1, std::uint32_t _seed = 1)
84  : min (_min)
85  , max (_max)
86  , seed (_seed)
87  {}
88 
89  T min;
90  T max;
91  std::uint32_t seed;
92  };
93 
94  /** Constructor
95  * \param min: included lower bound
96  * \param max: included higher bound
97  * \param seed: seeding value
98  */
99  UniformGenerator(T min = 0, T max = 1, std::uint32_t seed = -1);
100 
101  /** Constructor
102  * \param parameters uniform distribution parameters and generator seed
103  */
104  UniformGenerator(const Parameters& parameters);
105 
106  /** Change seed value
107  * \param[in] seed new generator seed value
108  */
109  void
110  setSeed (std::uint32_t seed);
111 
112  /** Set the uniform number generator parameters
113  * \param[in] min minimum allowed value
114  * \param[in] max maximum allowed value
115  * \param[in] seed random number generator seed (applied if != -1)
116  */
117  void
118  setParameters (T min, T max, std::uint32_t seed = -1);
119 
120  /** Set generator parameters
121  * \param parameters uniform distribution parameters and generator seed
122  */
123  void
124  setParameters (const Parameters& parameters);
125 
126  /// \return uniform distribution parameters and generator seed
127  const Parameters&
128  getParameters () { return (parameters_); }
129 
130  /// \return a randomly generated number in the interval [min, max]
131  inline T
132  run () { return (distribution_ (rng_)); }
133 
134  private:
135  using DistributionType = typename uniform_distribution<T>::type;
136  /// parameters
137  Parameters parameters_;
138  /// random number generator
139  std::mt19937 rng_;
140  /// uniform distribution
141  DistributionType distribution_;
142  };
143 
144  /** \brief NormalGenerator class generates a random number from a normal distribution specified
145  * by (mean, sigma).
146  *
147  * \author Nizar Sallem
148  */
149  template<typename T>
151  {
152  public:
153  struct Parameters
154  {
155  Parameters (T _mean = 0, T _sigma = 1, std::uint32_t _seed = 1)
156  : mean (_mean)
157  , sigma (_sigma)
158  , seed (_seed)
159  {}
160 
161  T mean;
162  T sigma;
163  std::uint32_t seed;
164  };
165 
166  /** Constructor
167  * \param[in] mean normal mean
168  * \param[in] sigma normal variation
169  * \param[in] seed seeding value
170  */
171  NormalGenerator(T mean = 0, T sigma = 1, std::uint32_t seed = -1);
172 
173  /** Constructor
174  * \param parameters normal distribution parameters and seed
175  */
176  NormalGenerator(const Parameters& parameters);
177 
178  /** Change seed value
179  * \param[in] seed new seed value
180  */
181  void
182  setSeed (std::uint32_t seed);
183 
184  /** Set the normal number generator parameters
185  * \param[in] mean mean of the normal distribution
186  * \param[in] sigma standard variation of the normal distribution
187  * \param[in] seed random number generator seed (applied if != -1)
188  */
189  void
190  setParameters (T mean, T sigma, std::uint32_t seed = -1);
191 
192  /** Set generator parameters
193  * \param parameters normal distribution parameters and seed
194  */
195  void
196  setParameters (const Parameters& parameters);
197 
198  /// \return normal distribution parameters and generator seed
199  const Parameters&
200  getParameters () { return (parameters_); }
201 
202  /// \return a randomly generated number in the normal distribution (mean, sigma)
203  inline T
204  run () { return (distribution_ (rng_)); }
205 
207  /// parameters
209  /// random number generator
210  std::mt19937 rng_;
211  /// normal distribution
213  };
214  }
215 }
216 
217 #include <pcl/common/impl/random.hpp>
NormalGenerator class generates a random number from a normal distribution specified by (mean,...
Definition: random.h:151
void setParameters(T mean, T sigma, std::uint32_t seed=-1)
Set the normal number generator parameters.
Definition: random.hpp:144
void setSeed(std::uint32_t seed)
Change seed value.
Definition: random.hpp:133
typename normal_distribution< T >::type DistributionType
Definition: random.h:206
std::mt19937 rng_
random number generator
Definition: random.h:210
Parameters parameters_
parameters
Definition: random.h:208
const Parameters & getParameters()
Definition: random.h:200
NormalGenerator(T mean=0, T sigma=1, std::uint32_t seed=-1)
Constructor.
Definition: random.hpp:113
DistributionType distribution_
normal distribution
Definition: random.h:212
UniformGenerator class generates a random number from range [min, max] at each run picked according t...
Definition: random.h:79
void setParameters(T min, T max, std::uint32_t seed=-1)
Set the uniform number generator parameters.
Definition: random.hpp:84
void setSeed(std::uint32_t seed)
Change seed value.
Definition: random.hpp:73
UniformGenerator(T min=0, T max=1, std::uint32_t seed=-1)
Constructor.
Definition: random.hpp:53
const Parameters & getParameters()
Definition: random.h:128
Defines all the PCL and non-PCL macros used.
Parameters(T _mean=0, T _sigma=1, std::uint32_t _seed=1)
Definition: random.h:155
Parameters(T _min=0, T _max=1, std::uint32_t _seed=1)
Definition: random.h:83
normal distribution
Definition: random.h:67
std::normal_distribution< T > type
Definition: random.h:68
uniform distribution dummy struct
Definition: random.h:51