From 2ceb9c4abe04915c5535297770a1f7da204c3013 Mon Sep 17 00:00:00 2001 From: Alexander Turkin Date: Tue, 8 Mar 2022 18:49:37 +0300 Subject: [PATCH] replace boost random with std equivalent --- filters/include/pcl/filters/boost.h | 2 - .../filters/impl/voxel_grid_covariance.hpp | 13 +++-- .../outofcore/impl/octree_disk_container.hpp | 47 +++++-------------- .../include/pcl/sample_consensus/boost.h | 2 +- .../include/pcl/sample_consensus/sac.h | 39 ++++++++------- .../include/pcl/sample_consensus/sac_model.h | 40 +++++++--------- 6 files changed, 58 insertions(+), 85 deletions(-) diff --git a/filters/include/pcl/filters/boost.h b/filters/include/pcl/filters/boost.h index 273c1e67729..46985d8b77e 100644 --- a/filters/include/pcl/filters/boost.h +++ b/filters/include/pcl/filters/boost.h @@ -46,8 +46,6 @@ PCL_DEPRECATED_HEADER(1, 15, "Please include the needed boost headers directly.") // Marking all Boost headers as system headers to remove warnings -#include -#include #include #include #include diff --git a/filters/include/pcl/filters/impl/voxel_grid_covariance.hpp b/filters/include/pcl/filters/impl/voxel_grid_covariance.hpp index 55821a64fd6..3845e251101 100644 --- a/filters/include/pcl/filters/impl/voxel_grid_covariance.hpp +++ b/filters/include/pcl/filters/impl/voxel_grid_covariance.hpp @@ -44,9 +44,8 @@ #include #include // for SelfAdjointEigenSolver #include // for size -#include // for mt19937 -#include // for normal_distribution -#include // for variate_generator + +#include ////////////////////////////////////////////////////////////////////////////////////////// template void @@ -447,9 +446,9 @@ pcl::VoxelGridCovariance::getDisplayCloud (pcl::PointCloud& ce cell_cloud.clear (); int pnt_per_cell = 1000; - boost::mt19937 rng; - boost::normal_distribution<> nd (0.0, 1.0); - boost::variate_generator > var_nor (rng, nd); + std::random_device rd {}; + std::mt19937 rng {rd ()}; + std::normal_distribution<> nd (0.0, 1.0); Eigen::LLT llt_of_cov; Eigen::Matrix3d cholesky_decomp; @@ -474,7 +473,7 @@ pcl::VoxelGridCovariance::getDisplayCloud (pcl::PointCloud& ce // Random points generated by sampling the normal distribution given by voxel mean and covariance matrix for (int i = 0; i < pnt_per_cell; i++) { - rand_point = Eigen::Vector3d (var_nor (), var_nor (), var_nor ()); + rand_point = Eigen::Vector3d (nd (rng), nd (rng), nd (rng)); dist_point = cell_mean + cholesky_decomp * rand_point; cell_cloud.push_back (PointXYZ (static_cast (dist_point (0)), static_cast (dist_point (1)), static_cast (dist_point (2)))); } diff --git a/outofcore/include/pcl/outofcore/impl/octree_disk_container.hpp b/outofcore/include/pcl/outofcore/impl/octree_disk_container.hpp index 5dffdb8c8ba..320d0d64b96 100644 --- a/outofcore/include/pcl/outofcore/impl/octree_disk_container.hpp +++ b/outofcore/include/pcl/outofcore/impl/octree_disk_container.hpp @@ -47,9 +47,6 @@ // Boost #include -#include -#include -#include // PCL #include // pcl::utils::ignore @@ -71,34 +68,12 @@ namespace pcl { namespace outofcore { - template - std::mutex OutofcoreOctreeDiskContainer::rng_mutex_; - - template boost::mt19937 - OutofcoreOctreeDiskContainer::rand_gen_ (static_cast (std::time(nullptr))); - - template - boost::uuids::basic_random_generator OutofcoreOctreeDiskContainer::uuid_gen_ (&rand_gen_); - template const std::uint64_t OutofcoreOctreeDiskContainer::READ_BLOCK_SIZE_ = static_cast (2e12); template const std::uint64_t OutofcoreOctreeDiskContainer::WRITE_BUFF_MAX_ = static_cast (2e12); - template void - OutofcoreOctreeDiskContainer::getRandomUUIDString (std::string& s) - { - boost::uuids::uuid u; - { - std::lock_guard lock (rng_mutex_); - u = uuid_gen_ (); - } - - std::stringstream ss; - ss << u; - s = ss.str (); - } - //////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////// template OutofcoreOctreeDiskContainer::OutofcoreOctreeDiskContainer () @@ -119,9 +94,7 @@ namespace pcl { if (boost::filesystem::is_directory (path)) { - std::string uuid; - getRandomUUIDString (uuid); - boost::filesystem::path filename (uuid); + boost::filesystem::path filename (boost::filesystem::unique_path()); boost::filesystem::path file = path / filename; disk_storage_filename_ = file.string (); @@ -298,12 +271,14 @@ namespace pcl { { std::lock_guard lock (rng_mutex_); - boost::bernoulli_distribution buffdist (percent); - boost::variate_generator > buffcoin (rand_gen_, buffdist); + + std::random_device buff_dev; + std::mt19937 buff_gen (buff_dev ()); + std::bernoulli_distribution buffdist (percent); for (std::size_t i = buffstart; i < static_cast (buffcount); i++) { - if (buffcoin ()) + if (buffdist (buff_gen)) { dst.push_back (writebuff_[i]); } @@ -318,11 +293,13 @@ namespace pcl { std::lock_guard lock (rng_mutex_); - boost::bernoulli_distribution filedist (percent); - boost::variate_generator > filecoin (rand_gen_, filedist); + std::random_device buff_dev; + std::mt19937 buff_gen (buff_dev ()); + std::bernoulli_distribution buffdist (percent); + for (std::uint64_t i = filestart; i < (filestart + filecount); i++) { - if (filecoin ()) + if (buffdist (buff_gen)) { offsets.push_back (i); } diff --git a/sample_consensus/include/pcl/sample_consensus/boost.h b/sample_consensus/include/pcl/sample_consensus/boost.h index ea42ca4c2bb..c9df7556121 100644 --- a/sample_consensus/include/pcl/sample_consensus/boost.h +++ b/sample_consensus/include/pcl/sample_consensus/boost.h @@ -44,4 +44,4 @@ #endif PCL_DEPRECATED_HEADER(1, 15, "Please include the needed boost headers directly.") -#include +#include diff --git a/sample_consensus/include/pcl/sample_consensus/sac.h b/sample_consensus/include/pcl/sample_consensus/sac.h index 0c28f5a17b2..b63113e4c85 100644 --- a/sample_consensus/include/pcl/sample_consensus/sac.h +++ b/sample_consensus/include/pcl/sample_consensus/sac.h @@ -43,11 +43,9 @@ #include #include -#include // for mt19937 -#include // for uniform_01 - #include #include +#include #include namespace pcl @@ -63,7 +61,7 @@ namespace pcl private: /** \brief Constructor for base SAC. */ - SampleConsensus () {}; + SampleConsensus () {} public: using Ptr = shared_ptr >; @@ -81,14 +79,15 @@ namespace pcl , threshold_ (std::numeric_limits::max ()) , max_iterations_ (1000) , threads_ (-1) - , rng_ (new boost::uniform_01 (rng_alg_)) + , rng_alg_ (rnd_dev_ ()) + , dist_ (new std::uniform_real_distribution<>(0.0, 1.0)) { // Create a random number generator object if (random) - rng_->base ().seed (static_cast (std::time (nullptr))); + rng_alg_.seed (static_cast (std::time (nullptr))); else - rng_->base ().seed (12345u); - }; + rng_alg_.seed (12345u); + } /** \brief Constructor for base SAC. * \param[in] model a Sample Consensus model @@ -104,14 +103,15 @@ namespace pcl , threshold_ (threshold) , max_iterations_ (1000) , threads_ (-1) - , rng_ (new boost::uniform_01 (rng_alg_)) + , rng_alg_ (rnd_dev_ ()) + , dist_ (new std::uniform_real_distribution<>(0.0, 1.0)) { // Create a random number generator object if (random) - rng_->base ().seed (static_cast (std::time (nullptr))); + rng_alg_.seed (static_cast (std::time (nullptr))); else - rng_->base ().seed (12345u); - }; + rng_alg_.seed (12345u); + } /** \brief Set the Sample Consensus model to use. * \param[in] model a Sample Consensus model @@ -130,7 +130,7 @@ namespace pcl } /** \brief Destructor for base SAC. */ - virtual ~SampleConsensus () {}; + virtual ~SampleConsensus () {} /** \brief Set the distance to model threshold. * \param[in] threshold distance to model threshold @@ -343,17 +343,20 @@ namespace pcl /** \brief The number of threads the scheduler should use, or a negative number if no parallelization is wanted. */ int threads_; - /** \brief Boost-based random number generator algorithm. */ - boost::mt19937 rng_alg_; + /** \brief Random number generator that produces non-deterministic random numbers. */ + std::random_device rnd_dev_; + + /** \brief Mersenne Twister random number generator algorithm. */ + std::mt19937 rng_alg_; - /** \brief Boost-based random number generator distribution. */ - std::shared_ptr > rng_; + /** \brief Uniform real random number distribution. */ + std::shared_ptr> dist_; /** \brief Boost-based random number generator. */ inline double rnd () { - return ((*rng_) ()); + return (*dist_)(rng_alg_); } }; } diff --git a/sample_consensus/include/pcl/sample_consensus/sac_model.h b/sample_consensus/include/pcl/sample_consensus/sac_model.h index 381867702f9..04bb75b63a3 100644 --- a/sample_consensus/include/pcl/sample_consensus/sac_model.h +++ b/sample_consensus/include/pcl/sample_consensus/sac_model.h @@ -43,10 +43,8 @@ #include #include #include +#include #include -#include // for mt19937 -#include // for uniform_int -#include // for variate_generator #include #include @@ -87,7 +85,9 @@ namespace pcl , radius_max_ (std::numeric_limits::max ()) , samples_radius_ (0.) , samples_radius_search_ () - , rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits::max ())) + , rng_dev_ (new std::random_device ()) + , rng_alg_ ((*rng_dev_) ()) + , rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits::max ())) , custom_model_constraints_ ([](auto){return true;}) { // Create a random number generator object @@ -95,9 +95,7 @@ namespace pcl rng_alg_.seed (static_cast (std::time(nullptr))); else rng_alg_.seed (12345u); - - rng_gen_.reset (new boost::variate_generator > (rng_alg_, *rng_dist_)); - } + } public: /** \brief Constructor for base SampleConsensusModel. @@ -110,7 +108,9 @@ namespace pcl , radius_max_ (std::numeric_limits::max ()) , samples_radius_ (0.) , samples_radius_search_ () - , rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits::max ())) + , rng_dev_ (new std::random_device ()) + , rng_alg_ ((*rng_dev_) ()) + , rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits::max ())) , custom_model_constraints_ ([](auto){return true;}) { if (random) @@ -120,9 +120,6 @@ namespace pcl // Sets the input cloud and creates a vector of "fake" indices setInputCloud (cloud); - - // Create a random number generator object - rng_gen_.reset (new boost::variate_generator > (rng_alg_, *rng_dist_)); } /** \brief Constructor for base SampleConsensusModel. @@ -139,7 +136,9 @@ namespace pcl , radius_max_ (std::numeric_limits::max ()) , samples_radius_ (0.) , samples_radius_search_ () - , rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits::max ())) + , rng_dev_ (new std::random_device ()) + , rng_alg_ ((*rng_dev_) ()) + , rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits::max ())) , custom_model_constraints_ ([](auto){return true;}) { if (random) @@ -156,10 +155,7 @@ namespace pcl indices_->clear (); } shuffled_indices_ = *indices_; - - // Create a random number generator object - rng_gen_.reset (new boost::variate_generator > (rng_alg_, *rng_dist_)); - }; + } /** \brief Destructor for base SampleConsensusModel. */ virtual ~SampleConsensusModel () {}; @@ -572,14 +568,14 @@ namespace pcl /** Data containing a shuffled version of the indices. This is used and modified when drawing samples. */ Indices shuffled_indices_; + /** \brief Random number generator that produces non-deterministic random numbers. */ + std::shared_ptr rng_dev_; + /** \brief Boost-based random number generator algorithm. */ - boost::mt19937 rng_alg_; + std::mt19937 rng_alg_; /** \brief Boost-based random number generator distribution. */ - std::shared_ptr > rng_dist_; - - /** \brief Boost-based random number generator. */ - std::shared_ptr > > rng_gen_; + std::shared_ptr > rng_dist_; /** \brief A vector holding the distances to the computed model. Used internally. */ std::vector error_sqr_dists_; @@ -594,7 +590,7 @@ namespace pcl inline int rnd () { - return ((*rng_gen_) ()); + return ((*rng_dist_) (rng_alg_)); } /** \brief A user defined function that takes model coefficients and returns whether the model is acceptable or not. */