Point Cloud Library (PCL)  1.7.1
sac_model_sphere.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_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_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 SampleConsensusModelSphere defines a model for 3D sphere segmentation.
50  * The model coefficients are defined as:
51  * - \b center.x : the X coordinate of the sphere's center
52  * - \b center.y : the Y coordinate of the sphere's center
53  * - \b center.z : the Z coordinate of the sphere's center
54  * - \b radius : the sphere's radius
55  *
56  * \author Radu B. Rusu
57  * \ingroup sample_consensus
58  */
59  template <typename PointT>
61  {
62  public:
68 
72 
73  typedef boost::shared_ptr<SampleConsensusModelSphere> Ptr;
74 
75  /** \brief Constructor for base SampleConsensusModelSphere.
76  * \param[in] cloud the input point cloud dataset
77  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
78  */
80  bool random = false)
81  : SampleConsensusModel<PointT> (cloud, random), tmp_inliers_ ()
82  {}
83 
84  /** \brief Constructor for base SampleConsensusModelSphere.
85  * \param[in] cloud the input point cloud dataset
86  * \param[in] indices a vector of point indices to be used from \a cloud
87  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
88  */
90  const std::vector<int> &indices,
91  bool random = false)
92  : SampleConsensusModel<PointT> (cloud, indices, random), tmp_inliers_ ()
93  {}
94 
95  /** \brief Empty destructor */
97 
98  /** \brief Copy constructor.
99  * \param[in] source the model to copy into this
100  */
102  SampleConsensusModel<PointT> (), tmp_inliers_ ()
103  {
104  *this = source;
105  }
106 
107  /** \brief Copy constructor.
108  * \param[in] source the model to copy into this
109  */
112  {
114  tmp_inliers_ = source.tmp_inliers_;
115  return (*this);
116  }
117 
118  /** \brief Check whether the given index samples can form a valid sphere model, compute the model
119  * coefficients from these samples and store them internally in model_coefficients.
120  * The sphere coefficients are: x, y, z, R.
121  * \param[in] samples the point indices found as possible good candidates for creating a valid model
122  * \param[out] model_coefficients the resultant model coefficients
123  */
124  bool
125  computeModelCoefficients (const std::vector<int> &samples,
126  Eigen::VectorXf &model_coefficients);
127 
128  /** \brief Compute all distances from the cloud data to a given sphere model.
129  * \param[in] model_coefficients the coefficients of a sphere model that we need to compute distances to
130  * \param[out] distances the resultant estimated distances
131  */
132  void
133  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
134  std::vector<double> &distances);
135 
136  /** \brief Select all the points which respect the given model coefficients as inliers.
137  * \param[in] model_coefficients the coefficients of a sphere model that we need to compute distances to
138  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
139  * \param[out] inliers the resultant model inliers
140  */
141  void
142  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
143  const double threshold,
144  std::vector<int> &inliers);
145 
146  /** \brief Count all the points which respect the given model coefficients as inliers.
147  *
148  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
149  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
150  * \return the resultant number of inliers
151  */
152  virtual int
153  countWithinDistance (const Eigen::VectorXf &model_coefficients,
154  const double threshold);
155 
156  /** \brief Recompute the sphere coefficients using the given inlier set and return them to the user.
157  * @note: these are the coefficients of the sphere model after refinement (eg. after SVD)
158  * \param[in] inliers the data inliers found as supporting the model
159  * \param[in] model_coefficients the initial guess for the optimization
160  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
161  */
162  void
163  optimizeModelCoefficients (const std::vector<int> &inliers,
164  const Eigen::VectorXf &model_coefficients,
165  Eigen::VectorXf &optimized_coefficients);
166 
167  /** \brief Create a new point cloud with inliers projected onto the sphere model.
168  * \param[in] inliers the data inliers that we want to project on the sphere model
169  * \param[in] model_coefficients the coefficients of a sphere model
170  * \param[out] projected_points the resultant projected points
171  * \param[in] copy_data_fields set to true if we need to copy the other data fields
172  * \todo implement this.
173  */
174  void
175  projectPoints (const std::vector<int> &inliers,
176  const Eigen::VectorXf &model_coefficients,
177  PointCloud &projected_points,
178  bool copy_data_fields = true);
179 
180  /** \brief Verify whether a subset of indices verifies the given sphere model coefficients.
181  * \param[in] indices the data indices that need to be tested against the sphere model
182  * \param[in] model_coefficients the sphere model coefficients
183  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
184  */
185  bool
186  doSamplesVerifyModel (const std::set<int> &indices,
187  const Eigen::VectorXf &model_coefficients,
188  const double threshold);
189 
190  /** \brief Return an unique id for this model (SACMODEL_SPHERE). */
191  inline pcl::SacModel getModelType () const { return (SACMODEL_SPHERE); }
192 
193  protected:
194  /** \brief Check whether a model is valid given the user constraints.
195  * \param[in] model_coefficients the set of model coefficients
196  */
197  inline bool
198  isModelValid (const Eigen::VectorXf &model_coefficients)
199  {
200  // Needs a valid model coefficients
201  if (model_coefficients.size () != 4)
202  {
203  PCL_ERROR ("[pcl::SampleConsensusModelSphere::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
204  return (false);
205  }
206 
207  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_)
208  return (false);
209  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
210  return (false);
211 
212  return (true);
213  }
214 
215  /** \brief Check if a sample of indices results in a good sample of points
216  * indices.
217  * \param[in] samples the resultant index samples
218  */
219  bool
220  isSampleGood(const std::vector<int> &samples) const;
221 
222  private:
223  /** \brief Temporary pointer to a list of given indices for optimizeModelCoefficients () */
224  const std::vector<int> *tmp_inliers_;
225 
226 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
227 #pragma GCC diagnostic ignored "-Weffc++"
228 #endif
229  struct OptimizationFunctor : pcl::Functor<float>
230  {
231  /** Functor constructor
232  * \param[in] m_data_points the number of data points to evaluate
233  * \param[in] estimator pointer to the estimator object
234  * \param[in] distance distance computation function pointer
235  */
236  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelSphere<PointT> *model) :
237  pcl::Functor<float>(m_data_points), model_ (model) {}
238 
239  /** Cost function to be minimized
240  * \param[in] x the variables array
241  * \param[out] fvec the resultant functions evaluations
242  * \return 0
243  */
244  int
245  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
246  {
247  Eigen::Vector4f cen_t;
248  cen_t[3] = 0;
249  for (int i = 0; i < values (); ++i)
250  {
251  // Compute the difference between the center of the sphere and the datapoint X_i
252  cen_t[0] = model_->input_->points[(*model_->tmp_inliers_)[i]].x - x[0];
253  cen_t[1] = model_->input_->points[(*model_->tmp_inliers_)[i]].y - x[1];
254  cen_t[2] = model_->input_->points[(*model_->tmp_inliers_)[i]].z - x[2];
255 
256  // g = sqrt ((x-a)^2 + (y-b)^2 + (z-c)^2) - R
257  fvec[i] = sqrtf (cen_t.dot (cen_t)) - x[3];
258  }
259  return (0);
260  }
261 
263  };
264 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
265 #pragma GCC diagnostic warning "-Weffc++"
266 #endif
267  };
268 }
269 
270 #ifdef PCL_NO_PRECOMPILE
271 #include <pcl/sample_consensus/impl/sac_model_sphere.hpp>
272 #endif
273 
274 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_H_