41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
44 #include <unsupported/Eigen/NonLinearOptimization>
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_);
61 std::abs ((*input_)[samples[0]].x - (*input_)[samples[1]].x) <= std::numeric_limits<float>::epsilon ()
63 std::abs ((*input_)[samples[0]].y - (*input_)[samples[1]].y) <= std::numeric_limits<float>::epsilon ()
65 std::abs ((*input_)[samples[0]].z - (*input_)[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
67 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::isSampleGood] The two sample points are (almost) identical!\n");
75 template <
typename Po
intT,
typename Po
intNT>
bool
77 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
80 if (!isSampleGood (samples))
82 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Invalid set of samples given!\n");
88 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] No input dataset containing normals was given!\n");
92 Eigen::Vector4f p1 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z, 0.0f);
93 Eigen::Vector4f p2 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z, 0.0f);
95 Eigen::Vector4f n1 ((*normals_)[samples[0]].normal[0], (*normals_)[samples[0]].normal[1], (*normals_)[samples[0]].normal[2], 0.0f);
96 Eigen::Vector4f n2 ((*normals_)[samples[1]].normal[0], (*normals_)[samples[1]].normal[1], (*normals_)[samples[1]].normal[2], 0.0f);
97 Eigen::Vector4f w = n1 + p1 - p2;
99 float a = n1.dot (n1);
100 float b = n1.dot (n2);
101 float c = n2.dot (n2);
102 float d = n1.dot (w);
103 float e = n2.dot (w);
104 float denominator = a*c - b*b;
107 if (denominator < 1e-8)
110 tc = (b > c ? d / b : e / c);
114 sc = (b*e - c*d) / denominator;
115 tc = (a*e - b*d) / denominator;
119 Eigen::Vector4f line_pt = p1 + n1 + sc * n1;
120 Eigen::Vector4f line_dir = p2 + tc * n2 - line_pt;
121 line_dir.normalize ();
123 model_coefficients.resize (model_size_);
125 model_coefficients[0] = line_pt[0];
126 model_coefficients[1] = line_pt[1];
127 model_coefficients[2] = line_pt[2];
129 model_coefficients[3] = line_dir[0];
130 model_coefficients[4] = line_dir[1];
131 model_coefficients[5] = line_dir[2];
135 if (model_coefficients[6] > radius_max_ || model_coefficients[6] < radius_min_)
138 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
139 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
140 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
145 template <
typename Po
intT,
typename Po
intNT>
void
147 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
150 if (!isModelValid (model_coefficients))
156 distances.resize (indices_->size ());
158 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
159 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
160 float ptdotdir = line_pt.dot (line_dir);
161 float dirdotdir = 1.0f / line_dir.dot (line_dir);
163 for (std::size_t i = 0; i < indices_->size (); ++i)
168 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
170 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
173 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
174 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
175 Eigen::Vector4f dir = pt - pt_proj;
179 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
180 double d_normal = std::abs (
getAngle3D (n, dir));
181 d_normal = (std::min) (d_normal,
M_PI - d_normal);
183 distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
188 template <
typename Po
intT,
typename Po
intNT>
void
190 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
193 if (!isModelValid (model_coefficients))
200 error_sqr_dists_.clear ();
201 inliers.reserve (indices_->size ());
202 error_sqr_dists_.reserve (indices_->size ());
204 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
205 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
206 float ptdotdir = line_pt.dot (line_dir);
207 float dirdotdir = 1.0f / line_dir.dot (line_dir);
209 for (std::size_t i = 0; i < indices_->size (); ++i)
213 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
214 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
215 if (weighted_euclid_dist > threshold)
219 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
220 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
221 Eigen::Vector4f dir = pt - pt_proj;
225 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
226 double d_normal = std::abs (
getAngle3D (n, dir));
227 d_normal = (std::min) (d_normal,
M_PI - d_normal);
229 double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
233 inliers.push_back ((*indices_)[i]);
234 error_sqr_dists_.push_back (
distance);
240 template <
typename Po
intT,
typename Po
intNT> std::size_t
242 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
245 if (!isModelValid (model_coefficients))
248 std::size_t nr_p = 0;
250 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
251 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
252 float ptdotdir = line_pt.dot (line_dir);
253 float dirdotdir = 1.0f / line_dir.dot (line_dir);
255 for (std::size_t i = 0; i < indices_->size (); ++i)
259 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
260 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
261 if (weighted_euclid_dist > threshold)
265 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
266 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
267 Eigen::Vector4f dir = pt - pt_proj;
271 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
272 double d_normal = std::abs (
getAngle3D (n, dir));
273 d_normal = (std::min) (d_normal,
M_PI - d_normal);
275 if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
282 template <
typename Po
intT,
typename Po
intNT>
void
284 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
286 optimized_coefficients = model_coefficients;
289 if (!isModelValid (model_coefficients))
291 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::optimizeModelCoefficients] Given model is invalid!\n");
296 if (inliers.size () <= sample_size_)
298 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
302 OptimizationFunctor functor (
this, inliers);
303 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
304 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
305 int info = lm.minimize (optimized_coefficients);
308 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",
309 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
310 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]);
312 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
313 line_dir.normalize ();
314 optimized_coefficients[3] = line_dir[0];
315 optimized_coefficients[4] = line_dir[1];
316 optimized_coefficients[5] = line_dir[2];
320 template <
typename Po
intT,
typename Po
intNT>
void
322 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
325 if (!isModelValid (model_coefficients))
327 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::projectPoints] Given model is invalid!\n");
331 projected_points.
header = input_->header;
332 projected_points.
is_dense = input_->is_dense;
334 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
335 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
336 float ptdotdir = line_pt.dot (line_dir);
337 float dirdotdir = 1.0f / line_dir.dot (line_dir);
340 if (copy_data_fields)
343 projected_points.
resize (input_->size ());
344 projected_points.
width = input_->width;
345 projected_points.
height = input_->height;
347 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
349 for (std::size_t i = 0; i < projected_points.
size (); ++i)
354 for (
const auto &inlier : inliers)
356 Eigen::Vector4f p ((*input_)[inlier].x,
361 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
364 pp.matrix () = line_pt + k * line_dir;
366 Eigen::Vector4f dir = p - pp;
371 pp += dir * model_coefficients[6];
377 projected_points.
resize (inliers.size ());
378 projected_points.
width = inliers.size ();
379 projected_points.
height = 1;
381 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
383 for (std::size_t i = 0; i < inliers.size (); ++i)
388 for (std::size_t i = 0; i < inliers.size (); ++i)
393 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
395 pp.matrix () = line_pt + k * line_dir;
397 Eigen::Vector4f dir = p - pp;
402 pp += dir * model_coefficients[6];
408 template <
typename Po
intT,
typename Po
intNT>
bool
410 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
413 if (!isModelValid (model_coefficients))
415 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::doSamplesVerifyModel] Given model is invalid!\n");
419 for (
const auto &index : indices)
424 Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
425 if (std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]) > threshold)
433 template <
typename Po
intT,
typename Po
intNT>
double
435 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
437 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
438 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
443 template <
typename Po
intT,
typename Po
intNT>
void
445 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj)
const
447 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
448 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
450 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) * line_dir.dot (line_dir);
451 pt_proj = line_pt + k * line_dir;
453 Eigen::Vector4f dir = pt - pt_proj;
457 pt_proj += dir * model_coefficients[6];
461 template <
typename Po
intT,
typename Po
intNT>
bool
468 if (eps_angle_ > 0.0)
471 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
473 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
474 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
476 if (angle_diff > eps_angle_)
478 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::isModelValid] Angle between cylinder direction and given axis is too large.\n");
483 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[6] < radius_min_)
485 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::isModelValid] Radius is too small: should be larger than %g, but is %g.\n",
486 radius_min_, model_coefficients[6]);
489 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[6] > radius_max_)
491 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::isModelValid] Radius is too big: should be smaller than %g, but is %g.\n",
492 radius_max_, model_coefficients[6]);
499 #define PCL_INSTANTIATE_SampleConsensusModelCylinder(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCylinder<PointT, PointNT>;
PointCloud represents the base class in PCL for storing collections of 3D points.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
void resize(std::size_t count)
Resizes the container to contain count elements.
std::uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
std::uint32_t height
The point cloud height (if organized as an image-structure).
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given cylinder model.
void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const override
Create a new point cloud with inliers projected onto the cylinder model.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the cylinder coefficients using the given inlier set and return them to the user.
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
void projectPointToCylinder(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj) const
Project a point onto a cylinder given by its model coefficients (point_on_axis, axis_direction,...
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given cylinder model coefficients.
double pointToLineDistance(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const
Get the distance from a point to a line (represented by a point and a direction)
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid cylinder model, compute the model coefficients...
SampleConsensusModel represents the base model class.
Define standard C methods and C++ classes that are common to all methods.
double getAngle3D(const Eigen::Vector4f &v1, const Eigen::Vector4f &v2, const bool in_degree=false)
Compute the smallest angle between two 3D vectors in radians (default) or degree.
double sqrPointToLineDistance(const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir)
Get the square distance from a point to a line (represented by a point and a direction)
float distance(const PointT &p1, const PointT &p2)
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
IndicesAllocator<> Indices
Type used for indices in PCL.
Helper functor structure for concatenate.