47 #include <boost/random/mersenne_twister.hpp>
48 #include <boost/random/uniform_int.hpp>
49 #include <boost/random/variate_generator.hpp>
54 #include <pcl/console/print.h>
55 #include <pcl/point_cloud.h>
57 #include <pcl/sample_consensus/model_types.h>
59 #include <pcl/search/search.h>
63 template<
class T>
class ProgressiveSampleConsensus;
70 template <
typename Po
intT>
79 using Ptr = shared_ptr<SampleConsensusModel<PointT> >;
80 using ConstPtr = shared_ptr<const SampleConsensusModel<PointT> >;
88 ,
radius_min_ (-std::numeric_limits<double>::max ())
92 ,
rng_dist_ (new
boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
97 rng_alg_.seed (std::random_device()());
113 ,
radius_min_ (-std::numeric_limits<double>::max ())
114 ,
radius_max_ (std::numeric_limits<double>::max ())
117 ,
rng_dist_ (new
boost::uniform_int<> (0, std::numeric_limits<int>::max ()))
121 rng_alg_.seed (std::random_device()());
134 PCL_ERROR(
"[pcl::SampleConsensusModel] Invalid index vector given with size "
135 "%zu while the input PointCloud has size %zu!\n",
137 static_cast<std::size_t
>(
input_->size()));
167 PCL_ERROR (
"[pcl::SampleConsensusModel::getSamples] Can not select %lu unique points out of %lu!\n",
168 samples.size (),
indices_->size ());
171 iterations = std::numeric_limits<int>::max() - 1;
188 PCL_DEBUG (
"[pcl::SampleConsensusModel::getSamples] Selected %lu samples.\n", samples.size ());
192 PCL_DEBUG (
"[pcl::SampleConsensusModel::getSamples] WARNING: Could not select %d sample points in %d iterations!\n",
getSampleSize (),
max_sample_checks_);
206 Eigen::VectorXf &model_coefficients)
const = 0;
220 const Eigen::VectorXf &model_coefficients,
221 Eigen::VectorXf &optimized_coefficients)
const = 0;
230 std::vector<double> &distances)
const = 0;
242 const double threshold,
256 const double threshold)
const = 0;
268 const Eigen::VectorXf &model_coefficients,
270 bool copy_data_fields =
true)
const = 0;
282 const Eigen::VectorXf &model_coefficients,
283 const double threshold)
const = 0;
298 for (std::size_t i = 0; i < cloud->size (); ++i)
305 inline PointCloudConstPtr
337 inline const std::string&
392 PCL_ERROR (
"[pcl::SampleConsensusModel::setModelConstraints] The given function is empty (i.e. does not contain a callable target)!\n");
427 std::vector<double> dists (error_sqr_dists);
428 const std::size_t medIdx = dists.size () >> 1;
429 std::nth_element (dists.begin (), dists.begin () + medIdx, dists.end ());
430 double median_error_sqr = dists[medIdx];
431 return (2.1981 * median_error_sqr);
443 PCL_ERROR (
"[pcl::SampleConsensusModel::computeVariance] The variance of the Sample Consensus model distances cannot be estimated, as the model has not been computed yet. Please compute the model first or at least run selectWithinDistance before continuing. Returning NAN!\n");
444 return (std::numeric_limits<double>::quiet_NaN ());
457 std::size_t sample_size = sample.size ();
459 for (std::size_t i = 0; i < sample_size; ++i)
474 std::size_t sample_size = sample.size ();
481 std::vector<float> sqr_dists;
491 if (indices.size () < sample_size - 1)
494 for(std::size_t i = 1; i < sample_size; ++i)
499 for (std::size_t i = 0; i < sample_size-1; ++i)
500 std::swap (indices[i], indices[i + (
rnd () % (indices.size () - i))]);
501 for (std::size_t i = 1; i < sample_size; ++i)
520 PCL_ERROR (
"[pcl::%s::isModelValid] Invalid number of model coefficients given (is %lu, should be %lu)!\n",
getClassName ().c_str (), model_coefficients.size (),
model_size_);
525 PCL_DEBUG (
"[pcl::%s::isModelValid] The user defined isModelValid function returned false.\n",
getClassName ().c_str ());
571 std::shared_ptr<boost::variate_generator< boost::mt19937&, boost::uniform_int<> > >
rng_gen_;
599 template <
typename Po
intT,
typename Po
intNT>
606 using Ptr = shared_ptr<SampleConsensusModelFromNormals<PointT, PointNT> >;
607 using ConstPtr = shared_ptr<const SampleConsensusModelFromNormals<PointT, PointNT> >;
623 if (w < 0.0 || w > 1.0)
625 PCL_ERROR (
"[pcl::SampleConsensusModel::setNormalDistanceWeight] w is %g, but should be in [0; 1]. Weight will not be set.", w);
666 template<
typename _Scalar,
int NX=Eigen::Dynamic,
int NY=Eigen::Dynamic>
676 using ValueType = Eigen::Matrix<Scalar,ValuesAtCompileTime,1>;
677 using InputType = Eigen::Matrix<Scalar,InputsAtCompileTime,1>;
678 using JacobianType = Eigen::Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime>;
686 Functor (
int m_data_points) : m_data_points_ (m_data_points) {}
692 values ()
const {
return (m_data_points_); }
695 const int m_data_points_;
PointCloud represents the base class in PCL for storing collections of 3D points.
shared_ptr< PointCloud< PointT > > Ptr
shared_ptr< const PointCloud< PointT > > ConstPtr
ProgressiveSampleConsensus represents an implementation of the PROSAC (PROgressive SAmple Consensus) ...
SampleConsensusModelFromNormals represents the base model class for models that require the use of su...
void setNormalDistanceWeight(const double w)
Set the normal angular distance weight.
PointCloudNConstPtr normals_
A pointer to the input dataset that contains the point normals of the XYZ dataset.
typename pcl::PointCloud< PointNT >::ConstPtr PointCloudNConstPtr
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input dataset that contains the point normals of the XYZ dataset.
double getNormalDistanceWeight() const
Get the normal angular distance weight.
SampleConsensusModelFromNormals()
Empty constructor for base SampleConsensusModelFromNormals.
virtual ~SampleConsensusModelFromNormals()=default
Destructor.
shared_ptr< const SampleConsensusModelFromNormals< PointT, PointNT > > ConstPtr
typename pcl::PointCloud< PointNT >::Ptr PointCloudNPtr
double normal_distance_weight_
The relative weight (between 0 and 1) to give to the angular distance (0 to pi/2) between point norma...
PointCloudNConstPtr getInputNormals() const
Get a pointer to the normals of the input XYZ point cloud dataset.
shared_ptr< SampleConsensusModelFromNormals< PointT, PointNT > > Ptr
SampleConsensusModel represents the base model class.
SampleConsensusModel(const PointCloudConstPtr &cloud, const bool random=false)
Constructor for base SampleConsensusModel.
virtual void getSamples(int &iterations, Indices &samples)
Get a set of random data samples and return them as point indices.
static const unsigned int max_sample_checks_
The maximum number of samples to try until we get a good one.
virtual bool isSampleGood(const Indices &samples) const =0
Check if a sample of indices results in a good sample of points indices.
virtual bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const =0
Check whether the given index samples can form a valid model, compute the model coefficients from the...
virtual SacModel getModelType() const =0
Return a unique id for each type of model employed.
void drawIndexSampleRadius(Indices &sample)
Fills a sample array with one random sample from the indices_ vector and other random samples that ar...
unsigned int getModelSize() const
Return the number of coefficients in the model.
double radius_min_
The minimum and maximum radius limits for the model.
std::function< bool(const Eigen::VectorXf &)> custom_model_constraints_
A user defined function that takes model coefficients and returns whether the model is acceptable or ...
void setRadiusLimits(const double &min_radius, const double &max_radius)
Set the minimum and maximum allowable radius limits for the model (applicable to models that estimate...
void getSamplesMaxDist(double &radius) const
Get maximum distance allowed when drawing random samples.
PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
virtual void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const =0
Recompute the model coefficients using the given inlier set and return them to the user.
shared_ptr< SampleConsensusModel< PointT > > Ptr
const std::string & getClassName() const
Get a string representation of the name of this class.
SearchPtr samples_radius_search_
The search object for picking subsequent samples using radius search.
virtual std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const =0
Count all the points which respect the given model coefficients as inliers.
double computeVariance(const std::vector< double > &error_sqr_dists) const
Compute the variance of the errors to the model.
unsigned int sample_size_
The size of a sample from which the model is computed.
typename PointCloud::ConstPtr PointCloudConstPtr
IndicesPtr getIndices() const
Get a pointer to the vector of indices used.
std::shared_ptr< boost::variate_generator< boost::mt19937 &, boost::uniform_int<> > > rng_gen_
Boost-based random number generator.
IndicesPtr indices_
A pointer to the vector of point indices to use.
double computeVariance() const
Compute the variance of the errors to the model from the internally estimated vector of distances.
virtual void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const =0
Create a new point cloud with inliers projected onto the model.
void setModelConstraints(std::function< bool(const Eigen::VectorXf &)> function)
This can be used to impose any kind of constraint on the model, e.g.
Indices shuffled_indices_
Data containing a shuffled version of the indices.
boost::mt19937 rng_alg_
Boost-based random number generator algorithm.
PointCloudConstPtr input_
A boost shared pointer to the point cloud data array.
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
virtual bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const =0
Verify whether a subset of indices verifies a given set of model coefficients.
SampleConsensusModel(const PointCloudConstPtr &cloud, const Indices &indices, const bool random=false)
Constructor for base SampleConsensusModel.
SampleConsensusModel(bool random=false)
Empty constructor for base SampleConsensusModel.
virtual ~SampleConsensusModel()=default
Destructor for base SampleConsensusModel.
std::shared_ptr< boost::uniform_int<> > rng_dist_
Boost-based random number generator distribution.
void setIndices(const Indices &indices)
Provide the vector of indices that represents the input data.
double samples_radius_
The maximum distance of subsequent samples from the first (radius search)
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
std::string model_name_
The model name.
unsigned int model_size_
The number of coefficients in the model.
int rnd()
Boost-based random number generator.
void getRadiusLimits(double &min_radius, double &max_radius) const
Get the minimum and maximum allowable radius limits for the model as set by the user.
typename pcl::search::Search< PointT >::Ptr SearchPtr
void drawIndexSample(Indices &sample)
Fills a sample array with random samples from the indices_ vector.
typename PointCloud::Ptr PointCloudPtr
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
std::vector< double > error_sqr_dists_
A vector holding the distances to the computed model.
void setSamplesMaxDist(const double &radius, SearchPtr search)
Set the maximum distance allowed when drawing random samples.
virtual void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const =0
Compute all distances from the cloud data to a given model.
virtual void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)=0
Select all the points which respect the given model coefficients as inliers.
unsigned int getSampleSize() const
Return the size of a sample from which the model is computed.
shared_ptr< pcl::search::Search< PointT > > Ptr
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Defines functions, macros and traits for allocating and using memory.
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
IndicesAllocator<> Indices
Type used for indices in PCL.
shared_ptr< Indices > IndicesPtr
Base functor all the models that need non linear optimization must define their own one and implement...
virtual ~Functor()=default
int values() const
Get the number of values.
Functor()
Empty Constructor.
Eigen::Matrix< Scalar, InputsAtCompileTime, 1 > InputType
Eigen::Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
Eigen::Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Functor(int m_data_points)
Constructor.
A point structure representing Euclidean xyz coordinates, and the RGB color.
Defines basic non-point types used by PCL.