Skip to content

Commit 077cda6

Browse files
author
Heiko Thiel
committed
Use random generator from C++11 instead of from Boost
1 parent d009887 commit 077cda6

File tree

44 files changed

+243
-484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+243
-484
lines changed

apps/3d_rec_framework/include/pcl/apps/3d_rec_framework/pipeline/impl/global_nn_recognizer_crh.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* Author: aitor
66
*/
77

8+
#include <random>
9+
810
#include <pcl/apps/3d_rec_framework/pipeline/global_nn_recognizer_crh.h>
911
#include <pcl/recognition/crh_alignment.h>
1012
#include <pcl/registration/icp.h>
11-
#include <boost/random.hpp>
12-
#include <boost/random/normal_distribution.hpp>
1313
#include <pcl/common/time.h>
1414

1515
template<template<class > class Distance, typename PointInT, typename FeatureT>
@@ -428,17 +428,12 @@ template<template<class > class Distance, typename PointInT, typename FeatureT>
428428

429429
if (noisify_)
430430
{
431-
double noise_std = noise_;
432-
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
433-
boost::posix_time::time_duration duration( time.time_of_day() );
434-
boost::mt19937 rng;
435-
rng.seed (static_cast<unsigned int> (duration.total_milliseconds()));
436-
boost::normal_distribution<> nd (0.0, noise_std);
437-
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
431+
std::random_device rd;
432+
std::mt19937 rng(rd());
433+
std::normal_distribution<float> nd (0.0f, noise_);
438434
// Noisify each point in the dataset
439435
for (size_t cp = 0; cp < view->points.size (); ++cp)
440-
view->points[cp].z += static_cast<float> (var_nor ());
441-
436+
view->points[cp].z += static_cast<float> (nd (rng));
442437
}
443438

444439
//pro view, compute signatures and CRH

apps/3d_rec_framework/include/pcl/apps/3d_rec_framework/pipeline/impl/global_nn_recognizer_cvfh.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#include <pcl/apps/3d_rec_framework/pipeline/global_nn_recognizer_cvfh.h>
99
#include <pcl/registration/icp.h>
10-
#include <boost/random.hpp>
11-
#include <boost/random/normal_distribution.hpp>
1210
#include <pcl/common/time.h>
1311
#include <pcl/visualization/pcl_visualizer.h>
1412

@@ -653,17 +651,12 @@ template<template<class > class Distance, typename PointInT, typename FeatureT>
653651

654652
if (noisify_)
655653
{
656-
double noise_std = noise_;
657-
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
658-
boost::posix_time::time_duration duration( time.time_of_day() );
659-
boost::mt19937 rng;
660-
rng.seed (static_cast<unsigned int> (duration.total_milliseconds()));
661-
boost::normal_distribution<> nd (0.0, noise_std);
662-
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
654+
std::random_device rd;
655+
std::mt19937 rng(rd());
656+
std::normal_distribution<float> nd (0.0f, noise_);
663657
// Noisify each point in the dataset
664658
for (size_t cp = 0; cp < view->points.size (); ++cp)
665-
view->points[cp].z += static_cast<float> (var_nor ());
666-
659+
view->points[cp].z += nd (rng);
667660
}
668661

669662
//pro view, compute signatures

common/include/pcl/common/impl/random.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,10 @@
4040
#ifndef PCL_COMMON_RANDOM_HPP_
4141
#define PCL_COMMON_RANDOM_HPP_
4242

43-
#include <boost/version.hpp>
44-
#include <pcl/pcl_macros.h>
45-
4643
/////////////////////////////////////////////////////////////////////////////////////////////////////////
4744
template <typename T>
4845
pcl::common::UniformGenerator<T>::UniformGenerator(T min, T max, pcl::uint32_t seed)
4946
: distribution_ (min, max)
50-
, generator_ (rng_, distribution_)
5147
{
5248
parameters_ = Parameters (min, max, seed);
5349
if(parameters_.seed != -1)
@@ -60,7 +56,6 @@ template <typename T>
6056
pcl::common::UniformGenerator<T>::UniformGenerator(const Parameters& parameters)
6157
: parameters_ (parameters)
6258
, distribution_ (parameters_.min, parameters_.max)
63-
, generator_ (rng_, distribution_)
6459
{
6560
if(parameters_.seed != -1)
6661
rng_.seed (parameters_.seed);
@@ -87,7 +82,6 @@ pcl::common::UniformGenerator<T>::setParameters (T min, T max, pcl::uint32_t see
8782
typename DistributionType::param_type params (parameters_.min, parameters_.max);
8883
distribution_.param (params);
8984
distribution_.reset ();
90-
generator_.distribution () = distribution_;
9185
if (seed != -1)
9286
{
9387
parameters_.seed = seed;
@@ -103,7 +97,6 @@ pcl::common::UniformGenerator<T>::setParameters (const Parameters& parameters)
10397
typename DistributionType::param_type params (parameters_.min, parameters_.max);
10498
distribution_.param (params);
10599
distribution_.reset ();
106-
generator_.distribution () = distribution_;
107100
if (parameters_.seed != -1)
108101
rng_.seed (parameters_.seed);
109102
}
@@ -112,7 +105,6 @@ pcl::common::UniformGenerator<T>::setParameters (const Parameters& parameters)
112105
template <typename T>
113106
pcl::common::NormalGenerator<T>::NormalGenerator(T mean, T sigma, pcl::uint32_t seed)
114107
: distribution_ (mean, sigma)
115-
, generator_ (rng_, distribution_)
116108
{
117109
parameters_ = Parameters (mean, sigma, seed);
118110
if(parameters_.seed != -1)
@@ -125,7 +117,6 @@ template <typename T>
125117
pcl::common::NormalGenerator<T>::NormalGenerator(const Parameters& parameters)
126118
: parameters_ (parameters)
127119
, distribution_ (parameters_.mean, parameters_.sigma)
128-
, generator_ (rng_, distribution_)
129120
{
130121
if(parameters_.seed != -1)
131122
rng_.seed (parameters_.seed);
@@ -152,7 +143,6 @@ pcl::common::NormalGenerator<T>::setParameters (T mean, T sigma, pcl::uint32_t s
152143
typename DistributionType::param_type params (parameters_.mean, parameters_.sigma);
153144
distribution_.param (params);
154145
distribution_.reset ();
155-
generator_.distribution () = distribution_;
156146
if (seed != -1)
157147
rng_.seed (parameters_.seed);
158148
}
@@ -165,7 +155,6 @@ pcl::common::NormalGenerator<T>::setParameters (const Parameters& parameters)
165155
typename DistributionType::param_type params (parameters_.mean, parameters_.sigma);
166156
distribution_.param (params);
167157
distribution_.reset ();
168-
generator_.distribution () = distribution_;
169158
if (parameters_.seed != -1)
170159
rng_.seed (parameters_.seed);
171160
}

common/include/pcl/common/random.h

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,33 @@
3939

4040
#pragma once
4141

42-
#ifdef __GNUC__
43-
#pragma GCC system_header
44-
#endif
45-
46-
#include <boost/random/uniform_real.hpp>
47-
#include <boost/random/uniform_int.hpp>
48-
#include <boost/random/variate_generator.hpp>
49-
#include <boost/random/normal_distribution.hpp>
50-
#include <boost/random/mersenne_twister.hpp>
42+
#include <random>
43+
5144
#include <pcl/pcl_macros.h>
5245

5346
namespace pcl
5447
{
5548
namespace common
5649
{
5750
/// uniform distribution dummy struct
58-
template <typename T> struct uniform_distribution;
51+
template <typename T, typename T2=void> struct uniform_distribution;
5952
/// uniform distribution int specialized
60-
template<>
61-
struct uniform_distribution<int>
53+
template <typename T>
54+
struct uniform_distribution<T, std::enable_if_t<std::is_integral<T>::value>>
6255
{
63-
typedef boost::uniform_int<int> type;
56+
typedef std::uniform_int_distribution<T> type;
6457
};
6558
/// uniform distribution float specialized
66-
template<>
67-
struct uniform_distribution<float>
59+
template <typename T>
60+
struct uniform_distribution<T, std::enable_if_t<std::is_floating_point<T>::value>>
6861
{
69-
typedef boost::uniform_real<float> type;
62+
typedef std::uniform_real_distribution<T> type;
7063
};
7164
/// normal distribution
7265
template<typename T>
7366
struct normal_distribution
7467
{
75-
typedef boost::normal_distribution<T> type;
68+
typedef std::normal_distribution<T> type;
7669
};
7770

7871
/** \brief UniformGenerator class generates a random number from range [min, max] at each run picked
@@ -136,19 +129,16 @@ namespace pcl
136129

137130
/// \return a randomly generated number in the interval [min, max]
138131
inline T
139-
run () { return (generator_ ()); }
132+
run () { return (distribution_ (rng_)); }
140133

141134
private:
142-
typedef boost::mt19937 EngineType;
143135
typedef typename uniform_distribution<T>::type DistributionType;
144136
/// parameters
145137
Parameters parameters_;
138+
/// random number generator
139+
std::mt19937 rng_;
146140
/// uniform distribution
147141
DistributionType distribution_;
148-
/// random number generator
149-
EngineType rng_;
150-
/// generator of random number from a uniform distribution
151-
boost::variate_generator<EngineType&, DistributionType> generator_;
152142
};
153143

154144
/** \brief NormalGenerator class generates a random number from a normal distribution specified
@@ -211,18 +201,15 @@ namespace pcl
211201

212202
/// \return a randomly generated number in the normal distribution (mean, sigma)
213203
inline T
214-
run () { return (generator_ ()); }
204+
run () { return (distribution_ (rng_)); }
215205

216-
typedef boost::mt19937 EngineType;
217206
typedef typename normal_distribution<T>::type DistributionType;
218207
/// parameters
219208
Parameters parameters_;
209+
/// random number generator
210+
std::mt19937 rng_;
220211
/// normal distribution
221212
DistributionType distribution_;
222-
/// random number generator
223-
EngineType rng_;
224-
/// generator of random number from a normal distribution
225-
boost::variate_generator<EngineType&, DistributionType > generator_;
226213
};
227214
}
228215
}

features/include/pcl/features/3dsc.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
#pragma once
4242

43+
#include <random>
44+
4345
#include <pcl/point_types.h>
4446
#include <pcl/features/boost.h>
4547
#include <pcl/features/feature.h>
@@ -103,16 +105,19 @@ namespace pcl
103105
point_density_radius_(0.2),
104106
descriptor_length_ (),
105107
rng_alg_ (),
106-
rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
108+
rng_ (0.0f, 1.0f)
107109
{
108110
feature_name_ = "ShapeContext3DEstimation";
109111
search_radius_ = 2.5;
110112

111113
// Create a random number generator object
112114
if (random)
113-
rng_->base ().seed (static_cast<unsigned> (std::time(0)));
115+
{
116+
std::random_device rd;
117+
rng_alg_.seed (rd());
118+
}
114119
else
115-
rng_->base ().seed (12345u);
120+
rng_alg_.seed (12345u);
116121
}
117122

118123
~ShapeContext3DEstimation() {}
@@ -211,11 +216,11 @@ namespace pcl
211216
/** \brief Descriptor length */
212217
size_t descriptor_length_;
213218

214-
/** \brief Boost-based random number generator algorithm. */
215-
boost::mt19937 rng_alg_;
219+
/** \brief Random number generator algorithm. */
220+
std::mt19937 rng_alg_;
216221

217-
/** \brief Boost-based random number generator distribution. */
218-
boost::shared_ptr<boost::uniform_01<boost::mt19937> > rng_;
222+
/** \brief Random number generator distribution. */
223+
std::uniform_real_distribution<> rng_;
219224

220225
/* \brief Shift computed descriptor "L" times along the azimuthal direction
221226
* \param[in] block_size the size of each azimuthal block
@@ -229,7 +234,7 @@ namespace pcl
229234
inline double
230235
rnd ()
231236
{
232-
return ((*rng_) ());
237+
return (rng_ (rng_alg_));
233238
}
234239
};
235240
}

features/include/pcl/features/boost.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include <boost/unordered_map.hpp>
4747
#include <boost/function.hpp>
4848
#include <boost/bind.hpp>
49-
#include <boost/random.hpp>
5049
#include <boost/property_map/property_map.hpp>
5150
//#include <boost/graph/adjacency_list.hpp>
5251
//#include <boost/graph/johnson_all_pairs_shortest.hpp>

filters/include/pcl/filters/boost.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
#endif
4646

4747
// Marking all Boost headers as system headers to remove warnings
48-
#include <boost/random.hpp>
49-
#include <boost/random/normal_distribution.hpp>
5048
#include <boost/shared_ptr.hpp>
5149
#include <boost/make_shared.hpp>
5250
#include <boost/dynamic_bitset.hpp>

filters/include/pcl/filters/impl/normal_space.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
#include <pcl/filters/normal_space.h>
4242
#include <pcl/common/io.h>
4343

44-
#include <vector>
4544
#include <list>
45+
#include <random>
46+
#include <vector>
4647

4748
///////////////////////////////////////////////////////////////////////////////
4849
template<typename PointT, typename NormalT> bool
@@ -59,12 +60,6 @@ pcl::NormalSpaceSampling<PointT, NormalT>::initCompute ()
5960
return false;
6061
}
6162

62-
boost::mt19937 rng (static_cast<unsigned int> (seed_));
63-
boost::uniform_int<unsigned int> uniform_distrib (0, unsigned (input_->size ()));
64-
if (rng_uniform_distribution_ != NULL)
65-
delete rng_uniform_distribution_;
66-
rng_uniform_distribution_ = new boost::variate_generator<boost::mt19937, boost::uniform_int<unsigned int> > (rng, uniform_distrib);
67-
6863
return (true);
6964
}
7065

@@ -228,6 +223,10 @@ pcl::NormalSpaceSampling<PointT, NormalT>::applyFilter (std::vector<int> &indice
228223
boost::dynamic_bitset<> is_sampled_flag (input_normals_->points.size ());
229224
// Maintaining flags to check if all points in the bin are sampled
230225
boost::dynamic_bitset<> bin_empty_flag (normals_hg.size ());
226+
227+
std::mt19937 rng (static_cast<unsigned int> (seed_));
228+
std::uniform_int_distribution<unsigned int> uniform_distrib (0, unsigned (input_->size ()));
229+
231230
unsigned int i = 0;
232231
while (i < sample_)
233232
{
@@ -244,7 +243,7 @@ pcl::NormalSpaceSampling<PointT, NormalT>::applyFilter (std::vector<int> &indice
244243
// Picking up a sample at random from jth bin
245244
do
246245
{
247-
random_index = static_cast<unsigned int> ((*rng_uniform_distribution_) () % M);
246+
random_index = static_cast<unsigned int> (uniform_distrib (rng) % M);
248247
pos = start_index[j] + random_index;
249248
} while (is_sampled_flag.test (pos));
250249

filters/include/pcl/filters/impl/voxel_grid_covariance.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#ifndef PCL_VOXEL_GRID_COVARIANCE_IMPL_H_
3939
#define PCL_VOXEL_GRID_COVARIANCE_IMPL_H_
4040

41+
#include <random>
42+
4143
#include <pcl/common/common.h>
4244
#include <pcl/filters/boost.h>
4345
#include <pcl/filters/voxel_grid_covariance.h>
@@ -411,9 +413,8 @@ pcl::VoxelGridCovariance<PointT>::getDisplayCloud (pcl::PointCloud<PointXYZ>& ce
411413
cell_cloud.clear ();
412414

413415
int pnt_per_cell = 1000;
414-
boost::mt19937 rng;
415-
boost::normal_distribution<> nd (0.0, leaf_size_.head (3).norm ());
416-
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
416+
std::mt19937 rng;
417+
std::normal_distribution<> nd (0.0, leaf_size_.head (3).norm ());
417418

418419
Eigen::LLT<Eigen::Matrix3d> llt_of_cov;
419420
Eigen::Matrix3d cholesky_decomp;
@@ -435,7 +436,7 @@ pcl::VoxelGridCovariance<PointT>::getDisplayCloud (pcl::PointCloud<PointXYZ>& ce
435436
// Random points generated by sampling the normal distribution given by voxel mean and covariance matrix
436437
for (int i = 0; i < pnt_per_cell; i++)
437438
{
438-
rand_point = Eigen::Vector3d (var_nor (), var_nor (), var_nor ());
439+
rand_point = Eigen::Vector3d (nd (rng), nd (rng), nd (rng));
439440
dist_point = cell_mean + cholesky_decomp * rand_point;
440441
cell_cloud.push_back (PointXYZ (static_cast<float> (dist_point (0)), static_cast<float> (dist_point (1)), static_cast<float> (dist_point (2))));
441442
}

0 commit comments

Comments
 (0)