Point Cloud Library (PCL)  1.14.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 <pcl/sample_consensus/sac_model_cone.h>
43 #include <pcl/common/common.h> // for getAngle3D
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  // Make sure that the samples are valid
64  if (!isSampleGood (samples))
65  {
66  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given\n");
67  return (false);
68  }
69 
70  if (!normals_)
71  {
72  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given! Use setInputNormals\n");
73  return (false);
74  }
75 
76  Eigen::Vector4f p1 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z, 0.0f);
77  Eigen::Vector4f p2 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z, 0.0f);
78  Eigen::Vector4f p3 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z, 0.0f);
79 
80  Eigen::Vector4f n1 ((*normals_)[samples[0]].normal[0], (*normals_)[samples[0]].normal[1], (*normals_)[samples[0]].normal[2], 0.0f);
81  Eigen::Vector4f n2 ((*normals_)[samples[1]].normal[0], (*normals_)[samples[1]].normal[1], (*normals_)[samples[1]].normal[2], 0.0f);
82  Eigen::Vector4f n3 ((*normals_)[samples[2]].normal[0], (*normals_)[samples[2]].normal[1], (*normals_)[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  if (std::abs(denominator) < Eigen::NumTraits<float>::dummy_precision ())
91  {
92  PCL_ERROR ("[pcl::SampleConsensusModelCone::computeModelCoefficients] Impossible to compute stable model with these points.\n");
93  return (false);
94  }
95 
96  float d1 = p1.dot (n1);
97  float d2 = p2.dot (n2);
98  float d3 = p3.dot (n3);
99 
100  Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
101 
102  //compute axis (normal of plane defined by: { apex+(p1-apex)/(||p1-apex||), apex+(p2-apex)/(||p2-apex||), apex+(p3-apex)/(||p3-apex||)}
103  Eigen::Vector4f ap1 = p1 - apex;
104  Eigen::Vector4f ap2 = p2 - apex;
105  Eigen::Vector4f ap3 = p3 - apex;
106 
107  Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
108  Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
109  Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
110 
111  Eigen::Vector4f np1np2 = np2 - np1;
112  Eigen::Vector4f np1np3 = np3 - np1;
113 
114  Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
115  axis_dir.normalize ();
116 
117  // normalize the vector (apex->p) for opening angle calculation
118  ap1.normalize ();
119  ap2.normalize ();
120  ap3.normalize ();
121 
122  //compute opening angle
123  float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
124 
125  model_coefficients.resize (model_size_);
126  // model_coefficients.template head<3> () = line_pt.template head<3> ();
127  model_coefficients[0] = apex[0];
128  model_coefficients[1] = apex[1];
129  model_coefficients[2] = apex[2];
130  // model_coefficients.template segment<3> (3) = line_dir.template head<3> ();
131  model_coefficients[3] = axis_dir[0];
132  model_coefficients[4] = axis_dir[1];
133  model_coefficients[5] = axis_dir[2];
134  // cone radius
135  model_coefficients[6] = opening_angle;
136 
137  if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
138  return (false);
139  if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
140  return (false);
141 
142  PCL_DEBUG ("[pcl::SampleConsensusModelCone::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
143  model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
144  model_coefficients[4], model_coefficients[5], model_coefficients[6]);
145  return (true);
146 }
147 
148 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
149 template <typename PointT, typename PointNT> void
151  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
152 {
153  // Check if the model is valid given the user constraints
154  if (!isModelValid (model_coefficients))
155  {
156  distances.clear ();
157  return;
158  }
159 
160  distances.resize (indices_->size ());
161 
162  Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
163  Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
164  const float sin_opening_angle = std::sin (model_coefficients[6]),
165  cos_opening_angle = std::cos (model_coefficients[6]),
166  tan_opening_angle = std::tan (model_coefficients[6]);
167 
168  float apexdotdir = apex.dot (axis_dir);
169  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
170  // Iterate through the 3d points and calculate the distances from them to the cone
171  for (std::size_t i = 0; i < indices_->size (); ++i)
172  {
173  Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
174 
175  // Calculate the point's projection on the cone axis
176  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
177  Eigen::Vector4f pt_proj = apex + k * axis_dir;
178 
179  // Calculate the actual radius of the cone at the level of the projected point
180  Eigen::Vector4f height = apex - pt_proj;
181  float actual_cone_radius = tan_opening_angle * height.norm ();
182 
183  // Approximate the distance from the point to the cone as the difference between
184  // dist(point,cone_axis) and actual cone radius
185  const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
186 
187  // Calculate the direction of the point from center
188  Eigen::Vector4f dir = pt - pt_proj;
189  dir.normalize ();
190 
191  // Calculate the cones perfect normals
192  height.normalize ();
193  Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * dir;
194 
195  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
196  Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
197  double d_normal = std::abs (getAngle3D (n, cone_normal));
198  d_normal = (std::min) (d_normal, M_PI - d_normal);
199 
200  distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
201  }
202 }
203 
204 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205 template <typename PointT, typename PointNT> void
207  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
208 {
209  // Check if the model is valid given the user constraints
210  if (!isModelValid (model_coefficients))
211  {
212  inliers.clear ();
213  return;
214  }
215 
216  inliers.clear ();
217  error_sqr_dists_.clear ();
218  inliers.reserve (indices_->size ());
219  error_sqr_dists_.reserve (indices_->size ());
220 
221  Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
222  Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
223  const float sin_opening_angle = std::sin (model_coefficients[6]),
224  cos_opening_angle = std::cos (model_coefficients[6]),
225  tan_opening_angle = std::tan (model_coefficients[6]);
226 
227  float apexdotdir = apex.dot (axis_dir);
228  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
229  // Iterate through the 3d points and calculate the distances from them to the cone
230  for (std::size_t i = 0; i < indices_->size (); ++i)
231  {
232  Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
233 
234  // Calculate the point's projection on the cone axis
235  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
236  Eigen::Vector4f pt_proj = apex + k * axis_dir;
237 
238  // Calculate the actual radius of the cone at the level of the projected point
239  Eigen::Vector4f height = apex - pt_proj;
240  double actual_cone_radius = tan_opening_angle * height.norm ();
241 
242  // Approximate the distance from the point to the cone as the difference between
243  // dist(point,cone_axis) and actual cone radius
244  const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
245  if (weighted_euclid_dist > threshold) // Early termination: cannot be an inlier
246  continue;
247 
248  // Calculate the direction of the point from center
249  Eigen::Vector4f pp_pt_dir = pt - pt_proj;
250  pp_pt_dir.normalize ();
251 
252  // Calculate the cones perfect normals
253  height.normalize ();
254  Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
255 
256  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
257  Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
258  double d_normal = std::abs (getAngle3D (n, cone_normal));
259  d_normal = (std::min) (d_normal, M_PI - d_normal);
260 
261  double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
262 
263  if (distance < threshold)
264  {
265  // Returns the indices of the points whose distances are smaller than the threshold
266  inliers.push_back ((*indices_)[i]);
267  error_sqr_dists_.push_back (distance);
268  }
269  }
270 }
271 
272 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
273 template <typename PointT, typename PointNT> std::size_t
275  const Eigen::VectorXf &model_coefficients, const double threshold) const
276 {
277 
278  // Check if the model is valid given the user constraints
279  if (!isModelValid (model_coefficients))
280  return (0);
281 
282  std::size_t nr_p = 0;
283 
284  Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
285  Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
286  const float sin_opening_angle = std::sin (model_coefficients[6]),
287  cos_opening_angle = std::cos (model_coefficients[6]),
288  tan_opening_angle = std::tan (model_coefficients[6]);
289 
290  float apexdotdir = apex.dot (axis_dir);
291  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
292  // Iterate through the 3d points and calculate the distances from them to the cone
293  for (std::size_t i = 0; i < indices_->size (); ++i)
294  {
295  Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
296 
297  // Calculate the point's projection on the cone axis
298  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
299  Eigen::Vector4f pt_proj = apex + k * axis_dir;
300 
301  // Calculate the actual radius of the cone at the level of the projected point
302  Eigen::Vector4f height = apex - pt_proj;
303  double actual_cone_radius = tan_opening_angle * height.norm ();
304 
305  // Approximate the distance from the point to the cone as the difference between
306  // dist(point,cone_axis) and actual cone radius
307  const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
308  if (weighted_euclid_dist > threshold) // Early termination: cannot be an inlier
309  continue;
310 
311  // Calculate the direction of the point from center
312  Eigen::Vector4f pp_pt_dir = pt - pt_proj;
313  pp_pt_dir.normalize ();
314 
315  // Calculate the cones perfect normals
316  height.normalize ();
317  Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
318 
319  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
320  Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
321  double d_normal = std::abs (getAngle3D (n, cone_normal));
322  d_normal = (std::min) (d_normal, M_PI - d_normal);
323 
324  if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
325  nr_p++;
326  }
327  return (nr_p);
328 }
329 
330 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
331 template <typename PointT, typename PointNT> void
333  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
334 {
335  optimized_coefficients = model_coefficients;
336 
337  // Needs a set of valid model coefficients
338  if (!isModelValid (model_coefficients))
339  {
340  PCL_ERROR ("[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
341  return;
342  }
343 
344  // Need more than the minimum sample size to make a difference
345  if (inliers.size () <= sample_size_)
346  {
347  PCL_ERROR ("[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
348  return;
349  }
350 
351  Eigen::ArrayXf pts_x(inliers.size());
352  Eigen::ArrayXf pts_y(inliers.size());
353  Eigen::ArrayXf pts_z(inliers.size());
354  std::size_t pos = 0;
355  for(const auto& index : inliers) {
356  pts_x[pos] = (*input_)[index].x;
357  pts_y[pos] = (*input_)[index].y;
358  pts_z[pos] = (*input_)[index].z;
359  ++pos;
360  }
361  pcl::internal::optimizeModelCoefficientsCone(optimized_coefficients, pts_x, pts_y, pts_z);
362 
363  PCL_DEBUG ("[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Initial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
364  model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
365  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]);
366 
367  Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
368  line_dir.normalize ();
369  optimized_coefficients[3] = line_dir[0];
370  optimized_coefficients[4] = line_dir[1];
371  optimized_coefficients[5] = line_dir[2];
372 }
373 
374 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
375 template <typename PointT, typename PointNT> void
377  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
378 {
379  // Needs a valid set of model coefficients
380  if (!isModelValid (model_coefficients))
381  {
382  PCL_ERROR ("[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
383  return;
384  }
385 
386  projected_points.header = input_->header;
387  projected_points.is_dense = input_->is_dense;
388 
389  Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
390  Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
391  const float tan_opening_angle = std::tan (model_coefficients[6]);
392 
393  float apexdotdir = apex.dot (axis_dir);
394  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
395 
396  // Copy all the data fields from the input cloud to the projected one?
397  if (copy_data_fields)
398  {
399  // Allocate enough space and copy the basics
400  projected_points.resize (input_->size ());
401  projected_points.width = input_->width;
402  projected_points.height = input_->height;
403 
404  using FieldList = typename pcl::traits::fieldList<PointT>::type;
405  // Iterate over each point
406  for (std::size_t i = 0; i < projected_points.size (); ++i)
407  // Iterate over each dimension
408  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[i], projected_points[i]));
409 
410  // Iterate through the 3d points and calculate the distances from them to the cone
411  for (const auto &inlier : inliers)
412  {
413  Eigen::Vector4f pt ((*input_)[inlier].x,
414  (*input_)[inlier].y,
415  (*input_)[inlier].z,
416  1);
417 
418  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
419 
420  pcl::Vector4fMap pp = projected_points[inlier].getVector4fMap ();
421  pp.matrix () = apex + k * axis_dir;
422 
423  Eigen::Vector4f dir = pt - pp;
424  dir.normalize ();
425 
426  // Calculate the actual radius of the cone at the level of the projected point
427  Eigen::Vector4f height = apex - pp;
428  float actual_cone_radius = tan_opening_angle * height.norm ();
429 
430  // Calculate the projection of the point onto the cone
431  pp += dir * actual_cone_radius;
432  }
433  }
434  else
435  {
436  // Allocate enough space and copy the basics
437  projected_points.resize (inliers.size ());
438  projected_points.width = inliers.size ();
439  projected_points.height = 1;
440 
441  using FieldList = typename pcl::traits::fieldList<PointT>::type;
442  // Iterate over each point
443  for (std::size_t i = 0; i < inliers.size (); ++i)
444  // Iterate over each dimension
445  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[inliers[i]], projected_points[i]));
446 
447  // Iterate through the 3d points and calculate the distances from them to the cone
448  for (std::size_t i = 0; i < inliers.size (); ++i)
449  {
450  pcl::Vector4fMap pp = projected_points[i].getVector4fMap ();
451  pcl::Vector4fMapConst pt = (*input_)[inliers[i]].getVector4fMap ();
452 
453  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
454  // Calculate the projection of the point on the line
455  pp.matrix () = apex + k * axis_dir;
456 
457  Eigen::Vector4f dir = pt - pp;
458  dir.normalize ();
459 
460  // Calculate the actual radius of the cone at the level of the projected point
461  Eigen::Vector4f height = apex - pp;
462  float actual_cone_radius = tan_opening_angle * height.norm ();
463 
464  // Calculate the projection of the point onto the cone
465  pp += dir * actual_cone_radius;
466  }
467  }
468 }
469 
470 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
471 template <typename PointT, typename PointNT> bool
473  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
474 {
475  // Needs a valid model coefficients
476  if (!isModelValid (model_coefficients))
477  {
478  PCL_ERROR ("[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
479  return (false);
480  }
481 
482  Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
483  Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
484  const float tan_opening_angle = std::tan (model_coefficients[6]);
485 
486  float apexdotdir = apex.dot (axis_dir);
487  float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
488 
489  // Iterate through the 3d points and calculate the distances from them to the cone
490  for (const auto &index : indices)
491  {
492  Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
493 
494  // Calculate the point's projection on the cone axis
495  float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
496  Eigen::Vector4f pt_proj = apex + k * axis_dir;
497  Eigen::Vector4f dir = pt - pt_proj;
498  dir.normalize ();
499 
500  // Calculate the actual radius of the cone at the level of the projected point
501  Eigen::Vector4f height = apex - pt_proj;
502  double actual_cone_radius = tan_opening_angle * height.norm ();
503 
504  // Approximate the distance from the point to the cone as the difference between
505  // dist(point,cone_axis) and actual cone radius
506  if (std::abs (static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
507  return (false);
508  }
509 
510  return (true);
511 }
512 
513 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
514 template <typename PointT, typename PointNT> double
516  const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const
517 {
518  Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
519  Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
520  return sqrt(pcl::sqrPointToLineDistance (pt, apex, axis_dir));
521 }
522 
523 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
524 template <typename PointT, typename PointNT> bool
525 pcl::SampleConsensusModelCone<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
526 {
527  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
528  return (false);
529 
530  // Check against template, if given
531  if (eps_angle_ > 0.0)
532  {
533  // Obtain the cone direction
534  const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
535 
536  double angle_diff = std::abs (getAngle3D (axis_, coeff));
537  angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
538  // Check whether the current cone model satisfies our angle threshold criterion with respect to the given axis
539  if (angle_diff > eps_angle_)
540  {
541  PCL_DEBUG ("[pcl::SampleConsensusModelCone::isModelValid] Angle between cone direction and given axis is too large.\n");
542  return (false);
543  }
544  }
545 
546  if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
547  {
548  PCL_DEBUG ("[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too small: should be larger than %g, but is %g.\n",
549  min_angle_, model_coefficients[6]);
550  return (false);
551  }
552  if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
553  {
554  PCL_DEBUG ("[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too big: should be smaller than %g, but is %g.\n",
555  max_angle_, model_coefficients[6]);
556  return (false);
557  }
558 
559  return (true);
560 }
561 
562 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<PointT, PointNT>;
563 
564 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
565 
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:71
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
PCL_EXPORTS int optimizeModelCoefficientsCone(Eigen::VectorXf &coeff, const Eigen::ArrayXf &pts_x, const Eigen::ArrayXf &pts_y, const Eigen::ArrayXf &pts_z)
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