Skip to content

Commit 8866006

Browse files
SunBlackSergioRAgostinho
authored andcommitted
Use random generator from C++11 instead of from Boost (#2956)
1 parent ec70de5 commit 8866006

File tree

34 files changed

+192
-279
lines changed

34 files changed

+192
-279
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>
@@ -424,17 +424,12 @@ template<template<class > class Distance, typename PointInT, typename FeatureT>
424424

425425
if (noisify_)
426426
{
427-
double noise_std = noise_;
428-
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
429-
boost::posix_time::time_duration duration( time.time_of_day() );
430-
boost::mt19937 rng;
431-
rng.seed (static_cast<unsigned int> (duration.total_milliseconds()));
432-
boost::normal_distribution<> nd (0.0, noise_std);
433-
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
427+
std::random_device rd;
428+
std::mt19937 rng(rd());
429+
std::normal_distribution<float> nd (0.0f, noise_);
434430
// Noisify each point in the dataset
435431
for (size_t cp = 0; cp < view->points.size (); ++cp)
436-
view->points[cp].z += static_cast<float> (var_nor ());
437-
432+
view->points[cp].z += nd (rng);
438433
}
439434

440435
//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

@@ -649,17 +647,12 @@ template<template<class > class Distance, typename PointInT, typename FeatureT>
649647

650648
if (noisify_)
651649
{
652-
double noise_std = noise_;
653-
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
654-
boost::posix_time::time_duration duration( time.time_of_day() );
655-
boost::mt19937 rng;
656-
rng.seed (static_cast<unsigned int> (duration.total_milliseconds()));
657-
boost::normal_distribution<> nd (0.0, noise_std);
658-
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
650+
std::random_device rd;
651+
std::mt19937 rng(rd());
652+
std::normal_distribution<float> nd (0.0f, noise_);
659653
// Noisify each point in the dataset
660654
for (size_t cp = 0; cp < view->points.size (); ++cp)
661-
view->points[cp].z += static_cast<float> (var_nor ());
662-
655+
view->points[cp].z += nd (rng);
663656
}
664657

665658
//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
}

outofcore/include/pcl/outofcore/boost.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,4 @@
4444

4545
#include <boost/filesystem.hpp>
4646
#include <boost/thread.hpp>
47-
#include <boost/random/uniform_int.hpp>
48-
#include <boost/uuid/uuid.hpp>
49-
#include <boost/uuid/uuid_generators.hpp>
50-
#include <boost/uuid/uuid_io.hpp>
51-
#include <boost/random/mersenne_twister.hpp>
52-
#include <boost/random/uniform_int.hpp>
53-
#include <boost/random/bernoulli_distribution.hpp>
5447
#include <boost/foreach.hpp>

outofcore/include/pcl/outofcore/impl/octree_base_node.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace pcl
7878
boost::mutex OutofcoreOctreeBaseNode<ContainerT, PointT>::rng_mutex_;
7979

8080
template<typename ContainerT, typename PointT>
81-
boost::mt19937 OutofcoreOctreeBaseNode<ContainerT, PointT>::rand_gen_;
81+
std::mt19937 OutofcoreOctreeBaseNode<ContainerT, PointT>::rng_;
8282

8383
template<typename ContainerT, typename PointT>
8484
const double OutofcoreOctreeBaseNode<ContainerT, PointT>::sample_percent_ = .125;
@@ -597,25 +597,23 @@ namespace pcl
597597

598598
// Create random number generator
599599
boost::mutex::scoped_lock lock(rng_mutex_);
600-
boost::uniform_int<boost::uint64_t> buffdist(0, inputsize-1);
601-
boost::variate_generator<boost::mt19937&, boost::uniform_int<boost::uint64_t> > buffdie(rand_gen_, buffdist);
600+
std::uniform_int_distribution<uint64_t> buffdist(0, inputsize-1);
602601

603602
// Randomly pick sampled points
604-
for(boost::uint64_t i = 0; i < samplesize; ++i)
603+
for(uint64_t i = 0; i < samplesize; ++i)
605604
{
606-
boost::uint64_t buffstart = buffdie();
605+
uint64_t buffstart = buffdist(rng_);
607606
insertBuff[i] = ( sampleBuff[buffstart] );
608607
}
609608
}
610609
// Have to do it the slow way
611610
else
612611
{
613612
boost::mutex::scoped_lock lock(rng_mutex_);
614-
boost::bernoulli_distribution<double> buffdist(percent);
615-
boost::variate_generator<boost::mt19937&, boost::bernoulli_distribution<double> > buffcoin(rand_gen_, buffdist);
613+
std::bernoulli_distribution buffdist(percent);
616614

617-
for(boost::uint64_t i = 0; i < inputsize; ++i)
618-
if(buffcoin())
615+
for(uint64_t i = 0; i < inputsize; ++i)
616+
if(buffdist(rng_))
619617
insertBuff.push_back( p[i] );
620618
}
621619
}

outofcore/include/pcl/outofcore/impl/octree_disk_container.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747

4848
// Boost
4949
#include <pcl/outofcore/boost.h>
50+
#include <boost/random/bernoulli_distribution.hpp>
51+
#include <boost/random/uniform_int.hpp>
52+
#include <boost/uuid/uuid_io.hpp>
5053

5154
// PCL
5255
#include <pcl/io/pcd_io.h>

outofcore/include/pcl/outofcore/impl/octree_ram_container.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ namespace pcl
5050
{
5151
namespace outofcore
5252
{
53-
5453
template<typename PointT>
5554
boost::mutex OutofcoreOctreeRamContainer<PointT>::rng_mutex_;
5655

5756
template<typename PointT>
58-
boost::mt19937 OutofcoreOctreeRamContainer<PointT>::rand_gen_ (static_cast<unsigned int>(std::time( NULL)));
57+
std::mt19937 OutofcoreOctreeRamContainer<PointT>::rng_ ([] {std::random_device rd; return rd(); } ());
5958

6059
template<typename PointT> void
6160
OutofcoreOctreeRamContainer<PointT>::convertToXYZ (const boost::filesystem::path& path)
@@ -121,16 +120,15 @@ namespace pcl
121120
const double percent,
122121
AlignedPointTVector& v)
123122
{
124-
boost::uint64_t samplesize = static_cast<boost::uint64_t> (percent * static_cast<double> (count));
123+
uint64_t samplesize = static_cast<uint64_t> (percent * static_cast<double> (count));
125124

126125
boost::mutex::scoped_lock lock (rng_mutex_);
127126

128-
boost::uniform_int < boost::uint64_t > buffdist (start, start + count);
129-
boost::variate_generator<boost::mt19937&, boost::uniform_int<boost::uint64_t> > buffdie (rand_gen_, buffdist);
127+
std::uniform_int_distribution < uint64_t > buffdist (start, start + count);
130128

131-
for (boost::uint64_t i = 0; i < samplesize; i++)
129+
for (uint64_t i = 0; i < samplesize; i++)
132130
{
133-
boost::uint64_t buffstart = buffdie ();
131+
uint64_t buffstart = buffdist (rng_);
134132
v.push_back (container_[buffstart]);
135133
}
136134
}

outofcore/include/pcl/outofcore/octree_abstract_node_container.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
#pragma once
4040

41-
#include <vector>
4241
#include <string>
42+
#include <vector>
4343

4444
#include <pcl/outofcore/boost.h>
4545

@@ -96,7 +96,6 @@ namespace pcl
9696
AlignedPointTVector container_;
9797

9898
static boost::mutex rng_mutex_;
99-
static boost::mt19937 rand_gen_;
10099
};
101100
}//namespace outofcore
102101
}//namespace pcl

outofcore/include/pcl/outofcore/octree_base_node.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
#pragma once
4141

42+
#include <random>
43+
4244
#include <pcl/common/io.h>
4345
#include <pcl/PCLPointCloud2.h>
4446

@@ -561,10 +563,8 @@ namespace pcl
561563

562564
/** \brief Mersenne Twister: A 623-dimensionally equidistributed uniform
563565
* pseudo-random number generator */
564-
static boost::mt19937 rand_gen_;
566+
static std::mt19937 rng_;
565567

566-
/** \brief Random number generator seed */
567-
const static boost::uint32_t rngseed = 0xAABBCCDD;
568568
/** \brief Extension for this class to find the pcd files on disk */
569569
const static std::string pcd_extension;
570570

0 commit comments

Comments
 (0)