Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
msac.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: msac.hpp 6144 2012-07-04 22:06:28Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
40 
42 
44 template <typename PointT> bool
46 {
47  // Warn and exit if no threshold was set
48  if (threshold_ == std::numeric_limits<double>::max())
49  {
50  PCL_ERROR ("[pcl::MEstimatorSampleConsensus::computeModel] No threshold set!\n");
51  return (false);
52  }
53 
54  iterations_ = 0;
55  double d_best_penalty = std::numeric_limits<double>::max();
56  double k = 1.0;
57 
58  std::vector<int> best_model;
59  std::vector<int> selection;
60  Eigen::VectorXf model_coefficients;
61  std::vector<double> distances;
62 
63  int n_inliers_count = 0;
64  unsigned skipped_count = 0;
65  // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
66  const unsigned max_skip = max_iterations_ * 10;
67 
68  // Iterate
69  while (iterations_ < k && skipped_count < max_skip)
70  {
71  // Get X samples which satisfy the model criteria
72  sac_model_->getSamples (iterations_, selection);
73 
74  if (selection.empty ()) break;
75 
76  // Search for inliers in the point cloud for the current plane model M
77  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
78  {
79  //iterations_++;
80  ++ skipped_count;
81  continue;
82  }
83 
84  double d_cur_penalty = 0;
85  // Iterate through the 3d points and calculate the distances from them to the model
86  sac_model_->getDistancesToModel (model_coefficients, distances);
87 
88  if (distances.empty () && k > 1.0)
89  continue;
90 
91  for (size_t i = 0; i < distances.size (); ++i)
92  d_cur_penalty += (std::min) (distances[i], threshold_);
93 
94  // Better match ?
95  if (d_cur_penalty < d_best_penalty)
96  {
97  d_best_penalty = d_cur_penalty;
98 
99  // Save the current model/coefficients selection as being the best so far
100  model_ = selection;
101  model_coefficients_ = model_coefficients;
102 
103  n_inliers_count = 0;
104  // Need to compute the number of inliers for this model to adapt k
105  for (size_t i = 0; i < distances.size (); ++i)
106  if (distances[i] <= threshold_)
107  ++n_inliers_count;
108 
109  // Compute the k parameter (k=log(z)/log(1-w^n))
110  double w = static_cast<double> (n_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
111  double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
112  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
113  p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
114  k = log (1.0 - probability_) / log (p_no_outliers);
115  }
116 
117  ++iterations_;
118  if (debug_verbosity_level > 1)
119  PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (ceil (k)), d_best_penalty);
120  if (iterations_ > max_iterations_)
121  {
122  if (debug_verbosity_level > 0)
123  PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
124  break;
125  }
126  }
127 
128  if (model_.empty ())
129  {
130  if (debug_verbosity_level > 0)
131  PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
132  return (false);
133  }
134 
135  // Iterate through the 3d points and calculate the distances from them to the model again
136  sac_model_->getDistancesToModel (model_coefficients_, distances);
137  std::vector<int> &indices = *sac_model_->getIndices ();
138 
139  if (distances.size () != indices.size ())
140  {
141  PCL_ERROR ("[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
142  return (false);
143  }
144 
145  inliers_.resize (distances.size ());
146  // Get the inliers for the best model found
147  n_inliers_count = 0;
148  for (size_t i = 0; i < distances.size (); ++i)
149  if (distances[i] <= threshold_)
150  inliers_[n_inliers_count++] = indices[i];
151 
152  // Resize the inliers vector
153  inliers_.resize (n_inliers_count);
154 
155  if (debug_verbosity_level > 0)
156  PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
157 
158  return (true);
159 }
160 
161 #define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<T>;
162 
163 #endif // PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_