Skip to content

Commit 2fd57f9

Browse files
committed
replace boost random with std equivalent
1 parent a26f6d4 commit 2fd57f9

File tree

6 files changed

+58
-85
lines changed

6 files changed

+58
-85
lines changed

filters/include/pcl/filters/boost.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
PCL_DEPRECATED_HEADER(1, 15, "Please include the needed boost headers directly.")
4747

4848
// Marking all Boost headers as system headers to remove warnings
49-
#include <boost/random.hpp>
50-
#include <boost/random/normal_distribution.hpp>
5149
#include <boost/dynamic_bitset.hpp>
5250
#include <boost/mpl/size.hpp>
5351
#include <boost/fusion/sequence/intrinsic/at_key.hpp>

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@
4444
#include <Eigen/Cholesky>
4545
#include <Eigen/Eigenvalues> // for SelfAdjointEigenSolver
4646
#include <boost/mpl/size.hpp> // for size
47-
#include <boost/random/mersenne_twister.hpp> // for mt19937
48-
#include <boost/random/normal_distribution.hpp> // for normal_distribution
49-
#include <boost/random/variate_generator.hpp> // for variate_generator
47+
48+
#include <random>
5049

5150
//////////////////////////////////////////////////////////////////////////////////////////
5251
template<typename PointT> void
@@ -447,9 +446,9 @@ pcl::VoxelGridCovariance<PointT>::getDisplayCloud (pcl::PointCloud<PointXYZ>& ce
447446
cell_cloud.clear ();
448447

449448
int pnt_per_cell = 1000;
450-
boost::mt19937 rng;
451-
boost::normal_distribution<> nd (0.0, 1.0);
452-
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
449+
std::random_device rd {};
450+
std::mt19937 rng {rd ()};
451+
std::normal_distribution<> nd (0.0, 1.0);
453452

454453
Eigen::LLT<Eigen::Matrix3d> llt_of_cov;
455454
Eigen::Matrix3d cholesky_decomp;
@@ -474,7 +473,7 @@ pcl::VoxelGridCovariance<PointT>::getDisplayCloud (pcl::PointCloud<PointXYZ>& ce
474473
// Random points generated by sampling the normal distribution given by voxel mean and covariance matrix
475474
for (int i = 0; i < pnt_per_cell; i++)
476475
{
477-
rand_point = Eigen::Vector3d (var_nor (), var_nor (), var_nor ());
476+
rand_point = Eigen::Vector3d (nd (rng), nd (rng), nd (rng));
478477
dist_point = cell_mean + cholesky_decomp * rand_point;
479478
cell_cloud.push_back (PointXYZ (static_cast<float> (dist_point (0)), static_cast<float> (dist_point (1)), static_cast<float> (dist_point (2))));
480479
}

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@
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>
5350

5451
// PCL
5552
#include <pcl/common/utils.h> // pcl::utils::ignore
@@ -71,34 +68,12 @@ namespace pcl
7168
{
7269
namespace outofcore
7370
{
74-
template<typename PointT>
75-
std::mutex OutofcoreOctreeDiskContainer<PointT>::rng_mutex_;
76-
77-
template<typename PointT> boost::mt19937
78-
OutofcoreOctreeDiskContainer<PointT>::rand_gen_ (static_cast<unsigned int> (std::time(nullptr)));
79-
80-
template<typename PointT>
81-
boost::uuids::basic_random_generator<boost::mt19937> OutofcoreOctreeDiskContainer<PointT>::uuid_gen_ (&rand_gen_);
82-
8371
template<typename PointT>
8472
const std::uint64_t OutofcoreOctreeDiskContainer<PointT>::READ_BLOCK_SIZE_ = static_cast<std::uint64_t> (2e12);
8573
template<typename PointT>
8674
const std::uint64_t OutofcoreOctreeDiskContainer<PointT>::WRITE_BUFF_MAX_ = static_cast<std::uint64_t> (2e12);
8775

88-
template<typename PointT> void
89-
OutofcoreOctreeDiskContainer<PointT>::getRandomUUIDString (std::string& s)
90-
{
91-
boost::uuids::uuid u;
92-
{
93-
std::lock_guard<std::mutex> lock (rng_mutex_);
94-
u = uuid_gen_ ();
95-
}
96-
97-
std::stringstream ss;
98-
ss << u;
99-
s = ss.str ();
100-
}
101-
////////////////////////////////////////////////////////////////////////////////
76+
////////////////////////////////////////////////////////////////////////////////
10277

10378
template<typename PointT>
10479
OutofcoreOctreeDiskContainer<PointT>::OutofcoreOctreeDiskContainer ()
@@ -119,9 +94,7 @@ namespace pcl
11994
{
12095
if (boost::filesystem::is_directory (path))
12196
{
122-
std::string uuid;
123-
getRandomUUIDString (uuid);
124-
boost::filesystem::path filename (uuid);
97+
boost::filesystem::path filename (boost::filesystem::unique_path());
12598
boost::filesystem::path file = path / filename;
12699

127100
disk_storage_filename_ = file.string ();
@@ -298,12 +271,14 @@ namespace pcl
298271
{
299272
{
300273
std::lock_guard<std::mutex> lock (rng_mutex_);
301-
boost::bernoulli_distribution<double> buffdist (percent);
302-
boost::variate_generator<boost::mt19937&, boost::bernoulli_distribution<double> > buffcoin (rand_gen_, buffdist);
274+
275+
std::random_device buff_dev;
276+
std::mt19937 buff_gen (buff_dev ());
277+
std::bernoulli_distribution<double> buffdist (percent);
303278

304279
for (std::size_t i = buffstart; i < static_cast<std::uint64_t> (buffcount); i++)
305280
{
306-
if (buffcoin ())
281+
if (buffdist (buff_gen))
307282
{
308283
dst.push_back (writebuff_[i]);
309284
}
@@ -318,11 +293,13 @@ namespace pcl
318293
{
319294
std::lock_guard<std::mutex> lock (rng_mutex_);
320295

321-
boost::bernoulli_distribution<double> filedist (percent);
322-
boost::variate_generator<boost::mt19937&, boost::bernoulli_distribution<double> > filecoin (rand_gen_, filedist);
296+
std::random_device buff_dev;
297+
std::mt19937 buff_gen (buff_dev ());
298+
std::bernoulli_distribution<double> buffdist (percent);
299+
323300
for (std::uint64_t i = filestart; i < (filestart + filecount); i++)
324301
{
325-
if (filecoin ())
302+
if (buffdist (buff_gen))
326303
{
327304
offsets.push_back (i);
328305
}

sample_consensus/include/pcl/sample_consensus/boost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444
#endif
4545
PCL_DEPRECATED_HEADER(1, 15, "Please include the needed boost headers directly.")
4646

47-
#include <boost/random.hpp>
47+
#include <random>

sample_consensus/include/pcl/sample_consensus/sac.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@
4343
#include <pcl/sample_consensus/sac_model.h>
4444
#include <pcl/pcl_base.h>
4545

46-
#include <boost/random/mersenne_twister.hpp> // for mt19937
47-
#include <boost/random/uniform_01.hpp> // for uniform_01
48-
4946
#include <ctime>
5047
#include <memory>
48+
#include <random>
5149
#include <set>
5250

5351
namespace pcl
@@ -63,7 +61,7 @@ namespace pcl
6361

6462
private:
6563
/** \brief Constructor for base SAC. */
66-
SampleConsensus () {};
64+
SampleConsensus () {}
6765

6866
public:
6967
using Ptr = shared_ptr<SampleConsensus<T> >;
@@ -81,14 +79,15 @@ namespace pcl
8179
, threshold_ (std::numeric_limits<double>::max ())
8280
, max_iterations_ (1000)
8381
, threads_ (-1)
84-
, rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
82+
, rng_alg_ (rnd_dev_ ())
83+
, dist_ (new std::uniform_real_distribution<>(0.0, 1.0))
8584
{
8685
// Create a random number generator object
8786
if (random)
88-
rng_->base ().seed (static_cast<unsigned> (std::time (nullptr)));
87+
rng_alg_.seed (static_cast<unsigned> (std::time (nullptr)));
8988
else
90-
rng_->base ().seed (12345u);
91-
};
89+
rng_alg_.seed (12345u);
90+
}
9291

9392
/** \brief Constructor for base SAC.
9493
* \param[in] model a Sample Consensus model
@@ -104,14 +103,15 @@ namespace pcl
104103
, threshold_ (threshold)
105104
, max_iterations_ (1000)
106105
, threads_ (-1)
107-
, rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
106+
, rng_alg_ (rnd_dev_ ())
107+
, dist_ (new std::uniform_real_distribution<>(0.0, 1.0))
108108
{
109109
// Create a random number generator object
110110
if (random)
111-
rng_->base ().seed (static_cast<unsigned> (std::time (nullptr)));
111+
rng_alg_.seed (static_cast<unsigned> (std::time (nullptr)));
112112
else
113-
rng_->base ().seed (12345u);
114-
};
113+
rng_alg_.seed (12345u);
114+
}
115115

116116
/** \brief Set the Sample Consensus model to use.
117117
* \param[in] model a Sample Consensus model
@@ -130,7 +130,7 @@ namespace pcl
130130
}
131131

132132
/** \brief Destructor for base SAC. */
133-
virtual ~SampleConsensus () {};
133+
virtual ~SampleConsensus () {}
134134

135135
/** \brief Set the distance to model threshold.
136136
* \param[in] threshold distance to model threshold
@@ -343,17 +343,20 @@ namespace pcl
343343
/** \brief The number of threads the scheduler should use, or a negative number if no parallelization is wanted. */
344344
int threads_;
345345

346-
/** \brief Boost-based random number generator algorithm. */
347-
boost::mt19937 rng_alg_;
346+
/** \brief Random number generator that produces non-deterministic random numbers. */
347+
std::random_device rnd_dev_;
348+
349+
/** \brief Mersenne Twister random number generator algorithm. */
350+
std::mt19937 rng_alg_;
348351

349-
/** \brief Boost-based random number generator distribution. */
350-
std::shared_ptr<boost::uniform_01<boost::mt19937> > rng_;
352+
/** \brief Uniform real random number distribution. */
353+
std::shared_ptr<std::uniform_real_distribution<>> dist_;
351354

352355
/** \brief Boost-based random number generator. */
353356
inline double
354357
rnd ()
355358
{
356-
return ((*rng_) ());
359+
return (*dist_)(rng_alg_);
357360
}
358361
};
359362
}

sample_consensus/include/pcl/sample_consensus/sac_model.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
#include <ctime>
4444
#include <limits>
4545
#include <memory>
46+
#include <random>
4647
#include <set>
47-
#include <boost/random/mersenne_twister.hpp> // for mt19937
48-
#include <boost/random/uniform_int.hpp> // for uniform_int
49-
#include <boost/random/variate_generator.hpp> // for variate_generator
5048

5149
#include <pcl/memory.h>
5250
#include <pcl/console/print.h>
@@ -87,17 +85,17 @@ namespace pcl
8785
, radius_max_ (std::numeric_limits<double>::max ())
8886
, samples_radius_ (0.)
8987
, samples_radius_search_ ()
90-
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
88+
, rng_dev_ (new std::random_device ())
89+
, rng_alg_ ((*rng_dev_) ())
90+
, rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits<int>::max ()))
9191
, custom_model_constraints_ ([](auto){return true;})
9292
{
9393
// Create a random number generator object
9494
if (random)
9595
rng_alg_.seed (static_cast<unsigned> (std::time(nullptr)));
9696
else
9797
rng_alg_.seed (12345u);
98-
99-
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
100-
}
98+
}
10199

102100
public:
103101
/** \brief Constructor for base SampleConsensusModel.
@@ -110,7 +108,9 @@ namespace pcl
110108
, radius_max_ (std::numeric_limits<double>::max ())
111109
, samples_radius_ (0.)
112110
, samples_radius_search_ ()
113-
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
111+
, rng_dev_ (new std::random_device ())
112+
, rng_alg_ ((*rng_dev_) ())
113+
, rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits<int>::max ()))
114114
, custom_model_constraints_ ([](auto){return true;})
115115
{
116116
if (random)
@@ -120,9 +120,6 @@ namespace pcl
120120

121121
// Sets the input cloud and creates a vector of "fake" indices
122122
setInputCloud (cloud);
123-
124-
// Create a random number generator object
125-
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
126123
}
127124

128125
/** \brief Constructor for base SampleConsensusModel.
@@ -139,7 +136,9 @@ namespace pcl
139136
, radius_max_ (std::numeric_limits<double>::max ())
140137
, samples_radius_ (0.)
141138
, samples_radius_search_ ()
142-
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
139+
, rng_dev_ (new std::random_device ())
140+
, rng_alg_ ((*rng_dev_) ())
141+
, rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits<int>::max ()))
143142
, custom_model_constraints_ ([](auto){return true;})
144143
{
145144
if (random)
@@ -156,10 +155,7 @@ namespace pcl
156155
indices_->clear ();
157156
}
158157
shuffled_indices_ = *indices_;
159-
160-
// Create a random number generator object
161-
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
162-
};
158+
}
163159

164160
/** \brief Destructor for base SampleConsensusModel. */
165161
virtual ~SampleConsensusModel () {};
@@ -572,14 +568,14 @@ namespace pcl
572568
/** Data containing a shuffled version of the indices. This is used and modified when drawing samples. */
573569
Indices shuffled_indices_;
574570

571+
/** \brief Random number generator that produces non-deterministic random numbers. */
572+
std::shared_ptr<std::random_device> rng_dev_;
573+
575574
/** \brief Boost-based random number generator algorithm. */
576-
boost::mt19937 rng_alg_;
575+
std::mt19937 rng_alg_;
577576

578577
/** \brief Boost-based random number generator distribution. */
579-
std::shared_ptr<boost::uniform_int<> > rng_dist_;
580-
581-
/** \brief Boost-based random number generator. */
582-
std::shared_ptr<boost::variate_generator< boost::mt19937&, boost::uniform_int<> > > rng_gen_;
578+
std::shared_ptr<std::uniform_int_distribution<> > rng_dist_;
583579

584580
/** \brief A vector holding the distances to the computed model. Used internally. */
585581
std::vector<double> error_sqr_dists_;
@@ -594,7 +590,7 @@ namespace pcl
594590
inline int
595591
rnd ()
596592
{
597-
return ((*rng_gen_) ());
593+
return ((*rng_dist_) (rng_alg_));
598594
}
599595

600596
/** \brief A user defined function that takes model coefficients and returns whether the model is acceptable or not. */

0 commit comments

Comments
 (0)