Point Cloud Library (PCL)  1.7.0
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)
69 {
70  // Need 3 samples
71  if (samples.size () != 3)
72  {
73  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%zu)!\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)
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)
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)
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 (%zu)!\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 (%zu)! Returning the same coefficients.\n", inliers.size ());
273  return;
274  }
275 
276  tmp_inliers_ = &inliers;
277 
278  OptimizationFunctor functor (static_cast<int> (inliers.size ()), this);
279  Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
280  Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, double> lm (num_diff);
281  Eigen::VectorXd coeff;
282  int info = lm.minimize (coeff);
283  for (size_t i = 0; i < coeff.size (); ++i)
284  optimized_coefficients[i] = static_cast<float> (coeff[i]);
285 
286  // Compute the L2 norm of the residuals
287  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",
288  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]);
289 }
290 
291 //////////////////////////////////////////////////////////////////////////
292 template <typename PointT> void
294  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients,
295  PointCloud &projected_points, bool copy_data_fields)
296 {
297  // Needs a valid set of model coefficients
298  if (model_coefficients.size () != 7)
299  {
300  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::projectPoints] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
301  return;
302  }
303 
304  projected_points.header = input_->header;
305  projected_points.is_dense = input_->is_dense;
306 
307  // Copy all the data fields from the input cloud to the projected one?
308  if (copy_data_fields)
309  {
310  // Allocate enough space and copy the basics
311  projected_points.points.resize (input_->points.size ());
312  projected_points.width = input_->width;
313  projected_points.height = input_->height;
314 
315  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
316  // Iterate over each point
317  for (size_t i = 0; i < projected_points.points.size (); ++i)
318  // Iterate over each dimension
319  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
320 
321  // Iterate through the 3d points and calculate the distances from them to the plane
322  for (size_t i = 0; i < inliers.size (); ++i)
323  {
324  // what i have:
325  // P : Sample Point
326  Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
327  // C : Circle Center
328  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
329  // N : Circle (Plane) Normal
330  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
331  // r : Radius
332  double r = model_coefficients[3];
333 
334  Eigen::Vector3d helper_vectorPC = P - C;
335  // 1.1. get line parameter
336  //float lambda = (helper_vectorPC.dot(N)) / N.squaredNorm() ;
337  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
338  // Projected Point on plane
339  Eigen::Vector3d P_proj = P + lambda * N;
340  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
341 
342  // K : Point on Circle
343  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
344 
345  projected_points.points[i].x = static_cast<float> (K[0]);
346  projected_points.points[i].y = static_cast<float> (K[1]);
347  projected_points.points[i].z = static_cast<float> (K[2]);
348  }
349  }
350  else
351  {
352  // Allocate enough space and copy the basics
353  projected_points.points.resize (inliers.size ());
354  projected_points.width = uint32_t (inliers.size ());
355  projected_points.height = 1;
356 
357  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
358  // Iterate over each point
359  for (size_t i = 0; i < inliers.size (); ++i)
360  // Iterate over each dimension
361  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
362 
363  // Iterate through the 3d points and calculate the distances from them to the plane
364  for (size_t i = 0; i < inliers.size (); ++i)
365  {
366  // what i have:
367  // P : Sample Point
368  Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
369  // C : Circle Center
370  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
371  // N : Circle (Plane) Normal
372  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
373  // r : Radius
374  double r = model_coefficients[3];
375 
376  Eigen::Vector3d helper_vectorPC = P - C;
377  // 1.1. get line parameter
378  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
379  // Projected Point on plane
380  Eigen::Vector3d P_proj = P + lambda * N;
381  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
382 
383  // K : Point on Circle
384  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
385 
386  projected_points.points[i].x = static_cast<float> (K[0]);
387  projected_points.points[i].y = static_cast<float> (K[1]);
388  projected_points.points[i].z = static_cast<float> (K[2]);
389  }
390  }
391 }
392 
393 //////////////////////////////////////////////////////////////////////////
394 template <typename PointT> bool
396  const std::set<int> &indices,
397  const Eigen::VectorXf &model_coefficients,
398  const double threshold)
399 {
400  // Needs a valid model coefficients
401  if (model_coefficients.size () != 7)
402  {
403  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
404  return (false);
405  }
406 
407  for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
408  {
409  // Calculate the distance from the point to the sphere as the difference between
410  //dist(point,sphere_origin) and sphere_radius
411 
412  // what i have:
413  // P : Sample Point
414  Eigen::Vector3d P (input_->points[*it].x, input_->points[*it].y, input_->points[*it].z);
415  // C : Circle Center
416  Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
417  // N : Circle (Plane) Normal
418  Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
419  // r : Radius
420  double r = model_coefficients[3];
421  Eigen::Vector3d helper_vectorPC = P - C;
422  // 1.1. get line parameter
423  double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
424  // Projected Point on plane
425  Eigen::Vector3d P_proj = P + lambda * N;
426  Eigen::Vector3d helper_vectorP_projC = P_proj - C;
427 
428  // K : Point on Circle
429  Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
430  Eigen::Vector3d distanceVector = P - K;
431 
432  if (distanceVector.norm () > threshold)
433  return (false);
434  }
435  return (true);
436 }
437 
438 //////////////////////////////////////////////////////////////////////////
439 template <typename PointT> bool
440 pcl::SampleConsensusModelCircle3D<PointT>::isModelValid (const Eigen::VectorXf &model_coefficients)
441 {
442  // Needs a valid model coefficients
443  if (model_coefficients.size () != 7)
444  {
445  PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
446  return (false);
447  }
448 
449  if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
450  return (false);
451  if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
452  return (false);
453 
454  return (true);
455 }
456 
457 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
458 
459 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_
460