41 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_H_ 42 #define PCL_SAMPLE_CONSENSUS_MODEL_H_ 49 #include <pcl/console/print.h> 50 #include <pcl/point_cloud.h> 51 #include <pcl/sample_consensus/boost.h> 52 #include <pcl/sample_consensus/model_types.h> 54 #include <pcl/search/search.h> 58 template<
class T>
class ProgressiveSampleConsensus;
65 template <
typename Po
intT>
74 typedef boost::shared_ptr<SampleConsensusModel>
Ptr;
75 typedef boost::shared_ptr<const SampleConsensusModel>
ConstPtr;
96 rng_alg_.seed (static_cast<unsigned> (std::time(0)));
122 rng_alg_.seed (static_cast<unsigned> (std::time (0)));
139 const std::vector<int> &indices,
154 rng_alg_.seed (static_cast<unsigned> (std::time(0)));
160 PCL_ERROR (
"[pcl::SampleConsensusModel] Invalid index vector given with size %lu while the input PointCloud has size %lu!\n",
indices_->size (),
input_->points.size ());
183 PCL_ERROR (
"[pcl::SampleConsensusModel::getSamples] Can not select %lu unique points out of %lu!\n",
184 samples.size (),
indices_->size ());
187 iterations = INT_MAX - 1;
204 PCL_DEBUG (
"[pcl::SampleConsensusModel::getSamples] Selected %lu samples.\n", samples.size ());
208 PCL_DEBUG (
"[pcl::SampleConsensusModel::getSamples] WARNING: Could not select %d sample points in %d iterations!\n",
getSampleSize (),
max_sample_checks_);
221 Eigen::VectorXf &model_coefficients) = 0;
235 const Eigen::VectorXf &model_coefficients,
236 Eigen::VectorXf &optimized_coefficients) = 0;
245 std::vector<double> &distances) = 0;
257 const double threshold,
258 std::vector<int> &inliers) = 0;
271 const double threshold) = 0;
283 const Eigen::VectorXf &model_coefficients,
285 bool copy_data_fields =
true) = 0;
297 const Eigen::VectorXf &model_coefficients,
298 const double threshold) = 0;
308 indices_.reset (
new std::vector<int> ());
312 indices_->resize (cloud->points.size ());
313 for (
size_t i = 0; i < cloud->points.size (); ++i)
314 (*
indices_)[i] = static_cast<int> (i);
320 inline PointCloudConstPtr
327 setIndices (
const boost::shared_ptr <std::vector<int> > &indices)
339 indices_.reset (
new std::vector<int> (indices));
344 inline boost::shared_ptr <std::vector<int> >
352 inline const std::string&
427 std::vector<double> dists (error_sqr_dists);
428 const 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 size_t sample_size = sample.size ();
459 for (
unsigned int i = 0; i < sample_size; ++i)
474 size_t sample_size = sample.size ();
480 std::vector<int> indices;
481 std::vector<float> sqr_dists;
491 if (indices.size () < sample_size - 1)
494 for(
unsigned int i = 1; i < sample_size; ++i)
499 for (
unsigned int i = 0; i < sample_size-1; ++i)
500 std::swap (indices[i], indices[i + (
rnd () % (indices.size () - i))]);
501 for (
unsigned int i = 1; i < sample_size; ++i)
520 PCL_ERROR (
"[pcl::%s::isModelValid] Invalid number of model coefficients given (%lu)!\n",
getClassName ().c_str (), model_coefficients.size ());
531 isSampleGood (
const std::vector<int> &samples)
const = 0;
566 boost::shared_ptr<boost::variate_generator< boost::mt19937&, boost::uniform_int<> > >
rng_gen_;
584 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
590 template <
typename Po
intT,
typename Po
intNT>
597 typedef boost::shared_ptr<SampleConsensusModelFromNormals>
Ptr;
598 typedef boost::shared_ptr<const SampleConsensusModelFromNormals>
ConstPtr;
652 template<
typename _Scalar,
int NX=Eigen::Dynamic,
int NY=Eigen::Dynamic>
662 typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,1>
ValueType;
663 typedef Eigen::Matrix<Scalar,InputsAtCompileTime,1>
InputType;
664 typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime>
JacobianType;
672 Functor (
int m_data_points) : m_data_points_ (m_data_points) {}
678 values ()
const {
return (m_data_points_); }
681 const int m_data_points_;
685 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_H_ double normal_distance_weight_
The relative weight (between 0 and 1) to give to the angular distance (0 to pi/2) between point norma...
double radius_min_
The minimum and maximum radius limits for the model.
double computeVariance()
Compute the variance of the errors to the model from the internally estimated vector of distances.
boost::shared_ptr< const SampleConsensusModelFromNormals > ConstPtr
pcl::PointCloud< PointNT >::ConstPtr PointCloudNConstPtr
SampleConsensusModelFromNormals()
Empty constructor for base SampleConsensusModelFromNormals.
void setIndices(const std::vector< int > &indices)
Provide the vector of indices that represents the input data.
boost::shared_ptr< boost::uniform_int<> > rng_dist_
Boost-based random number generator distribution.
virtual SacModel getModelType() const =0
Return an unique id for each type of model employed.
unsigned int getSampleSize() const
Return the size of a sample from which the model is computed.
virtual void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)=0
Recompute the model coefficients using the given inlier set and return them to the user.
virtual void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)=0
Select all the points which respect the given model coefficients as inliers.
SampleConsensusModel(bool random=false)
Empty constructor for base SampleConsensusModel.
RandomSampleConsensus represents an implementation of the RANSAC (RAndom SAmple Consensus) algorithm,...
unsigned int model_size_
The number of coefficients in the model.
void setIndices(const boost::shared_ptr< std::vector< int > > &indices)
Provide a pointer to the vector of indices that represents the input data.
SampleConsensusModel(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModel.
pcl::PointCloud< PointNT >::Ptr PointCloudNPtr
Base functor all the models that need non linear optimization must define their own one and implement...
Eigen::Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
boost::shared_ptr< std::vector< int > > indices_
A pointer to the vector of point indices to use.
double samples_radius_
The maximum distance of subsequent samples from the first (radius search)
boost::shared_ptr< std::vector< int > > getIndices() const
Get a pointer to the vector of indices used.
int values() const
Get the number of values.
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients)
Check whether a model is valid given the user constraints.
virtual bool isSampleGood(const std::vector< int > &samples) const =0
Check if a sample of indices results in a good sample of points indices.
Eigen::Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
void setSamplesMaxDist(const double &radius, SearchPtr search)
Set the maximum distance allowed when drawing random samples.
pcl::search::Search< PointT >::Ptr SearchPtr
static const unsigned int max_sample_checks_
The maximum number of samples to try until we get a good one.
boost::shared_ptr< PointCloud< PointT > > Ptr
PointCloudConstPtr input_
A boost shared pointer to the point cloud data array.
virtual bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)=0
Verify whether a subset of indices verifies a given set of model coefficients.
pcl::PointCloud< PointT > PointCloud
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)=0
Count all the points which respect the given model coefficients as inliers.
SampleConsensusModel represents the base model class.
int rnd()
Boost-based random number generator.
const std::string & getClassName() const
Get a string representation of the name of this class.
Functor(int m_data_points)
Constructor.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
std::string model_name_
The model name.
pcl::PointCloud< PointT >::Ptr PointCloudPtr
boost::shared_ptr< pcl::search::Search< PointT > > Ptr
double computeVariance(const std::vector< double > &error_sqr_dists)
Compute the variance of the errors to the model.
void setNormalDistanceWeight(const double w)
Set the normal angular distance weight.
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input dataset that contains the point normals of the XYZ dataset.
std::vector< int > shuffled_indices_
Data containing a shuffled version of the indices.
void getRadiusLimits(double &min_radius, double &max_radius)
Get the minimum and maximum allowable radius limits for the model as set by the user.
Functor()
Empty Constructor.
void drawIndexSampleRadius(std::vector< int > &sample)
Fills a sample array with one random sample from the indices_ vector and other random samples that ar...
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
PointCloudNConstPtr normals_
A pointer to the input dataset that contains the point normals of the XYZ dataset.
void drawIndexSample(std::vector< int > &sample)
Fills a sample array with random samples from the indices_ vector.
virtual bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)=0
Check whether the given index samples can form a valid model, compute the model coefficients from the...
SampleConsensusModelFromNormals represents the base model class for models that require the use of su...
virtual void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true)=0
Create a new point cloud with inliers projected onto the model.
PointCloud represents the base class in PCL for storing collections of 3D points.
boost::shared_ptr< const SampleConsensusModel > ConstPtr
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
boost::shared_ptr< boost::variate_generator< boost::mt19937 &, boost::uniform_int<> > > rng_gen_
Boost-based random number generator.
void getSamplesMaxDist(double &radius)
Get maximum distance allowed when drawing random samples.
SampleConsensusModel(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModel.
virtual void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)=0
Compute all distances from the cloud data to a given model.
PointCloudNConstPtr getInputNormals()
Get a pointer to the normals of the input XYZ point cloud dataset.
boost::shared_ptr< SampleConsensusModel > Ptr
boost::mt19937 rng_alg_
Boost-based random number generator algorithm.
Eigen::Matrix< Scalar, InputsAtCompileTime, 1 > InputType
boost::shared_ptr< SampleConsensusModelFromNormals > Ptr
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...
std::vector< double > error_sqr_dists_
A vector holding the distances to the computed model.
virtual ~SampleConsensusModelFromNormals()
Destructor.
double getNormalDistanceWeight()
Get the normal angular distance weight.
A point structure representing Euclidean xyz coordinates, and the RGB color.
PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
SearchPtr samples_radius_search_
The search object for picking subsequent samples using radius search.
virtual void getSamples(int &iterations, std::vector< int > &samples)
Get a set of random data samples and return them as point indices.
unsigned int sample_size_
The size of a sample from which the model is computed.
unsigned int getModelSize() const
Return the number of coefficients in the model.
virtual ~SampleConsensusModel()
Destructor for base SampleConsensusModel.