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