Skip to content

Commit

Permalink
replace boost random with std equivalent
Browse files Browse the repository at this point in the history
  • Loading branch information
al-tu committed Mar 13, 2022
1 parent a26f6d4 commit 2ceb9c4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 85 deletions.
2 changes: 0 additions & 2 deletions filters/include/pcl/filters/boost.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/dynamic_bitset.hpp>
#include <boost/mpl/size.hpp>
#include <boost/fusion/sequence/intrinsic/at_key.hpp>
Expand Down
13 changes: 6 additions & 7 deletions filters/include/pcl/filters/impl/voxel_grid_covariance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@
#include <Eigen/Cholesky>
#include <Eigen/Eigenvalues> // for SelfAdjointEigenSolver
#include <boost/mpl/size.hpp> // for size
#include <boost/random/mersenne_twister.hpp> // for mt19937
#include <boost/random/normal_distribution.hpp> // for normal_distribution
#include <boost/random/variate_generator.hpp> // for variate_generator

#include <random>

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

int pnt_per_cell = 1000;
boost::mt19937 rng;
boost::normal_distribution<> nd (0.0, 1.0);
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
std::random_device rd {};
std::mt19937 rng {rd ()};
std::normal_distribution<> nd (0.0, 1.0);

Eigen::LLT<Eigen::Matrix3d> llt_of_cov;
Eigen::Matrix3d cholesky_decomp;
Expand All @@ -474,7 +473,7 @@ pcl::VoxelGridCovariance<PointT>::getDisplayCloud (pcl::PointCloud<PointXYZ>& 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<float> (dist_point (0)), static_cast<float> (dist_point (1)), static_cast<float> (dist_point (2))));
}
Expand Down
47 changes: 12 additions & 35 deletions outofcore/include/pcl/outofcore/impl/octree_disk_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@

// Boost
#include <pcl/outofcore/boost.h>
#include <boost/random/bernoulli_distribution.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/uuid/uuid_io.hpp>

// PCL
#include <pcl/common/utils.h> // pcl::utils::ignore
Expand All @@ -71,34 +68,12 @@ namespace pcl
{
namespace outofcore
{
template<typename PointT>
std::mutex OutofcoreOctreeDiskContainer<PointT>::rng_mutex_;

template<typename PointT> boost::mt19937
OutofcoreOctreeDiskContainer<PointT>::rand_gen_ (static_cast<unsigned int> (std::time(nullptr)));

template<typename PointT>
boost::uuids::basic_random_generator<boost::mt19937> OutofcoreOctreeDiskContainer<PointT>::uuid_gen_ (&rand_gen_);

template<typename PointT>
const std::uint64_t OutofcoreOctreeDiskContainer<PointT>::READ_BLOCK_SIZE_ = static_cast<std::uint64_t> (2e12);
template<typename PointT>
const std::uint64_t OutofcoreOctreeDiskContainer<PointT>::WRITE_BUFF_MAX_ = static_cast<std::uint64_t> (2e12);

template<typename PointT> void
OutofcoreOctreeDiskContainer<PointT>::getRandomUUIDString (std::string& s)
{
boost::uuids::uuid u;
{
std::lock_guard<std::mutex> lock (rng_mutex_);
u = uuid_gen_ ();
}

std::stringstream ss;
ss << u;
s = ss.str ();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

template<typename PointT>
OutofcoreOctreeDiskContainer<PointT>::OutofcoreOctreeDiskContainer ()
Expand All @@ -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 ();
Expand Down Expand Up @@ -298,12 +271,14 @@ namespace pcl
{
{
std::lock_guard<std::mutex> lock (rng_mutex_);
boost::bernoulli_distribution<double> buffdist (percent);
boost::variate_generator<boost::mt19937&, boost::bernoulli_distribution<double> > 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<std::uint64_t> (buffcount); i++)
{
if (buffcoin ())
if (buffdist (buff_gen))
{
dst.push_back (writebuff_[i]);
}
Expand All @@ -318,11 +293,13 @@ namespace pcl
{
std::lock_guard<std::mutex> lock (rng_mutex_);

boost::bernoulli_distribution<double> filedist (percent);
boost::variate_generator<boost::mt19937&, boost::bernoulli_distribution<double> > 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);
}
Expand Down
2 changes: 1 addition & 1 deletion sample_consensus/include/pcl/sample_consensus/boost.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
#endif
PCL_DEPRECATED_HEADER(1, 15, "Please include the needed boost headers directly.")

#include <boost/random.hpp>
#include <random>
39 changes: 21 additions & 18 deletions sample_consensus/include/pcl/sample_consensus/sac.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@
#include <pcl/sample_consensus/sac_model.h>
#include <pcl/pcl_base.h>

#include <boost/random/mersenne_twister.hpp> // for mt19937
#include <boost/random/uniform_01.hpp> // for uniform_01

#include <ctime>
#include <memory>
#include <random>
#include <set>

namespace pcl
Expand All @@ -63,7 +61,7 @@ namespace pcl

private:
/** \brief Constructor for base SAC. */
SampleConsensus () {};
SampleConsensus () {}

public:
using Ptr = shared_ptr<SampleConsensus<T> >;
Expand All @@ -81,14 +79,15 @@ namespace pcl
, threshold_ (std::numeric_limits<double>::max ())
, max_iterations_ (1000)
, threads_ (-1)
, rng_ (new boost::uniform_01<boost::mt19937> (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<unsigned> (std::time (nullptr)));
rng_alg_.seed (static_cast<unsigned> (std::time (nullptr)));
else
rng_->base ().seed (12345u);
};
rng_alg_.seed (12345u);
}

/** \brief Constructor for base SAC.
* \param[in] model a Sample Consensus model
Expand All @@ -104,14 +103,15 @@ namespace pcl
, threshold_ (threshold)
, max_iterations_ (1000)
, threads_ (-1)
, rng_ (new boost::uniform_01<boost::mt19937> (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<unsigned> (std::time (nullptr)));
rng_alg_.seed (static_cast<unsigned> (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
Expand All @@ -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
Expand Down Expand Up @@ -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<boost::uniform_01<boost::mt19937> > rng_;
/** \brief Uniform real random number distribution. */
std::shared_ptr<std::uniform_real_distribution<>> dist_;

/** \brief Boost-based random number generator. */
inline double
rnd ()
{
return ((*rng_) ());
return (*dist_)(rng_alg_);
}
};
}
40 changes: 18 additions & 22 deletions sample_consensus/include/pcl/sample_consensus/sac_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@
#include <ctime>
#include <limits>
#include <memory>
#include <random>
#include <set>
#include <boost/random/mersenne_twister.hpp> // for mt19937
#include <boost/random/uniform_int.hpp> // for uniform_int
#include <boost/random/variate_generator.hpp> // for variate_generator

#include <pcl/memory.h>
#include <pcl/console/print.h>
Expand Down Expand Up @@ -87,17 +85,17 @@ namespace pcl
, radius_max_ (std::numeric_limits<double>::max ())
, samples_radius_ (0.)
, samples_radius_search_ ()
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
, rng_dev_ (new std::random_device ())
, rng_alg_ ((*rng_dev_) ())
, rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits<int>::max ()))
, custom_model_constraints_ ([](auto){return true;})
{
// Create a random number generator object
if (random)
rng_alg_.seed (static_cast<unsigned> (std::time(nullptr)));
else
rng_alg_.seed (12345u);

rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
}
}

public:
/** \brief Constructor for base SampleConsensusModel.
Expand All @@ -110,7 +108,9 @@ namespace pcl
, radius_max_ (std::numeric_limits<double>::max ())
, samples_radius_ (0.)
, samples_radius_search_ ()
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
, rng_dev_ (new std::random_device ())
, rng_alg_ ((*rng_dev_) ())
, rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits<int>::max ()))
, custom_model_constraints_ ([](auto){return true;})
{
if (random)
Expand All @@ -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<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
}

/** \brief Constructor for base SampleConsensusModel.
Expand All @@ -139,7 +136,9 @@ namespace pcl
, radius_max_ (std::numeric_limits<double>::max ())
, samples_radius_ (0.)
, samples_radius_search_ ()
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
, rng_dev_ (new std::random_device ())
, rng_alg_ ((*rng_dev_) ())
, rng_dist_ (new std::uniform_int_distribution<> (0, std::numeric_limits<int>::max ()))
, custom_model_constraints_ ([](auto){return true;})
{
if (random)
Expand All @@ -156,10 +155,7 @@ namespace pcl
indices_->clear ();
}
shuffled_indices_ = *indices_;

// Create a random number generator object
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
};
}

/** \brief Destructor for base SampleConsensusModel. */
virtual ~SampleConsensusModel () {};
Expand Down Expand Up @@ -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<std::random_device> 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<boost::uniform_int<> > rng_dist_;

/** \brief Boost-based random number generator. */
std::shared_ptr<boost::variate_generator< boost::mt19937&, boost::uniform_int<> > > rng_gen_;
std::shared_ptr<std::uniform_int_distribution<> > rng_dist_;

/** \brief A vector holding the distances to the computed model. Used internally. */
std::vector<double> error_sqr_dists_;
Expand All @@ -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. */
Expand Down

0 comments on commit 2ceb9c4

Please sign in to comment.