Point Cloud Library (PCL)  1.7.1
sac_model_sphere.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, 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_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
43 
44 #include <pcl/sample_consensus/eigen.h>
45 #include <pcl/sample_consensus/sac_model_sphere.h>
46 
47 //////////////////////////////////////////////////////////////////////////
48 template <typename PointT> bool
50 {
51  return (true);
52 }
53 
54 //////////////////////////////////////////////////////////////////////////
55 template <typename PointT> bool
57  const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
58 {
59  // Need 4 samples
60  if (samples.size () != 4)
61  {
62  PCL_ERROR ("[pcl::SampleConsensusModelSphere::computeModelCoefficients] Invalid set of samples given (%zu)!\n", samples.size ());
63  return (false);
64  }
65 
66  Eigen::Matrix4f temp;
67  for (int i = 0; i < 4; i++)
68  {
69  temp (i, 0) = input_->points[samples[i]].x;
70  temp (i, 1) = input_->points[samples[i]].y;
71  temp (i, 2) = input_->points[samples[i]].z;
72  temp (i, 3) = 1;
73  }
74  float m11 = temp.determinant ();
75  if (m11 == 0)
76  return (false); // the points don't define a sphere!
77 
78  for (int i = 0; i < 4; ++i)
79  temp (i, 0) = (input_->points[samples[i]].x) * (input_->points[samples[i]].x) +
80  (input_->points[samples[i]].y) * (input_->points[samples[i]].y) +
81  (input_->points[samples[i]].z) * (input_->points[samples[i]].z);
82  float m12 = temp.determinant ();
83 
84  for (int i = 0; i < 4; ++i)
85  {
86  temp (i, 1) = temp (i, 0);
87  temp (i, 0) = input_->points[samples[i]].x;
88  }
89  float m13 = temp.determinant ();
90 
91  for (int i = 0; i < 4; ++i)
92  {
93  temp (i, 2) = temp (i, 1);
94  temp (i, 1) = input_->points[samples[i]].y;
95  }
96  float m14 = temp.determinant ();
97 
98  for (int i = 0; i < 4; ++i)
99  {
100  temp (i, 0) = temp (i, 2);
101  temp (i, 1) = input_->points[samples[i]].x;
102  temp (i, 2) = input_->points[samples[i]].y;
103  temp (i, 3) = input_->points[samples[i]].z;
104  }
105  float m15 = temp.determinant ();
106 
107  // Center (x , y, z)
108  model_coefficients.resize (4);
109  model_coefficients[0] = 0.5f * m12 / m11;
110  model_coefficients[1] = 0.5f * m13 / m11;
111  model_coefficients[2] = 0.5f * m14 / m11;
112  // Radius
113  model_coefficients[3] = sqrtf (
114  model_coefficients[0] * model_coefficients[0] +
115  model_coefficients[1] * model_coefficients[1] +
116  model_coefficients[2] * model_coefficients[2] - m15 / m11);
117 
118  return (true);
119 }
120 
121 //////////////////////////////////////////////////////////////////////////
122 template <typename PointT> void
124  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
125 {
126  // Check if the model is valid given the user constraints
127  if (!isModelValid (model_coefficients))
128  {
129  distances.clear ();
130  return;
131  }
132  distances.resize (indices_->size ());
133 
134  // Iterate through the 3d points and calculate the distances from them to the sphere
135  for (size_t i = 0; i < indices_->size (); ++i)
136  // Calculate the distance from the point to the sphere as the difference between
137  //dist(point,sphere_origin) and sphere_radius
138  distances[i] = fabs (sqrtf (
139  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
140  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
141 
142  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
143  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) +
144 
145  ( input_->points[(*indices_)[i]].z - model_coefficients[2] ) *
146  ( input_->points[(*indices_)[i]].z - model_coefficients[2] )
147  ) - model_coefficients[3]);
148 }
149 
150 //////////////////////////////////////////////////////////////////////////
151 template <typename PointT> void
153  const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
154 {
155  // Check if the model is valid given the user constraints
156  if (!isModelValid (model_coefficients))
157  {
158  inliers.clear ();
159  return;
160  }
161 
162  int nr_p = 0;
163  inliers.resize (indices_->size ());
164  error_sqr_dists_.resize (indices_->size ());
165 
166  // Iterate through the 3d points and calculate the distances from them to the sphere
167  for (size_t i = 0; i < indices_->size (); ++i)
168  {
169  double distance = fabs (sqrtf (
170  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
171  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
172 
173  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
174  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) +
175 
176  ( input_->points[(*indices_)[i]].z - model_coefficients[2] ) *
177  ( input_->points[(*indices_)[i]].z - model_coefficients[2] )
178  ) - model_coefficients[3]);
179  // Calculate the distance from the point to the sphere as the difference between
180  // dist(point,sphere_origin) and sphere_radius
181  if (distance < threshold)
182  {
183  // Returns the indices of the points whose distances are smaller than the threshold
184  inliers[nr_p] = (*indices_)[i];
185  error_sqr_dists_[nr_p] = static_cast<double> (distance);
186  ++nr_p;
187  }
188  }
189  inliers.resize (nr_p);
190  error_sqr_dists_.resize (nr_p);
191 }
192 
193 //////////////////////////////////////////////////////////////////////////
194 template <typename PointT> int
196  const Eigen::VectorXf &model_coefficients, const double threshold)
197 {
198  // Check if the model is valid given the user constraints
199  if (!isModelValid (model_coefficients))
200  return (0);
201 
202  int nr_p = 0;
203 
204  // Iterate through the 3d points and calculate the distances from them to the sphere
205  for (size_t i = 0; i < indices_->size (); ++i)
206  {
207  // Calculate the distance from the point to the sphere as the difference between
208  // dist(point,sphere_origin) and sphere_radius
209  if (fabs (sqrtf (
210  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
211  ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
212 
213  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
214  ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) +
215 
216  ( input_->points[(*indices_)[i]].z - model_coefficients[2] ) *
217  ( input_->points[(*indices_)[i]].z - model_coefficients[2] )
218  ) - model_coefficients[3]) < threshold)
219  nr_p++;
220  }
221  return (nr_p);
222 }
223 
224 //////////////////////////////////////////////////////////////////////////
225 template <typename PointT> void
227  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
228 {
229  optimized_coefficients = model_coefficients;
230 
231  // Needs a set of valid model coefficients
232  if (model_coefficients.size () != 4)
233  {
234  PCL_ERROR ("[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
235  return;
236  }
237 
238  // Need at least 4 samples
239  if (inliers.size () <= 4)
240  {
241  PCL_ERROR ("[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Not enough inliers found to support a model (%zu)! Returning the same coefficients.\n", inliers.size ());
242  return;
243  }
244 
245  tmp_inliers_ = &inliers;
246 
247  OptimizationFunctor functor (static_cast<int> (inliers.size ()), this);
248  Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
249  Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, float> lm (num_diff);
250  int info = lm.minimize (optimized_coefficients);
251 
252  // Compute the L2 norm of the residuals
253  PCL_DEBUG ("[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g \nFinal solution: %g %g %g %g\n",
254  info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3]);
255 }
256 
257 //////////////////////////////////////////////////////////////////////////
258 template <typename PointT> void
260  const std::vector<int> &, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool)
261 {
262  // Needs a valid model coefficients
263  if (model_coefficients.size () != 4)
264  {
265  PCL_ERROR ("[pcl::SampleConsensusModelSphere::projectPoints] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
266  return;
267  }
268 
269  // Allocate enough space and copy the basics
270  projected_points.points.resize (input_->points.size ());
271  projected_points.header = input_->header;
272  projected_points.width = input_->width;
273  projected_points.height = input_->height;
274  projected_points.is_dense = input_->is_dense;
275 
276  PCL_WARN ("[pcl::SampleConsensusModelSphere::projectPoints] Not implemented yet.\n");
277  projected_points.points = input_->points;
278 }
279 
280 //////////////////////////////////////////////////////////////////////////
281 template <typename PointT> bool
283  const std::set<int> &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
284 {
285  // Needs a valid model coefficients
286  if (model_coefficients.size () != 4)
287  {
288  PCL_ERROR ("[pcl::SampleConsensusModelSphere::doSamplesVerifyModel] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
289  return (false);
290  }
291 
292  for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
293  // Calculate the distance from the point to the sphere as the difference between
294  //dist(point,sphere_origin) and sphere_radius
295  if (fabs (sqrt (
296  ( input_->points[*it].x - model_coefficients[0] ) *
297  ( input_->points[*it].x - model_coefficients[0] ) +
298  ( input_->points[*it].y - model_coefficients[1] ) *
299  ( input_->points[*it].y - model_coefficients[1] ) +
300  ( input_->points[*it].z - model_coefficients[2] ) *
301  ( input_->points[*it].z - model_coefficients[2] )
302  ) - model_coefficients[3]) > threshold)
303  return (false);
304 
305  return (true);
306 }
307 
308 #define PCL_INSTANTIATE_SampleConsensusModelSphere(T) template class PCL_EXPORTS pcl::SampleConsensusModelSphere<T>;
309 
310 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
311