236template <
typename Po
intT> std::size_t
238 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
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]);
245 for (; i < indices_->size (); ++i)
249 const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
250 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
257#if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
258template <
typename Po
intT> std::size_t
260 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
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]);
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);
270 for (; (i + 4) <= indices_->size (); i += 4)
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));
274 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
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);
287 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
293#if defined (__AVX__) && defined (__AVX2__)
294template <
typename Po
intT> std::size_t
296 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
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]);
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);
306 for (; (i + 8) <= indices_->size (); i += 8)
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));
310 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
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);
331 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
337template <
typename Po
intT>
void
339 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
341 optimized_coefficients = model_coefficients;
344 if (!isModelValid (model_coefficients))
346 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Given model is invalid!\n");
351 if (inliers.size () <= sample_size_)
353 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
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);
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]);
368template <
typename Po
intT>
void
370 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
373 if (!isModelValid (model_coefficients))
375 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::projectPoints] Given model is invalid!\n");
379 projected_points.header = input_->header;
380 projected_points.is_dense = input_->is_dense;
383 const Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
385 const double r = model_coefficients[3];
388 if (copy_data_fields)
391 projected_points.resize (input_->size ());
392 projected_points.width = input_->width;
393 projected_points.height = input_->height;
395 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
397 for (std::size_t i = 0; i < projected_points.points.size (); ++i)
399 pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
402 for (std::size_t i = 0; i < inliers.size (); ++i)
406 const Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
408 const Eigen::Vector3d direction = (P - C).normalized();
411 const Eigen::Vector3d
K = C + r * direction;
413 projected_points.points[inliers[i]].x =
static_cast<float> (
K[0]);
414 projected_points.points[inliers[i]].y =
static_cast<float> (
K[1]);
415 projected_points.points[inliers[i]].z =
static_cast<float> (
K[2]);
421 projected_points.resize (inliers.size ());
422 projected_points.width =
static_cast<uint32_t
> (inliers.size ());
423 projected_points.height = 1;
425 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
427 for (std::size_t i = 0; i < inliers.size (); ++i)
429 pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
432 for (std::size_t i = 0; i < inliers.size (); ++i)
436 const Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
438 const Eigen::Vector3d direction = (P - C).normalized();
441 const Eigen::Vector3d
K = C + r * direction;
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]);
451template <
typename Po
intT>
bool
453 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
456 if (!isModelValid (model_coefficients))
458 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::doSamplesVerifyModel] Given model is invalid!\n");
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)
469 const float sqr_dist = ((*input_)[index].getVector3fMap () - center).squaredNorm ();
470 if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
479#define PCL_INSTANTIATE_SampleConsensusModelSphere(T) template class PCL_EXPORTS pcl::SampleConsensusModelSphere<T>;