Point Cloud Library (PCL)  1.7.1
sac_model_cylinder.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2010, 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  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
43 
44 #include <pcl/sample_consensus/eigen.h>
45 #include <pcl/sample_consensus/sac_model_cylinder.h>
46 #include <pcl/common/concatenate.h>
47 
48 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49 template <typename PointT, typename PointNT> bool
51 {
52  return (true);
53 }
54 
55 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56 template <typename PointT, typename PointNT> bool
58  const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
59 {
60  // Need 2 samples
61  if (samples.size () != 2)
62  {
63  PCL_ERROR ("[pcl::SampleConsensusModelCylinder::computeModelCoefficients] Invalid set of samples given (%zu)!\n", samples.size ());
64  return (false);
65  }
66 
67  if (!normals_)
68  {
69  PCL_ERROR ("[pcl::SampleConsensusModelCylinder::computeModelCoefficients] No input dataset containing normals was given!\n");
70  return (false);
71  }
72 
73  if (fabs (input_->points[samples[0]].x - input_->points[samples[1]].x) <= std::numeric_limits<float>::epsilon () &&
74  fabs (input_->points[samples[0]].y - input_->points[samples[1]].y) <= std::numeric_limits<float>::epsilon () &&
75  fabs (input_->points[samples[0]].z - input_->points[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
76  {
77  return (false);
78  }
79 
80  Eigen::Vector4f p1 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z, 0);
81  Eigen::Vector4f p2 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z, 0);
82 
83  Eigen::Vector4f n1 (normals_->points[samples[0]].normal[0], normals_->points[samples[0]].normal[1], normals_->points[samples[0]].normal[2], 0);
84  Eigen::Vector4f n2 (normals_->points[samples[1]].normal[0], normals_->points[samples[1]].normal[1], normals_->points[samples[1]].normal[2], 0);
85  Eigen::Vector4f w = n1 + p1 - p2;
86 
87  float a = n1.dot (n1);
88  float b = n1.dot (n2);
89  float c = n2.dot (n2);
90  float d = n1.dot (w);
91  float e = n2.dot (w);
92  float denominator = a*c - b*b;
93  float sc, tc;
94  // Compute the line parameters of the two closest points
95  if (denominator < 1e-8) // The lines are almost parallel
96  {
97  sc = 0.0f;
98  tc = (b > c ? d / b : e / c); // Use the largest denominator
99  }
100  else
101  {
102  sc = (b*e - c*d) / denominator;
103  tc = (a*e - b*d) / denominator;
104  }
105 
106  // point_on_axis, axis_direction
107  Eigen::Vector4f line_pt = p1 + n1 + sc * n1;
108  Eigen::Vector4f line_dir = p2 + tc * n2 - line_pt;
109  line_dir.normalize ();
110 
111  model_coefficients.resize (7);
112  // model_coefficients.template head<3> () = line_pt.template head<3> ();
113  model_coefficients[0] = line_pt[0];
114  model_coefficients[1] = line_pt[1];
115  model_coefficients[2] = line_pt[2];
116  // model_coefficients.template segment<3> (3) = line_dir.template head<3> ();
117  model_coefficients[3] = line_dir[0];
118  model_coefficients[4] = line_dir[1];
119  model_coefficients[5] = line_dir[2];
120  // cylinder radius
121  model_coefficients[6] = static_cast<float> (sqrt (pcl::sqrPointToLineDistance (p1, line_pt, line_dir)));
122 
123  if (model_coefficients[6] > radius_max_ || model_coefficients[6] < radius_min_)
124  return (false);
125 
126  return (true);
127 }
128 
129 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130 template <typename PointT, typename PointNT> void
132  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
133 {
134  // Check if the model is valid given the user constraints
135  if (!isModelValid (model_coefficients))
136  {
137  distances.clear ();
138  return;
139  }
140 
141  distances.resize (indices_->size ());
142 
143  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
144  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
145  float ptdotdir = line_pt.dot (line_dir);
146  float dirdotdir = 1.0f / line_dir.dot (line_dir);
147  // Iterate through the 3d points and calculate the distances from them to the sphere
148  for (size_t i = 0; i < indices_->size (); ++i)
149  {
150  // Aproximate the distance from the point to the cylinder as the difference between
151  // dist(point,cylinder_axis) and cylinder radius
152  // @note need to revise this.
153  Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
154  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
155 
156  double d_euclid = fabs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
157 
158  // Calculate the point's projection on the cylinder axis
159  float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
160  Eigen::Vector4f pt_proj = line_pt + k * line_dir;
161  Eigen::Vector4f dir = pt - pt_proj;
162  dir.normalize ();
163 
164  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
165  double d_normal = fabs (getAngle3D (n, dir));
166  d_normal = (std::min) (d_normal, M_PI - d_normal);
167 
168  distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
169  }
170 }
171 
172 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
173 template <typename PointT, typename PointNT> void
175  const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
176 {
177  // Check if the model is valid given the user constraints
178  if (!isModelValid (model_coefficients))
179  {
180  inliers.clear ();
181  return;
182  }
183 
184  int nr_p = 0;
185  inliers.resize (indices_->size ());
186  error_sqr_dists_.resize (indices_->size ());
187 
188  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
189  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
190  float ptdotdir = line_pt.dot (line_dir);
191  float dirdotdir = 1.0f / line_dir.dot (line_dir);
192  // Iterate through the 3d points and calculate the distances from them to the sphere
193  for (size_t i = 0; i < indices_->size (); ++i)
194  {
195  // Aproximate the distance from the point to the cylinder as the difference between
196  // dist(point,cylinder_axis) and cylinder radius
197  Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
198  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
199  double d_euclid = fabs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
200 
201  // Calculate the point's projection on the cylinder axis
202  float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
203  Eigen::Vector4f pt_proj = line_pt + k * line_dir;
204  Eigen::Vector4f dir = pt - pt_proj;
205  dir.normalize ();
206 
207  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
208  double d_normal = fabs (getAngle3D (n, dir));
209  d_normal = (std::min) (d_normal, M_PI - d_normal);
210 
211  double distance = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
212  if (distance < threshold)
213  {
214  // Returns the indices of the points whose distances are smaller than the threshold
215  inliers[nr_p] = (*indices_)[i];
216  error_sqr_dists_[nr_p] = distance;
217  ++nr_p;
218  }
219  }
220  inliers.resize (nr_p);
221  error_sqr_dists_.resize (nr_p);
222 }
223 
224 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
225 template <typename PointT, typename PointNT> int
227  const Eigen::VectorXf &model_coefficients, const double threshold)
228 {
229  // Check if the model is valid given the user constraints
230  if (!isModelValid (model_coefficients))
231  return (0);
232 
233  int nr_p = 0;
234 
235  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
236  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
237  float ptdotdir = line_pt.dot (line_dir);
238  float dirdotdir = 1.0f / line_dir.dot (line_dir);
239  // Iterate through the 3d points and calculate the distances from them to the sphere
240  for (size_t i = 0; i < indices_->size (); ++i)
241  {
242  // Aproximate the distance from the point to the cylinder as the difference between
243  // dist(point,cylinder_axis) and cylinder radius
244  Eigen::Vector4f pt (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
245  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
246  double d_euclid = fabs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]);
247 
248  // Calculate the point's projection on the cylinder axis
249  float k = (pt.dot (line_dir) - ptdotdir) * dirdotdir;
250  Eigen::Vector4f pt_proj = line_pt + k * line_dir;
251  Eigen::Vector4f dir = pt - pt_proj;
252  dir.normalize ();
253 
254  // Calculate the angular distance between the point normal and the (dir=pt_proj->pt) vector
255  double d_normal = fabs (getAngle3D (n, dir));
256  d_normal = (std::min) (d_normal, M_PI - d_normal);
257 
258  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
259  nr_p++;
260  }
261  return (nr_p);
262 }
263 
264 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
265 template <typename PointT, typename PointNT> void
267  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
268 {
269  optimized_coefficients = model_coefficients;
270 
271  // Needs a set of valid model coefficients
272  if (model_coefficients.size () != 7)
273  {
274  PCL_ERROR ("[pcl::SampleConsensusModelCylinder::optimizeModelCoefficients] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
275  return;
276  }
277 
278  if (inliers.empty ())
279  {
280  PCL_DEBUG ("[pcl::SampleConsensusModelCylinder:optimizeModelCoefficients] Inliers vector empty! Returning the same coefficients.\n");
281  return;
282  }
283 
284  tmp_inliers_ = &inliers;
285 
286  OptimizationFunctor functor (static_cast<int> (inliers.size ()), this);
287  Eigen::NumericalDiff<OptimizationFunctor > num_diff (functor);
288  Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, float> lm (num_diff);
289  int info = lm.minimize (optimized_coefficients);
290 
291  // Compute the L2 norm of the residuals
292  PCL_DEBUG ("[pcl::SampleConsensusModelCylinder::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",
293  info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
294  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]);
295 
296  Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
297  line_dir.normalize ();
298  optimized_coefficients[3] = line_dir[0];
299  optimized_coefficients[4] = line_dir[1];
300  optimized_coefficients[5] = line_dir[2];
301 }
302 
303 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
304 template <typename PointT, typename PointNT> void
306  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields)
307 {
308  // Needs a valid set of model coefficients
309  if (model_coefficients.size () != 7)
310  {
311  PCL_ERROR ("[pcl::SampleConsensusModelCylinder::projectPoints] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
312  return;
313  }
314 
315  projected_points.header = input_->header;
316  projected_points.is_dense = input_->is_dense;
317 
318  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
319  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
320  float ptdotdir = line_pt.dot (line_dir);
321  float dirdotdir = 1.0f / line_dir.dot (line_dir);
322 
323  // Copy all the data fields from the input cloud to the projected one?
324  if (copy_data_fields)
325  {
326  // Allocate enough space and copy the basics
327  projected_points.points.resize (input_->points.size ());
328  projected_points.width = input_->width;
329  projected_points.height = input_->height;
330 
331  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
332  // Iterate over each point
333  for (size_t i = 0; i < projected_points.points.size (); ++i)
334  // Iterate over each dimension
335  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
336 
337  // Iterate through the 3d points and calculate the distances from them to the cylinder
338  for (size_t i = 0; i < inliers.size (); ++i)
339  {
340  Eigen::Vector4f p (input_->points[inliers[i]].x,
341  input_->points[inliers[i]].y,
342  input_->points[inliers[i]].z,
343  1);
344 
345  float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
346 
347  pcl::Vector4fMap pp = projected_points.points[inliers[i]].getVector4fMap ();
348  pp.matrix () = line_pt + k * line_dir;
349 
350  Eigen::Vector4f dir = p - pp;
351  dir.normalize ();
352 
353  // Calculate the projection of the point onto the cylinder
354  pp += dir * model_coefficients[6];
355  }
356  }
357  else
358  {
359  // Allocate enough space and copy the basics
360  projected_points.points.resize (inliers.size ());
361  projected_points.width = static_cast<uint32_t> (inliers.size ());
362  projected_points.height = 1;
363 
364  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
365  // Iterate over each point
366  for (size_t i = 0; i < inliers.size (); ++i)
367  // Iterate over each dimension
368  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
369 
370  // Iterate through the 3d points and calculate the distances from them to the plane
371  for (size_t i = 0; i < inliers.size (); ++i)
372  {
373  pcl::Vector4fMap pp = projected_points.points[i].getVector4fMap ();
374  pcl::Vector4fMapConst p = input_->points[inliers[i]].getVector4fMap ();
375 
376  float k = (p.dot (line_dir) - ptdotdir) * dirdotdir;
377  // Calculate the projection of the point on the line
378  pp.matrix () = line_pt + k * line_dir;
379 
380  Eigen::Vector4f dir = p - pp;
381  dir.normalize ();
382 
383  // Calculate the projection of the point onto the cylinder
384  pp += dir * model_coefficients[6];
385  }
386  }
387 }
388 
389 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390 template <typename PointT, typename PointNT> bool
392  const std::set<int> &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
393 {
394  // Needs a valid model coefficients
395  if (model_coefficients.size () != 7)
396  {
397  PCL_ERROR ("[pcl::SampleConsensusModelCylinder::doSamplesVerifyModel] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
398  return (false);
399  }
400 
401  for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
402  {
403  // Aproximate the distance from the point to the cylinder as the difference between
404  // dist(point,cylinder_axis) and cylinder radius
405  // @note need to revise this.
406  Eigen::Vector4f pt (input_->points[*it].x, input_->points[*it].y, input_->points[*it].z, 0);
407  if (fabs (pointToLineDistance (pt, model_coefficients) - model_coefficients[6]) > threshold)
408  return (false);
409  }
410 
411  return (true);
412 }
413 
414 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
415 template <typename PointT, typename PointNT> double
417  const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients)
418 {
419  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
420  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
421  return sqrt(pcl::sqrPointToLineDistance (pt, line_pt, line_dir));
422 }
423 
424 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
425 template <typename PointT, typename PointNT> void
427  const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj)
428 {
429  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
430  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
431 
432  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) * line_dir.dot (line_dir);
433  pt_proj = line_pt + k * line_dir;
434 
435  Eigen::Vector4f dir = pt - pt_proj;
436  dir.normalize ();
437 
438  // Calculate the projection of the point onto the cylinder
439  pt_proj += dir * model_coefficients[6];
440 }
441 
442 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
443 template <typename PointT, typename PointNT> bool
444 pcl::SampleConsensusModelCylinder<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients)
445 {
446  // Needs a valid model coefficients
447  if (model_coefficients.size () != 7)
448  {
449  PCL_ERROR ("[pcl::SampleConsensusModelCylinder::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
450  return (false);
451  }
452 
453  // Check against template, if given
454  if (eps_angle_ > 0.0)
455  {
456  // Obtain the cylinder direction
457  Eigen::Vector4f coeff;
458  coeff[0] = model_coefficients[3];
459  coeff[1] = model_coefficients[4];
460  coeff[2] = model_coefficients[5];
461  coeff[3] = 0;
462 
463  Eigen::Vector4f axis (axis_[0], axis_[1], axis_[2], 0);
464  double angle_diff = fabs (getAngle3D (axis, coeff));
465  angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
466  // Check whether the current cylinder model satisfies our angle threshold criterion with respect to the given axis
467  if (angle_diff > eps_angle_)
468  return (false);
469  }
470 
471  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[6] < radius_min_)
472  return (false);
473  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[6] > radius_max_)
474  return (false);
475 
476  return (true);
477 }
478 
479 #define PCL_INSTANTIATE_SampleConsensusModelCylinder(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCylinder<PointT, PointNT>;
480 
481 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CYLINDER_H_
482