Point Cloud Library (PCL)  1.11.0
 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 Indices &samples) const
50 {
51  if (samples.size () != sample_size_)
52  {
53  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
54  return (false);
55  }
56  // Get the values at the three points
57  Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
58  Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
59  Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
60 
61  // calculate vectors between points
62  p1 -= p0;
63  p2 -= p0;
64 
65  return (p1.dot (p2) < 0.000001);
66 }
67 
68 //////////////////////////////////////////////////////////////////////////
69 template <typename PointT> bool
70 pcl::SampleConsensusModelCircle3D<PointT>::computeModelCoefficients (const Indices &samples, Eigen::VectorXf &model_coefficients) const
71 {
72  // Need 3 samples
73  if (samples.size () != sample_size_)
74  {
75  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
76  return (false);
77  }
78 
79  model_coefficients.resize (model_size_); //needing 7 coefficients: centerX, centerY, centerZ, radius, normalX, normalY, normalZ
80 
81  Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
82  Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
83  Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
84 
85 
86  Eigen::Vector3d helper_vec01 = p0 - p1;
87  Eigen::Vector3d helper_vec02 = p0 - p2;
88  Eigen::Vector3d helper_vec10 = p1 - p0;
89  Eigen::Vector3d helper_vec12 = p1 - p2;
90  Eigen::Vector3d helper_vec20 = p2 - p0;
91  Eigen::Vector3d helper_vec21 = p2 - p1;
92 
93  Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
94 
95  double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
96 
97  double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
98  double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
99  double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
100 
101  Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
102 
103  Eigen::Vector3d circle_radiusVector = circle_center - p0;
104  double circle_radius = circle_radiusVector.norm ();
105  Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
106 
107  model_coefficients[0] = static_cast<float> (circle_center[0]);
108  model_coefficients[1] = static_cast<float> (circle_center[1]);
109  model_coefficients[2] = static_cast<float> (circle_center[2]);
110  model_coefficients[3] = static_cast<float> (circle_radius);
111  model_coefficients[4] = static_cast<float> (circle_normal[0]);
112  model_coefficients[5] = static_cast<float> (circle_normal[1]);
113  model_coefficients[6] = static_cast<float> (circle_normal[2]);
114 
115  return (true);
116 }
117 
118 //////////////////////////////////////////////////////////////////////////
119 template <typename PointT> void
120 pcl::SampleConsensusModelCircle3D<PointT>::getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
121 {
122  // Check if the model is valid given the user constraints
123  if (!isModelValid (model_coefficients))
124  {
125  distances.clear ();
126  return;
127  }
128  distances.resize (indices_->size ());
129 
130  // Iterate through the 3d points and calculate the distances from them to the sphere
131  for (std::size_t i = 0; i < indices_->size (); ++i)
132  // Calculate the distance from the point to the circle:
133  // 1. calculate intersection point of the plane in which the circle lies and the
134  // line from the sample point with the direction of the plane normal (projected point)
135  // 2. calculate the intersection point of the line from the circle center to the projected point
136  // with the circle
137  // 3. calculate distance from corresponding point on the circle to the sample point
138  {
139  // what i have:
140  // P : Sample Point
141  Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
142  // C : Circle Center
143  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
144  // N : Circle (Plane) Normal
145  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
146  // r : Radius
147  double r = model_coefficients[3];
148 
149  Eigen::Vector3d helper_vectorPC = P - C;
150  // 1.1. get line parameter
151  double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
152 
153  // Projected Point on plane
154  Eigen::Vector3d P_proj = P + lambda * N;
155  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
156 
157  // K : Point on Circle
158  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
159  Eigen::Vector3d distanceVector = P - K;
160 
161  distances[i] = distanceVector.norm ();
162  }
163 }
164 
165 //////////////////////////////////////////////////////////////////////////
166 template <typename PointT> void
168  const Eigen::VectorXf &model_coefficients, const double threshold,
169  Indices &inliers)
170 {
171  // Check if the model is valid given the user constraints
172  if (!isModelValid (model_coefficients))
173  {
174  inliers.clear ();
175  return;
176  }
177  inliers.clear ();
178  inliers.reserve (indices_->size ());
179 
180  // Iterate through the 3d points and calculate the distances from them to the sphere
181  for (std::size_t i = 0; i < indices_->size (); ++i)
182  {
183  // what i have:
184  // P : Sample Point
185  Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
186  // C : Circle Center
187  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
188  // N : Circle (Plane) Normal
189  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
190  // r : Radius
191  double r = model_coefficients[3];
192 
193  Eigen::Vector3d helper_vectorPC = P - C;
194  // 1.1. get line parameter
195  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
196  // Projected Point on plane
197  Eigen::Vector3d P_proj = P + lambda * N;
198  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
199 
200  // K : Point on Circle
201  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
202  Eigen::Vector3d distanceVector = P - K;
203 
204  if (distanceVector.norm () < threshold)
205  {
206  // Returns the indices of the points whose distances are smaller than the threshold
207  inliers.push_back ((*indices_)[i]);
208  }
209  }
210 }
211 
212 //////////////////////////////////////////////////////////////////////////
213 template <typename PointT> std::size_t
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  std::size_t nr_p = 0;
221 
222  // Iterate through the 3d points and calculate the distances from them to the sphere
223  for (std::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 Indices &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 (!isModelValid (model_coefficients))
264  {
265  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
266  return;
267  }
268 
269  // Need more than the minimum sample size to make a difference
270  if (inliers.size () <= sample_size_)
271  {
272  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%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 (Eigen::Index 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 Indices &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 (!isModelValid (model_coefficients))
297  {
298  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
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  using FieldList = typename pcl::traits::fieldList<PointT>::type;
314  // Iterate over each point
315  for (std::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 (std::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 = std::uint32_t (inliers.size ());
353  projected_points.height = 1;
354 
355  using FieldList = typename pcl::traits::fieldList<PointT>::type;
356  // Iterate over each point
357  for (std::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 (std::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<index_t> &indices,
395  const Eigen::VectorXf &model_coefficients,
396  const double threshold) const
397 {
398  // Needs a valid model coefficients
399  if (!isModelValid (model_coefficients))
400  {
401  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
402  return (false);
403  }
404 
405  for (const auto &index : indices)
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[index].x, input_->points[index].y, input_->points[index].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 
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 3d circle coefficients using the given inlier set and return them to the user...
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.
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
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 3d circle model.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
SampleConsensusModel represents the base model class.
Definition: sac_model.h:70
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given 3D circle model.
Definition: norms.h:54
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
Helper functor structure for concatenate.
Definition: concatenate.h:51
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:418
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 3D circle model.
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 3d circle model coefficients.
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407