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 (!isSampleGood (samples))
67 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given\n");
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 const float sin_opening_angle = std::sin (model_coefficients[6]),
161 cos_opening_angle = std::cos (model_coefficients[6]),
162 tan_opening_angle = std::tan (model_coefficients[6]);
164 float apexdotdir = apex.dot (axis_dir);
165 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
167 for (std::size_t i = 0; i < indices_->size (); ++i)
169 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
172 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
173 Eigen::Vector4f pt_proj = apex + k * axis_dir;
176 Eigen::Vector4f height = apex - pt_proj;
177 float actual_cone_radius = tan_opening_angle * height.norm ();
181 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
184 Eigen::Vector4f dir = pt - pt_proj;
189 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * dir;
192 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
193 double d_normal = std::abs (
getAngle3D (n, cone_normal));
194 d_normal = (std::min) (d_normal,
M_PI - d_normal);
196 distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
201 template <
typename Po
intT,
typename Po
intNT>
void
203 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
206 if (!isModelValid (model_coefficients))
213 error_sqr_dists_.clear ();
214 inliers.reserve (indices_->size ());
215 error_sqr_dists_.reserve (indices_->size ());
217 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
218 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
219 const float sin_opening_angle = std::sin (model_coefficients[6]),
220 cos_opening_angle = std::cos (model_coefficients[6]),
221 tan_opening_angle = std::tan (model_coefficients[6]);
223 float apexdotdir = apex.dot (axis_dir);
224 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
226 for (std::size_t i = 0; i < indices_->size (); ++i)
228 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
231 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
232 Eigen::Vector4f pt_proj = apex + k * axis_dir;
235 Eigen::Vector4f height = apex - pt_proj;
236 double actual_cone_radius = tan_opening_angle * height.norm ();
240 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
241 if (weighted_euclid_dist > threshold)
245 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
246 pp_pt_dir.normalize ();
250 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
253 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
254 double d_normal = std::abs (
getAngle3D (n, cone_normal));
255 d_normal = (std::min) (d_normal,
M_PI - d_normal);
257 double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
262 inliers.push_back ((*indices_)[i]);
263 error_sqr_dists_.push_back (
distance);
269 template <
typename Po
intT,
typename Po
intNT> std::size_t
271 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
275 if (!isModelValid (model_coefficients))
278 std::size_t nr_p = 0;
280 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
281 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
282 const float sin_opening_angle = std::sin (model_coefficients[6]),
283 cos_opening_angle = std::cos (model_coefficients[6]),
284 tan_opening_angle = std::tan (model_coefficients[6]);
286 float apexdotdir = apex.dot (axis_dir);
287 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
289 for (std::size_t i = 0; i < indices_->size (); ++i)
291 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
294 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
295 Eigen::Vector4f pt_proj = apex + k * axis_dir;
298 Eigen::Vector4f height = apex - pt_proj;
299 double actual_cone_radius = tan_opening_angle * height.norm ();
303 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
304 if (weighted_euclid_dist > threshold)
308 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
309 pp_pt_dir.normalize ();
313 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
316 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
317 double d_normal = std::abs (
getAngle3D (n, cone_normal));
318 d_normal = (std::min) (d_normal,
M_PI - d_normal);
320 if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
327 template <
typename Po
intT,
typename Po
intNT>
void
329 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
331 optimized_coefficients = model_coefficients;
334 if (!isModelValid (model_coefficients))
336 PCL_ERROR (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
341 if (inliers.size () <= sample_size_)
343 PCL_ERROR (
"[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
347 OptimizationFunctor functor (
this, inliers);
348 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
349 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
350 int info = lm.minimize (optimized_coefficients);
353 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",
354 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
355 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]);
357 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
358 line_dir.normalize ();
359 optimized_coefficients[3] = line_dir[0];
360 optimized_coefficients[4] = line_dir[1];
361 optimized_coefficients[5] = line_dir[2];
365 template <
typename Po
intT,
typename Po
intNT>
void
367 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
370 if (!isModelValid (model_coefficients))
372 PCL_ERROR (
"[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
376 projected_points.
header = input_->header;
377 projected_points.
is_dense = input_->is_dense;
379 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
380 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
381 const float tan_opening_angle = std::tan (model_coefficients[6]);
383 float apexdotdir = apex.dot (axis_dir);
384 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
387 if (copy_data_fields)
390 projected_points.
resize (input_->size ());
391 projected_points.
width = input_->width;
392 projected_points.
height = input_->height;
394 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
396 for (std::size_t i = 0; i < projected_points.
size (); ++i)
401 for (
const auto &inlier : inliers)
403 Eigen::Vector4f pt ((*input_)[inlier].x,
408 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
411 pp.matrix () = apex + k * axis_dir;
413 Eigen::Vector4f dir = pt - pp;
417 Eigen::Vector4f height = apex - pp;
418 float actual_cone_radius = tan_opening_angle * height.norm ();
421 pp += dir * actual_cone_radius;
427 projected_points.
resize (inliers.size ());
428 projected_points.
width = inliers.size ();
429 projected_points.
height = 1;
431 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
433 for (std::size_t i = 0; i < inliers.size (); ++i)
438 for (std::size_t i = 0; i < inliers.size (); ++i)
443 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
445 pp.matrix () = apex + k * axis_dir;
447 Eigen::Vector4f dir = pt - pp;
451 Eigen::Vector4f height = apex - pp;
452 float actual_cone_radius = tan_opening_angle * height.norm ();
455 pp += dir * actual_cone_radius;
461 template <
typename Po
intT,
typename Po
intNT>
bool
463 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
466 if (!isModelValid (model_coefficients))
468 PCL_ERROR (
"[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
472 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
473 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
474 const float tan_opening_angle = std::tan (model_coefficients[6]);
476 float apexdotdir = apex.dot (axis_dir);
477 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
480 for (
const auto &index : indices)
482 Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
485 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
486 Eigen::Vector4f pt_proj = apex + k * axis_dir;
487 Eigen::Vector4f dir = pt - pt_proj;
491 Eigen::Vector4f height = apex - pt_proj;
492 double actual_cone_radius = tan_opening_angle * height.norm ();
496 if (std::abs (
static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
504 template <
typename Po
intT,
typename Po
intNT>
double
506 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
508 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
509 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
514 template <
typename Po
intT,
typename Po
intNT>
bool
521 if (eps_angle_ > 0.0)
524 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
526 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
527 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
529 if (angle_diff > eps_angle_)
531 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] Angle between cone direction and given axis is too large.\n");
536 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
538 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too small: should be larger than %g, but is %g.\n",
539 min_angle_, model_coefficients[6]);
542 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
544 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too big: should be smaller than %g, but is %g.\n",
545 max_angle_, model_coefficients[6]);
552 #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.