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
54 template <
typename Po
intT,
typename Po
intNT>
bool
56 const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
59 if (samples.size () != 3)
61 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given (%zu)!\n", samples.size ());
67 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given!\n");
71 Eigen::Vector4f p1 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z, 0);
72 Eigen::Vector4f p2 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z, 0);
73 Eigen::Vector4f p3 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z, 0);
75 Eigen::Vector4f n1 (normals_->points[samples[0]].normal[0], normals_->points[samples[0]].normal[1], normals_->points[samples[0]].normal[2], 0);
76 Eigen::Vector4f n2 (normals_->points[samples[1]].normal[0], normals_->points[samples[1]].normal[1], normals_->points[samples[1]].normal[2], 0);
77 Eigen::Vector4f n3 (normals_->points[samples[2]].normal[0], normals_->points[samples[2]].normal[1], normals_->points[samples[2]].normal[2], 0);
80 Eigen::Vector4f ortho12 = n1.cross3(n2);
81 Eigen::Vector4f ortho23 = n2.cross3(n3);
82 Eigen::Vector4f ortho31 = n3.cross3(n1);
84 float denominator = n1.dot(ortho23);
86 float d1 = p1.dot (n1);
87 float d2 = p2.dot (n2);
88 float d3 = p3.dot (n3);
90 Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
93 Eigen::Vector4f ap1 = p1 - apex;
94 Eigen::Vector4f ap2 = p2 - apex;
95 Eigen::Vector4f ap3 = p3 - apex;
97 Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
98 Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
99 Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
101 Eigen::Vector4f np1np2 = np2 - np1;
102 Eigen::Vector4f np1np3 = np3 - np1;
104 Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
105 axis_dir.normalize ();
113 float opening_angle = ( acosf (ap1.dot (axis_dir)) + acosf (ap2.dot (axis_dir)) + acosf (ap3.dot (axis_dir)) ) / 3.0f;
115 model_coefficients.resize (7);
117 model_coefficients[0] = apex[0];
118 model_coefficients[1] = apex[1];
119 model_coefficients[2] = apex[2];
121 model_coefficients[3] = axis_dir[0];
122 model_coefficients[4] = axis_dir[1];
123 model_coefficients[5] = axis_dir[2];
125 model_coefficients[6] = opening_angle;
127 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
129 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
136 template <
typename Po
intT,
typename Po
intNT>
void
138 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
141 if (!isModelValid (model_coefficients))
147 distances.resize (indices_->size ());
149 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
150 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
151 float opening_angle = model_coefficients[6];
153 float apexdotdir = apex.dot (axis_dir);
154 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
156 for (
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);
159 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
162 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
163 Eigen::Vector4f pt_proj = apex + k * axis_dir;
164 Eigen::Vector4f dir = pt - pt_proj;
168 Eigen::Vector4f height = apex - pt_proj;
169 float actual_cone_radius = tanf (opening_angle) * height.norm ();
173 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + cosf (opening_angle) * dir;
177 double d_euclid = fabs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
180 double d_normal = fabs (
getAngle3D (n, cone_normal));
181 d_normal = (std::min) (d_normal, M_PI - d_normal);
183 distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
188 template <
typename Po
intT,
typename Po
intNT>
void
190 const Eigen::VectorXf &model_coefficients,
const double threshold, std::vector<int> &inliers)
193 if (!isModelValid (model_coefficients))
200 inliers.resize (indices_->size ());
201 error_sqr_dists_.resize (indices_->size ());
203 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
204 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
205 float opening_angle = model_coefficients[6];
207 float apexdotdir = apex.dot (axis_dir);
208 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
210 for (
size_t i = 0; i < indices_->size (); ++i)
212 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
213 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
216 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
217 Eigen::Vector4f pt_proj = apex + k * axis_dir;
220 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
221 pp_pt_dir.normalize ();
224 Eigen::Vector4f height = apex - pt_proj;
225 double actual_cone_radius = tan(opening_angle) * height.norm ();
229 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + cosf (opening_angle) * pp_pt_dir;
233 double d_euclid = fabs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
236 double d_normal = fabs (
getAngle3D (n, cone_normal));
237 d_normal = (std::min) (d_normal, M_PI - d_normal);
239 double distance = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
241 if (distance < threshold)
244 inliers[nr_p] = (*indices_)[i];
245 error_sqr_dists_[nr_p] = distance;
249 inliers.resize (nr_p);
250 error_sqr_dists_.resize (nr_p);
254 template <
typename Po
intT,
typename Po
intNT>
int
256 const Eigen::VectorXf &model_coefficients,
const double threshold)
260 if (!isModelValid (model_coefficients))
265 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
266 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
267 float opening_angle = model_coefficients[6];
269 float apexdotdir = apex.dot (axis_dir);
270 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
272 for (
size_t i = 0; i < indices_->size (); ++i)
274 Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
275 Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
278 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
279 Eigen::Vector4f pt_proj = apex + k * axis_dir;
282 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
283 pp_pt_dir.normalize ();
286 Eigen::Vector4f height = apex - pt_proj;
287 double actual_cone_radius = tan(opening_angle) * height.norm ();
291 Eigen::Vector4f cone_normal = sinf (opening_angle) * height + cosf (opening_angle) * pp_pt_dir;
295 double d_euclid = fabs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
298 double d_normal = fabs (
getAngle3D (n, cone_normal));
299 d_normal = (std::min) (d_normal, M_PI - d_normal);
301 if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
308 template <
typename Po
intT,
typename Po
intNT>
void
310 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
312 optimized_coefficients = model_coefficients;
315 if (model_coefficients.size () != 7)
317 PCL_ERROR (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
321 if (inliers.empty ())
323 PCL_DEBUG (
"[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Inliers vector empty! Returning the same coefficients.\n");
327 tmp_inliers_ = &inliers;
329 OptimizationFunctor functor (static_cast<int> (inliers.size ()),
this);
330 Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
331 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
332 int info = lm.minimize (optimized_coefficients);
335 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",
336 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
337 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 template <
typename Po
intT,
typename Po
intNT>
void
343 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
346 if (model_coefficients.size () != 7)
348 PCL_ERROR (
"[pcl::SampleConsensusModelCone::projectPoints] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
352 projected_points.
header = input_->header;
353 projected_points.
is_dense = input_->is_dense;
355 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
356 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
357 float opening_angle = model_coefficients[6];
359 float apexdotdir = apex.dot (axis_dir);
360 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
363 if (copy_data_fields)
366 projected_points.
points.resize (input_->points.size ());
367 projected_points.
width = input_->width;
368 projected_points.
height = input_->height;
372 for (
size_t i = 0; i < projected_points.
points.size (); ++i)
377 for (
size_t i = 0; i < inliers.size (); ++i)
379 Eigen::Vector4f pt (input_->points[inliers[i]].x,
380 input_->points[inliers[i]].y,
381 input_->points[inliers[i]].z,
384 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
387 pp.matrix () = apex + k * axis_dir;
389 Eigen::Vector4f dir = pt - pp;
393 Eigen::Vector4f height = apex - pp;
394 float actual_cone_radius = tanf (opening_angle) * height.norm ();
397 pp += dir * actual_cone_radius;
403 projected_points.
points.resize (inliers.size ());
404 projected_points.
width =
static_cast<uint32_t
> (inliers.size ());
405 projected_points.
height = 1;
409 for (
size_t i = 0; i < inliers.size (); ++i)
414 for (
size_t i = 0; i < inliers.size (); ++i)
419 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
421 pp.matrix () = apex + k * axis_dir;
423 Eigen::Vector4f dir = pt - pp;
427 Eigen::Vector4f height = apex - pp;
428 float actual_cone_radius = tanf (opening_angle) * height.norm ();
431 pp += dir * actual_cone_radius;
437 template <
typename Po
intT,
typename Po
intNT>
bool
439 const std::set<int> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
442 if (model_coefficients.size () != 7)
444 PCL_ERROR (
"[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
448 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
449 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
450 float openning_angle = model_coefficients[6];
452 float apexdotdir = apex.dot (axis_dir);
453 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
456 for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
458 Eigen::Vector4f pt (input_->points[*it].x, input_->points[*it].y, input_->points[*it].z, 0);
461 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
462 Eigen::Vector4f pt_proj = apex + k * axis_dir;
463 Eigen::Vector4f dir = pt - pt_proj;
467 Eigen::Vector4f height = apex - pt_proj;
468 double actual_cone_radius = tan (openning_angle) * height.norm ();
472 if (fabs (static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
480 template <
typename Po
intT,
typename Po
intNT>
double
482 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
484 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
485 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
490 template <
typename Po
intT,
typename Po
intNT>
bool
494 if (model_coefficients.size () != 7)
496 PCL_ERROR (
"[pcl::SampleConsensusModelCone::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
501 if (eps_angle_ > 0.0)
504 Eigen::Vector4f coeff;
505 coeff[0] = model_coefficients[3];
506 coeff[1] = model_coefficients[4];
507 coeff[2] = model_coefficients[5];
510 Eigen::Vector4f axis (axis_[0], axis_[1], axis_[2], 0);
511 double angle_diff = fabs (
getAngle3D (axis, coeff));
512 angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
514 if (angle_diff > eps_angle_)
518 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
520 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
526 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<PointT, PointNT>;
528 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
Recompute the cone coefficients using the given inlier set and return them to the user...
pcl::PCLHeader header
The point cloud header.
double pointToAxisDistance(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients)
Get the distance from a point to a line (represented by a point and a direction)
uint32_t height
The point cloud height (if organized as an image-structure).
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Select all the points which respect the given model coefficients as inliers.
double getAngle3D(const Eigen::Vector4f &v1, const Eigen::Vector4f &v2)
Compute the smallest angle between two vectors in the [ 0, PI ) interval in 3D.
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values).
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true)
Create a new point cloud with inliers projected onto the cone model.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid cone model, compute the model coefficients fro...
bool isModelValid(const Eigen::VectorXf &model_coefficients)
Check whether a model is valid given the user constraints.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
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) ...
uint32_t width
The point cloud width (if organized as an image-structure).
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
Verify whether a subset of indices verifies the given cone model coefficients.
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
Helper functor structure for concatenate.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
Compute all distances from the cloud data to a given cone model.