Point Cloud Library (PCL)  1.9.1
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)
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)
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  */
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  return (*this);
124  }
125 
126  /** \brief Check whether the given index samples can form a valid 2D circle model, compute the model coefficients
127  * from these samples and store them in model_coefficients. The circle coefficients are: x, y, R.
128  * \param[in] samples the point indices found as possible good candidates for creating a valid model
129  * \param[out] model_coefficients the resultant model coefficients
130  */
131  bool
132  computeModelCoefficients (const std::vector<int> &samples,
133  Eigen::VectorXf &model_coefficients) const;
134 
135  /** \brief Compute all distances from the cloud data to a given 2D circle model.
136  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
137  * \param[out] distances the resultant estimated distances
138  */
139  void
140  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
141  std::vector<double> &distances) const;
142 
143  /** \brief Compute all distances from the cloud data to a given 2D circle model.
144  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
145  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
146  * \param[out] inliers the resultant model inliers
147  */
148  void
149  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
150  const double threshold,
151  std::vector<int> &inliers);
152 
153  /** \brief Count all the points which respect the given model coefficients as inliers.
154  *
155  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
156  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
157  * \return the resultant number of inliers
158  */
159  virtual int
160  countWithinDistance (const Eigen::VectorXf &model_coefficients,
161  const double threshold) const;
162 
163  /** \brief Recompute the 2d circle coefficients using the given inlier set and return them to the user.
164  * @note: these are the coefficients of the 2d circle model after refinement (e.g. after SVD)
165  * \param[in] inliers the data inliers found as supporting the model
166  * \param[in] model_coefficients the initial guess for the optimization
167  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
168  */
169  void
170  optimizeModelCoefficients (const std::vector<int> &inliers,
171  const Eigen::VectorXf &model_coefficients,
172  Eigen::VectorXf &optimized_coefficients) const;
173 
174  /** \brief Create a new point cloud with inliers projected onto the 2d circle model.
175  * \param[in] inliers the data inliers that we want to project on the 2d circle model
176  * \param[in] model_coefficients the coefficients of a 2d circle model
177  * \param[out] projected_points the resultant projected points
178  * \param[in] copy_data_fields set to true if we need to copy the other data fields
179  */
180  void
181  projectPoints (const std::vector<int> &inliers,
182  const Eigen::VectorXf &model_coefficients,
183  PointCloud &projected_points,
184  bool copy_data_fields = true) const;
185 
186  /** \brief Verify whether a subset of indices verifies the given 2d circle model coefficients.
187  * \param[in] indices the data indices that need to be tested against the 2d circle model
188  * \param[in] model_coefficients the 2d circle model coefficients
189  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
190  */
191  bool
192  doSamplesVerifyModel (const std::set<int> &indices,
193  const Eigen::VectorXf &model_coefficients,
194  const double threshold) const;
195 
196  /** \brief Return an unique id for this model (SACMODEL_CIRCLE2D). */
197  inline pcl::SacModel
198  getModelType () const { return (SACMODEL_CIRCLE2D); }
199 
200  protected:
203 
204  /** \brief Check whether a model is valid given the user constraints.
205  * \param[in] model_coefficients the set of model coefficients
206  */
207  virtual bool
208  isModelValid (const Eigen::VectorXf &model_coefficients) const;
209 
210  /** \brief Check if a sample of indices results in a good sample of points indices.
211  * \param[in] samples the resultant index samples
212  */
213  bool
214  isSampleGood(const std::vector<int> &samples) const;
215 
216  private:
217 
218 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
219 #pragma GCC diagnostic ignored "-Weffc++"
220 #endif
221  /** \brief Functor for the optimization function */
222  struct OptimizationFunctor : pcl::Functor<float>
223  {
224  /** \brief Functor constructor
225  * \param[in] indices the indices of data points to evaluate
226  * \param[in] estimator pointer to the estimator object
227  */
228  OptimizationFunctor (const pcl::SampleConsensusModelCircle2D<PointT> *model, const std::vector<int>& indices) :
229  pcl::Functor<float> (indices.size ()), model_ (model), indices_ (indices) {}
230 
231  /** Cost function to be minimized
232  * \param[in] x the variables array
233  * \param[out] fvec the resultant functions evaluations
234  * \return 0
235  */
236  int
237  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
238  {
239  for (int i = 0; i < values (); ++i)
240  {
241  // Compute the difference between the center of the circle and the datapoint X_i
242  float xt = model_->input_->points[indices_[i]].x - x[0];
243  float yt = model_->input_->points[indices_[i]].y - x[1];
244 
245  // g = sqrt ((x-a)^2 + (y-b)^2) - R
246  fvec[i] = std::sqrt (xt * xt + yt * yt) - x[2];
247  }
248  return (0);
249  }
250 
252  const std::vector<int> &indices_;
253  };
254 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
255 #pragma GCC diagnostic warning "-Weffc++"
256 #endif
257  };
258 }
259 
260 #ifdef PCL_NO_PRECOMPILE
261 #include <pcl/sample_consensus/impl/sac_model_circle.hpp>
262 #endif
263 
264 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_
boost::shared_ptr< SampleConsensusModelCircle2D > Ptr
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
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.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const
Create a new point cloud with inliers projected onto the 2d circle model.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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
boost::shared_ptr< std::vector< int > > indices_
A pointer to the vector of point indices to use.
Definition: sac_model.h:540
SampleConsensusModelCircle2D defines a model for 2D circle segmentation on the X-Y plane...
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
std::string model_name_
The model name.
Definition: sac_model.h:534
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
Verify whether a subset of indices verifies the given 2d circle model coefficients.
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
virtual ~SampleConsensusModelCircle2D()
Empty destructor.
SampleConsensusModelCircle2D(const SampleConsensusModelCircle2D &source)
Copy constructor.
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_CIRCLE2D).
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
SacModel
Definition: model_types.h:46
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
SampleConsensusModelCircle2D(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCircle2D.
SampleConsensusModel< PointT >::PointCloud PointCloud
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
Recompute the 2d circle coefficients using the given inlier set and return them to the user...
SampleConsensusModelCircle2D & operator=(const SampleConsensusModelCircle2D &source)
Copy constructor.
A point structure representing Euclidean xyz coordinates, and the RGB color.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all distances from the cloud data to a given 2D circle model.
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572