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/sac_model_cylinder.h>
46#include <pcl/common/concatenate.h>
49template <
typename Po
intT,
typename Po
intNT>
bool
54 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (),
sample_size_);
60 std::abs ((*
input_)[samples[0]].x - (*
input_)[samples[1]].x) <= std::numeric_limits<float>::epsilon ()
62 std::abs ((*
input_)[samples[0]].y - (*
input_)[samples[1]].y) <= std::numeric_limits<float>::epsilon ()
64 std::abs ((*
input_)[samples[0]].z - (*
input_)[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
66 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::isSampleGood] The two sample points are (almost) identical!\n");
74template <
typename Po
intT,
typename Po
intNT>
bool
76 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
81 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Invalid set of samples given!\n");
87 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] No input dataset containing normals was given! Use setInputNormals\n");
91 Eigen::Vector4f p1 ((*
input_)[samples[0]].x, (*
input_)[samples[0]].y, (*
input_)[samples[0]].z, 0.0f);
92 Eigen::Vector4f p2 ((*
input_)[samples[1]].x, (*
input_)[samples[1]].y, (*
input_)[samples[1]].z, 0.0f);
94 Eigen::Vector4f n1 ((*
normals_)[samples[0]].normal[0], (*
normals_)[samples[0]].normal[1], (*
normals_)[samples[0]].normal[2], 0.0f);
95 Eigen::Vector4f n2 ((*
normals_)[samples[1]].normal[0], (*
normals_)[samples[1]].normal[1], (*
normals_)[samples[1]].normal[2], 0.0f);
96 Eigen::Vector4f w = n1 + p1 - p2;
98 float a = n1.dot (n1);
99 float b = n1.dot (n2);
100 float c = n2.dot (n2);
101 float d = n1.dot (w);
102 float e = n2.dot (w);
103 float denominator = a*c - b*b;
106 if (denominator < 1e-8)
109 tc = (b > c ? d / b : e / c);
113 sc = (b*e - c*d) / denominator;
114 tc = (a*e - b*d) / denominator;
118 Eigen::Vector4f line_pt = p1 + n1 + sc * n1;
119 Eigen::Vector4f line_dir = p2 + tc * n2 - line_pt;
120 line_dir.normalize ();
124 model_coefficients[0] = line_pt[0];
125 model_coefficients[1] = line_pt[1];
126 model_coefficients[2] = line_pt[2];
128 model_coefficients[3] = line_dir[0];
129 model_coefficients[4] = line_dir[1];
130 model_coefficients[5] = line_dir[2];
137 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
138 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
139 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
144template <
typename Po
intT,
typename Po
intNT>
void
146 const Eigen::VectorXf &model_coefficients, std::vector<double> &
distances)
const
157 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
158 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
159 float ptdotdir = line_pt.dot (line_dir);
160 float dirdotdir = 1.0f / line_dir.dot (line_dir);
162 for (std::size_t i = 0; i <
indices_->size (); ++i)
172 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
173 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
174 Eigen::Vector4f dir = pt - pt_proj;
179 double d_normal = std::abs (
getAngle3D (n, dir));
180 d_normal = (std::min) (d_normal,
M_PI - d_normal);
187template <
typename Po
intT,
typename Po
intNT>
void
189 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
200 inliers.reserve (
indices_->size ());
203 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
204 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
205 float ptdotdir = line_pt.dot (line_dir);
206 float dirdotdir = 1.0f / line_dir.dot (line_dir);
208 for (std::size_t i = 0; i <
indices_->size (); ++i)
214 if (weighted_euclid_dist > threshold)
218 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
219 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
220 Eigen::Vector4f dir = pt - pt_proj;
225 double d_normal = std::abs (
getAngle3D (n, dir));
226 d_normal = (std::min) (d_normal,
M_PI - d_normal);
229 if (distance < threshold)
239template <
typename Po
intT,
typename Po
intNT> std::size_t
241 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
247 std::size_t nr_p = 0;
249 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
250 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
251 float ptdotdir = line_pt.dot (line_dir);
252 float dirdotdir = 1.0f / line_dir.dot (line_dir);
254 for (std::size_t i = 0; i <
indices_->size (); ++i)
260 if (weighted_euclid_dist > threshold)
264 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
265 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
266 Eigen::Vector4f dir = pt - pt_proj;
271 double d_normal = std::abs (
getAngle3D (n, dir));
272 d_normal = (std::min) (d_normal,
M_PI - d_normal);
281template <
typename Po
intT,
typename Po
intNT>
void
283 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
285 optimized_coefficients = model_coefficients;
290 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::optimizeModelCoefficients] Given model is invalid!\n");
297 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
301 Eigen::ArrayXf pts_x(inliers.size());
302 Eigen::ArrayXf pts_y(inliers.size());
303 Eigen::ArrayXf pts_z(inliers.size());
305 for(
const auto& index : inliers) {
306 pts_x[pos] = (*input_)[index].x;
307 pts_y[pos] = (*input_)[index].y;
308 pts_z[pos] = (*input_)[index].z;
313 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::optimizeModelCoefficients] Initial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
314 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
315 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]);
317 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
318 line_dir.normalize ();
319 optimized_coefficients[3] = line_dir[0];
320 optimized_coefficients[4] = line_dir[1];
321 optimized_coefficients[5] = line_dir[2];
325template <
typename Po
intT,
typename Po
intNT>
void
327 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
332 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::projectPoints] Given model is invalid!\n");
336 projected_points.header =
input_->header;
337 projected_points.is_dense =
input_->is_dense;
339 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
340 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
341 float ptdotdir = line_pt.dot (line_dir);
342 float dirdotdir = 1.0f / line_dir.dot (line_dir);
345 if (copy_data_fields)
348 projected_points.resize (
input_->size ());
349 projected_points.width =
input_->width;
350 projected_points.height =
input_->height;
352 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
354 for (std::size_t i = 0; i < projected_points.size (); ++i)
359 for (
const auto &inlier : inliers)
361 Eigen::Vector4f p ((*
input_)[inlier].x,
366 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
369 pp.matrix () = line_pt + k * line_dir;
371 Eigen::Vector4f dir = p - pp;
376 pp += dir * model_coefficients[6];
382 projected_points.resize (inliers.size ());
383 projected_points.width = inliers.size ();
384 projected_points.height = 1;
386 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
388 for (std::size_t i = 0; i < inliers.size (); ++i)
393 for (std::size_t i = 0; i < inliers.size (); ++i)
398 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
400 pp.matrix () = line_pt + k * line_dir;
402 Eigen::Vector4f dir = p - pp;
407 pp += dir * model_coefficients[6];
413template <
typename Po
intT,
typename Po
intNT>
bool
415 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
420 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::doSamplesVerifyModel] Given model is invalid!\n");
424 for (
const auto &index : indices)
429 Eigen::Vector4f pt ((*
input_)[index].x, (*
input_)[index].y, (*
input_)[index].z, 0.0f);
430 if (std::abs (
pointToLineDistance (pt, model_coefficients) - model_coefficients[6]) > threshold)
438template <
typename Po
intT,
typename Po
intNT>
double
440 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
442 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
443 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
448template <
typename Po
intT,
typename Po
intNT>
void
450 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj)
const
452 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
453 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
455 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
456 pt_proj = line_pt + k * line_dir;
458 Eigen::Vector4f dir = pt - pt_proj;
462 pt_proj += dir * model_coefficients[6];
466template <
typename Po
intT,
typename Po
intNT>
bool
473 if (eps_angle_ > 0.0)
476 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
478 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
479 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
481 if (angle_diff > eps_angle_)
483 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::isModelValid] Angle between cylinder direction and given axis is too large.\n");
490 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::isModelValid] Radius is too small: should be larger than %g, but is %g.\n",
496 PCL_DEBUG (
"[pcl::SampleConsensusModelCylinder::isModelValid] Radius is too big: should be smaller than %g, but is %g.\n",
504#define PCL_INSTANTIATE_SampleConsensusModelCylinder(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCylinder<PointT, PointNT>;
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.
typename SampleConsensusModel< PointT >::PointCloud PointCloud
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...
PointCloudNConstPtr normals_
A pointer to the input dataset that contains the point normals of the XYZ dataset.
double normal_distance_weight_
The relative weight (between 0 and 1) to give to the angular distance (0 to pi/2) between point norma...
double radius_min_
The minimum and maximum radius limits for the model.
unsigned int sample_size_
The size of a sample from which the model is computed.
IndicesPtr indices_
A pointer to the vector of point indices to use.
PointCloudConstPtr input_
A boost shared pointer to the point cloud data array.
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
unsigned int model_size_
The number of coefficients in the model.
std::vector< double > error_sqr_dists_
A vector holding the distances to the computed model.
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)
PCL_EXPORTS int optimizeModelCoefficientsCylinder(Eigen::VectorXf &coeff, const Eigen::ArrayXf &pts_x, const Eigen::ArrayXf &pts_y, const Eigen::ArrayXf &pts_z)
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
IndicesAllocator<> Indices
Type used for indices in PCL.
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst