Point Cloud Library (PCL) 1.14.0
Loading...
Searching...
No Matches
msac.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_MSAC_H_
42#define PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
43
44#include <pcl/sample_consensus/msac.h>
45
46//////////////////////////////////////////////////////////////////////////
47template <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::MEstimatorSampleConsensus::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
68 int n_inliers_count = 0;
69 unsigned skipped_count = 0;
70 // suppress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
71 const unsigned max_skip = max_iterations_ * 10;
72
73 // Iterate
74 while (iterations_ < k && skipped_count < max_skip)
75 {
76 // Get X samples which satisfy the model criteria
77 sac_model_->getSamples (iterations_, selection);
78
79 if (selection.empty ()) break;
80
81 // Search for inliers in the point cloud for the current plane model M
82 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
83 {
84 //iterations_++;
85 ++ skipped_count;
86 continue;
87 }
88
89 double d_cur_penalty = 0;
90 // Iterate through the 3d points and calculate the distances from them to the model
91 sac_model_->getDistancesToModel (model_coefficients, distances);
92
93 if (distances.empty ())
94 {
95 // skip invalid model suppress infinite loops
96 ++ skipped_count;
97 continue;
98 }
99
100 for (const double &distance : distances)
101 d_cur_penalty += (std::min) (distance, threshold_);
102
103 // Better match ?
104 if (d_cur_penalty < d_best_penalty)
105 {
106 d_best_penalty = d_cur_penalty;
107
108 // Save the current model/coefficients selection as being the best so far
109 model_ = selection;
110 model_coefficients_ = model_coefficients;
111
112 n_inliers_count = 0;
113 // Need to compute the number of inliers for this model to adapt k
114 for (const double &distance : distances)
115 if (distance <= threshold_)
116 ++n_inliers_count;
117
118 // Compute the k parameter (k=std::log(z)/std::log(1-w^n))
119 const double w = static_cast<double> (n_inliers_count) * one_over_indices;
120 double p_outliers = 1.0 - std::pow (w, static_cast<double> (selection.size ())); // Probability that selection is contaminated by at least one outlier
121 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by -Inf
122 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by 0.
123 k = log_probability / std::log (p_outliers);
124 }
125
126 ++iterations_;
127 if (debug_verbosity_level > 1)
128 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (std::ceil (k)), d_best_penalty);
129 if (iterations_ > max_iterations_)
130 {
131 if (debug_verbosity_level > 0)
132 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
133 break;
134 }
135 }
136
137 if (model_.empty ())
138 {
139 if (debug_verbosity_level > 0)
140 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
141 return (false);
142 }
143
144 // Iterate through the 3d points and calculate the distances from them to the model again
145 sac_model_->getDistancesToModel (model_coefficients_, distances);
146 Indices &indices = *sac_model_->getIndices ();
147
148 if (distances.size () != indices.size ())
149 {
150 PCL_ERROR ("[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
151 return (false);
152 }
153
154 inliers_.resize (distances.size ());
155 // Get the inliers for the best model found
156 n_inliers_count = 0;
157 for (std::size_t i = 0; i < distances.size (); ++i)
158 if (distances[i] <= threshold_)
159 inliers_[n_inliers_count++] = indices[i];
160
161 // Resize the inliers vector
162 inliers_.resize (n_inliers_count);
163
164 if (debug_verbosity_level > 0)
165 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
166
167 return (true);
168}
169
170#define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<T>;
171
172#endif // PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition msac.hpp:48
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133