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>
46 #include <pcl/common/concatenate.h>
49 template <
typename Po
intT,
typename Po
intNT>
bool
52 if (samples.size () != sample_size_)
54 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
61 template <
typename Po
intT,
typename Po
intNT>
bool
63 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
66 if (samples.size () != sample_size_)
68 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
74 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::computeModelCoefficients] No input dataset containing normals was given!\n");
78 if (std::abs (input_->points[samples[0]].x - input_->points[samples[1]].x) <= std::numeric_limits<float>::epsilon () &&
79 std::abs (input_->points[samples[0]].y - input_->points[samples[1]].y) <= std::numeric_limits<float>::epsilon () &&
80 std::abs (input_->points[samples[0]].z - input_->points[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
85 Eigen::Vector4f p1 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z, 0.0f);
86 Eigen::Vector4f p2 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z, 0.0f);
88 Eigen::Vector4f n1 (normals_->points[samples[0]].normal[0], normals_->points[samples[0]].normal[1], normals_->points[samples[0]].normal[2], 0.0f);
89 Eigen::Vector4f n2 (normals_->points[samples[1]].normal[0], normals_->points[samples[1]].normal[1], normals_->points[samples[1]].normal[2], 0.0f);
90 Eigen::Vector4f w = n1 + p1 - p2;
92 float a = n1.dot (n1);
93 float b = n1.dot (n2);
94 float c = n2.dot (n2);
97 float denominator = a*c - b*b;
100 if (denominator < 1e-8)
103 tc = (b > c ? d / b : e / c);
107 sc = (b*e - c*d) / denominator;
108 tc = (a*e - b*d) / denominator;
112 Eigen::Vector4f line_pt = p1 + n1 + sc * n1;
113 Eigen::Vector4f line_dir = p2 + tc * n2 - line_pt;
114 line_dir.normalize ();
116 model_coefficients.resize (model_size_);
118 model_coefficients[0] = line_pt[0];
119 model_coefficients[1] = line_pt[1];
120 model_coefficients[2] = line_pt[2];
122 model_coefficients[3] = line_dir[0];
123 model_coefficients[4] = line_dir[1];
124 model_coefficients[5] = line_dir[2];
128 if (model_coefficients[6] > radius_max_ || model_coefficients[6] < radius_min_)
135 template <
typename Po
intT,
typename Po
intNT>
void
137 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
140 if (!isModelValid (model_coefficients))
146 distances.resize (indices_->size ());
148 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
149 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
150 float ptdotdir = line_pt.dot (line_dir);
151 float dirdotdir = 1.0f / line_dir.dot (line_dir);
153 for (std::size_t i = 0; i < indices_->size (); ++i)
158 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0.0f);
159 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0.0f);
161 double d_euclid = 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 double d_normal = std::abs (
getAngle3D (n, dir));
171 d_normal = (std::min) (d_normal, M_PI - d_normal);
173 distances[i] = std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid);
178 template <
typename Po
intT,
typename Po
intNT>
void
180 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
183 if (!isModelValid (model_coefficients))
190 error_sqr_dists_.clear ();
191 inliers.reserve (indices_->size ());
192 error_sqr_dists_.reserve (indices_->size ());
194 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
195 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
196 float ptdotdir = line_pt.dot (line_dir);
197 float dirdotdir = 1.0f / line_dir.dot (line_dir);
199 for (std::size_t i = 0; i < indices_->size (); ++i)
203 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0.0f);
204 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0.0f);
205 double d_euclid = std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
208 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
209 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
210 Eigen::Vector4f dir = pt - pt_proj;
214 double d_normal = std::abs (
getAngle3D (n, dir));
215 d_normal = (std::min) (d_normal, M_PI - d_normal);
217 double distance = std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid);
218 if (distance < threshold)
221 inliers.push_back ((*indices_)[i]);
222 error_sqr_dists_.push_back (distance);
228 template <
typename Po
intT,
typename Po
intNT> std::size_t
230 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
233 if (!isModelValid (model_coefficients))
236 std::size_t nr_p = 0;
238 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
239 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
240 float ptdotdir = line_pt.dot (line_dir);
241 float dirdotdir = 1.0f / line_dir.dot (line_dir);
243 for (std::size_t i = 0; i < indices_->size (); ++i)
247 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0.0f);
248 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0.0f);
249 double d_euclid = std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
252 float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
253 Eigen::Vector4f pt_proj = line_pt + k * line_dir;
254 Eigen::Vector4f dir = pt - pt_proj;
258 double d_normal = std::abs (
getAngle3D (n, dir));
259 d_normal = (std::min) (d_normal, M_PI - d_normal);
261 if (std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid) < threshold)
268 template <
typename Po
intT,
typename Po
intNT>
void
270 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
272 optimized_coefficients = model_coefficients;
275 if (!isModelValid (model_coefficients))
277 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::optimizeModelCoefficients] Given model is invalid!\n");
282 if (inliers.size () <= sample_size_)
284 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
288 OptimizationFunctor functor (
this, inliers);
289 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
290 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
291 int info = lm.minimize (optimized_coefficients);
294 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",
295 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
296 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]);
298 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
299 line_dir.normalize ();
300 optimized_coefficients[3] = line_dir[0];
301 optimized_coefficients[4] = line_dir[1];
302 optimized_coefficients[5] = line_dir[2];
306 template <
typename Po
intT,
typename Po
intNT>
void
308 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
311 if (!isModelValid (model_coefficients))
313 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::projectPoints] Given model is invalid!\n");
317 projected_points.
header = input_->header;
318 projected_points.
is_dense = input_->is_dense;
320 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
321 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
322 float ptdotdir = line_pt.dot (line_dir);
323 float dirdotdir = 1.0f / line_dir.dot (line_dir);
326 if (copy_data_fields)
329 projected_points.
points.resize (input_->points.size ());
330 projected_points.
width = input_->width;
331 projected_points.
height = input_->height;
335 for (std::size_t i = 0; i < projected_points.
points.size (); ++i)
340 for (
const auto &inlier : inliers)
342 Eigen::Vector4f p (input_->points[inlier].x,
343 input_->points[inlier].y,
344 input_->points[inlier].z,
347 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
350 pp.matrix () = line_pt + k * line_dir;
352 Eigen::Vector4f dir = p - pp;
356 pp += dir * model_coefficients[6];
362 projected_points.
points.resize (inliers.size ());
363 projected_points.
width =
static_cast<std::uint32_t
> (inliers.size ());
364 projected_points.
height = 1;
368 for (std::size_t i = 0; i < inliers.size (); ++i)
373 for (std::size_t i = 0; i < inliers.size (); ++i)
378 float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
380 pp.matrix () = line_pt + k * line_dir;
382 Eigen::Vector4f dir = p - pp;
386 pp += dir * model_coefficients[6];
392 template <
typename Po
intT,
typename Po
intNT>
bool
394 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
397 if (!isModelValid (model_coefficients))
399 PCL_ERROR (
"[pcl::SampleConsensusModelCylinder::doSamplesVerifyModel] Given model is invalid!\n");
403 for (
const auto &index : indices)
408 Eigen::Vector4f pt (input_->points[index].x, input_->points[index].y, input_->points[index].z, 0.0f);
409 if (std::abs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]) > threshold)
417 template <
typename Po
intT,
typename Po
intNT>
double
419 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
421 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
422 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
427 template <
typename Po
intT,
typename Po
intNT>
void
429 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj)
const
431 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
432 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
434 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) * line_dir.dot (line_dir);
435 pt_proj = line_pt + k * line_dir;
437 Eigen::Vector4f dir = pt - pt_proj;
441 pt_proj += dir * model_coefficients[6];
445 template <
typename Po
intT,
typename Po
intNT>
bool
452 if (eps_angle_ > 0.0)
455 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
457 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
458 angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
460 if (angle_diff > eps_angle_)
464 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[6] < radius_min_)
466 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[6] > radius_max_)
472 #define PCL_INSTANTIATE_SampleConsensusModelCylinder(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCylinder<PointT, PointNT>;
474 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
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.
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. ...
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
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.
std::vector< index_t > Indices
Type used for indices in PCL.
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 isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
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 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...
std::uint32_t width
The point cloud width (if organized as an image-structure).
SampleConsensusModel represents the base model class.
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) ...
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...
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.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
std::uint32_t height
The point cloud height (if organized as an image-structure).
PointCloud represents the base class in PCL for storing collections of 3D points. ...
pcl::PCLHeader header
The point cloud header.
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, cylinder_radius_R)
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
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.
Helper functor structure for concatenate.
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap