Point Cloud Library (PCL)  1.13.0-dev
sac_model_sphere.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, 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_IMPL_SAC_MODEL_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
43 
44 #include <unsupported/Eigen/NonLinearOptimization> // for LevenbergMarquardt
45 #include <pcl/sample_consensus/sac_model_sphere.h>
46 
47 //////////////////////////////////////////////////////////////////////////
48 template <typename PointT> bool
50 {
51  if (samples.size () != sample_size_)
52  {
53  PCL_ERROR ("[pcl::SampleConsensusModelSphere::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
54  return (false);
55  }
56  return (true);
57 }
58 
59 //////////////////////////////////////////////////////////////////////////
60 template <typename PointT> bool
62  const Indices &samples, Eigen::VectorXf &model_coefficients) const
63 {
64  // Need 4 samples
65  if (samples.size () != sample_size_)
66  {
67  PCL_ERROR ("[pcl::SampleConsensusModelSphere::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
68  return (false);
69  }
70 
71  // TODO maybe find a more stable algorithm for this?
72  Eigen::Matrix4d temp;
73  for (int i = 0; i < 4; i++)
74  {
75  temp (i, 0) = (*input_)[samples[i]].x;
76  temp (i, 1) = (*input_)[samples[i]].y;
77  temp (i, 2) = (*input_)[samples[i]].z;
78  temp (i, 3) = 1;
79  }
80  const double m11 = temp.determinant ();
81  if (m11 == 0)
82  {
83  return (false); // the points don't define a sphere!
84  }
85 
86  for (int i = 0; i < 4; ++i)
87  {
88  temp (i, 0) = ((*input_)[samples[i]].x) * ((*input_)[samples[i]].x) +
89  ((*input_)[samples[i]].y) * ((*input_)[samples[i]].y) +
90  ((*input_)[samples[i]].z) * ((*input_)[samples[i]].z);
91  }
92  const double m12 = temp.determinant ();
93 
94  for (int i = 0; i < 4; ++i)
95  {
96  temp (i, 1) = temp (i, 0);
97  temp (i, 0) = (*input_)[samples[i]].x;
98  }
99  const double m13 = temp.determinant ();
100 
101  for (int i = 0; i < 4; ++i)
102  {
103  temp (i, 2) = temp (i, 1);
104  temp (i, 1) = (*input_)[samples[i]].y;
105  }
106  const double m14 = temp.determinant ();
107 
108  for (int i = 0; i < 4; ++i)
109  {
110  temp (i, 0) = temp (i, 2);
111  temp (i, 1) = (*input_)[samples[i]].x;
112  temp (i, 2) = (*input_)[samples[i]].y;
113  temp (i, 3) = (*input_)[samples[i]].z;
114  }
115  const double m15 = temp.determinant ();
116 
117  // Center (x , y, z)
118  model_coefficients.resize (model_size_);
119  model_coefficients[0] = 0.5f * m12 / m11;
120  model_coefficients[1] = 0.5f * m13 / m11;
121  model_coefficients[2] = 0.5f * m14 / m11;
122  // Radius
123  model_coefficients[3] = std::sqrt (model_coefficients[0] * model_coefficients[0] +
124  model_coefficients[1] * model_coefficients[1] +
125  model_coefficients[2] * model_coefficients[2] - m15 / m11);
126 
127  PCL_DEBUG ("[pcl::SampleConsensusModelSphere::computeModelCoefficients] Model is (%g,%g,%g,%g)\n",
128  model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3]);
129  return (true);
130 }
131 
132 #define AT(POS) ((*input_)[(*indices_)[(POS)]])
133 
134 #ifdef __AVX__
135 // This function computes the squared distances (i.e. the distances without the square root) of 8 points to the center of the sphere
136 template <typename PointT> inline __m256 pcl::SampleConsensusModelSphere<PointT>::sqr_dist8 (const std::size_t i, const __m256 a_vec, const __m256 b_vec, const __m256 c_vec) const
137 {
138  const __m256 tmp1 = _mm256_sub_ps (_mm256_set_ps (AT(i ).x, AT(i+1).x, AT(i+2).x, AT(i+3).x, AT(i+4).x, AT(i+5).x, AT(i+6).x, AT(i+7).x), a_vec);
139  const __m256 tmp2 = _mm256_sub_ps (_mm256_set_ps (AT(i ).y, AT(i+1).y, AT(i+2).y, AT(i+3).y, AT(i+4).y, AT(i+5).y, AT(i+6).y, AT(i+7).y), b_vec);
140  const __m256 tmp3 = _mm256_sub_ps (_mm256_set_ps (AT(i ).z, AT(i+1).z, AT(i+2).z, AT(i+3).z, AT(i+4).z, AT(i+5).z, AT(i+6).z, AT(i+7).z), c_vec);
141  return _mm256_add_ps (_mm256_add_ps (_mm256_mul_ps (tmp1, tmp1), _mm256_mul_ps (tmp2, tmp2)), _mm256_mul_ps(tmp3, tmp3));
142 }
143 #endif // ifdef __AVX__
144 
145 #ifdef __SSE__
146 // This function computes the squared distances (i.e. the distances without the square root) of 4 points to the center of the sphere
147 template <typename PointT> inline __m128 pcl::SampleConsensusModelSphere<PointT>::sqr_dist4 (const std::size_t i, const __m128 a_vec, const __m128 b_vec, const __m128 c_vec) const
148 {
149  const __m128 tmp1 = _mm_sub_ps (_mm_set_ps (AT(i ).x, AT(i+1).x, AT(i+2).x, AT(i+3).x), a_vec);
150  const __m128 tmp2 = _mm_sub_ps (_mm_set_ps (AT(i ).y, AT(i+1).y, AT(i+2).y, AT(i+3).y), b_vec);
151  const __m128 tmp3 = _mm_sub_ps (_mm_set_ps (AT(i ).z, AT(i+1).z, AT(i+2).z, AT(i+3).z), c_vec);
152  return _mm_add_ps (_mm_add_ps (_mm_mul_ps (tmp1, tmp1), _mm_mul_ps (tmp2, tmp2)), _mm_mul_ps(tmp3, tmp3));
153 }
154 #endif // ifdef __SSE__
155 
156 #undef AT
157 
158 //////////////////////////////////////////////////////////////////////////
159 template <typename PointT> void
161  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
162 {
163  // Check if the model is valid given the user constraints
164  if (!isModelValid (model_coefficients))
165  {
166  distances.clear ();
167  return;
168  }
169  distances.resize (indices_->size ());
170 
171  const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
172  // Iterate through the 3d points and calculate the distances from them to the sphere
173  for (std::size_t i = 0; i < indices_->size (); ++i)
174  {
175  // Calculate the distance from the point to the sphere as the difference between
176  //dist(point,sphere_origin) and sphere_radius
177  distances[i] = std::abs (((*input_)[(*indices_)[i]].getVector3fMap () - center).norm () - model_coefficients[3]);
178  }
179 }
180 
181 //////////////////////////////////////////////////////////////////////////
182 template <typename PointT> void
184  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
185 {
186  // Check if the model is valid given the user constraints
187  if (!isModelValid (model_coefficients))
188  {
189  inliers.clear ();
190  return;
191  }
192 
193  inliers.clear ();
194  error_sqr_dists_.clear ();
195  inliers.reserve (indices_->size ());
196  error_sqr_dists_.reserve (indices_->size ());
197 
198  const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
199  const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
200  const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
201  // Iterate through the 3d points and calculate the distances from them to the sphere
202  for (std::size_t i = 0; i < indices_->size (); ++i)
203  {
204  // To avoid sqrt computation: consider one larger sphere (radius + threshold) and one smaller sphere (radius - threshold).
205  // Valid if point is in larger sphere, but not in smaller sphere.
206  const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
207  if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
208  {
209  // Returns the indices of the points whose distances are smaller than the threshold
210  inliers.push_back ((*indices_)[i]);
211  // Only compute exact distance if necessary (if point is inlier)
212  error_sqr_dists_.push_back (static_cast<double> (std::abs (std::sqrt (sqr_dist) - model_coefficients[3])));
213  }
214  }
215 }
216 
217 //////////////////////////////////////////////////////////////////////////
218 template <typename PointT> std::size_t
220  const Eigen::VectorXf &model_coefficients, const double threshold) const
221 {
222  // Check if the model is valid given the user constraints
223  if (!isModelValid (model_coefficients))
224  return (0);
225 
226 #if defined (__AVX__) && defined (__AVX2__)
227  return countWithinDistanceAVX (model_coefficients, threshold);
228 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
229  return countWithinDistanceSSE (model_coefficients, threshold);
230 #else
231  return countWithinDistanceStandard (model_coefficients, threshold);
232 #endif
233 }
234 
235 //////////////////////////////////////////////////////////////////////////
236 template <typename PointT> std::size_t
238  const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i) const
239 {
240  std::size_t nr_p = 0;
241  const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
242  const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
243  const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
244  // Iterate through the 3d points and calculate the distances from them to the sphere
245  for (; i < indices_->size (); ++i)
246  {
247  // To avoid sqrt computation: consider one larger sphere (radius + threshold) and one smaller sphere (radius - threshold).
248  // Valid if point is in larger sphere, but not in smaller sphere.
249  const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
250  if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
251  nr_p++;
252  }
253  return (nr_p);
254 }
255 
256 //////////////////////////////////////////////////////////////////////////
257 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
258 template <typename PointT> std::size_t
260  const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i) const
261 {
262  std::size_t nr_p = 0;
263  const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
264  const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
265  const __m128 c_vec = _mm_set1_ps (model_coefficients[2]);
266  // To avoid sqrt computation: consider one larger sphere (radius + threshold) and one smaller sphere (radius - threshold). Valid if point is in larger sphere, but not in smaller sphere.
267  const __m128 sqr_inner_radius = _mm_set1_ps ((model_coefficients[3] <= threshold ? 0.0 : (model_coefficients[3]-threshold)*(model_coefficients[3]-threshold)));
268  const __m128 sqr_outer_radius = _mm_set1_ps ((model_coefficients[3]+threshold)*(model_coefficients[3]+threshold));
269  __m128i res = _mm_set1_epi32(0); // This corresponds to nr_p: 4 32bit integers that, summed together, hold the number of inliers
270  for (; (i + 4) <= indices_->size (); i += 4)
271  {
272  const __m128 sqr_dist = sqr_dist4 (i, a_vec, b_vec, c_vec);
273  const __m128 mask = _mm_and_ps (_mm_cmplt_ps (sqr_inner_radius, sqr_dist), _mm_cmplt_ps (sqr_dist, sqr_outer_radius)); // The mask contains 1 bits if the corresponding points are inliers, else 0 bits
274  res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask))); // The latter part creates a vector with ones (as 32bit integers) where the points are inliers
275  //const int res = _mm_movemask_ps (mask);
276  //if (res & 1) nr_p++;
277  //if (res & 2) nr_p++;
278  //if (res & 4) nr_p++;
279  //if (res & 8) nr_p++;
280  }
281  nr_p += _mm_extract_epi32 (res, 0);
282  nr_p += _mm_extract_epi32 (res, 1);
283  nr_p += _mm_extract_epi32 (res, 2);
284  nr_p += _mm_extract_epi32 (res, 3);
285 
286  // Process the remaining points (at most 3)
287  nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
288  return (nr_p);
289 }
290 #endif
291 
292 //////////////////////////////////////////////////////////////////////////
293 #if defined (__AVX__) && defined (__AVX2__)
294 template <typename PointT> std::size_t
296  const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i) const
297 {
298  std::size_t nr_p = 0;
299  const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
300  const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
301  const __m256 c_vec = _mm256_set1_ps (model_coefficients[2]);
302  // To avoid sqrt computation: consider one larger sphere (radius + threshold) and one smaller sphere (radius - threshold). Valid if point is in larger sphere, but not in smaller sphere.
303  const __m256 sqr_inner_radius = _mm256_set1_ps ((model_coefficients[3] <= threshold ? 0.0 : (model_coefficients[3]-threshold)*(model_coefficients[3]-threshold)));
304  const __m256 sqr_outer_radius = _mm256_set1_ps ((model_coefficients[3]+threshold)*(model_coefficients[3]+threshold));
305  __m256i res = _mm256_set1_epi32(0); // This corresponds to nr_p: 8 32bit integers that, summed together, hold the number of inliers
306  for (; (i + 8) <= indices_->size (); i += 8)
307  {
308  const __m256 sqr_dist = sqr_dist8 (i, a_vec, b_vec, c_vec);
309  const __m256 mask = _mm256_and_ps (_mm256_cmp_ps (sqr_inner_radius, sqr_dist, _CMP_LT_OQ), _mm256_cmp_ps (sqr_dist, sqr_outer_radius, _CMP_LT_OQ)); // The mask contains 1 bits if the corresponding points are inliers, else 0 bits
310  res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask))); // The latter part creates a vector with ones (as 32bit integers) where the points are inliers
311  //const int res = _mm256_movemask_ps (mask);
312  //if (res & 1) nr_p++;
313  //if (res & 2) nr_p++;
314  //if (res & 4) nr_p++;
315  //if (res & 8) nr_p++;
316  //if (res & 16) nr_p++;
317  //if (res & 32) nr_p++;
318  //if (res & 64) nr_p++;
319  //if (res & 128) nr_p++;
320  }
321  nr_p += _mm256_extract_epi32 (res, 0);
322  nr_p += _mm256_extract_epi32 (res, 1);
323  nr_p += _mm256_extract_epi32 (res, 2);
324  nr_p += _mm256_extract_epi32 (res, 3);
325  nr_p += _mm256_extract_epi32 (res, 4);
326  nr_p += _mm256_extract_epi32 (res, 5);
327  nr_p += _mm256_extract_epi32 (res, 6);
328  nr_p += _mm256_extract_epi32 (res, 7);
329 
330  // Process the remaining points (at most 7)
331  nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
332  return (nr_p);
333 }
334 #endif
335 
336 //////////////////////////////////////////////////////////////////////////
337 template <typename PointT> void
339  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
340 {
341  optimized_coefficients = model_coefficients;
342 
343  // Needs a set of valid model coefficients
344  if (!isModelValid (model_coefficients))
345  {
346  PCL_ERROR ("[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Given model is invalid!\n");
347  return;
348  }
349 
350  // Need more than the minimum sample size to make a difference
351  if (inliers.size () <= sample_size_)
352  {
353  PCL_ERROR ("[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
354  return;
355  }
356 
357  OptimizationFunctor functor (this, inliers);
358  Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
359  Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, float> lm (num_diff);
360  int info = lm.minimize (optimized_coefficients);
361 
362  // Compute the L2 norm of the residuals
363  PCL_DEBUG ("[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g \nFinal solution: %g %g %g %g\n",
364  info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3]);
365 }
366 
367 //////////////////////////////////////////////////////////////////////////
368 template <typename PointT> void
370  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
371 {
372  // Needs a valid set of model coefficients
373  if (!isModelValid (model_coefficients))
374  {
375  PCL_ERROR ("[pcl::SampleConsensusModelSphere::projectPoints] Given model is invalid!\n");
376  return;
377  }
378 
379  projected_points.header = input_->header;
380  projected_points.is_dense = input_->is_dense;
381 
382  // C : sphere center
383  const Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
384  // r : radius
385  const double r = model_coefficients[3];
386 
387  // Copy all the data fields from the input cloud to the projected one?
388  if (copy_data_fields)
389  {
390  // Allocate enough space and copy the basics
391  projected_points.resize (input_->size ());
392  projected_points.width = input_->width;
393  projected_points.height = input_->height;
394 
395  using FieldList = typename pcl::traits::fieldList<PointT>::type;
396  // Iterate over each point
397  for (std::size_t i = 0; i < projected_points.points.size (); ++i)
398  // Iterate over each dimension
399  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
400 
401  // Iterate through the 3d points and calculate the distances from them to the sphere
402  for (const auto& inlier : inliers)
403  {
404  // what i have:
405  // P : Sample Point
406  const Eigen::Vector3d P (input_->points[inlier].x, input_->points[inlier].y, input_->points[inlier].z);
407 
408  const Eigen::Vector3d direction = (P - C).normalized();
409 
410  // K : Point on Sphere
411  const Eigen::Vector3d K = C + r * direction;
412 
413  projected_points.points[inlier].x = static_cast<float> (K[0]);
414  projected_points.points[inlier].y = static_cast<float> (K[1]);
415  projected_points.points[inlier].z = static_cast<float> (K[2]);
416  }
417  }
418  else
419  {
420  // Allocate enough space and copy the basics
421  projected_points.resize (inliers.size ());
422  projected_points.width = static_cast<uint32_t> (inliers.size ());
423  projected_points.height = 1;
424 
425  using FieldList = typename pcl::traits::fieldList<PointT>::type;
426  // Iterate over each point
427  for (std::size_t i = 0; i < inliers.size (); ++i)
428  // Iterate over each dimension
429  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
430 
431  // Iterate through the 3d points and calculate the distances from them to the plane
432  for (std::size_t i = 0; i < inliers.size (); ++i)
433  {
434  // what i have:
435  // P : Sample Point
436  const Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
437 
438  const Eigen::Vector3d direction = (P - C).normalized();
439 
440  // K : Point on Sphere
441  const Eigen::Vector3d K = C + r * direction;
442 
443  projected_points.points[i].x = static_cast<float> (K[0]);
444  projected_points.points[i].y = static_cast<float> (K[1]);
445  projected_points.points[i].z = static_cast<float> (K[2]);
446  }
447  }
448 }
449 
450 //////////////////////////////////////////////////////////////////////////
451 template <typename PointT> bool
453  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
454 {
455  // Needs a valid model coefficients
456  if (!isModelValid (model_coefficients))
457  {
458  PCL_ERROR ("[pcl::SampleConsensusModelSphere::doSamplesVerifyModel] Given model is invalid!\n");
459  return (false);
460  }
461 
462  const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
463  const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
464  const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
465  for (const auto &index : indices)
466  {
467  // To avoid sqrt computation: consider one larger sphere (radius + threshold) and one smaller sphere (radius - threshold).
468  // Valid if point is in larger sphere, but not in smaller sphere.
469  const float sqr_dist = ((*input_)[index].getVector3fMap () - center).squaredNorm ();
470  if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
471  {
472  return (false);
473  }
474  }
475 
476  return (true);
477 }
478 
479 #define PCL_INSTANTIATE_SampleConsensusModelSphere(T) template class PCL_EXPORTS pcl::SampleConsensusModelSphere<T>;
480 
481 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
482 
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:392
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:395
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.
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.
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 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...
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.
@ K
Definition: norms.h:54
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
Helper functor structure for concatenate.
Definition: concatenate.h:50