Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
sac_model_circle3d.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  *
37  */
38 
39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
41 
42 #include <pcl/sample_consensus/eigen.h>
43 #include <pcl/sample_consensus/sac_model_circle3d.h>
44 #include <pcl/common/concatenate.h>
45 
46 //////////////////////////////////////////////////////////////////////////
47 template <typename PointT> bool
49  const std::vector<int> &samples) const
50 {
51  // Get the values at the three points
52  Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
53  Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
54  Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
55 
56  // calculate vectors between points
57  p1 -= p0;
58  p2 -= p0;
59 
60  if (p1.dot (p2) < 0.000001)
61  return (true);
62  else
63  return (false);
64 }
65 
66 //////////////////////////////////////////////////////////////////////////
67 template <typename PointT> bool
68 pcl::SampleConsensusModelCircle3D<PointT>::computeModelCoefficients (const std::vector<int> &samples, Eigen::VectorXf &model_coefficients) const
69 {
70  // Need 3 samples
71  if (samples.size () != 3)
72  {
73  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
74  return (false);
75  }
76 
77  model_coefficients.resize (7); //needing 7 coefficients: centerX, centerY, centerZ, radius, normalX, normalY, normalZ
78 
79  Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
80  Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
81  Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
82 
83 
84  Eigen::Vector3d helper_vec01 = p0 - p1;
85  Eigen::Vector3d helper_vec02 = p0 - p2;
86  Eigen::Vector3d helper_vec10 = p1 - p0;
87  Eigen::Vector3d helper_vec12 = p1 - p2;
88  Eigen::Vector3d helper_vec20 = p2 - p0;
89  Eigen::Vector3d helper_vec21 = p2 - p1;
90 
91  Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
92 
93  double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
94 
95  double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
96  double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
97  double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
98 
99  Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
100 
101  Eigen::Vector3d circle_radiusVector = circle_center - p0;
102  double circle_radius = circle_radiusVector.norm ();
103  Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
104 
105  model_coefficients[0] = static_cast<float> (circle_center[0]);
106  model_coefficients[1] = static_cast<float> (circle_center[1]);
107  model_coefficients[2] = static_cast<float> (circle_center[2]);
108  model_coefficients[3] = static_cast<float> (circle_radius);
109  model_coefficients[4] = static_cast<float> (circle_normal[0]);
110  model_coefficients[5] = static_cast<float> (circle_normal[1]);
111  model_coefficients[6] = static_cast<float> (circle_normal[2]);
112 
113  return (true);
114 }
115 
116 //////////////////////////////////////////////////////////////////////////
117 template <typename PointT> void
118 pcl::SampleConsensusModelCircle3D<PointT>::getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
119 {
120  // Check if the model is valid given the user constraints
121  if (!isModelValid (model_coefficients))
122  {
123  distances.clear ();
124  return;
125  }
126  distances.resize (indices_->size ());
127 
128  // Iterate through the 3d points and calculate the distances from them to the sphere
129  for (size_t i = 0; i < indices_->size (); ++i)
130  // Calculate the distance from the point to the circle:
131  // 1. calculate intersection point of the plane in which the circle lies and the
132  // line from the sample point with the direction of the plane normal (projected point)
133  // 2. calculate the intersection point of the line from the circle center to the projected point
134  // with the circle
135  // 3. calculate distance from corresponding point on the circle to the sample point
136  {
137  // what i have:
138  // P : Sample Point
139  Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
140  // C : Circle Center
141  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
142  // N : Circle (Plane) Normal
143  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
144  // r : Radius
145  double r = model_coefficients[3];
146 
147  Eigen::Vector3d helper_vectorPC = P - C;
148  // 1.1. get line parameter
149  double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
150 
151  // Projected Point on plane
152  Eigen::Vector3d P_proj = P + lambda * N;
153  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
154 
155  // K : Point on Circle
156  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
157  Eigen::Vector3d distanceVector = P - K;
158 
159  distances[i] = distanceVector.norm ();
160  }
161 }
162 
163 //////////////////////////////////////////////////////////////////////////
164 template <typename PointT> void
166  const Eigen::VectorXf &model_coefficients, const double threshold,
167  std::vector<int> &inliers)
168 {
169  // Check if the model is valid given the user constraints
170  if (!isModelValid (model_coefficients))
171  {
172  inliers.clear ();
173  return;
174  }
175  int nr_p = 0;
176  inliers.resize (indices_->size ());
177 
178  // Iterate through the 3d points and calculate the distances from them to the sphere
179  for (size_t i = 0; i < indices_->size (); ++i)
180  {
181  // what i have:
182  // P : Sample Point
183  Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
184  // C : Circle Center
185  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
186  // N : Circle (Plane) Normal
187  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
188  // r : Radius
189  double r = model_coefficients[3];
190 
191  Eigen::Vector3d helper_vectorPC = P - C;
192  // 1.1. get line parameter
193  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
194  // Projected Point on plane
195  Eigen::Vector3d P_proj = P + lambda * N;
196  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
197 
198  // K : Point on Circle
199  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
200  Eigen::Vector3d distanceVector = P - K;
201 
202  if (distanceVector.norm () < threshold)
203  {
204  // Returns the indices of the points whose distances are smaller than the threshold
205  inliers[nr_p] = (*indices_)[i];
206  nr_p++;
207  }
208  }
209  inliers.resize (nr_p);
210 }
211 
212 //////////////////////////////////////////////////////////////////////////
213 template <typename PointT> int
215  const Eigen::VectorXf &model_coefficients, const double threshold) const
216 {
217  // Check if the model is valid given the user constraints
218  if (!isModelValid (model_coefficients))
219  return (0);
220  int nr_p = 0;
221 
222  // Iterate through the 3d points and calculate the distances from them to the sphere
223  for (size_t i = 0; i < indices_->size (); ++i)
224  {
225  // what i have:
226  // P : Sample Point
227  Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
228  // C : Circle Center
229  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
230  // N : Circle (Plane) Normal
231  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
232  // r : Radius
233  double r = model_coefficients[3];
234 
235  Eigen::Vector3d helper_vectorPC = P - C;
236  // 1.1. get line parameter
237  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
238 
239  // Projected Point on plane
240  Eigen::Vector3d P_proj = P + lambda * N;
241  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
242 
243  // K : Point on Circle
244  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
245  Eigen::Vector3d distanceVector = P - K;
246 
247  if (distanceVector.norm () < threshold)
248  nr_p++;
249  }
250  return (nr_p);
251 }
252 
253 //////////////////////////////////////////////////////////////////////////
254 template <typename PointT> void
256  const std::vector<int> &inliers,
257  const Eigen::VectorXf &model_coefficients,
258  Eigen::VectorXf &optimized_coefficients) const
259 {
260  optimized_coefficients = model_coefficients;
261 
262  // Needs a set of valid model coefficients
263  if (model_coefficients.size () != 7)
264  {
265  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
266  return;
267  }
268 
269  // Need at least 3 samples
270  if (inliers.size () <= 3)
271  {
272  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers found to support a model (%lu)! Returning the same coefficients.\n", inliers.size ());
273  return;
274  }
275 
276  OptimizationFunctor functor (this, inliers);
277  Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
278  Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, double> lm (num_diff);
279  Eigen::VectorXd coeff;
280  int info = lm.minimize (coeff);
281  for (int i = 0; i < coeff.size (); ++i)
282  optimized_coefficients[i] = static_cast<float> (coeff[i]);
283 
284  // Compute the L2 norm of the residuals
285  PCL_DEBUG ("[pcl::SampleConsensusModelCircle3D::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",
286  info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], 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]);
287 }
288 
289 //////////////////////////////////////////////////////////////////////////
290 template <typename PointT> void
292  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients,
293  PointCloud &projected_points, bool copy_data_fields) const
294 {
295  // Needs a valid set of model coefficients
296  if (model_coefficients.size () != 7)
297  {
298  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::projectPoints] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
299  return;
300  }
301 
302  projected_points.header = input_->header;
303  projected_points.is_dense = input_->is_dense;
304 
305  // Copy all the data fields from the input cloud to the projected one?
306  if (copy_data_fields)
307  {
308  // Allocate enough space and copy the basics
309  projected_points.points.resize (input_->points.size ());
310  projected_points.width = input_->width;
311  projected_points.height = input_->height;
312 
313  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
314  // Iterate over each point
315  for (size_t i = 0; i < projected_points.points.size (); ++i)
316  // Iterate over each dimension
317  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
318 
319  // Iterate through the 3d points and calculate the distances from them to the plane
320  for (size_t i = 0; i < inliers.size (); ++i)
321  {
322  // what i have:
323  // P : Sample Point
324  Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
325  // C : Circle Center
326  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
327  // N : Circle (Plane) Normal
328  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
329  // r : Radius
330  double r = model_coefficients[3];
331 
332  Eigen::Vector3d helper_vectorPC = P - C;
333  // 1.1. get line parameter
334  //float lambda = (helper_vectorPC.dot(N)) / N.squaredNorm() ;
335  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
336  // Projected Point on plane
337  Eigen::Vector3d P_proj = P + lambda * N;
338  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
339 
340  // K : Point on Circle
341  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
342 
343  projected_points.points[i].x = static_cast<float> (K[0]);
344  projected_points.points[i].y = static_cast<float> (K[1]);
345  projected_points.points[i].z = static_cast<float> (K[2]);
346  }
347  }
348  else
349  {
350  // Allocate enough space and copy the basics
351  projected_points.points.resize (inliers.size ());
352  projected_points.width = uint32_t (inliers.size ());
353  projected_points.height = 1;
354 
355  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
356  // Iterate over each point
357  for (size_t i = 0; i < inliers.size (); ++i)
358  // Iterate over each dimension
359  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
360 
361  // Iterate through the 3d points and calculate the distances from them to the plane
362  for (size_t i = 0; i < inliers.size (); ++i)
363  {
364  // what i have:
365  // P : Sample Point
366  Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
367  // C : Circle Center
368  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
369  // N : Circle (Plane) Normal
370  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
371  // r : Radius
372  double r = model_coefficients[3];
373 
374  Eigen::Vector3d helper_vectorPC = P - C;
375  // 1.1. get line parameter
376  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
377  // Projected Point on plane
378  Eigen::Vector3d P_proj = P + lambda * N;
379  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
380 
381  // K : Point on Circle
382  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
383 
384  projected_points.points[i].x = static_cast<float> (K[0]);
385  projected_points.points[i].y = static_cast<float> (K[1]);
386  projected_points.points[i].z = static_cast<float> (K[2]);
387  }
388  }
389 }
390 
391 //////////////////////////////////////////////////////////////////////////
392 template <typename PointT> bool
394  const std::set<int> &indices,
395  const Eigen::VectorXf &model_coefficients,
396  const double threshold) const
397 {
398  // Needs a valid model coefficients
399  if (model_coefficients.size () != 7)
400  {
401  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
402  return (false);
403  }
404 
405  for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
406  {
407  // Calculate the distance from the point to the sphere as the difference between
408  //dist(point,sphere_origin) and sphere_radius
409 
410  // what i have:
411  // P : Sample Point
412  Eigen::Vector3d P (input_->points[*it].x, input_->points[*it].y, input_->points[*it].z);
413  // C : Circle Center
414  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
415  // N : Circle (Plane) Normal
416  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
417  // r : Radius
418  double r = model_coefficients[3];
419  Eigen::Vector3d helper_vectorPC = P - C;
420  // 1.1. get line parameter
421  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
422  // Projected Point on plane
423  Eigen::Vector3d P_proj = P + lambda * N;
424  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
425 
426  // K : Point on Circle
427  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
428  Eigen::Vector3d distanceVector = P - K;
429 
430  if (distanceVector.norm () > threshold)
431  return (false);
432  }
433  return (true);
434 }
435 
436 //////////////////////////////////////////////////////////////////////////
437 template <typename PointT> bool
438 pcl::SampleConsensusModelCircle3D<PointT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
439 {
440  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
441  return (false);
442 
443  if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
444  return (false);
445  if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
446  return (false);
447 
448  return (true);
449 }
450 
451 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
452 
453 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_
454 
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Compute all distances from the cloud data to a given 3D circle model.
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
Recompute the 3d circle coefficients using the given inlier set and return them to the user...
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all distances from the cloud data to a given 3D circle 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:66
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const
Create a new point cloud with inliers projected onto the 3d circle model.
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
PointCloud represents the base class in PCL for storing collections of 3D points. ...
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
Verify whether a subset of indices verifies the given 3d circle model coefficients.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
Definition: norms.h:55
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
Helper functor structure for concatenate.
Definition: concatenate.h:64
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
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407