Point Cloud Library (PCL)  1.12.1-dev
sac_model_cone.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
41 
42 #include <unsupported/Eigen/NonLinearOptimization> // for LevenbergMarquardt
43 #include <pcl/sample_consensus/sac_model_cone.h>
44 #include <pcl/common/common.h> // for getAngle3D
45 #include <pcl/common/concatenate.h>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointT, typename PointNT> bool
50 {
51  if (samples.size () != sample_size_)
52  {
53  PCL_ERROR ("[pcl::SampleConsensusModelCone::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
54  return (false);
55  }
56  return (true);
57 }
58 
59 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60 template <typename PointT, typename PointNT> bool
62  const Indices &samples, Eigen::VectorXf &model_coefficients) const
63 {
64  // Make sure that the samples are valid
65  if (!isSampleGood (samples))
66  {
67  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given\n");
68  return (false);
69  }
70 
71  if (!normals_)
72  {
73  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given!\n");
74  return (false);
75  }
76 
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);
80 
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);
84 
85  //calculate apex (intersection of the three planes defined by points and belonging normals
86  Eigen::Vector4f ortho12 = n1.cross3(n2);
87  Eigen::Vector4f ortho23 = n2.cross3(n3);
88  Eigen::Vector4f ortho31 = n3.cross3(n1);
89 
90  float denominator = n1.dot(ortho23);
91 
92  float d1 = p1.dot (n1);
93  float d2 = p2.dot (n2);
94  float d3 = p3.dot (n3);
95 
96  Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
97 
98  //compute axis (normal of plane defined by: { apex+(p1-apex)/(||p1-apex||), apex+(p2-apex)/(||p2-apex||), apex+(p3-apex)/(||p3-apex||)}
99  Eigen::Vector4f ap1 = p1 - apex;
100  Eigen::Vector4f ap2 = p2 - apex;
101  Eigen::Vector4f ap3 = p3 - apex;
102 
103  Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
104  Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
105  Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
106 
107  Eigen::Vector4f np1np2 = np2 - np1;
108  Eigen::Vector4f np1np3 = np3 - np1;
109 
110  Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
111  axis_dir.normalize ();
112 
113  // normalize the vector (apex->p) for opening angle calculation
114  ap1.normalize ();
115  ap2.normalize ();
116  ap3.normalize ();
117 
118  //compute opening angle
119  float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
120 
121  model_coefficients.resize (model_size_);
122  // model_coefficients.template head<3> () = line_pt.template head<3> ();
123  model_coefficients[0] = apex[0];
124  model_coefficients[1] = apex[1];
125  model_coefficients[2] = apex[2];
126  // model_coefficients.template segment<3> (3) = line_dir.template head<3> ();
127  model_coefficients[3] = axis_dir[0];
128  model_coefficients[4] = axis_dir[1];
129  model_coefficients[5] = axis_dir[2];
130  // cone radius
131  model_coefficients[6] = opening_angle;
132 
133  if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
134  return (false);
135  if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
136  return (false);
137 
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]);
141  return (true);
142 }
143 
144 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
145 template <typename PointT, typename PointNT> void
147  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
148 {
149  // Check if the model is valid given the user constraints
150  if (!isModelValid (model_coefficients))
151  {
152  distances.clear ();
153  return;
154  }
155 
156  distances.resize (indices_->size ());
157 
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]);
163 
164  float apexdotdir = apex.dot (axis_dir);
165  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
166  // Iterate through the 3d points and calculate the distances from them to the cone
167  for (std::size_t i = 0; i < indices_->size (); ++i)
168  {
169  Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
170 
171  // Calculate the point's projection on the cone axis
172  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
173  Eigen::Vector4f pt_proj = apex + k * axis_dir;
174 
175  // Calculate the actual radius of the cone at the level of the projected point
176  Eigen::Vector4f height = apex - pt_proj;
177  float actual_cone_radius = tan_opening_angle * height.norm ();
178 
179  // Approximate the distance from the point to the cone as the difference between
180  // dist(point,cone_axis) and actual cone radius
181  const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
182 
183  // Calculate the direction of the point from center
184  Eigen::Vector4f dir = pt - pt_proj;
185  dir.normalize ();
186 
187  // Calculate the cones perfect normals
188  height.normalize ();
189  Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * dir;
190 
191  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
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);
195 
196  distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
197  }
198 }
199 
200 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201 template <typename PointT, typename PointNT> void
203  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
204 {
205  // Check if the model is valid given the user constraints
206  if (!isModelValid (model_coefficients))
207  {
208  inliers.clear ();
209  return;
210  }
211 
212  inliers.clear ();
213  error_sqr_dists_.clear ();
214  inliers.reserve (indices_->size ());
215  error_sqr_dists_.reserve (indices_->size ());
216 
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]);
222 
223  float apexdotdir = apex.dot (axis_dir);
224  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
225  // Iterate through the 3d points and calculate the distances from them to the cone
226  for (std::size_t i = 0; i < indices_->size (); ++i)
227  {
228  Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
229 
230  // Calculate the point's projection on the cone axis
231  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
232  Eigen::Vector4f pt_proj = apex + k * axis_dir;
233 
234  // Calculate the actual radius of the cone at the level of the projected point
235  Eigen::Vector4f height = apex - pt_proj;
236  double actual_cone_radius = tan_opening_angle * height.norm ();
237 
238  // Approximate the distance from the point to the cone as the difference between
239  // dist(point,cone_axis) and actual cone radius
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) // Early termination: cannot be an inlier
242  continue;
243 
244  // Calculate the direction of the point from center
245  Eigen::Vector4f pp_pt_dir = pt - pt_proj;
246  pp_pt_dir.normalize ();
247 
248  // Calculate the cones perfect normals
249  height.normalize ();
250  Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
251 
252  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
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);
256 
257  double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
258 
259  if (distance < threshold)
260  {
261  // Returns the indices of the points whose distances are smaller than the threshold
262  inliers.push_back ((*indices_)[i]);
263  error_sqr_dists_.push_back (distance);
264  }
265  }
266 }
267 
268 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
269 template <typename PointT, typename PointNT> std::size_t
271  const Eigen::VectorXf &model_coefficients, const double threshold) const
272 {
273 
274  // Check if the model is valid given the user constraints
275  if (!isModelValid (model_coefficients))
276  return (0);
277 
278  std::size_t nr_p = 0;
279 
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]);
285 
286  float apexdotdir = apex.dot (axis_dir);
287  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
288  // Iterate through the 3d points and calculate the distances from them to the cone
289  for (std::size_t i = 0; i < indices_->size (); ++i)
290  {
291  Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
292 
293  // Calculate the point's projection on the cone axis
294  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
295  Eigen::Vector4f pt_proj = apex + k * axis_dir;
296 
297  // Calculate the actual radius of the cone at the level of the projected point
298  Eigen::Vector4f height = apex - pt_proj;
299  double actual_cone_radius = tan_opening_angle * height.norm ();
300 
301  // Approximate the distance from the point to the cone as the difference between
302  // dist(point,cone_axis) and actual cone radius
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) // Early termination: cannot be an inlier
305  continue;
306 
307  // Calculate the direction of the point from center
308  Eigen::Vector4f pp_pt_dir = pt - pt_proj;
309  pp_pt_dir.normalize ();
310 
311  // Calculate the cones perfect normals
312  height.normalize ();
313  Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
314 
315  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
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);
319 
320  if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
321  nr_p++;
322  }
323  return (nr_p);
324 }
325 
326 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
327 template <typename PointT, typename PointNT> void
329  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
330 {
331  optimized_coefficients = model_coefficients;
332 
333  // Needs a set of valid model coefficients
334  if (!isModelValid (model_coefficients))
335  {
336  PCL_ERROR ("[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
337  return;
338  }
339 
340  // Need more than the minimum sample size to make a difference
341  if (inliers.size () <= sample_size_)
342  {
343  PCL_ERROR ("[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
344  return;
345  }
346 
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);
351 
352  // Compute the L2 norm of the residuals
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]);
356 
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];
362 }
363 
364 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
365 template <typename PointT, typename PointNT> void
367  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
368 {
369  // Needs a valid set of model coefficients
370  if (!isModelValid (model_coefficients))
371  {
372  PCL_ERROR ("[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
373  return;
374  }
375 
376  projected_points.header = input_->header;
377  projected_points.is_dense = input_->is_dense;
378 
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]);
382 
383  float apexdotdir = apex.dot (axis_dir);
384  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
385 
386  // Copy all the data fields from the input cloud to the projected one?
387  if (copy_data_fields)
388  {
389  // Allocate enough space and copy the basics
390  projected_points.resize (input_->size ());
391  projected_points.width = input_->width;
392  projected_points.height = input_->height;
393 
394  using FieldList = typename pcl::traits::fieldList<PointT>::type;
395  // Iterate over each point
396  for (std::size_t i = 0; i < projected_points.size (); ++i)
397  // Iterate over each dimension
398  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[i], projected_points[i]));
399 
400  // Iterate through the 3d points and calculate the distances from them to the cone
401  for (const auto &inlier : inliers)
402  {
403  Eigen::Vector4f pt ((*input_)[inlier].x,
404  (*input_)[inlier].y,
405  (*input_)[inlier].z,
406  1);
407 
408  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
409 
410  pcl::Vector4fMap pp = projected_points[inlier].getVector4fMap ();
411  pp.matrix () = apex + k * axis_dir;
412 
413  Eigen::Vector4f dir = pt - pp;
414  dir.normalize ();
415 
416  // Calculate the actual radius of the cone at the level of the projected point
417  Eigen::Vector4f height = apex - pp;
418  float actual_cone_radius = tan_opening_angle * height.norm ();
419 
420  // Calculate the projection of the point onto the cone
421  pp += dir * actual_cone_radius;
422  }
423  }
424  else
425  {
426  // Allocate enough space and copy the basics
427  projected_points.resize (inliers.size ());
428  projected_points.width = inliers.size ();
429  projected_points.height = 1;
430 
431  using FieldList = typename pcl::traits::fieldList<PointT>::type;
432  // Iterate over each point
433  for (std::size_t i = 0; i < inliers.size (); ++i)
434  // Iterate over each dimension
435  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[inliers[i]], projected_points[i]));
436 
437  // Iterate through the 3d points and calculate the distances from them to the cone
438  for (std::size_t i = 0; i < inliers.size (); ++i)
439  {
440  pcl::Vector4fMap pp = projected_points[i].getVector4fMap ();
441  pcl::Vector4fMapConst pt = (*input_)[inliers[i]].getVector4fMap ();
442 
443  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
444  // Calculate the projection of the point on the line
445  pp.matrix () = apex + k * axis_dir;
446 
447  Eigen::Vector4f dir = pt - pp;
448  dir.normalize ();
449 
450  // Calculate the actual radius of the cone at the level of the projected point
451  Eigen::Vector4f height = apex - pp;
452  float actual_cone_radius = tan_opening_angle * height.norm ();
453 
454  // Calculate the projection of the point onto the cone
455  pp += dir * actual_cone_radius;
456  }
457  }
458 }
459 
460 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
461 template <typename PointT, typename PointNT> bool
463  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
464 {
465  // Needs a valid model coefficients
466  if (!isModelValid (model_coefficients))
467  {
468  PCL_ERROR ("[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
469  return (false);
470  }
471 
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]);
475 
476  float apexdotdir = apex.dot (axis_dir);
477  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
478 
479  // Iterate through the 3d points and calculate the distances from them to the cone
480  for (const auto &index : indices)
481  {
482  Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
483 
484  // Calculate the point's projection on the cone axis
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;
488  dir.normalize ();
489 
490  // Calculate the actual radius of the cone at the level of the projected point
491  Eigen::Vector4f height = apex - pt_proj;
492  double actual_cone_radius = tan_opening_angle * height.norm ();
493 
494  // Approximate the distance from the point to the cone as the difference between
495  // dist(point,cone_axis) and actual cone radius
496  if (std::abs (static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
497  return (false);
498  }
499 
500  return (true);
501 }
502 
503 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
504 template <typename PointT, typename PointNT> double
506  const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const
507 {
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);
510  return sqrt(pcl::sqrPointToLineDistance (pt, apex, axis_dir));
511 }
512 
513 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
514 template <typename PointT, typename PointNT> bool
515 pcl::SampleConsensusModelCone<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
516 {
517  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
518  return (false);
519 
520  // Check against template, if given
521  if (eps_angle_ > 0.0)
522  {
523  // Obtain the cone direction
524  const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
525 
526  double angle_diff = std::abs (getAngle3D (axis_, coeff));
527  angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
528  // Check whether the current cone model satisfies our angle threshold criterion with respect to the given axis
529  if (angle_diff > eps_angle_)
530  {
531  PCL_DEBUG ("[pcl::SampleConsensusModelCone::isModelValid] Angle between cone direction and given axis is too large.\n");
532  return (false);
533  }
534  }
535 
536  if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
537  {
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]);
540  return (false);
541  }
542  if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
543  {
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]);
546  return (false);
547  }
548 
549  return (true);
550 }
551 
552 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<PointT, PointNT>;
553 
554 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
555 
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:392
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
std::size_t size() const
Definition: point_cloud.h:443
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.
Definition: sac_model.h:70
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.
Definition: common.hpp:47
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)
Definition: distances.h:75
float distance(const PointT &p1, const PointT &p2)
Definition: geometry.h:60
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define M_PI
Definition: pcl_macros.h:201
Helper functor structure for concatenate.
Definition: concatenate.h:50