39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
42 #include <pcl/sample_consensus/eigen.h>
43 #include <pcl/sample_consensus/sac_model_circle3d.h>
44 #include <pcl/common/concatenate.h>
47 template <
typename Po
intT>
bool
49 const std::vector<int> &samples)
const
52 Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
53 Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
54 Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
60 return (p1.dot (p2) < 0.000001);
64 template <
typename Po
intT>
bool
68 if (samples.size () != 3)
70 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
74 model_coefficients.resize (7);
76 Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
77 Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
78 Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
81 Eigen::Vector3d helper_vec01 = p0 - p1;
82 Eigen::Vector3d helper_vec02 = p0 - p2;
83 Eigen::Vector3d helper_vec10 = p1 - p0;
84 Eigen::Vector3d helper_vec12 = p1 - p2;
85 Eigen::Vector3d helper_vec20 = p2 - p0;
86 Eigen::Vector3d helper_vec21 = p2 - p1;
88 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
90 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
92 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
93 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
94 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
96 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
98 Eigen::Vector3d circle_radiusVector = circle_center - p0;
99 double circle_radius = circle_radiusVector.norm ();
100 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
102 model_coefficients[0] = static_cast<float> (circle_center[0]);
103 model_coefficients[1] = static_cast<float> (circle_center[1]);
104 model_coefficients[2] = static_cast<float> (circle_center[2]);
105 model_coefficients[3] = static_cast<float> (circle_radius);
106 model_coefficients[4] = static_cast<float> (circle_normal[0]);
107 model_coefficients[5] = static_cast<float> (circle_normal[1]);
108 model_coefficients[6] = static_cast<float> (circle_normal[2]);
114 template <
typename Po
intT>
void
118 if (!isModelValid (model_coefficients))
123 distances.resize (indices_->size ());
126 for (std::size_t i = 0; i < indices_->size (); ++i)
136 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
138 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
140 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
142 double r = model_coefficients[3];
144 Eigen::Vector3d helper_vectorPC = P - C;
146 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
149 Eigen::Vector3d P_proj = P + lambda * N;
150 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
153 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
154 Eigen::Vector3d distanceVector = P -
K;
156 distances[i] = distanceVector.norm ();
161 template <
typename Po
intT>
void
163 const Eigen::VectorXf &model_coefficients,
const double threshold,
164 std::vector<int> &inliers)
167 if (!isModelValid (model_coefficients))
173 inliers.resize (indices_->size ());
176 for (std::size_t i = 0; i < indices_->size (); ++i)
180 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
182 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
184 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
186 double r = model_coefficients[3];
188 Eigen::Vector3d helper_vectorPC = P - C;
190 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
192 Eigen::Vector3d P_proj = P + lambda * N;
193 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
196 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
197 Eigen::Vector3d distanceVector = P -
K;
199 if (distanceVector.norm () < threshold)
202 inliers[nr_p] = (*indices_)[i];
206 inliers.resize (nr_p);
210 template <
typename Po
intT> std::size_t
212 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
215 if (!isModelValid (model_coefficients))
217 std::size_t nr_p = 0;
220 for (std::size_t i = 0; i < indices_->size (); ++i)
224 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
226 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
228 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
230 double r = model_coefficients[3];
232 Eigen::Vector3d helper_vectorPC = P - C;
234 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
237 Eigen::Vector3d P_proj = P + lambda * N;
238 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
241 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
242 Eigen::Vector3d distanceVector = P -
K;
244 if (distanceVector.norm () < threshold)
251 template <
typename Po
intT>
void
253 const std::vector<int> &inliers,
254 const Eigen::VectorXf &model_coefficients,
255 Eigen::VectorXf &optimized_coefficients)
const
257 optimized_coefficients = model_coefficients;
260 if (model_coefficients.size () != 7)
262 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
267 if (inliers.size () <= 3)
269 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers found to support a model (%lu)! Returning the same coefficients.\n", inliers.size ());
273 OptimizationFunctor functor (
this, inliers);
274 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
275 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
276 Eigen::VectorXd coeff;
277 int info = lm.minimize (coeff);
278 for (Eigen::Index i = 0; i < coeff.size (); ++i)
279 optimized_coefficients[i] = static_cast<float> (coeff[i]);
282 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",
283 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]);
287 template <
typename Po
intT>
void
289 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients,
290 PointCloud &projected_points,
bool copy_data_fields)
const
293 if (model_coefficients.size () != 7)
295 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
299 projected_points.
header = input_->header;
300 projected_points.
is_dense = input_->is_dense;
303 if (copy_data_fields)
306 projected_points.
points.resize (input_->points.size ());
307 projected_points.
width = input_->width;
308 projected_points.
height = input_->height;
312 for (std::size_t i = 0; i < projected_points.
points.size (); ++i)
317 for (std::size_t i = 0; i < inliers.size (); ++i)
321 Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
323 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
325 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
327 double r = model_coefficients[3];
329 Eigen::Vector3d helper_vectorPC = P - C;
332 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
334 Eigen::Vector3d P_proj = P + lambda * N;
335 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
338 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
340 projected_points.
points[i].x = static_cast<float> (
K[0]);
341 projected_points.
points[i].y = static_cast<float> (
K[1]);
342 projected_points.
points[i].z = static_cast<float> (
K[2]);
348 projected_points.
points.resize (inliers.size ());
349 projected_points.
width = std::uint32_t (inliers.size ());
350 projected_points.
height = 1;
354 for (std::size_t i = 0; i < inliers.size (); ++i)
359 for (std::size_t i = 0; i < inliers.size (); ++i)
363 Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
365 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
367 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
369 double r = model_coefficients[3];
371 Eigen::Vector3d helper_vectorPC = P - C;
373 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
375 Eigen::Vector3d P_proj = P + lambda * N;
376 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
379 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
381 projected_points.
points[i].x = static_cast<float> (
K[0]);
382 projected_points.
points[i].y = static_cast<float> (
K[1]);
383 projected_points.
points[i].z = static_cast<float> (
K[2]);
389 template <
typename Po
intT>
bool
391 const std::set<int> &indices,
392 const Eigen::VectorXf &model_coefficients,
393 const double threshold)
const
396 if (model_coefficients.size () != 7)
398 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
402 for (
const int &index : indices)
409 Eigen::Vector3d P (input_->points[index].x, input_->points[index].y, input_->points[index].z);
411 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
413 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
415 double r = model_coefficients[3];
416 Eigen::Vector3d helper_vectorPC = P - C;
418 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
420 Eigen::Vector3d P_proj = P + lambda * N;
421 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
424 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
425 Eigen::Vector3d distanceVector = P -
K;
427 if (distanceVector.norm () > threshold)
434 template <
typename Po
intT>
bool
440 if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
442 if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
448 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
450 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_