Point Cloud Library (PCL)  1.8.0
sac_model_circle.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_
42 #define PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_
43 
44 #include <pcl/sample_consensus/sac_model.h>
45 #include <pcl/sample_consensus/model_types.h>
46 
47 namespace pcl
48 {
49  /** \brief SampleConsensusModelCircle2D defines a model for 2D circle segmentation on the X-Y plane.
50  *
51  * The model coefficients are defined as:
52  * - \b center.x : the X coordinate of the circle's center
53  * - \b center.y : the Y coordinate of the circle's center
54  * - \b radius : the circle's radius
55  *
56  * \author Radu B. Rusu
57  * \ingroup sample_consensus
58  */
59  template <typename PointT>
61  {
62  public:
69 
73 
74  typedef boost::shared_ptr<SampleConsensusModelCircle2D> Ptr;
75 
76  /** \brief Constructor for base SampleConsensusModelCircle2D.
77  * \param[in] cloud the input point cloud dataset
78  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
79  */
80  SampleConsensusModelCircle2D (const PointCloudConstPtr &cloud, bool random = false)
81  : SampleConsensusModel<PointT> (cloud, random), tmp_inliers_ ()
82  {
83  model_name_ = "SampleConsensusModelCircle2D";
84  sample_size_ = 3;
85  model_size_ = 3;
86  }
87 
88  /** \brief Constructor for base SampleConsensusModelCircle2D.
89  * \param[in] cloud the input point cloud dataset
90  * \param[in] indices a vector of point indices to be used from \a cloud
91  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
92  */
93  SampleConsensusModelCircle2D (const PointCloudConstPtr &cloud,
94  const std::vector<int> &indices,
95  bool random = false)
96  : SampleConsensusModel<PointT> (cloud, indices, random), tmp_inliers_ ()
97  {
98  model_name_ = "SampleConsensusModelCircle2D";
99  sample_size_ = 3;
100  model_size_ = 3;
101  }
102 
103  /** \brief Copy constructor.
104  * \param[in] source the model to copy into this
105  */
107  SampleConsensusModel<PointT> (), tmp_inliers_ ()
108  {
109  *this = source;
110  model_name_ = "SampleConsensusModelCircle2D";
111  }
112 
113  /** \brief Empty destructor */
115 
116  /** \brief Copy constructor.
117  * \param[in] source the model to copy into this
118  */
121  {
123  tmp_inliers_ = source.tmp_inliers_;
124  return (*this);
125  }
126 
127  /** \brief Check whether the given index samples can form a valid 2D circle model, compute the model coefficients
128  * from these samples and store them in model_coefficients. The circle coefficients are: x, y, R.
129  * \param[in] samples the point indices found as possible good candidates for creating a valid model
130  * \param[out] model_coefficients the resultant model coefficients
131  */
132  bool
133  computeModelCoefficients (const std::vector<int> &samples,
134  Eigen::VectorXf &model_coefficients);
135 
136  /** \brief Compute all distances from the cloud data to a given 2D circle model.
137  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
138  * \param[out] distances the resultant estimated distances
139  */
140  void
141  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
142  std::vector<double> &distances);
143 
144  /** \brief Compute all distances from the cloud data to a given 2D circle model.
145  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
146  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
147  * \param[out] inliers the resultant model inliers
148  */
149  void
150  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
151  const double threshold,
152  std::vector<int> &inliers);
153 
154  /** \brief Count all the points which respect the given model coefficients as inliers.
155  *
156  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
157  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
158  * \return the resultant number of inliers
159  */
160  virtual int
161  countWithinDistance (const Eigen::VectorXf &model_coefficients,
162  const double threshold);
163 
164  /** \brief Recompute the 2d circle coefficients using the given inlier set and return them to the user.
165  * @note: these are the coefficients of the 2d circle model after refinement (e.g. after SVD)
166  * \param[in] inliers the data inliers found as supporting the model
167  * \param[in] model_coefficients the initial guess for the optimization
168  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
169  */
170  void
171  optimizeModelCoefficients (const std::vector<int> &inliers,
172  const Eigen::VectorXf &model_coefficients,
173  Eigen::VectorXf &optimized_coefficients);
174 
175  /** \brief Create a new point cloud with inliers projected onto the 2d circle model.
176  * \param[in] inliers the data inliers that we want to project on the 2d circle model
177  * \param[in] model_coefficients the coefficients of a 2d circle model
178  * \param[out] projected_points the resultant projected points
179  * \param[in] copy_data_fields set to true if we need to copy the other data fields
180  */
181  void
182  projectPoints (const std::vector<int> &inliers,
183  const Eigen::VectorXf &model_coefficients,
184  PointCloud &projected_points,
185  bool copy_data_fields = true);
186 
187  /** \brief Verify whether a subset of indices verifies the given 2d circle model coefficients.
188  * \param[in] indices the data indices that need to be tested against the 2d circle model
189  * \param[in] model_coefficients the 2d circle model coefficients
190  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
191  */
192  bool
193  doSamplesVerifyModel (const std::set<int> &indices,
194  const Eigen::VectorXf &model_coefficients,
195  const double threshold);
196 
197  /** \brief Return an unique id for this model (SACMODEL_CIRCLE2D). */
198  inline pcl::SacModel
199  getModelType () const { return (SACMODEL_CIRCLE2D); }
200 
201  protected:
204 
205  /** \brief Check whether a model is valid given the user constraints.
206  * \param[in] model_coefficients the set of model coefficients
207  */
208  virtual bool
209  isModelValid (const Eigen::VectorXf &model_coefficients);
210 
211  /** \brief Check if a sample of indices results in a good sample of points indices.
212  * \param[in] samples the resultant index samples
213  */
214  bool
215  isSampleGood(const std::vector<int> &samples) const;
216 
217  private:
218  /** \brief Temporary pointer to a list of given indices for optimizeModelCoefficients () */
219  const std::vector<int> *tmp_inliers_;
220 
221 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
222 #pragma GCC diagnostic ignored "-Weffc++"
223 #endif
224  /** \brief Functor for the optimization function */
225  struct OptimizationFunctor : pcl::Functor<float>
226  {
227  /** \brief Functor constructor
228  * \param[in] m_data_points the number of data points to evaluate
229  * \param[in] estimator pointer to the estimator object
230  * \param[in] distance distance computation function pointer
231  */
232  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCircle2D<PointT> *model) :
233  pcl::Functor<float>(m_data_points), model_ (model) {}
234 
235  /** Cost function to be minimized
236  * \param[in] x the variables array
237  * \param[out] fvec the resultant functions evaluations
238  * \return 0
239  */
240  int
241  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
242  {
243  for (int i = 0; i < values (); ++i)
244  {
245  // Compute the difference between the center of the circle and the datapoint X_i
246  float xt = model_->input_->points[(*model_->tmp_inliers_)[i]].x - x[0];
247  float yt = model_->input_->points[(*model_->tmp_inliers_)[i]].y - x[1];
248 
249  // g = sqrt ((x-a)^2 + (y-b)^2) - R
250  fvec[i] = sqrtf (xt * xt + yt * yt) - x[2];
251  }
252  return (0);
253  }
254 
256  };
257 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
258 #pragma GCC diagnostic warning "-Weffc++"
259 #endif
260  };
261 }
262 
263 #ifdef PCL_NO_PRECOMPILE
264 #include <pcl/sample_consensus/impl/sac_model_circle.hpp>
265 #endif
266 
267 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_
boost::shared_ptr< SampleConsensusModelCircle2D > Ptr
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
Compute all distances from the cloud data to a given 2D circle model.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Compute all distances from the cloud data to a given 2D circle model.
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:575
SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
SampleConsensusModelCircle2D(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelCircle2D.
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:653
SampleConsensusModelCircle2D defines a model for 2D circle segmentation on the X-Y plane...
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true)
Create a new point cloud with inliers projected onto the 2d circle model.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
std::string model_name_
The model name.
Definition: sac_model.h:534
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
virtual ~SampleConsensusModelCircle2D()
Empty destructor.
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
Verify whether a subset of indices verifies the given 2d circle model coefficients.
SampleConsensusModelCircle2D(const SampleConsensusModelCircle2D &source)
Copy constructor.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
SacModel
Definition: model_types.h:48
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
SampleConsensusModelCircle2D(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCircle2D.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
SampleConsensusModel< PointT >::PointCloud PointCloud
SampleConsensusModelCircle2D & operator=(const SampleConsensusModelCircle2D &source)
Copy constructor.
A point structure representing Euclidean xyz coordinates, and the RGB color.
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
Recompute the 2d circle coefficients using the given inlier set and return them to the user...
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_CIRCLE2D).
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients)
Check whether a model is valid given the user constraints.
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572