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