39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
42 #include <unsupported/Eigen/NonLinearOptimization>
43 #include <pcl/sample_consensus/sac_model_cone.h>
45 #include <pcl/common/concatenate.h>
48 template <
typename Po
intT,
typename Po
intNT>
bool
51 if (samples.size () != sample_size_)
53 PCL_ERROR (
"[pcl::SampleConsensusModelCone::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
60 template <
typename Po
intT,
typename Po
intNT>
bool
62 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
65 if (samples.size () != sample_size_)
67 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
73 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given!\n");
77 Eigen::Vector4f p1 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z, 0.0f);
78 Eigen::Vector4f p2 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z, 0.0f);
79 Eigen::Vector4f p3 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z, 0.0f);
81 Eigen::Vector4f n1 ((*normals_)[samples[0]].normal[0], (*normals_)[samples[0]].normal[1], (*normals_)[samples[0]].normal[2], 0.0f);
82 Eigen::Vector4f n2 ((*normals_)[samples[1]].normal[0], (*normals_)[samples[1]].normal[1], (*normals_)[samples[1]].normal[2], 0.0f);
83 Eigen::Vector4f n3 ((*normals_)[samples[2]].normal[0], (*normals_)[samples[2]].normal[1], (*normals_)[samples[2]].normal[2], 0.0f);
86 Eigen::Vector4f ortho12 = n1.cross3(n2);
87 Eigen::Vector4f ortho23 = n2.cross3(n3);
88 Eigen::Vector4f ortho31 = n3.cross3(n1);
90 float denominator = n1.dot(ortho23);
92 float d1 = p1.dot (n1);
93 float d2 = p2.dot (n2);
94 float d3 = p3.dot (n3);
96 Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
99 Eigen::Vector4f ap1 = p1 - apex;
100 Eigen::Vector4f ap2 = p2 - apex;
101 Eigen::Vector4f ap3 = p3 - apex;
103 Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
104 Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
105 Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
107 Eigen::Vector4f np1np2 = np2 - np1;
108 Eigen::Vector4f np1np3 = np3 - np1;
110 Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
111 axis_dir.normalize ();
119 float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
121 model_coefficients.resize (model_size_);
123 model_coefficients[0] = apex[0];
124 model_coefficients[1] = apex[1];
125 model_coefficients[2] = apex[2];
127 model_coefficients[3] = axis_dir[0];
128 model_coefficients[4] = axis_dir[1];
129 model_coefficients[5] = axis_dir[2];
131 model_coefficients[6] = opening_angle;
133 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
135 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
138 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::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 apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
159 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
160 float opening_angle = model_coefficients[6];
162 float apexdotdir = apex.dot (axis_dir);
163 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
165 for (std::size_t i = 0; i < indices_->size (); ++i)
167 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
170 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
171 Eigen::Vector4f pt_proj = apex + k * axis_dir;
174 Eigen::Vector4f height = apex - pt_proj;
175 float actual_cone_radius = tanf (opening_angle) * height.norm ();
179 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
182 Eigen::Vector4f dir = pt - pt_proj;
187 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * dir;
190 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
191 double d_normal = std::abs (
getAngle3D (n, cone_normal));
192 d_normal = (std::min) (d_normal,
M_PI - d_normal);
194 distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
199 template <
typename Po
intT,
typename Po
intNT>
void
201 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
204 if (!isModelValid (model_coefficients))
211 error_sqr_dists_.clear ();
212 inliers.reserve (indices_->size ());
213 error_sqr_dists_.reserve (indices_->size ());
215 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
216 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
217 float opening_angle = model_coefficients[6];
219 float apexdotdir = apex.dot (axis_dir);
220 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
222 for (std::size_t i = 0; i < indices_->size (); ++i)
224 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
227 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
228 Eigen::Vector4f pt_proj = apex + k * axis_dir;
231 Eigen::Vector4f height = apex - pt_proj;
232 double actual_cone_radius = tan(opening_angle) * height.norm ();
236 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
237 if (weighted_euclid_dist > threshold)
241 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
242 pp_pt_dir.normalize ();
246 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * pp_pt_dir;
249 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
250 double d_normal = std::abs (
getAngle3D (n, cone_normal));
251 d_normal = (std::min) (d_normal,
M_PI - d_normal);
253 double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
258 inliers.push_back ((*indices_)[i]);
259 error_sqr_dists_.push_back (
distance);
265 template <
typename Po
intT,
typename Po
intNT> std::size_t
267 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
271 if (!isModelValid (model_coefficients))
274 std::size_t nr_p = 0;
276 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
277 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
278 float opening_angle = model_coefficients[6];
280 float apexdotdir = apex.dot (axis_dir);
281 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
283 for (std::size_t i = 0; i < indices_->size (); ++i)
285 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
288 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
289 Eigen::Vector4f pt_proj = apex + k * axis_dir;
292 Eigen::Vector4f height = apex - pt_proj;
293 double actual_cone_radius = tan(opening_angle) * height.norm ();
297 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
298 if (weighted_euclid_dist > threshold)
302 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
303 pp_pt_dir.normalize ();
307 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * pp_pt_dir;
310 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
311 double d_normal = std::abs (
getAngle3D (n, cone_normal));
312 d_normal = (std::min) (d_normal,
M_PI - d_normal);
314 if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
321 template <
typename Po
intT,
typename Po
intNT>
void
323 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
325 optimized_coefficients = model_coefficients;
328 if (!isModelValid (model_coefficients))
330 PCL_ERROR (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
335 if (inliers.size () <= sample_size_)
337 PCL_ERROR (
"[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
341 OptimizationFunctor functor (
this, inliers);
342 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
343 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
344 int info = lm.minimize (optimized_coefficients);
347 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::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",
348 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
349 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]);
351 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
352 line_dir.normalize ();
353 optimized_coefficients[3] = line_dir[0];
354 optimized_coefficients[4] = line_dir[1];
355 optimized_coefficients[5] = line_dir[2];
359 template <
typename Po
intT,
typename Po
intNT>
void
361 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
364 if (!isModelValid (model_coefficients))
366 PCL_ERROR (
"[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
370 projected_points.
header = input_->header;
371 projected_points.
is_dense = input_->is_dense;
373 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
374 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
375 float opening_angle = model_coefficients[6];
377 float apexdotdir = apex.dot (axis_dir);
378 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
381 if (copy_data_fields)
384 projected_points.
resize (input_->size ());
385 projected_points.
width = input_->width;
386 projected_points.
height = input_->height;
388 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
390 for (std::size_t i = 0; i < projected_points.
size (); ++i)
395 for (
const auto &inlier : inliers)
397 Eigen::Vector4f pt ((*input_)[inlier].x,
402 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
405 pp.matrix () = apex + k * axis_dir;
407 Eigen::Vector4f dir = pt - pp;
411 Eigen::Vector4f height = apex - pp;
412 float actual_cone_radius = tanf (opening_angle) * height.norm ();
415 pp += dir * actual_cone_radius;
421 projected_points.
resize (inliers.size ());
422 projected_points.
width = inliers.size ();
423 projected_points.
height = 1;
425 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
427 for (std::size_t i = 0; i < inliers.size (); ++i)
432 for (std::size_t i = 0; i < inliers.size (); ++i)
437 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
439 pp.matrix () = apex + k * axis_dir;
441 Eigen::Vector4f dir = pt - pp;
445 Eigen::Vector4f height = apex - pp;
446 float actual_cone_radius = tanf (opening_angle) * height.norm ();
449 pp += dir * actual_cone_radius;
455 template <
typename Po
intT,
typename Po
intNT>
bool
457 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
460 if (!isModelValid (model_coefficients))
462 PCL_ERROR (
"[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
466 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
467 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
468 float openning_angle = model_coefficients[6];
470 float apexdotdir = apex.dot (axis_dir);
471 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
474 for (
const auto &index : indices)
476 Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
479 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
480 Eigen::Vector4f pt_proj = apex + k * axis_dir;
481 Eigen::Vector4f dir = pt - pt_proj;
485 Eigen::Vector4f height = apex - pt_proj;
486 double actual_cone_radius = tan (openning_angle) * height.norm ();
490 if (std::abs (
static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
498 template <
typename Po
intT,
typename Po
intNT>
double
500 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
502 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
503 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
508 template <
typename Po
intT,
typename Po
intNT>
bool
515 if (eps_angle_ > 0.0)
518 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
520 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
521 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
523 if (angle_diff > eps_angle_)
525 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] Angle between cone direction and given axis is too large.\n");
530 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
532 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too small: should be larger than %g, but is %g.\n",
533 min_angle_, model_coefficients[6]);
536 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
538 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too big: should be smaller than %g, but is %g.\n",
539 max_angle_, model_coefficients[6]);
546 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<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 optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the cone coefficients using the given inlier set and return them to the user.
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 cone model.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given cone model.
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 computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid cone model, compute the model coefficients fro...
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
double pointToAxisDistance(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 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 cone model coefficients.
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.
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.