Point Cloud Library (PCL)  1.12.1-dev
rmsac.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_RMSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
43 
44 #include <pcl/sample_consensus/rmsac.h>
45 
46 //////////////////////////////////////////////////////////////////////////
47 template <typename PointT> bool
49 {
50  // Warn and exit if no threshold was set
51  if (threshold_ == std::numeric_limits<double>::max())
52  {
53  PCL_ERROR ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] No threshold set!\n");
54  return (false);
55  }
56 
57  iterations_ = 0;
58  double d_best_penalty = std::numeric_limits<double>::max();
59  double k = 1.0;
60 
61  const double log_probability = std::log (1.0 - probability_);
62  const double one_over_indices = 1.0 / static_cast<double> (sac_model_->getIndices ()->size ());
63 
64  Indices selection;
65  Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
66  std::vector<double> distances;
67  std::set<index_t> indices_subset;
68 
69  int n_inliers_count = 0;
70  unsigned skipped_count = 0;
71  // suppress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
72  const unsigned max_skip = max_iterations_ * 10;
73 
74  // Number of samples to try randomly
75  std::size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
76 
77  // Iterate
78  while (iterations_ < k && skipped_count < max_skip)
79  {
80  // Get X samples which satisfy the model criteria
81  sac_model_->getSamples (iterations_, selection);
82 
83  if (selection.empty ()) break;
84 
85  // Search for inliers in the point cloud for the current plane model M
86  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
87  {
88  //iterations_++;
89  ++ skipped_count;
90  continue;
91  }
92 
93  // RMSAC addon: verify a random fraction of the data
94  // Get X random samples which satisfy the model criterion
95  this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
96 
97  if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
98  {
99  // Unfortunately we cannot "continue" after the first iteration, because k might not be set, while iterations gets incremented
100  if (k != 1.0)
101  {
102  ++iterations_;
103  continue;
104  }
105  }
106 
107  double d_cur_penalty = 0;
108  // Iterate through the 3d points and calculate the distances from them to the model
109  sac_model_->getDistancesToModel (model_coefficients, distances);
110 
111  if (distances.empty () && k > 1.0)
112  continue;
113 
114  for (const double &distance : distances)
115  d_cur_penalty += std::min (distance, threshold_);
116 
117  // Better match ?
118  if (d_cur_penalty < d_best_penalty)
119  {
120  d_best_penalty = d_cur_penalty;
121 
122  // Save the current model/coefficients selection as being the best so far
123  model_ = selection;
124  model_coefficients_ = model_coefficients;
125 
126  n_inliers_count = 0;
127  // Need to compute the number of inliers for this model to adapt k
128  for (const double &distance : distances)
129  if (distance <= threshold_)
130  n_inliers_count++;
131 
132  // Compute the k parameter (k=std::log(z)/std::log(1-w^n))
133  const double w = static_cast<double> (n_inliers_count) * one_over_indices;
134  double p_outliers = 1.0 - std::pow (w, static_cast<double> (selection.size ())); // Probability that selection is contaminated by at least one outlier
135  p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by -Inf
136  p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by 0.
137  k = log_probability / std::log (p_outliers);
138  }
139 
140  ++iterations_;
141  if (debug_verbosity_level > 1)
142  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (std::ceil (k)), d_best_penalty);
143  if (iterations_ > max_iterations_)
144  {
145  if (debug_verbosity_level > 0)
146  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
147  break;
148  }
149  }
150 
151  if (model_.empty ())
152  {
153  if (debug_verbosity_level > 0)
154  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
155  return (false);
156  }
157 
158  // Iterate through the 3d points and calculate the distances from them to the model again
159  sac_model_->getDistancesToModel (model_coefficients_, distances);
160  Indices &indices = *sac_model_->getIndices ();
161  if (distances.size () != indices.size ())
162  {
163  PCL_ERROR ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
164  return (false);
165  }
166 
167  inliers_.resize (distances.size ());
168  // Get the inliers for the best model found
169  n_inliers_count = 0;
170  for (std::size_t i = 0; i < distances.size (); ++i)
171  if (distances[i] <= threshold_)
172  inliers_[n_inliers_count++] = indices[i];
173 
174  // Resize the inliers vector
175  inliers_.resize (n_inliers_count);
176 
177  if (debug_verbosity_level > 0)
178  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
179 
180  return (true);
181 }
182 
183 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
184 
185 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
186 
pcl::geometry::distance
float distance(const PointT &p1, const PointT &p2)
Definition: geometry.h:60
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
pcl::RandomizedMEstimatorSampleConsensus::computeModel
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: rmsac.hpp:48
pcl_lrint
#define pcl_lrint(x)
Definition: pcl_macros.h:253