Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_circle.h
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: sac_model_circle.h 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_
41 #define PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_
42 
45 
46 namespace pcl
47 {
58  template <typename PointT>
60  {
65 
66  public:
70 
71  typedef boost::shared_ptr<SampleConsensusModelCircle2D> Ptr;
72 
77  SampleConsensusModel<PointT> (cloud), tmp_inliers_ ()
78  {};
79 
84  SampleConsensusModelCircle2D (const PointCloudConstPtr &cloud, const std::vector<int> &indices) :
85  SampleConsensusModel<PointT> (cloud, indices), tmp_inliers_ ()
86  {};
87 
92  SampleConsensusModel<PointT> (), tmp_inliers_ ()
93  {
94  *this = source;
95  }
96 
102  {
104  tmp_inliers_ = source.tmp_inliers_;
105  return (*this);
106  }
107 
113  bool
114  computeModelCoefficients (const std::vector<int> &samples,
115  Eigen::VectorXf &model_coefficients);
116 
121  void
122  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
123  std::vector<double> &distances);
124 
130  void
131  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
132  const double threshold,
133  std::vector<int> &inliers);
134 
141  virtual int
142  countWithinDistance (const Eigen::VectorXf &model_coefficients,
143  const double threshold);
144 
151  void
152  optimizeModelCoefficients (const std::vector<int> &inliers,
153  const Eigen::VectorXf &model_coefficients,
154  Eigen::VectorXf &optimized_coefficients);
155 
162  void
163  projectPoints (const std::vector<int> &inliers,
164  const Eigen::VectorXf &model_coefficients,
165  PointCloud &projected_points,
166  bool copy_data_fields = true);
167 
173  bool
174  doSamplesVerifyModel (const std::set<int> &indices,
175  const Eigen::VectorXf &model_coefficients,
176  const double threshold);
177 
179  inline pcl::SacModel
180  getModelType () const { return (SACMODEL_CIRCLE2D); }
181 
182  protected:
186  bool
187  isModelValid (const Eigen::VectorXf &model_coefficients);
188 
192  bool
193  isSampleGood(const std::vector<int> &samples) const;
194 
195  private:
197  const std::vector<int> *tmp_inliers_;
198 
199 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
200 #pragma GCC diagnostic ignored "-Weffc++"
201 #endif
202 
203  struct OptimizationFunctor : pcl::Functor<float>
204  {
210  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCircle2D<PointT> *model) :
211  pcl::Functor<float>(m_data_points), model_ (model) {}
212 
218  int
219  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
220  {
221  for (int i = 0; i < values (); ++i)
222  {
223  // Compute the difference between the center of the circle and the datapoint X_i
224  float xt = model_->input_->points[(*model_->tmp_inliers_)[i]].x - x[0];
225  float yt = model_->input_->points[(*model_->tmp_inliers_)[i]].y - x[1];
226 
227  // g = sqrt ((x-a)^2 + (y-b)^2) - R
228  fvec[i] = sqrtf (xt * xt + yt * yt) - x[2];
229  }
230  return (0);
231  }
232 
234  };
235 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
236 #pragma GCC diagnostic warning "-Weffc++"
237 #endif
238  };
239 }
240 
241 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE2D_H_