41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
44 #include <pcl/sample_consensus/eigen.h>
45 #include <pcl/sample_consensus/sac_model_cylinder.h>
47 #include <pcl/common/concatenate.h>
50 template <
typename Po
intT,
typename Po
intNT>
bool
53 if (samples.size () != sample_size_)
55 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
62 template <
typename Po
intT,
typename Po
intNT>
bool
64 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
67 if (samples.size () != sample_size_)
69 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
75 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] No input dataset containing normals was given!\n");
79 if (std::abs ((*input_)[samples[0]].x - (*input_)[samples[1]].x) <= std::numeric_limits<float>::epsilon () &&
80 std::abs ((*input_)[samples[0]].y - (*input_)[samples[1]].y) <= std::numeric_limits<float>::epsilon () &&
81 std::abs ((*input_)[samples[0]].z - (*input_)[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
86 Eigen::Vector4f p1 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z, 0.0f);
87 Eigen::Vector4f p2 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z, 0.0f);
89 Eigen::Vector4f n1 ((*normals_)[samples[0]].normal[0], (*normals_)[samples[0]].normal[1], (*normals_)[samples[0]].normal[2], 0.0f);
90 Eigen::Vector4f n2 ((*normals_)[samples[1]].normal[0], (*normals_)[samples[1]].normal[1], (*normals_)[samples[1]].normal[2], 0.0f);
91 Eigen::Vector4f w = n1 + p1 - p2;
93 float a = n1.dot (n1);
94 float b = n1.dot (n2);
95 float c = n2.dot (n2);
98 float denominator = a*c - b*b;
101 if (denominator < 1e-8)
104 tc = (b > c ? d / b : e / c);
108 sc = (b*e - c*d) / denominator;
109 tc = (a*e - b*d) / denominator;
113 Eigen::Vector4f line_pt = p1 + n1 + sc * n1;
114 Eigen::Vector4f line_dir = p2 + tc * n2 - line_pt;
115 line_dir.normalize ();
117 model_coefficients.resize (model_size_);
119 model_coefficients[0] = line_pt[0];
120 model_coefficients[1] = line_pt[1];
121 model_coefficients[2] = line_pt[2];
123 model_coefficients[3] = line_dir[0];
124 model_coefficients[4] = line_dir[1];
125 model_coefficients[5] = line_dir[2];
129 if (model_coefficients[6] > radius_max_ || model_coefficients[6] < radius_min_)
136 template <
typename Po
intT,
typename Po
intNT>
void
138 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
141 if (!isModelValid (model_coefficients))
147 distances.resize (indices_->size ());
149 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
150 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
151 float ptdotdir = line_pt.dot (line_dir);
152 float dirdotdir = 1.0f / line_dir.dot (line_dir);
154 for (std::size_t i = 0; i < indices_->size (); ++i)
159 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
161 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
164 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
165 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
166 Eigen::Vector4f dir = pt - pt_proj;
170 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
171 double d_normal = std::abs (
getAngle3D (n, dir));
172 d_normal = (std::min) (d_normal,
M_PI - d_normal);
174 distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
179 template <
typename Po
intT,
typename Po
intNT>
void
181 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
184 if (!isModelValid (model_coefficients))
191 error_sqr_dists_.clear ();
192 inliers.reserve (indices_->size ());
193 error_sqr_dists_.reserve (indices_->size ());
195 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
196 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
197 float ptdotdir = line_pt.dot (line_dir);
198 float dirdotdir = 1.0f / line_dir.dot (line_dir);
200 for (std::size_t i = 0; i < indices_->size (); ++i)
204 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
205 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
206 if (weighted_euclid_dist > threshold)
210 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
211 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
212 Eigen::Vector4f dir = pt - pt_proj;
216 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
217 double d_normal = std::abs (
getAngle3D (n, dir));
218 d_normal = (std::min) (d_normal,
M_PI - d_normal);
220 double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
224 inliers.push_back ((*indices_)[i]);
225 error_sqr_dists_.push_back (
distance);
231 template <
typename Po
intT,
typename Po
intNT> std::size_t
233 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
236 if (!isModelValid (model_coefficients))
239 std::size_t nr_p = 0;
241 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
242 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
243 float ptdotdir = line_pt.dot (line_dir);
244 float dirdotdir = 1.0f / line_dir.dot (line_dir);
246 for (std::size_t i = 0; i < indices_->size (); ++i)
250 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
251 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
252 if (weighted_euclid_dist > threshold)
256 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
257 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
258 Eigen::Vector4f dir = pt - pt_proj;
262 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
263 double d_normal = std::abs (
getAngle3D (n, dir));
264 d_normal = (std::min) (d_normal,
M_PI - d_normal);
266 if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
273 template <
typename Po
intT,
typename Po
intNT>
void
275 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
277 optimized_coefficients = model_coefficients;
280 if (!isModelValid (model_coefficients))
282 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::optimizeModelCoefficients] Given model is invalid!\n");
287 if (inliers.size () <= sample_size_)
289 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
293 OptimizationFunctor functor (
this, inliers);
294 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
295 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
296 int info = lm.minimize (optimized_coefficients);
299 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::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",
300 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
301 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]);
303 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
304 line_dir.normalize ();
305 optimized_coefficients[3] = line_dir[0];
306 optimized_coefficients[4] = line_dir[1];
307 optimized_coefficients[5] = line_dir[2];
311 template <
typename Po
intT,
typename Po
intNT>
void
313 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
316 if (!isModelValid (model_coefficients))
318 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::projectPoints] Given model is invalid!\n");
322 projected_points.
header = input_->header;
323 projected_points.
is_dense = input_->is_dense;
325 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
326 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
327 float ptdotdir = line_pt.dot (line_dir);
328 float dirdotdir = 1.0f / line_dir.dot (line_dir);
331 if (copy_data_fields)
334 projected_points.
resize (input_->size ());
335 projected_points.
width = input_->width;
336 projected_points.
height = input_->height;
338 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
340 for (std::size_t i = 0; i < projected_points.
size (); ++i)
345 for (
const auto &inlier : inliers)
347 Eigen::Vector4f p ((*input_)[inlier].x,
352 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
355 pp.matrix () = line_pt + k * line_dir;
357 Eigen::Vector4f dir = p - pp;
361 pp += dir * model_coefficients[6];
367 projected_points.
resize (inliers.size ());
368 projected_points.
width = inliers.size ();
369 projected_points.
height = 1;
371 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
373 for (std::size_t i = 0; i < inliers.size (); ++i)
378 for (std::size_t i = 0; i < inliers.size (); ++i)
383 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
385 pp.matrix () = line_pt + k * line_dir;
387 Eigen::Vector4f dir = p - pp;
391 pp += dir * model_coefficients[6];
397 template <
typename Po
intT,
typename Po
intNT>
bool
399 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
402 if (!isModelValid (model_coefficients))
404 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::doSamplesVerifyModel] Given model is invalid!\n");
408 for (
const auto &index : indices)
413 Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
414 if (std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]) > threshold)
422 template <
typename Po
intT,
typename Po
intNT>
double
424 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
426 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
427 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
432 template <
typename Po
intT,
typename Po
intNT>
void
434 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj)
const
436 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
437 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
439 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) * line_dir.dot (line_dir);
440 pt_proj = line_pt + k * line_dir;
442 Eigen::Vector4f dir = pt - pt_proj;
446 pt_proj += dir * model_coefficients[6];
450 template <
typename Po
intT,
typename Po
intNT>
bool
457 if (eps_angle_ > 0.0)
460 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
462 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
463 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
465 if (angle_diff > eps_angle_)
469 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[6] < radius_min_)
471 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[6] > radius_max_)
477 #define PCL_INSTANTIATE_SampleConsensusModelCylinder(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCylinder<PointT, PointNT>;
479 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_