Point Cloud Library (PCL)  1.14.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 #ifdef __SSE__
44 #include <xmmintrin.h> // for __m128
45 #endif // ifdef __SSE__
46 #ifdef __AVX__
47 #include <immintrin.h> // for __m256
48 #endif // ifdef __AVX__
49 
50 #include <pcl/sample_consensus/sac_model.h>
51 #include <pcl/sample_consensus/model_types.h>
52 #include <pcl/pcl_exports.h>
53 
54 namespace pcl
55 {
56  namespace internal {
57  PCL_EXPORTS int optimizeModelCoefficientsSphere (Eigen::VectorXf& coeff, const Eigen::ArrayXf& pts_x, const Eigen::ArrayXf& pts_y, const Eigen::ArrayXf& pts_z);
58  } // namespace internal
59 
60  /** \brief SampleConsensusModelSphere defines a model for 3D sphere segmentation.
61  * The model coefficients are defined as:
62  * - \b center.x : the X coordinate of the sphere's center
63  * - \b center.y : the Y coordinate of the sphere's center
64  * - \b center.z : the Z coordinate of the sphere's center
65  * - \b radius : the sphere's radius
66  *
67  * \author Radu B. Rusu
68  * \ingroup sample_consensus
69  */
70  template <typename PointT>
72  {
73  public:
80 
84 
85  using Ptr = shared_ptr<SampleConsensusModelSphere<PointT> >;
86  using ConstPtr = shared_ptr<const SampleConsensusModelSphere<PointT>>;
87 
88  /** \brief Constructor for base SampleConsensusModelSphere.
89  * \param[in] cloud the input point cloud dataset
90  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
91  */
93  bool random = false)
94  : SampleConsensusModel<PointT> (cloud, random)
95  {
96  model_name_ = "SampleConsensusModelSphere";
97  sample_size_ = 4;
98  model_size_ = 4;
99  }
100 
101  /** \brief Constructor for base SampleConsensusModelSphere.
102  * \param[in] cloud the input point cloud dataset
103  * \param[in] indices a vector of point indices to be used from \a cloud
104  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
105  */
107  const Indices &indices,
108  bool random = false)
109  : SampleConsensusModel<PointT> (cloud, indices, random)
110  {
111  model_name_ = "SampleConsensusModelSphere";
112  sample_size_ = 4;
113  model_size_ = 4;
114  }
115 
116  /** \brief Empty destructor */
117  ~SampleConsensusModelSphere () override = default;
118 
119  /** \brief Copy constructor.
120  * \param[in] source the model to copy into this
121  */
124  {
125  *this = source;
126  model_name_ = "SampleConsensusModelSphere";
127  }
128 
129  /** \brief Copy constructor.
130  * \param[in] source the model to copy into this
131  */
134  {
136  return (*this);
137  }
138 
139  /** \brief Check whether the given index samples can form a valid sphere model, compute the model
140  * coefficients from these samples and store them internally in model_coefficients.
141  * The sphere coefficients are: x, y, z, R.
142  * \param[in] samples the point indices found as possible good candidates for creating a valid model
143  * \param[out] model_coefficients the resultant model coefficients
144  */
145  bool
146  computeModelCoefficients (const Indices &samples,
147  Eigen::VectorXf &model_coefficients) const override;
148 
149  /** \brief Compute all distances from the cloud data to a given sphere model.
150  * \param[in] model_coefficients the coefficients of a sphere model that we need to compute distances to
151  * \param[out] distances the resultant estimated distances
152  */
153  void
154  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
155  std::vector<double> &distances) const override;
156 
157  /** \brief Select all the points which respect the given model coefficients as inliers.
158  * \param[in] model_coefficients the coefficients of a sphere model that we need to compute distances to
159  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
160  * \param[out] inliers the resultant model inliers
161  */
162  void
163  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
164  const double threshold,
165  Indices &inliers) override;
166 
167  /** \brief Count all the points which respect the given model coefficients as inliers.
168  *
169  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
170  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
171  * \return the resultant number of inliers
172  */
173  std::size_t
174  countWithinDistance (const Eigen::VectorXf &model_coefficients,
175  const double threshold) const override;
176 
177  /** \brief Recompute the sphere coefficients using the given inlier set and return them to the user.
178  * @note: these are the coefficients of the sphere model after refinement (e.g. after SVD)
179  * \param[in] inliers the data inliers found as supporting the model
180  * \param[in] model_coefficients the initial guess for the optimization
181  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
182  */
183  void
184  optimizeModelCoefficients (const Indices &inliers,
185  const Eigen::VectorXf &model_coefficients,
186  Eigen::VectorXf &optimized_coefficients) const override;
187 
188  /** \brief Create a new point cloud with inliers projected onto the sphere model.
189  * \param[in] inliers the data inliers that we want to project on the sphere model
190  * \param[in] model_coefficients the coefficients of a sphere model
191  * \param[out] projected_points the resultant projected points
192  * \param[in] copy_data_fields set to true if we need to copy the other data fields
193  * \todo implement this.
194  */
195  void
196  projectPoints (const Indices &inliers,
197  const Eigen::VectorXf &model_coefficients,
198  PointCloud &projected_points,
199  bool copy_data_fields = true) const override;
200 
201  /** \brief Verify whether a subset of indices verifies the given sphere model coefficients.
202  * \param[in] indices the data indices that need to be tested against the sphere model
203  * \param[in] model_coefficients the sphere model coefficients
204  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
205  */
206  bool
207  doSamplesVerifyModel (const std::set<index_t> &indices,
208  const Eigen::VectorXf &model_coefficients,
209  const double threshold) const override;
210 
211  /** \brief Return a unique id for this model (SACMODEL_SPHERE). */
212  inline pcl::SacModel getModelType () const override { return (SACMODEL_SPHERE); }
213 
214  protected:
217 
218  /** \brief Check whether a model is valid given the user constraints.
219  * \param[in] model_coefficients the set of model coefficients
220  */
221  bool
222  isModelValid (const Eigen::VectorXf &model_coefficients) const override
223  {
224  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
225  return (false);
226 
227  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_) {
228  PCL_DEBUG("[SampleConsensusModelSphere::isModelValid] Model radius %g is smaller than user specified minimum radius %g\n", model_coefficients[3], radius_min_);
229  return (false);
230  }
231  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_) {
232  PCL_DEBUG("[SampleConsensusModelSphere::isModelValid] Model radius %g is bigger than user specified maximum radius %g\n", model_coefficients[3], radius_max_);
233  return (false);
234  }
235 
236  return (true);
237  }
238 
239  /** \brief Check if a sample of indices results in a good sample of points
240  * indices.
241  * \param[in] samples the resultant index samples
242  */
243  bool
244  isSampleGood(const Indices &samples) const override;
245 
246  /** This implementation uses no SIMD instructions. It is not intended for normal use.
247  * See countWithinDistance which automatically uses the fastest implementation.
248  */
249  std::size_t
250  countWithinDistanceStandard (const Eigen::VectorXf &model_coefficients,
251  const double threshold,
252  std::size_t i = 0) const;
253 
254 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
255  /** This implementation uses SSE, SSE2, and SSE4.1 instructions. It is not intended for normal use.
256  * See countWithinDistance which automatically uses the fastest implementation.
257  */
258  std::size_t
259  countWithinDistanceSSE (const Eigen::VectorXf &model_coefficients,
260  const double threshold,
261  std::size_t i = 0) const;
262 #endif
263 
264 #if defined (__AVX__) && defined (__AVX2__)
265  /** This implementation uses AVX and AVX2 instructions. It is not intended for normal use.
266  * See countWithinDistance which automatically uses the fastest implementation.
267  */
268  std::size_t
269  countWithinDistanceAVX (const Eigen::VectorXf &model_coefficients,
270  const double threshold,
271  std::size_t i = 0) const;
272 #endif
273 
274  private:
275 #ifdef __AVX__
276  inline __m256 sqr_dist8 (const std::size_t i, const __m256 a_vec, const __m256 b_vec, const __m256 c_vec) const;
277 #endif
278 
279 #ifdef __SSE__
280  inline __m128 sqr_dist4 (const std::size_t i, const __m128 a_vec, const __m128 b_vec, const __m128 c_vec) const;
281 #endif
282  };
283 }
284 
285 #ifdef PCL_NO_PRECOMPILE
286 #include <pcl/sample_consensus/impl/sac_model_sphere.hpp>
287 #endif
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
SampleConsensusModel represents the base model class.
Definition: sac_model.h:71
double radius_min_
The minimum and maximum radius limits for the model.
Definition: sac_model.h:565
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:78
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:589
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:74
std::string model_name_
The model name.
Definition: sac_model.h:551
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:592
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:75
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:79
SampleConsensusModelSphere defines a model for 3D sphere segmentation.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
SampleConsensusModelSphere & operator=(const SampleConsensusModelSphere &source)
Copy constructor.
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_SPHERE).
SampleConsensusModelSphere(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelSphere.
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.
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.
SampleConsensusModelSphere(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelSphere.
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.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
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.
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
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.
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...
~SampleConsensusModelSphere() override=default
Empty destructor.
SampleConsensusModelSphere(const SampleConsensusModelSphere &source)
Copy constructor.
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.
PCL_EXPORTS int optimizeModelCoefficientsSphere(Eigen::VectorXf &coeff, const Eigen::ArrayXf &pts_x, const Eigen::ArrayXf &pts_y, const Eigen::ArrayXf &pts_z)
SacModel
Definition: model_types.h:46
@ SACMODEL_SPHERE
Definition: model_types.h:51
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define PCL_EXPORTS
Definition: pcl_macros.h:325
A point structure representing Euclidean xyz coordinates, and the RGB color.