39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
42 #include <pcl/sample_consensus/eigen.h>
43 #include <pcl/sample_consensus/sac_model_cone.h>
44 #include <pcl/common/concatenate.h>
47 template <
typename Po
intT,
typename Po
intNT>
bool
50 if (samples.size () != sample_size_)
52 PCL_ERROR (
"[pcl::SampleConsensusModelCone::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
59 template <
typename Po
intT,
typename Po
intNT>
bool
61 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
64 if (samples.size () != sample_size_)
66 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
72 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given!\n");
76 Eigen::Vector4f p1 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z, 0.0f);
77 Eigen::Vector4f p2 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z, 0.0f);
78 Eigen::Vector4f p3 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z, 0.0f);
80 Eigen::Vector4f n1 (normals_->points[samples[0]].normal[0], normals_->points[samples[0]].normal[1], normals_->points[samples[0]].normal[2], 0.0f);
81 Eigen::Vector4f n2 (normals_->points[samples[1]].normal[0], normals_->points[samples[1]].normal[1], normals_->points[samples[1]].normal[2], 0.0f);
82 Eigen::Vector4f n3 (normals_->points[samples[2]].normal[0], normals_->points[samples[2]].normal[1], normals_->points[samples[2]].normal[2], 0.0f);
85 Eigen::Vector4f ortho12 = n1.cross3(n2);
86 Eigen::Vector4f ortho23 = n2.cross3(n3);
87 Eigen::Vector4f ortho31 = n3.cross3(n1);
89 float denominator = n1.dot(ortho23);
91 float d1 = p1.dot (n1);
92 float d2 = p2.dot (n2);
93 float d3 = p3.dot (n3);
95 Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
98 Eigen::Vector4f ap1 = p1 - apex;
99 Eigen::Vector4f ap2 = p2 - apex;
100 Eigen::Vector4f ap3 = p3 - apex;
102 Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
103 Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
104 Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
106 Eigen::Vector4f np1np2 = np2 - np1;
107 Eigen::Vector4f np1np3 = np3 - np1;
109 Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
110 axis_dir.normalize ();
118 float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
120 model_coefficients.resize (model_size_);
122 model_coefficients[0] = apex[0];
123 model_coefficients[1] = apex[1];
124 model_coefficients[2] = apex[2];
126 model_coefficients[3] = axis_dir[0];
127 model_coefficients[4] = axis_dir[1];
128 model_coefficients[5] = axis_dir[2];
130 model_coefficients[6] = opening_angle;
132 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
134 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
141 template <
typename Po
intT,
typename Po
intNT>
void
143 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
146 if (!isModelValid (model_coefficients))
152 distances.resize (indices_->size ());
154 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
155 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
156 float opening_angle = model_coefficients[6];
158 float apexdotdir = apex.dot (axis_dir);
159 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
161 for (std::size_t i = 0; i < indices_->size (); ++i)
163 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0.0f);
164 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0.0f);
167 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
168 Eigen::Vector4f pt_proj = apex + k * axis_dir;
169 Eigen::Vector4f dir = pt - pt_proj;
173 Eigen::Vector4f height = apex - pt_proj;
174 float actual_cone_radius = tanf (opening_angle) * height.norm ();
178 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * dir;
182 double d_euclid = std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
185 double d_normal = std::abs (
getAngle3D (n, cone_normal));
186 d_normal = (std::min) (d_normal, M_PI - d_normal);
188 distances[i] = std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid);
193 template <
typename Po
intT,
typename Po
intNT>
void
195 const Eigen::VectorXf &model_coefficients,
const double threshold, Indices &inliers)
198 if (!isModelValid (model_coefficients))
205 error_sqr_dists_.clear ();
206 inliers.reserve (indices_->size ());
207 error_sqr_dists_.reserve (indices_->size ());
209 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
210 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
211 float opening_angle = model_coefficients[6];
213 float apexdotdir = apex.dot (axis_dir);
214 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
216 for (std::size_t i = 0; i < indices_->size (); ++i)
218 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0.0f);
219 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0.0f);
222 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
223 Eigen::Vector4f pt_proj = apex + k * axis_dir;
226 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
227 pp_pt_dir.normalize ();
230 Eigen::Vector4f height = apex - pt_proj;
231 double actual_cone_radius = tan(opening_angle) * height.norm ();
235 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * pp_pt_dir;
239 double d_euclid = std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
242 double d_normal = std::abs (
getAngle3D (n, cone_normal));
243 d_normal = (std::min) (d_normal, M_PI - d_normal);
245 double distance = std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid);
247 if (distance < threshold)
250 inliers.push_back ((*indices_)[i]);
251 error_sqr_dists_.push_back (distance);
257 template <
typename Po
intT,
typename Po
intNT> std::size_t
259 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
263 if (!isModelValid (model_coefficients))
266 std::size_t nr_p = 0;
268 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
269 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
270 float opening_angle = model_coefficients[6];
272 float apexdotdir = apex.dot (axis_dir);
273 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
275 for (std::size_t i = 0; i < indices_->size (); ++i)
277 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0.0f);
278 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0.0f);
281 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
282 Eigen::Vector4f pt_proj = apex + k * axis_dir;
285 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
286 pp_pt_dir.normalize ();
289 Eigen::Vector4f height = apex - pt_proj;
290 double actual_cone_radius = tan(opening_angle) * height.norm ();
294 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * pp_pt_dir;
298 double d_euclid = std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
301 double d_normal = std::abs (
getAngle3D (n, cone_normal));
302 d_normal = (std::min) (d_normal, M_PI - d_normal);
304 if (std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid) < threshold)
311 template <
typename Po
intT,
typename Po
intNT>
void
313 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
315 optimized_coefficients = model_coefficients;
318 if (!isModelValid (model_coefficients))
320 PCL_ERROR (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
325 if (inliers.size () <= sample_size_)
327 PCL_ERROR (
"[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
331 OptimizationFunctor functor (
this, inliers);
332 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
333 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
334 int info = lm.minimize (optimized_coefficients);
337 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",
338 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
339 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]);
341 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
342 line_dir.normalize ();
343 optimized_coefficients[3] = line_dir[0];
344 optimized_coefficients[4] = line_dir[1];
345 optimized_coefficients[5] = line_dir[2];
349 template <
typename Po
intT,
typename Po
intNT>
void
351 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
354 if (!isModelValid (model_coefficients))
356 PCL_ERROR (
"[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
360 projected_points.
header = input_->header;
361 projected_points.
is_dense = input_->is_dense;
363 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
364 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
365 float opening_angle = model_coefficients[6];
367 float apexdotdir = apex.dot (axis_dir);
368 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
371 if (copy_data_fields)
374 projected_points.
points.resize (input_->points.size ());
375 projected_points.
width = input_->width;
376 projected_points.
height = input_->height;
380 for (std::size_t i = 0; i < projected_points.
points.size (); ++i)
385 for (
const auto &inlier : inliers)
387 Eigen::Vector4f pt (input_->points[inlier].x,
388 input_->points[inlier].y,
389 input_->points[inlier].z,
392 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
395 pp.matrix () = apex + k * axis_dir;
397 Eigen::Vector4f dir = pt - pp;
401 Eigen::Vector4f height = apex - pp;
402 float actual_cone_radius = tanf (opening_angle) * height.norm ();
405 pp += dir * actual_cone_radius;
411 projected_points.
points.resize (inliers.size ());
412 projected_points.
width =
static_cast<std::uint32_t
> (inliers.size ());
413 projected_points.
height = 1;
417 for (std::size_t i = 0; i < inliers.size (); ++i)
422 for (std::size_t i = 0; i < inliers.size (); ++i)
427 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
429 pp.matrix () = apex + k * axis_dir;
431 Eigen::Vector4f dir = pt - pp;
435 Eigen::Vector4f height = apex - pp;
436 float actual_cone_radius = tanf (opening_angle) * height.norm ();
439 pp += dir * actual_cone_radius;
445 template <
typename Po
intT,
typename Po
intNT>
bool
447 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
450 if (!isModelValid (model_coefficients))
452 PCL_ERROR (
"[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
456 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
457 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
458 float openning_angle = model_coefficients[6];
460 float apexdotdir = apex.dot (axis_dir);
461 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
464 for (
const auto &index : indices)
466 Eigen::Vector4f pt (input_->points[index].x, input_->points[index].y, input_->points[index].z, 0.0f);
469 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
470 Eigen::Vector4f pt_proj = apex + k * axis_dir;
471 Eigen::Vector4f dir = pt - pt_proj;
475 Eigen::Vector4f height = apex - pt_proj;
476 double actual_cone_radius = tan (openning_angle) * height.norm ();
480 if (std::abs (static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
488 template <
typename Po
intT,
typename Po
intNT>
double
490 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
492 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
493 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
498 template <
typename Po
intT,
typename Po
intNT>
bool
505 if (eps_angle_ > 0.0)
508 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
510 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
511 angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
513 if (angle_diff > eps_angle_)
517 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
519 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
525 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<PointT, PointNT>;
527 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_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.
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...
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. ...
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
std::uint32_t width
The point cloud width (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 cone model.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
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 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.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
std::uint32_t height
The point cloud height (if organized as an image-structure).
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)
Helper functor structure for concatenate.
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 cone model coefficients.
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
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.
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...
pcl::PCLHeader header
The point cloud header.