Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_circle.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: sac_model_circle.hpp 6144 2012-07-04 22:06:28Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
40 
42 #include <pcl/common/concatenate.h>
43 #include <unsupported/Eigen/NonLinearOptimization>
44 
46 template <typename PointT> bool
47 pcl::SampleConsensusModelCircle2D<PointT>::isSampleGood(const std::vector<int> &samples) const
48 {
49  // Get the values at the two points
50  Eigen::Array2d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y);
51  Eigen::Array2d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y);
52  Eigen::Array2d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y);
53 
54  // Compute the segment values (in 2d) between p1 and p0
55  p1 -= p0;
56  // Compute the segment values (in 2d) between p2 and p0
57  p2 -= p0;
58 
59  Eigen::Array2d dy1dy2 = p1 / p2;
60 
61  return (dy1dy2[0] != dy1dy2[1]);
62 }
63 
65 template <typename PointT> bool
66 pcl::SampleConsensusModelCircle2D<PointT>::computeModelCoefficients (const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
67 {
68  // Need 3 samples
69  if (samples.size () != 3)
70  {
71  PCL_ERROR ("[pcl::SampleConsensusModelCircle2D::computeModelCoefficients] Invalid set of samples given (%zu)!\n", samples.size ());
72  return (false);
73  }
74 
75  model_coefficients.resize (3);
76 
77  Eigen::Vector2d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y);
78  Eigen::Vector2d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y);
79  Eigen::Vector2d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y);
80 
81  Eigen::Vector2d u = (p0 + p1) / 2.0;
82  Eigen::Vector2d v = (p1 + p2) / 2.0;
83 
84  Eigen::Vector2d p1p0dif = p1 - p0;
85  Eigen::Vector2d p2p1dif = p2 - p1;
86  Eigen::Vector2d uvdif = u - v;
87 
88  Eigen::Vector2d m (- p1p0dif[0] / p1p0dif[1], - p2p1dif[0] / p2p1dif[1]);
89 
90  // Center (x, y)
91  model_coefficients[0] = static_cast<float> ((m[0] * u[0] - m[1] * v[0] - uvdif[1] ) / (m[0] - m[1]));
92  model_coefficients[1] = static_cast<float> ((m[0] * m[1] * uvdif[0] + m[0] * v[1] - m[1] * u[1]) / (m[0] - m[1]));
93 
94  // Radius
95  model_coefficients[2] = static_cast<float> (sqrt ((model_coefficients[0] - p0[0]) * (model_coefficients[0] - p0[0]) +
96  (model_coefficients[1] - p0[1]) * (model_coefficients[1] - p0[1])));
97  return (true);
98 }
99 
101 template <typename PointT> void
102 pcl::SampleConsensusModelCircle2D<PointT>::getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
103 {
104  // Check if the model is valid given the user constraints
105  if (!isModelValid (model_coefficients))
106  {
107  distances.clear ();
108  return;
109  }
110  distances.resize (indices_->size ());
111 
112  // Iterate through the 3d points and calculate the distances from them to the sphere
113  for (size_t i = 0; i < indices_->size (); ++i)
114  // Calculate the distance from the point to the circle as the difference between
115  // dist(point,circle_origin) and circle_radius
116  distances[i] = fabsf (sqrtf (
117  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
118  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
119 
120  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
121  ( input_->points[(*indices_)[i]].y - model_coefficients[1] )
122  ) - model_coefficients[2]);
123 }
124 
126 template <typename PointT> void
128  const Eigen::VectorXf &model_coefficients, const double threshold,
129  std::vector<int> &inliers)
130 {
131  // Check if the model is valid given the user constraints
132  if (!isModelValid (model_coefficients))
133  {
134  inliers.clear ();
135  return;
136  }
137  int nr_p = 0;
138  inliers.resize (indices_->size ());
139 
140  // Iterate through the 3d points and calculate the distances from them to the sphere
141  for (size_t i = 0; i < indices_->size (); ++i)
142  {
143  // Calculate the distance from the point to the sphere as the difference between
144  // dist(point,sphere_origin) and sphere_radius
145  float distance = fabsf (sqrtf (
146  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
147  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
148 
149  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
150  ( input_->points[(*indices_)[i]].y - model_coefficients[1] )
151  ) - model_coefficients[2]);
152  if (distance < threshold)
153  {
154  // Returns the indices of the points whose distances are smaller than the threshold
155  inliers[nr_p] = (*indices_)[i];
156  nr_p++;
157  }
158  }
159  inliers.resize (nr_p);
160 }
161 
163 template <typename PointT> int
165  const Eigen::VectorXf &model_coefficients, const double threshold)
166 {
167  // Check if the model is valid given the user constraints
168  if (!isModelValid (model_coefficients))
169  return (0);
170  int nr_p = 0;
171 
172  // Iterate through the 3d points and calculate the distances from them to the sphere
173  for (size_t i = 0; i < indices_->size (); ++i)
174  {
175  // Calculate the distance from the point to the sphere as the difference between
176  // dist(point,sphere_origin) and sphere_radius
177  float distance = fabsf (sqrtf (
178  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
179  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
180 
181  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
182  ( input_->points[(*indices_)[i]].y - model_coefficients[1] )
183  ) - model_coefficients[2]);
184  if (distance < threshold)
185  nr_p++;
186  }
187  return (nr_p);
188 }
189 
191 template <typename PointT> void
193  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
194 {
195  optimized_coefficients = model_coefficients;
196 
197  // Needs a set of valid model coefficients
198  if (model_coefficients.size () != 3)
199  {
200  PCL_ERROR ("[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
201  return;
202  }
203 
204  // Need at least 3 samples
205  if (inliers.size () <= 3)
206  {
207  PCL_ERROR ("[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Not enough inliers found to support a model (%zu)! Returning the same coefficients.\n", inliers.size ());
208  return;
209  }
210 
211  tmp_inliers_ = &inliers;
212 
213  OptimizationFunctor functor (static_cast<int> (inliers.size ()), this);
214  Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
215  Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, float> lm (num_diff);
216  int info = lm.minimize (optimized_coefficients);
217 
218  // Compute the L2 norm of the residuals
219  PCL_DEBUG ("[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g \nFinal solution: %g %g %g\n",
220  info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2]);
221 }
222 
224 template <typename PointT> void
226  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients,
227  PointCloud &projected_points, bool copy_data_fields)
228 {
229  // Needs a valid set of model coefficients
230  if (model_coefficients.size () != 3)
231  {
232  PCL_ERROR ("[pcl::SampleConsensusModelCircle2D::projectPoints] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
233  return;
234  }
235 
236  projected_points.header = input_->header;
237  projected_points.is_dense = input_->is_dense;
238 
239  // Copy all the data fields from the input cloud to the projected one?
240  if (copy_data_fields)
241  {
242  // Allocate enough space and copy the basics
243  projected_points.points.resize (input_->points.size ());
244  projected_points.width = input_->width;
245  projected_points.height = input_->height;
246 
247  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
248  // Iterate over each point
249  for (size_t i = 0; i < projected_points.points.size (); ++i)
250  // Iterate over each dimension
251  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
252 
253  // Iterate through the 3d points and calculate the distances from them to the plane
254  for (size_t i = 0; i < inliers.size (); ++i)
255  {
256  float dx = input_->points[inliers[i]].x - model_coefficients[0];
257  float dy = input_->points[inliers[i]].y - model_coefficients[1];
258  float a = sqrtf ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
259 
260  projected_points.points[inliers[i]].x = a * dx + model_coefficients[0];
261  projected_points.points[inliers[i]].y = a * dy + model_coefficients[1];
262  }
263  }
264  else
265  {
266  // Allocate enough space and copy the basics
267  projected_points.points.resize (inliers.size ());
268  projected_points.width = static_cast<uint32_t> (inliers.size ());
269  projected_points.height = 1;
270 
271  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
272  // Iterate over each point
273  for (size_t i = 0; i < inliers.size (); ++i)
274  // Iterate over each dimension
275  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
276 
277  // Iterate through the 3d points and calculate the distances from them to the plane
278  for (size_t i = 0; i < inliers.size (); ++i)
279  {
280  float dx = input_->points[inliers[i]].x - model_coefficients[0];
281  float dy = input_->points[inliers[i]].y - model_coefficients[1];
282  float a = sqrtf ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
283 
284  projected_points.points[i].x = a * dx + model_coefficients[0];
285  projected_points.points[i].y = a * dy + model_coefficients[1];
286  }
287  }
288 }
289 
291 template <typename PointT> bool
293  const std::set<int> &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
294 {
295  // Needs a valid model coefficients
296  if (model_coefficients.size () != 3)
297  {
298  PCL_ERROR ("[pcl::SampleConsensusModelCircle2D::doSamplesVerifyModel] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
299  return (false);
300  }
301 
302  for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
303  // Calculate the distance from the point to the sphere as the difference between
304  //dist(point,sphere_origin) and sphere_radius
305  if (fabsf (sqrtf (
306  ( input_->points[*it].x - model_coefficients[0] ) *
307  ( input_->points[*it].x - model_coefficients[0] ) +
308  ( input_->points[*it].y - model_coefficients[1] ) *
309  ( input_->points[*it].y - model_coefficients[1] )
310  ) - model_coefficients[2]) > threshold)
311  return (false);
312 
313  return (true);
314 }
315 
317 template <typename PointT> bool
318 pcl::SampleConsensusModelCircle2D<PointT>::isModelValid (const Eigen::VectorXf &model_coefficients)
319 {
320  // Needs a valid model coefficients
321  if (model_coefficients.size () != 3)
322  {
323  PCL_ERROR ("[pcl::SampleConsensusModelCircle2D::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
324  return (false);
325  }
326 
327  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[2] < radius_min_)
328  return (false);
329  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[2] > radius_max_)
330  return (false);
331 
332  return (true);
333 }
334 
335 #define PCL_INSTANTIATE_SampleConsensusModelCircle2D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle2D<T>;
336 
337 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
338