235 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
238 if (!isModelValid (model_coefficients))
240 std::size_t nr_p = 0;
242 const auto squared_threshold = threshold * threshold;
244 for (std::size_t i = 0; i < indices_->size (); ++i)
248 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
250 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
252 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
254 double r = model_coefficients[3];
256 Eigen::Vector3d helper_vectorPC = P - C;
258 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
261 Eigen::Vector3d P_proj = P + lambda * N;
262 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
265 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
266 Eigen::Vector3d distanceVector = P -
K;
268 if (distanceVector.squaredNorm () < squared_threshold)
278 const Eigen::VectorXf &model_coefficients,
279 Eigen::VectorXf &optimized_coefficients)
const
281 optimized_coefficients = model_coefficients;
284 if (!isModelValid (model_coefficients))
286 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
291 if (inliers.size () <= sample_size_)
293 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
297 OptimizationFunctor functor (
this, inliers);
298 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
299 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
300 Eigen::VectorXd coeff;
301 int info = lm.minimize (coeff);
302 for (Eigen::Index i = 0; i < coeff.size (); ++i)
303 optimized_coefficients[i] =
static_cast<float> (coeff[i]);
306 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
307 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], model_coefficients[4], model_coefficients[5], model_coefficients[6], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5], optimized_coefficients[6]);
313 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
314 PointCloud &projected_points,
bool copy_data_fields)
const
317 if (!isModelValid (model_coefficients))
319 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
323 projected_points.header = input_->header;
324 projected_points.is_dense = input_->is_dense;
327 if (copy_data_fields)
330 projected_points.resize (input_->size ());
331 projected_points.width = input_->width;
332 projected_points.height = input_->height;
334 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
336 for (std::size_t i = 0; i < projected_points.size (); ++i)
338 pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[i], projected_points[i]));
341 for (std::size_t i = 0; i < inliers.size (); ++i)
345 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
347 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
349 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
351 double r = model_coefficients[3];
353 Eigen::Vector3d helper_vectorPC = P - C;
356 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
358 Eigen::Vector3d P_proj = P + lambda * N;
359 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
362 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
364 projected_points[i].x =
static_cast<float> (
K[0]);
365 projected_points[i].y =
static_cast<float> (
K[1]);
366 projected_points[i].z =
static_cast<float> (
K[2]);
372 projected_points.resize (inliers.size ());
373 projected_points.width = inliers.size ();
374 projected_points.height = 1;
376 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
378 for (std::size_t i = 0; i < inliers.size (); ++i)
380 pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[inliers[i]], projected_points[i]));
383 for (std::size_t i = 0; i < inliers.size (); ++i)
387 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
389 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
391 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
393 double r = model_coefficients[3];
395 Eigen::Vector3d helper_vectorPC = P - C;
397 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
399 Eigen::Vector3d P_proj = P + lambda * N;
400 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
403 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
405 projected_points[i].x =
static_cast<float> (
K[0]);
406 projected_points[i].y =
static_cast<float> (
K[1]);
407 projected_points[i].z =
static_cast<float> (
K[2]);
415 const std::set<index_t> &indices,
416 const Eigen::VectorXf &model_coefficients,
417 const double threshold)
const
420 if (!isModelValid (model_coefficients))
422 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
426 const auto squared_threshold = threshold * threshold;
427 for (
const auto &index : indices)
434 Eigen::Vector3d P ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z);
436 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
438 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
440 double r = model_coefficients[3];
441 Eigen::Vector3d helper_vectorPC = P - C;
443 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
445 Eigen::Vector3d P_proj = P + lambda * N;
446 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
449 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
450 Eigen::Vector3d distanceVector = P -
K;
452 if (distanceVector.squaredNorm () > squared_threshold)