Point Cloud Library (PCL)  1.11.1-dev
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 #pragma once
42 
43 #include <pcl/sample_consensus/sac_model.h>
44 #include <pcl/sample_consensus/model_types.h>
45 
46 namespace pcl
47 {
48  /** \brief SampleConsensusModelSphere defines a model for 3D sphere segmentation.
49  * The model coefficients are defined as:
50  * - \b center.x : the X coordinate of the sphere's center
51  * - \b center.y : the Y coordinate of the sphere's center
52  * - \b center.z : the Z coordinate of the sphere's center
53  * - \b radius : the sphere's radius
54  *
55  * \author Radu B. Rusu
56  * \ingroup sample_consensus
57  */
58  template <typename PointT>
60  {
61  public:
68 
72 
73  using Ptr = shared_ptr<SampleConsensusModelSphere<PointT> >;
74  using ConstPtr = shared_ptr<const SampleConsensusModelSphere<PointT>>;
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)
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 Indices &indices,
96  bool random = false)
97  : SampleConsensusModel<PointT> (cloud, indices, random)
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  */
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  return (*this);
125  }
126 
127  /** \brief Check whether the given index samples can form a valid sphere model, compute the model
128  * coefficients from these samples and store them internally in model_coefficients.
129  * The sphere coefficients are: x, y, z, R.
130  * \param[in] samples the point indices found as possible good candidates for creating a valid model
131  * \param[out] model_coefficients the resultant model coefficients
132  */
133  bool
134  computeModelCoefficients (const Indices &samples,
135  Eigen::VectorXf &model_coefficients) const override;
136 
137  /** \brief Compute all distances from the cloud data to a given sphere model.
138  * \param[in] model_coefficients the coefficients of a sphere model that we need to compute distances to
139  * \param[out] distances the resultant estimated distances
140  */
141  void
142  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
143  std::vector<double> &distances) const override;
144 
145  /** \brief Select all the points which respect the given model coefficients as inliers.
146  * \param[in] model_coefficients the coefficients of a sphere model that we need to compute distances to
147  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
148  * \param[out] inliers the resultant model inliers
149  */
150  void
151  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
152  const double threshold,
153  Indices &inliers) override;
154 
155  /** \brief Count all the points which respect the given model coefficients as inliers.
156  *
157  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
158  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
159  * \return the resultant number of inliers
160  */
161  std::size_t
162  countWithinDistance (const Eigen::VectorXf &model_coefficients,
163  const double threshold) const override;
164 
165  /** \brief Recompute the sphere coefficients using the given inlier set and return them to the user.
166  * @note: these are the coefficients of the sphere model after refinement (e.g. after SVD)
167  * \param[in] inliers the data inliers found as supporting the model
168  * \param[in] model_coefficients the initial guess for the optimization
169  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
170  */
171  void
172  optimizeModelCoefficients (const Indices &inliers,
173  const Eigen::VectorXf &model_coefficients,
174  Eigen::VectorXf &optimized_coefficients) const override;
175 
176  /** \brief Create a new point cloud with inliers projected onto the sphere model.
177  * \param[in] inliers the data inliers that we want to project on the sphere model
178  * \param[in] model_coefficients the coefficients of a sphere model
179  * \param[out] projected_points the resultant projected points
180  * \param[in] copy_data_fields set to true if we need to copy the other data fields
181  * \todo implement this.
182  */
183  void
184  projectPoints (const Indices &inliers,
185  const Eigen::VectorXf &model_coefficients,
186  PointCloud &projected_points,
187  bool copy_data_fields = true) const override;
188 
189  /** \brief Verify whether a subset of indices verifies the given sphere model coefficients.
190  * \param[in] indices the data indices that need to be tested against the sphere model
191  * \param[in] model_coefficients the sphere model coefficients
192  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
193  */
194  bool
195  doSamplesVerifyModel (const std::set<index_t> &indices,
196  const Eigen::VectorXf &model_coefficients,
197  const double threshold) const override;
198 
199  /** \brief Return a unique id for this model (SACMODEL_SPHERE). */
200  inline pcl::SacModel getModelType () const override { return (SACMODEL_SPHERE); }
201 
202  protected:
205 
206  /** \brief Check whether a model is valid given the user constraints.
207  * \param[in] model_coefficients the set of model coefficients
208  */
209  bool
210  isModelValid (const Eigen::VectorXf &model_coefficients) const override
211  {
212  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
213  return (false);
214 
215  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_)
216  return (false);
217  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
218  return (false);
219 
220  return (true);
221  }
222 
223  /** \brief Check if a sample of indices results in a good sample of points
224  * indices.
225  * \param[in] samples the resultant index samples
226  */
227  bool
228  isSampleGood(const Indices &samples) const override;
229 
230  /** This implementation uses no SIMD instructions. It is not intended for normal use.
231  * See countWithinDistance which automatically uses the fastest implementation.
232  */
233  std::size_t
234  countWithinDistanceStandard (const Eigen::VectorXf &model_coefficients,
235  const double threshold,
236  std::size_t i = 0) const;
237 
238 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
239  /** This implementation uses SSE, SSE2, and SSE4.1 instructions. It is not intended for normal use.
240  * See countWithinDistance which automatically uses the fastest implementation.
241  */
242  std::size_t
243  countWithinDistanceSSE (const Eigen::VectorXf &model_coefficients,
244  const double threshold,
245  std::size_t i = 0) const;
246 #endif
247 
248 #if defined (__AVX__) && defined (__AVX2__)
249  /** This implementation uses AVX and AVX2 instructions. It is not intended for normal use.
250  * See countWithinDistance which automatically uses the fastest implementation.
251  */
252  std::size_t
253  countWithinDistanceAVX (const Eigen::VectorXf &model_coefficients,
254  const double threshold,
255  std::size_t i = 0) const;
256 #endif
257 
258  private:
259  struct OptimizationFunctor : pcl::Functor<float>
260  {
261  /** Functor constructor
262  * \param[in] indices the indices of data points to evaluate
263  * \param[in] estimator pointer to the estimator object
264  */
265  OptimizationFunctor (const pcl::SampleConsensusModelSphere<PointT> *model, const Indices& indices) :
266  pcl::Functor<float> (indices.size ()), model_ (model), indices_ (indices) {}
267 
268  /** Cost function to be minimized
269  * \param[in] x the variables array
270  * \param[out] fvec the resultant functions evaluations
271  * \return 0
272  */
273  int
274  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
275  {
276  Eigen::Vector4f cen_t;
277  cen_t[3] = 0;
278  for (int i = 0; i < values (); ++i)
279  {
280  // Compute the difference between the center of the sphere and the datapoint X_i
281  cen_t.head<3>() = (*model_->input_)[indices_[i]].getVector3fMap() - x.head<3>();
282 
283  // g = sqrt ((x-a)^2 + (y-b)^2 + (z-c)^2) - R
284  fvec[i] = std::sqrt (cen_t.dot (cen_t)) - x[3];
285  }
286  return (0);
287  }
288 
290  const Indices &indices_;
291  };
292 
293 #ifdef __AVX__
294  inline __m256 sqr_dist8 (const std::size_t i, const __m256 a_vec, const __m256 b_vec, const __m256 c_vec) const;
295 #endif
296 
297 #ifdef __SSE__
298  inline __m128 sqr_dist4 (const std::size_t i, const __m128 a_vec, const __m128 b_vec, const __m128 c_vec) const;
299 #endif
300  };
301 }
302 
303 #ifdef PCL_NO_PRECOMPILE
304 #include <pcl/sample_consensus/impl/sac_model_sphere.hpp>
305 #endif
pcl
Definition: convolution.h:46
pcl::SampleConsensusModelSphere::countWithinDistance
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
Definition: sac_model_sphere.hpp:229
pcl::SampleConsensusModelSphere::isSampleGood
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
Definition: sac_model_sphere.hpp:49
pcl::SampleConsensusModelSphere::~SampleConsensusModelSphere
~SampleConsensusModelSphere()
Empty destructor.
Definition: sac_model_sphere.h:105
pcl::SampleConsensusModel::sample_size_
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:588
pcl::SampleConsensusModelSphere::SampleConsensusModelSphere
SampleConsensusModelSphere(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelSphere.
Definition: sac_model_sphere.h:94
pcl::SampleConsensusModel::model_size_
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:591
pcl::PointCloud< pcl::PointXYZRGB >
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::SampleConsensusModelSphere::getDistancesToModel
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given sphere model.
Definition: sac_model_sphere.hpp:157
pcl::SampleConsensusModelSphere::optimizeModelCoefficients
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the sphere coefficients using the given inlier set and return them to the user.
Definition: sac_model_sphere.hpp:353
pcl::Functor
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:672
pcl::SampleConsensusModelSphere::countWithinDistanceStandard
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
Definition: sac_model_sphere.hpp:247
pcl::SacModel
SacModel
Definition: model_types.h:45
pcl::SampleConsensusModelSphere::computeModelCoefficients
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid sphere model, compute the model coefficients f...
Definition: sac_model_sphere.hpp:61
pcl::SampleConsensusModel::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: sac_model.h:556
pcl::SampleConsensusModel< pcl::PointXYZRGB >::ConstPtr
shared_ptr< const SampleConsensusModel< pcl::PointXYZRGB > > ConstPtr
Definition: sac_model.h:78
pcl::SampleConsensusModelSphere::getModelType
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_SPHERE).
Definition: sac_model_sphere.h:200
pcl::SACMODEL_SPHERE
@ SACMODEL_SPHERE
Definition: model_types.h:51
pcl::SampleConsensusModel::radius_min_
double radius_min_
The minimum and maximum radius limits for the model.
Definition: sac_model.h:564
pcl::SampleConsensusModel< pcl::PointXYZRGB >::Ptr
shared_ptr< SampleConsensusModel< pcl::PointXYZRGB > > Ptr
Definition: sac_model.h:77
pcl::SampleConsensusModel::model_name_
std::string model_name_
The model name.
Definition: sac_model.h:550
pcl::SampleConsensusModel::radius_max_
double radius_max_
Definition: sac_model.h:564
pcl::SampleConsensusModelSphere::SampleConsensusModelSphere
SampleConsensusModelSphere(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelSphere.
Definition: sac_model_sphere.h:80
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:141
pcl::SampleConsensusModelSphere::doSamplesVerifyModel
bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given sphere model coefficients.
Definition: sac_model_sphere.hpp:407
pcl::SampleConsensusModel< pcl::PointXYZRGB >::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:73
pcl::SampleConsensusModelSphere::isModelValid
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
Definition: sac_model_sphere.h:210
pcl::SampleConsensusModelSphere::SampleConsensusModelSphere
SampleConsensusModelSphere(const SampleConsensusModelSphere &source)
Copy constructor.
Definition: sac_model_sphere.h:110
pcl::SampleConsensusModelSphere::projectPoints
void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const override
Create a new point cloud with inliers projected onto the sphere model.
Definition: sac_model_sphere.hpp:384
pcl::SampleConsensusModelSphere::selectWithinDistance
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
Definition: sac_model_sphere.hpp:188
pcl::SampleConsensusModelSphere::operator=
SampleConsensusModelSphere & operator=(const SampleConsensusModelSphere &source)
Copy constructor.
Definition: sac_model_sphere.h:121
pcl::SampleConsensusModel< pcl::PointXYZRGB >::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:74
pcl::SampleConsensusModel
SampleConsensusModel represents the base model class.
Definition: sac_model.h:69
pcl::SampleConsensusModelSphere
SampleConsensusModelSphere defines a model for 3D sphere segmentation.
Definition: sac_model_sphere.h:59