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
51 if (samples.size () != sample_size_)
53 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
57 Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
58 Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
59 Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
65 return (p1.dot (p2) < 0.000001);
69 template <
typename Po
intT>
bool
73 if (samples.size () != sample_size_)
75 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
79 model_coefficients.resize (model_size_);
81 Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
82 Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
83 Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
86 Eigen::Vector3d helper_vec01 = p0 - p1;
87 Eigen::Vector3d helper_vec02 = p0 - p2;
88 Eigen::Vector3d helper_vec10 = p1 - p0;
89 Eigen::Vector3d helper_vec12 = p1 - p2;
90 Eigen::Vector3d helper_vec20 = p2 - p0;
91 Eigen::Vector3d helper_vec21 = p2 - p1;
93 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
95 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
97 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
98 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
99 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
101 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
103 Eigen::Vector3d circle_radiusVector = circle_center - p0;
104 double circle_radius = circle_radiusVector.norm ();
105 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
107 model_coefficients[0] =
static_cast<float> (circle_center[0]);
108 model_coefficients[1] =
static_cast<float> (circle_center[1]);
109 model_coefficients[2] =
static_cast<float> (circle_center[2]);
110 model_coefficients[3] =
static_cast<float> (circle_radius);
111 model_coefficients[4] =
static_cast<float> (circle_normal[0]);
112 model_coefficients[5] =
static_cast<float> (circle_normal[1]);
113 model_coefficients[6] =
static_cast<float> (circle_normal[2]);
119 template <
typename Po
intT>
void
123 if (!isModelValid (model_coefficients))
128 distances.resize (indices_->size ());
131 for (std::size_t i = 0; i < indices_->size (); ++i)
141 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
143 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
145 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
147 double r = model_coefficients[3];
149 Eigen::Vector3d helper_vectorPC = P - C;
151 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
154 Eigen::Vector3d P_proj = P + lambda * N;
155 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
158 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
159 Eigen::Vector3d distanceVector = P -
K;
161 distances[i] = distanceVector.norm ();
166 template <
typename Po
intT>
void
168 const Eigen::VectorXf &model_coefficients,
const double threshold,
172 if (!isModelValid (model_coefficients))
178 inliers.reserve (indices_->size ());
181 for (std::size_t i = 0; i < indices_->size (); ++i)
185 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
187 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
189 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
191 double r = model_coefficients[3];
193 Eigen::Vector3d helper_vectorPC = P - C;
195 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
197 Eigen::Vector3d P_proj = P + lambda * N;
198 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
201 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
202 Eigen::Vector3d distanceVector = P -
K;
204 if (distanceVector.norm () < threshold)
207 inliers.push_back ((*indices_)[i]);
213 template <
typename Po
intT> std::size_t
215 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
218 if (!isModelValid (model_coefficients))
220 std::size_t nr_p = 0;
223 for (std::size_t i = 0; i < indices_->size (); ++i)
227 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
229 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
231 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
233 double r = model_coefficients[3];
235 Eigen::Vector3d helper_vectorPC = P - C;
237 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
240 Eigen::Vector3d P_proj = P + lambda * N;
241 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
244 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
245 Eigen::Vector3d distanceVector = P -
K;
247 if (distanceVector.norm () < threshold)
254 template <
typename Po
intT>
void
257 const Eigen::VectorXf &model_coefficients,
258 Eigen::VectorXf &optimized_coefficients)
const
260 optimized_coefficients = model_coefficients;
263 if (!isModelValid (model_coefficients))
265 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
270 if (inliers.size () <= sample_size_)
272 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
276 OptimizationFunctor functor (
this, inliers);
277 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
278 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
279 Eigen::VectorXd coeff;
280 int info = lm.minimize (coeff);
281 for (Eigen::Index i = 0; i < coeff.size (); ++i)
282 optimized_coefficients[i] =
static_cast<float> (coeff[i]);
285 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",
286 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]);
290 template <
typename Po
intT>
void
292 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
293 PointCloud &projected_points,
bool copy_data_fields)
const
296 if (!isModelValid (model_coefficients))
298 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
302 projected_points.
header = input_->header;
303 projected_points.
is_dense = input_->is_dense;
306 if (copy_data_fields)
309 projected_points.
points.resize (input_->points.size ());
310 projected_points.
width = input_->width;
311 projected_points.
height = input_->height;
315 for (std::size_t i = 0; i < projected_points.
points.size (); ++i)
320 for (std::size_t i = 0; i < inliers.size (); ++i)
324 Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
326 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
328 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
330 double r = model_coefficients[3];
332 Eigen::Vector3d helper_vectorPC = P - C;
335 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
337 Eigen::Vector3d P_proj = P + lambda * N;
338 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
341 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
343 projected_points.
points[i].x =
static_cast<float> (
K[0]);
344 projected_points.
points[i].y =
static_cast<float> (
K[1]);
345 projected_points.
points[i].z =
static_cast<float> (
K[2]);
351 projected_points.
points.resize (inliers.size ());
353 projected_points.
height = 1;
357 for (std::size_t i = 0; i < inliers.size (); ++i)
362 for (std::size_t i = 0; i < inliers.size (); ++i)
366 Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
368 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
370 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
372 double r = model_coefficients[3];
374 Eigen::Vector3d helper_vectorPC = P - C;
376 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
378 Eigen::Vector3d P_proj = P + lambda * N;
379 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
382 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
384 projected_points.
points[i].x =
static_cast<float> (
K[0]);
385 projected_points.
points[i].y =
static_cast<float> (
K[1]);
386 projected_points.
points[i].z =
static_cast<float> (
K[2]);
392 template <
typename Po
intT>
bool
394 const std::set<index_t> &indices,
395 const Eigen::VectorXf &model_coefficients,
396 const double threshold)
const
399 if (!isModelValid (model_coefficients))
401 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
405 for (
const auto &index : indices)
412 Eigen::Vector3d P (input_->points[index].x, input_->points[index].y, input_->points[index].z);
414 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
416 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
418 double r = model_coefficients[3];
419 Eigen::Vector3d helper_vectorPC = P - C;
421 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
423 Eigen::Vector3d P_proj = P + lambda * N;
424 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
427 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
428 Eigen::Vector3d distanceVector = P -
K;
430 if (distanceVector.norm () > threshold)
437 template <
typename Po
intT>
bool
443 if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
445 if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
451 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
453 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_