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