Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 <pcl/sample_consensus/eigen.h>
43 #include <pcl/sample_consensus/sac_model_cone.h>
44 #include <pcl/common/concatenate.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointT, typename PointNT> bool
49 {
50  if (samples.size () != sample_size_)
51  {
52  PCL_ERROR ("[pcl::SampleConsensusModelCone::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
53  return (false);
54  }
55  return (true);
56 }
57 
58 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59 template <typename PointT, typename PointNT> bool
61  const Indices &samples, Eigen::VectorXf &model_coefficients) const
62 {
63  // Need 3 samples
64  if (samples.size () != sample_size_)
65  {
66  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
67  return (false);
68  }
69 
70  if (!normals_)
71  {
72  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given!\n");
73  return (false);
74  }
75 
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);
79 
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);
83 
84  //calculate apex (intersection of the three planes defined by points and belonging normals
85  Eigen::Vector4f ortho12 = n1.cross3(n2);
86  Eigen::Vector4f ortho23 = n2.cross3(n3);
87  Eigen::Vector4f ortho31 = n3.cross3(n1);
88 
89  float denominator = n1.dot(ortho23);
90 
91  float d1 = p1.dot (n1);
92  float d2 = p2.dot (n2);
93  float d3 = p3.dot (n3);
94 
95  Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
96 
97  //compute axis (normal of plane defined by: { apex+(p1-apex)/(||p1-apex||), apex+(p2-apex)/(||p2-apex||), apex+(p3-apex)/(||p3-apex||)}
98  Eigen::Vector4f ap1 = p1 - apex;
99  Eigen::Vector4f ap2 = p2 - apex;
100  Eigen::Vector4f ap3 = p3 - apex;
101 
102  Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
103  Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
104  Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
105 
106  Eigen::Vector4f np1np2 = np2 - np1;
107  Eigen::Vector4f np1np3 = np3 - np1;
108 
109  Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
110  axis_dir.normalize ();
111 
112  // normalize the vector (apex->p) for opening angle calculation
113  ap1.normalize ();
114  ap2.normalize ();
115  ap3.normalize ();
116 
117  //compute opening angle
118  float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
119 
120  model_coefficients.resize (model_size_);
121  // model_coefficients.template head<3> () = line_pt.template head<3> ();
122  model_coefficients[0] = apex[0];
123  model_coefficients[1] = apex[1];
124  model_coefficients[2] = apex[2];
125  // model_coefficients.template segment<3> (3) = line_dir.template head<3> ();
126  model_coefficients[3] = axis_dir[0];
127  model_coefficients[4] = axis_dir[1];
128  model_coefficients[5] = axis_dir[2];
129  // cone radius
130  model_coefficients[6] = opening_angle;
131 
132  if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
133  return (false);
134  if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
135  return (false);
136 
137  return (true);
138 }
139 
140 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141 template <typename PointT, typename PointNT> void
143  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
144 {
145  // Check if the model is valid given the user constraints
146  if (!isModelValid (model_coefficients))
147  {
148  distances.clear ();
149  return;
150  }
151 
152  distances.resize (indices_->size ());
153 
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];
157 
158  float apexdotdir = apex.dot (axis_dir);
159  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
160  // Iterate through the 3d points and calculate the distances from them to the cone
161  for (std::size_t i = 0; i < indices_->size (); ++i)
162  {
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);
165 
166  // Calculate the point's projection on the cone axis
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;
170  dir.normalize ();
171 
172  // Calculate the actual radius of the cone at the level of the projected point
173  Eigen::Vector4f height = apex - pt_proj;
174  float actual_cone_radius = tanf (opening_angle) * height.norm ();
175  height.normalize ();
176 
177  // Calculate the cones perfect normals
178  Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * dir;
179 
180  // Approximate the distance from the point to the cone as the difference between
181  // dist(point,cone_axis) and actual cone radius
182  double d_euclid = std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
183 
184  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
185  double d_normal = std::abs (getAngle3D (n, cone_normal));
186  d_normal = (std::min) (d_normal, M_PI - d_normal);
187 
188  distances[i] = std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid);
189  }
190 }
191 
192 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
193 template <typename PointT, typename PointNT> void
195  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
196 {
197  // Check if the model is valid given the user constraints
198  if (!isModelValid (model_coefficients))
199  {
200  inliers.clear ();
201  return;
202  }
203 
204  inliers.clear ();
205  error_sqr_dists_.clear ();
206  inliers.reserve (indices_->size ());
207  error_sqr_dists_.reserve (indices_->size ());
208 
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];
212 
213  float apexdotdir = apex.dot (axis_dir);
214  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
215  // Iterate through the 3d points and calculate the distances from them to the cone
216  for (std::size_t i = 0; i < indices_->size (); ++i)
217  {
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);
220 
221  // Calculate the point's projection on the cone axis
222  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
223  Eigen::Vector4f pt_proj = apex + k * axis_dir;
224 
225  // Calculate the direction of the point from center
226  Eigen::Vector4f pp_pt_dir = pt - pt_proj;
227  pp_pt_dir.normalize ();
228 
229  // Calculate the actual radius of the cone at the level of the projected point
230  Eigen::Vector4f height = apex - pt_proj;
231  double actual_cone_radius = tan(opening_angle) * height.norm ();
232  height.normalize ();
233 
234  // Calculate the cones perfect normals
235  Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * pp_pt_dir;
236 
237  // Approximate the distance from the point to the cone as the difference between
238  // dist(point,cone_axis) and actual cone radius
239  double d_euclid = std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
240 
241  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
242  double d_normal = std::abs (getAngle3D (n, cone_normal));
243  d_normal = (std::min) (d_normal, M_PI - d_normal);
244 
245  double distance = std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid);
246 
247  if (distance < threshold)
248  {
249  // Returns the indices of the points whose distances are smaller than the threshold
250  inliers.push_back ((*indices_)[i]);
251  error_sqr_dists_.push_back (distance);
252  }
253  }
254 }
255 
256 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
257 template <typename PointT, typename PointNT> std::size_t
259  const Eigen::VectorXf &model_coefficients, const double threshold) const
260 {
261 
262  // Check if the model is valid given the user constraints
263  if (!isModelValid (model_coefficients))
264  return (0);
265 
266  std::size_t nr_p = 0;
267 
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];
271 
272  float apexdotdir = apex.dot (axis_dir);
273  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
274  // Iterate through the 3d points and calculate the distances from them to the cone
275  for (std::size_t i = 0; i < indices_->size (); ++i)
276  {
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);
279 
280  // Calculate the point's projection on the cone axis
281  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
282  Eigen::Vector4f pt_proj = apex + k * axis_dir;
283 
284  // Calculate the direction of the point from center
285  Eigen::Vector4f pp_pt_dir = pt - pt_proj;
286  pp_pt_dir.normalize ();
287 
288  // Calculate the actual radius of the cone at the level of the projected point
289  Eigen::Vector4f height = apex - pt_proj;
290  double actual_cone_radius = tan(opening_angle) * height.norm ();
291  height.normalize ();
292 
293  // Calculate the cones perfect normals
294  Eigen::Vector4f cone_normal = sinf (opening_angle) * height + std::cos (opening_angle) * pp_pt_dir;
295 
296  // Approximate the distance from the point to the cone as the difference between
297  // dist(point,cone_axis) and actual cone radius
298  double d_euclid = std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
299 
300  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
301  double d_normal = std::abs (getAngle3D (n, cone_normal));
302  d_normal = (std::min) (d_normal, M_PI - d_normal);
303 
304  if (std::abs (normal_distance_weight_ * d_normal + (1.0 - normal_distance_weight_) * d_euclid) < threshold)
305  nr_p++;
306  }
307  return (nr_p);
308 }
309 
310 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
311 template <typename PointT, typename PointNT> void
313  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
314 {
315  optimized_coefficients = model_coefficients;
316 
317  // Needs a set of valid model coefficients
318  if (!isModelValid (model_coefficients))
319  {
320  PCL_ERROR ("[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
321  return;
322  }
323 
324  // Need more than the minimum sample size to make a difference
325  if (inliers.size () <= sample_size_)
326  {
327  PCL_ERROR ("[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
328  return;
329  }
330 
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);
335 
336  // Compute the L2 norm of the residuals
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]);
340 
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];
346 }
347 
348 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
349 template <typename PointT, typename PointNT> void
351  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
352 {
353  // Needs a valid set of model coefficients
354  if (!isModelValid (model_coefficients))
355  {
356  PCL_ERROR ("[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
357  return;
358  }
359 
360  projected_points.header = input_->header;
361  projected_points.is_dense = input_->is_dense;
362 
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];
366 
367  float apexdotdir = apex.dot (axis_dir);
368  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
369 
370  // Copy all the data fields from the input cloud to the projected one?
371  if (copy_data_fields)
372  {
373  // Allocate enough space and copy the basics
374  projected_points.points.resize (input_->points.size ());
375  projected_points.width = input_->width;
376  projected_points.height = input_->height;
377 
378  using FieldList = typename pcl::traits::fieldList<PointT>::type;
379  // Iterate over each point
380  for (std::size_t i = 0; i < projected_points.points.size (); ++i)
381  // Iterate over each dimension
382  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
383 
384  // Iterate through the 3d points and calculate the distances from them to the cone
385  for (const auto &inlier : inliers)
386  {
387  Eigen::Vector4f pt (input_->points[inlier].x,
388  input_->points[inlier].y,
389  input_->points[inlier].z,
390  1);
391 
392  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
393 
394  pcl::Vector4fMap pp = projected_points.points[inlier].getVector4fMap ();
395  pp.matrix () = apex + k * axis_dir;
396 
397  Eigen::Vector4f dir = pt - pp;
398  dir.normalize ();
399 
400  // Calculate the actual radius of the cone at the level of the projected point
401  Eigen::Vector4f height = apex - pp;
402  float actual_cone_radius = tanf (opening_angle) * height.norm ();
403 
404  // Calculate the projection of the point onto the cone
405  pp += dir * actual_cone_radius;
406  }
407  }
408  else
409  {
410  // Allocate enough space and copy the basics
411  projected_points.points.resize (inliers.size ());
412  projected_points.width = static_cast<std::uint32_t> (inliers.size ());
413  projected_points.height = 1;
414 
415  using FieldList = typename pcl::traits::fieldList<PointT>::type;
416  // Iterate over each point
417  for (std::size_t i = 0; i < inliers.size (); ++i)
418  // Iterate over each dimension
419  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
420 
421  // Iterate through the 3d points and calculate the distances from them to the cone
422  for (std::size_t i = 0; i < inliers.size (); ++i)
423  {
424  pcl::Vector4fMap pp = projected_points.points[i].getVector4fMap ();
425  pcl::Vector4fMapConst pt = input_->points[inliers[i]].getVector4fMap ();
426 
427  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
428  // Calculate the projection of the point on the line
429  pp.matrix () = apex + k * axis_dir;
430 
431  Eigen::Vector4f dir = pt - pp;
432  dir.normalize ();
433 
434  // Calculate the actual radius of the cone at the level of the projected point
435  Eigen::Vector4f height = apex - pp;
436  float actual_cone_radius = tanf (opening_angle) * height.norm ();
437 
438  // Calculate the projection of the point onto the cone
439  pp += dir * actual_cone_radius;
440  }
441  }
442 }
443 
444 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
445 template <typename PointT, typename PointNT> bool
447  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
448 {
449  // Needs a valid model coefficients
450  if (!isModelValid (model_coefficients))
451  {
452  PCL_ERROR ("[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
453  return (false);
454  }
455 
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];
459 
460  float apexdotdir = apex.dot (axis_dir);
461  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
462 
463  // Iterate through the 3d points and calculate the distances from them to the cone
464  for (const auto &index : indices)
465  {
466  Eigen::Vector4f pt (input_->points[index].x, input_->points[index].y, input_->points[index].z, 0.0f);
467 
468  // Calculate the point's projection on the cone axis
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;
472  dir.normalize ();
473 
474  // Calculate the actual radius of the cone at the level of the projected point
475  Eigen::Vector4f height = apex - pt_proj;
476  double actual_cone_radius = tan (openning_angle) * height.norm ();
477 
478  // Approximate the distance from the point to the cone as the difference between
479  // dist(point,cone_axis) and actual cone radius
480  if (std::abs (static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
481  return (false);
482  }
483 
484  return (true);
485 }
486 
487 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
488 template <typename PointT, typename PointNT> double
490  const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const
491 {
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);
494  return sqrt(pcl::sqrPointToLineDistance (pt, apex, axis_dir));
495 }
496 
497 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
498 template <typename PointT, typename PointNT> bool
499 pcl::SampleConsensusModelCone<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
500 {
501  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
502  return (false);
503 
504  // Check against template, if given
505  if (eps_angle_ > 0.0)
506  {
507  // Obtain the cone direction
508  const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
509 
510  double angle_diff = std::abs (getAngle3D (axis_, coeff));
511  angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
512  // Check whether the current cone model satisfies our angle threshold criterion with respect to the given axis
513  if (angle_diff > eps_angle_)
514  return (false);
515  }
516 
517  if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
518  return (false);
519  if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
520  return (false);
521 
522  return (true);
523 }
524 
525 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<PointT, PointNT>;
526 
527 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
528 
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. ...
Definition: common.hpp:46
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).
Definition: point_cloud.h:413
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.
Definition: point_cloud.h:410
SampleConsensusModel represents the base model class.
Definition: sac_model.h:70
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:71
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).
Definition: point_cloud.h:415
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.
Definition: concatenate.h:51
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:418
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.
Definition: point_cloud.h:407