39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
44 #include <pcl/sample_consensus/eigen.h>
45 #include <pcl/sample_consensus/sac_model_circle3d.h>
46 #include <pcl/common/concatenate.h>
49 template <
typename Po
intT>
bool
53 if (samples.size () != sample_size_)
55 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
59 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
60 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
61 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
67 return (p1.dot (p2) < 0.000001);
71 template <
typename Po
intT>
bool
75 if (samples.size () != sample_size_)
77 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
81 model_coefficients.resize (model_size_);
83 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
84 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
85 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
88 Eigen::Vector3d helper_vec01 = p0 - p1;
89 Eigen::Vector3d helper_vec02 = p0 - p2;
90 Eigen::Vector3d helper_vec10 = p1 - p0;
91 Eigen::Vector3d helper_vec12 = p1 - p2;
92 Eigen::Vector3d helper_vec20 = p2 - p0;
93 Eigen::Vector3d helper_vec21 = p2 - p1;
95 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
97 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
99 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
100 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
101 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
103 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
105 Eigen::Vector3d circle_radiusVector = circle_center - p0;
106 double circle_radius = circle_radiusVector.norm ();
107 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
109 model_coefficients[0] =
static_cast<float> (circle_center[0]);
110 model_coefficients[1] =
static_cast<float> (circle_center[1]);
111 model_coefficients[2] =
static_cast<float> (circle_center[2]);
112 model_coefficients[3] =
static_cast<float> (circle_radius);
113 model_coefficients[4] =
static_cast<float> (circle_normal[0]);
114 model_coefficients[5] =
static_cast<float> (circle_normal[1]);
115 model_coefficients[6] =
static_cast<float> (circle_normal[2]);
121 template <
typename Po
intT>
void
125 if (!isModelValid (model_coefficients))
130 distances.resize (indices_->size ());
133 for (std::size_t i = 0; i < indices_->size (); ++i)
143 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
145 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
147 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
149 double r = model_coefficients[3];
151 Eigen::Vector3d helper_vectorPC = P - C;
153 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
156 Eigen::Vector3d P_proj = P + lambda * N;
157 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
160 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
161 Eigen::Vector3d distanceVector = P -
K;
163 distances[i] = distanceVector.norm ();
168 template <
typename Po
intT>
void
170 const Eigen::VectorXf &model_coefficients,
const double threshold,
174 if (!isModelValid (model_coefficients))
180 inliers.reserve (indices_->size ());
182 const auto squared_threshold = threshold * threshold;
184 for (std::size_t i = 0; i < indices_->size (); ++i)
188 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
190 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
192 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
194 double r = model_coefficients[3];
196 Eigen::Vector3d helper_vectorPC = P - C;
198 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
200 Eigen::Vector3d P_proj = P + lambda * N;
201 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
204 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
205 Eigen::Vector3d distanceVector = P -
K;
207 if (distanceVector.squaredNorm () < squared_threshold)
210 inliers.push_back ((*indices_)[i]);
216 template <
typename Po
intT> std::size_t
218 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
221 if (!isModelValid (model_coefficients))
223 std::size_t nr_p = 0;
225 const auto squared_threshold = threshold * threshold;
227 for (std::size_t i = 0; i < indices_->size (); ++i)
231 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
233 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
235 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
237 double r = model_coefficients[3];
239 Eigen::Vector3d helper_vectorPC = P - C;
241 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
244 Eigen::Vector3d P_proj = P + lambda * N;
245 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
248 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
249 Eigen::Vector3d distanceVector = P -
K;
251 if (distanceVector.squaredNorm () < squared_threshold)
258 template <
typename Po
intT>
void
261 const Eigen::VectorXf &model_coefficients,
262 Eigen::VectorXf &optimized_coefficients)
const
264 optimized_coefficients = model_coefficients;
267 if (!isModelValid (model_coefficients))
269 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
274 if (inliers.size () <= sample_size_)
276 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
280 OptimizationFunctor functor (
this, inliers);
281 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
282 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
283 Eigen::VectorXd coeff;
284 int info = lm.minimize (coeff);
285 for (Eigen::Index i = 0; i < coeff.size (); ++i)
286 optimized_coefficients[i] =
static_cast<float> (coeff[i]);
289 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",
290 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]);
294 template <
typename Po
intT>
void
296 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
297 PointCloud &projected_points,
bool copy_data_fields)
const
300 if (!isModelValid (model_coefficients))
302 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
306 projected_points.
header = input_->header;
307 projected_points.
is_dense = input_->is_dense;
310 if (copy_data_fields)
313 projected_points.
resize (input_->size ());
314 projected_points.
width = input_->width;
315 projected_points.
height = input_->height;
317 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
319 for (std::size_t i = 0; i < projected_points.
size (); ++i)
324 for (std::size_t i = 0; i < inliers.size (); ++i)
328 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
330 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
332 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
334 double r = model_coefficients[3];
336 Eigen::Vector3d helper_vectorPC = P - C;
339 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
341 Eigen::Vector3d P_proj = P + lambda * N;
342 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
345 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
347 projected_points[i].x =
static_cast<float> (
K[0]);
348 projected_points[i].y =
static_cast<float> (
K[1]);
349 projected_points[i].z =
static_cast<float> (
K[2]);
355 projected_points.
resize (inliers.size ());
356 projected_points.
width = inliers.size ();
357 projected_points.
height = 1;
359 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
361 for (std::size_t i = 0; i < inliers.size (); ++i)
366 for (std::size_t i = 0; i < inliers.size (); ++i)
370 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
372 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
374 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
376 double r = model_coefficients[3];
378 Eigen::Vector3d helper_vectorPC = P - C;
380 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
382 Eigen::Vector3d P_proj = P + lambda * N;
383 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
386 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
388 projected_points[i].x =
static_cast<float> (
K[0]);
389 projected_points[i].y =
static_cast<float> (
K[1]);
390 projected_points[i].z =
static_cast<float> (
K[2]);
396 template <
typename Po
intT>
bool
398 const std::set<index_t> &indices,
399 const Eigen::VectorXf &model_coefficients,
400 const double threshold)
const
403 if (!isModelValid (model_coefficients))
405 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
409 const auto squared_threshold = threshold * threshold;
410 for (
const auto &index : indices)
417 Eigen::Vector3d P ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z);
419 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
421 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
423 double r = model_coefficients[3];
424 Eigen::Vector3d helper_vectorPC = P - C;
426 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
428 Eigen::Vector3d P_proj = P + lambda * N;
429 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
432 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
433 Eigen::Vector3d distanceVector = P -
K;
435 if (distanceVector.squaredNorm () > squared_threshold)
442 template <
typename Po
intT>
bool
448 if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
450 if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
456 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
458 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_