Skip to content

Commit 22c5c1f

Browse files
Heiko ThielSergioRAgostinho
authored andcommitted
Use random generator from C++11 instead of from Boost in module sample_consensus
1 parent 112d870 commit 22c5c1f

File tree

5 files changed

+31
-118
lines changed

5 files changed

+31
-118
lines changed

sample_consensus/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ set(srcs
3030
)
3131

3232
set(incs
33-
"include/pcl/${SUBSYS_NAME}/boost.h"
3433
"include/pcl/${SUBSYS_NAME}/eigen.h"
3534
"include/pcl/${SUBSYS_NAME}/lmeds.h"
3635
"include/pcl/${SUBSYS_NAME}/method_types.h"

sample_consensus/include/pcl/sample_consensus/boost.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

sample_consensus/include/pcl/sample_consensus/sac.h

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
#pragma once
4242

43-
#include <pcl/sample_consensus/boost.h>
4443
#include <pcl/sample_consensus/sac_model.h>
4544
#include <ctime>
4645
#include <set>
@@ -56,10 +55,6 @@ namespace pcl
5655
{
5756
typedef typename SampleConsensusModel<T>::Ptr SampleConsensusModelPtr;
5857

59-
private:
60-
/** \brief Constructor for base SAC. */
61-
SampleConsensus () {};
62-
6358
public:
6459
typedef boost::shared_ptr<SampleConsensus> Ptr;
6560
typedef boost::shared_ptr<const SampleConsensus> ConstPtr;
@@ -69,18 +64,8 @@ namespace pcl
6964
* \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
7065
*/
7166
SampleConsensus (const SampleConsensusModelPtr &model, bool random = false)
72-
: sac_model_ (model)
73-
, probability_ (0.99)
74-
, iterations_ (0)
75-
, threshold_ (std::numeric_limits<double>::max ())
76-
, max_iterations_ (1000)
77-
, rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
67+
: SampleConsensus(model, std::numeric_limits<double>::max (), random)
7868
{
79-
// Create a random number generator object
80-
if (random)
81-
rng_->base ().seed (static_cast<unsigned> (std::time (nullptr)));
82-
else
83-
rng_->base ().seed (12345u);
8469
};
8570

8671
/** \brief Constructor for base SAC.
@@ -96,13 +81,16 @@ namespace pcl
9681
, iterations_ (0)
9782
, threshold_ (threshold)
9883
, max_iterations_ (1000)
99-
, rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
84+
, rng_dist_ (0.0f, 1.0f)
10085
{
10186
// Create a random number generator object
10287
if (random)
103-
rng_->base ().seed (static_cast<unsigned> (std::time (nullptr)));
88+
{
89+
std::random_device rd;
90+
rng_.seed (rd());
91+
}
10492
else
105-
rng_->base ().seed (12345u);
93+
rng_.seed (12345u);
10694
};
10795

10896
/** \brief Set the Sample Consensus model to use.
@@ -322,16 +310,16 @@ namespace pcl
322310
int max_iterations_;
323311

324312
/** \brief Boost-based random number generator algorithm. */
325-
boost::mt19937 rng_alg_;
313+
std::mt19937 rng_;
326314

327315
/** \brief Boost-based random number generator distribution. */
328-
boost::shared_ptr<boost::uniform_01<boost::mt19937> > rng_;
316+
std::uniform_real_distribution<> rng_dist_;
329317

330318
/** \brief Boost-based random number generator. */
331319
inline double
332320
rnd ()
333321
{
334-
return ((*rng_) ());
322+
return (rng_dist_ (rng_));
335323
}
336324
};
337325
}

sample_consensus/include/pcl/sample_consensus/sac_model.h

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
#include <cfloat>
4444
#include <ctime>
4545
#include <climits>
46+
#include <random>
4647
#include <set>
4748

4849
#include <pcl/console/print.h>
4950
#include <pcl/point_cloud.h>
50-
#include <pcl/sample_consensus/boost.h>
5151
#include <pcl/sample_consensus/model_types.h>
5252

5353
#include <pcl/search/search.h>
@@ -78,20 +78,19 @@ namespace pcl
7878
* \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
7979
*/
8080
SampleConsensusModel (bool random = false)
81-
: input_ ()
82-
, radius_min_ (-std::numeric_limits<double>::max ())
81+
: radius_min_ (-std::numeric_limits<double>::max ())
8382
, radius_max_ (std::numeric_limits<double>::max ())
8483
, samples_radius_ (0.)
85-
, samples_radius_search_ ()
86-
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
84+
, rng_dist_ (new std::uniform_int_distribution<> ())
8785
{
8886
// Create a random number generator object
8987
if (random)
90-
rng_alg_.seed (static_cast<unsigned> (std::time(nullptr)));
88+
{
89+
std::random_device rd;
90+
rng_.seed (rd());
91+
}
9192
else
92-
rng_alg_.seed (12345u);
93-
94-
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
93+
rng_.seed (12345u);
9594
}
9695

9796
public:
@@ -100,23 +99,10 @@ namespace pcl
10099
* \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
101100
*/
102101
SampleConsensusModel (const PointCloudConstPtr &cloud, bool random = false)
103-
: input_ ()
104-
, radius_min_ (-std::numeric_limits<double>::max ())
105-
, radius_max_ (std::numeric_limits<double>::max ())
106-
, samples_radius_ (0.)
107-
, samples_radius_search_ ()
108-
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
102+
: SampleConsensusModel(random)
109103
{
110-
if (random)
111-
rng_alg_.seed (static_cast<unsigned> (std::time (nullptr)));
112-
else
113-
rng_alg_.seed (12345u);
114-
115104
// Sets the input cloud and creates a vector of "fake" indices
116105
setInputCloud (cloud);
117-
118-
// Create a random number generator object
119-
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
120106
}
121107

122108
/** \brief Constructor for base SampleConsensusModel.
@@ -127,28 +113,17 @@ namespace pcl
127113
SampleConsensusModel (const PointCloudConstPtr &cloud,
128114
const std::vector<int> &indices,
129115
bool random = false)
130-
: input_ (cloud)
131-
, indices_ (new std::vector<int> (indices))
132-
, radius_min_ (-std::numeric_limits<double>::max ())
133-
, radius_max_ (std::numeric_limits<double>::max ())
134-
, samples_radius_ (0.)
135-
, samples_radius_search_ ()
136-
, rng_dist_ (new boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
116+
: SampleConsensusModel(random)
137117
{
138-
if (random)
139-
rng_alg_.seed (static_cast<unsigned> (std::time(nullptr)));
140-
else
141-
rng_alg_.seed (12345u);
118+
input_ = cloud;
119+
indices_.reset(new std::vector<int> (indices));
142120

143121
if (indices_->size () > input_->points.size ())
144122
{
145123
PCL_ERROR ("[pcl::SampleConsensusModel] Invalid index vector given with size %lu while the input PointCloud has size %lu!\n", indices_->size (), input_->points.size ());
146124
indices_->clear ();
147125
}
148126
shuffled_indices_ = *indices_;
149-
150-
// Create a random number generator object
151-
rng_gen_.reset (new boost::variate_generator<boost::mt19937&, boost::uniform_int<> > (rng_alg_, *rng_dist_));
152127
};
153128

154129
/** \brief Destructor for base SampleConsensusModel. */
@@ -541,14 +516,11 @@ namespace pcl
541516
/** Data containing a shuffled version of the indices. This is used and modified when drawing samples. */
542517
std::vector<int> shuffled_indices_;
543518

544-
/** \brief Boost-based random number generator algorithm. */
545-
boost::mt19937 rng_alg_;
519+
/** \brief Random number generator algorithm. */
520+
std::mt19937 rng_;
546521

547-
/** \brief Boost-based random number generator distribution. */
548-
boost::shared_ptr<boost::uniform_int<> > rng_dist_;
549-
550-
/** \brief Boost-based random number generator. */
551-
boost::shared_ptr<boost::variate_generator< boost::mt19937&, boost::uniform_int<> > > rng_gen_;
522+
/** \brief Random number generator distribution. */
523+
std::shared_ptr<std::uniform_int_distribution<> > rng_dist_;
552524

553525
/** \brief A vector holding the distances to the computed model. Used internally. */
554526
std::vector<double> error_sqr_dists_;
@@ -563,7 +535,7 @@ namespace pcl
563535
inline int
564536
rnd ()
565537
{
566-
return ((*rng_gen_) ());
538+
return ((*rng_dist_) (rng_));
567539
}
568540
public:
569541
EIGEN_MAKE_ALIGNED_OPERATOR_NEW

test/sample_consensus/test_sample_consensus_quadric_models.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ TEST (SampleConsensusModelCone, RANSAC)
305305
EXPECT_EQ (7, coeff.size ());
306306
EXPECT_NEAR (0.000000, coeff[0], 1e-2);
307307
EXPECT_NEAR (0.100000, coeff[1], 1e-2);
308-
EXPECT_NEAR (0.349066, coeff[6], 1e-2);
308+
EXPECT_NEAR (0.510614, coeff[6], 1e-2);
309309

310310
Eigen::VectorXf coeff_refined;
311311
model->optimizeModelCoefficients (inliers, coeff, coeff_refined);
@@ -387,9 +387,9 @@ TEST (SampleConsensusModelCylinder, RANSAC)
387387
Eigen::VectorXf coeff;
388388
sac.getModelCoefficients (coeff);
389389
EXPECT_EQ (7, coeff.size ());
390-
EXPECT_NEAR (-0.5, coeff[0], 1e-3);
391-
EXPECT_NEAR ( 1.7, coeff[1], 1e-3);
392-
EXPECT_NEAR ( 0.5, coeff[6], 1e-3);
390+
EXPECT_NEAR (-0.49, coeff[0], 1e-3);
391+
EXPECT_NEAR ( 1.69, coeff[1], 1e-3);
392+
EXPECT_NEAR ( 0.49, coeff[6], 1e-3);
393393

394394
Eigen::VectorXf coeff_refined;
395395
model->optimizeModelCoefficients (inliers, coeff, coeff_refined);

0 commit comments

Comments
 (0)