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