Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_sphere.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_sphere.h 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_H_
41 #define PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_H_
42 
45 
46 namespace pcl
47 {
58  template <typename PointT>
60  {
61  public:
66 
67 
71 
72  typedef boost::shared_ptr<SampleConsensusModelSphere> Ptr;
73 
78  SampleConsensusModel<PointT> (cloud), tmp_inliers_ ()
79  {}
80 
85  SampleConsensusModelSphere (const PointCloudConstPtr &cloud, const std::vector<int> &indices) :
86  SampleConsensusModel<PointT> (cloud, indices), tmp_inliers_ ()
87  {}
88 
93  SampleConsensusModel<PointT> (), tmp_inliers_ ()
94  {
95  *this = source;
96  }
97 
103  {
105  tmp_inliers_ = source.tmp_inliers_;
106  return (*this);
107  }
108 
115  bool
116  computeModelCoefficients (const std::vector<int> &samples,
117  Eigen::VectorXf &model_coefficients);
118 
123  void
124  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
125  std::vector<double> &distances);
126 
132  void
133  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
134  const double threshold,
135  std::vector<int> &inliers);
136 
143  virtual int
144  countWithinDistance (const Eigen::VectorXf &model_coefficients,
145  const double threshold);
146 
153  void
154  optimizeModelCoefficients (const std::vector<int> &inliers,
155  const Eigen::VectorXf &model_coefficients,
156  Eigen::VectorXf &optimized_coefficients);
157 
165  void
166  projectPoints (const std::vector<int> &inliers,
167  const Eigen::VectorXf &model_coefficients,
168  PointCloud &projected_points,
169  bool copy_data_fields = true);
170 
176  bool
177  doSamplesVerifyModel (const std::set<int> &indices,
178  const Eigen::VectorXf &model_coefficients,
179  const double threshold);
180 
182  inline pcl::SacModel getModelType () const { return (SACMODEL_SPHERE); }
183 
184  protected:
188  inline bool
189  isModelValid (const Eigen::VectorXf &model_coefficients)
190  {
191  // Needs a valid model coefficients
192  if (model_coefficients.size () != 4)
193  {
194  PCL_ERROR ("[pcl::SampleConsensusModelSphere::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
195  return (false);
196  }
197 
198  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_)
199  return (false);
200  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
201  return (false);
202 
203  return (true);
204  }
205 
210  bool
211  isSampleGood(const std::vector<int> &samples) const;
212 
213  private:
215  const std::vector<int> *tmp_inliers_;
216 
217 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
218 #pragma GCC diagnostic ignored "-Weffc++"
219 #endif
220  struct OptimizationFunctor : pcl::Functor<float>
221  {
227  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelSphere<PointT> *model) :
228  pcl::Functor<float>(m_data_points), model_ (model) {}
229 
235  int
236  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
237  {
238  Eigen::Vector4f cen_t;
239  cen_t[3] = 0;
240  for (int i = 0; i < values (); ++i)
241  {
242  // Compute the difference between the center of the sphere and the datapoint X_i
243  cen_t[0] = model_->input_->points[(*model_->tmp_inliers_)[i]].x - x[0];
244  cen_t[1] = model_->input_->points[(*model_->tmp_inliers_)[i]].y - x[1];
245  cen_t[2] = model_->input_->points[(*model_->tmp_inliers_)[i]].z - x[2];
246 
247  // g = sqrt ((x-a)^2 + (y-b)^2 + (z-c)^2) - R
248  fvec[i] = sqrtf (cen_t.dot (cen_t)) - x[3];
249  }
250  return (0);
251  }
252 
254  };
255 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
256 #pragma GCC diagnostic warning "-Weffc++"
257 #endif
258  };
259 }
260 
261 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_H_