Point Cloud Library (PCL) 1.15.0
Loading...
Searching...
No Matches
rransac.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_RRANSAC_H_
42#define PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
43
44#include <pcl/sample_consensus/rransac.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::RandomizedRandomSampleConsensus::computeModel] No threshold set!\n");
54 return (false);
55 }
56
57 iterations_ = 0;
58 std::size_t n_best_inliers_count = 0;
59 double k = std::numeric_limits<double>::max();
60
61 Indices selection;
62 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
63 std::set<index_t> indices_subset;
64
65 const double log_probability = std::log (1.0 - probability_);
66 const double one_over_indices = 1.0 / static_cast<double> (sac_model_->getIndices ()->size ());
67
68 std::size_t n_inliers_count;
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 // Number of samples to try randomly
74 const std::size_t fraction_nr_points = (fraction_nr_pretest_ < 0.0 ? nr_samples_pretest_ : pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0));
75 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Using %lu points for RRANSAC pre-test.\n", fraction_nr_points);
76
77 // Iterate
78 while (iterations_ < k)
79 {
80 // Get X samples which satisfy the model criteria
81 sac_model_->getSamples (iterations_, selection);
82
83 if (selection.empty ())
84 {
85 PCL_ERROR ("[pcl::RandomizedRandomSampleConsensus::computeModel] No samples could be selected!\n");
86 break;
87 }
88
89 // Search for inliers in the point cloud for the current plane model M
90 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
91 {
92 //iterations_++;
93 ++skipped_count;
94 if (skipped_count < max_skip)
95 {
96 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, so continue with next iteration.\n");
97 continue;
98 }
99 else
100 {
101 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, and RRANSAC reached the maximum number of trials.\n");
102 break;
103 }
104 }
105
106 // RRANSAC addon: verify a random fraction of the data
107 // Get X random samples which satisfy the model criterion
108 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
109 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
110 {
111 ++iterations_;
112 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] The function doSamplesVerifyModel failed, so continue with next iteration.\n");
113 continue;
114 }
115
116 // Select the inliers that are within threshold_ from the model
117 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
118
119 // Better match ?
120 if (n_inliers_count > n_best_inliers_count)
121 {
122 n_best_inliers_count = n_inliers_count;
123
124 // Save the current model/inlier/coefficients selection as being the best so far
125 model_ = selection;
126 model_coefficients_ = model_coefficients;
127
128 // Compute the k parameter (k=std::log(z)/std::log(1-w^n))
129 const double w = static_cast<double> (n_best_inliers_count) * one_over_indices;
130 double p_outliers = 1.0 - std::pow (w, static_cast<double> (selection.size ())); // Probability that selection is contaminated by at least one outlier
131 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by -Inf
132 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by 0.
133 k = log_probability / std::log (p_outliers);
134 }
135
136 ++iterations_;
137
138 if (debug_verbosity_level > 1)
139 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Trial %d out of %d: %u inliers (best is: %u so far).\n", iterations_, static_cast<int> (std::ceil (k)), n_inliers_count, n_best_inliers_count);
140 if (iterations_ > max_iterations_)
141 {
142 if (debug_verbosity_level > 0)
143 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n");
144 break;
145 }
146 }
147
148 if (debug_verbosity_level > 0)
149 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
150
151 if (model_.empty ())
152 {
153 PCL_ERROR ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC found no model.\n");
154 inliers_.clear ();
155 return (false);
156 }
157
158 // Get the set of inliers that correspond to the best model found so far
159 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
160 return (true);
161}
162
163#define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<T>;
164
165#endif // PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
166
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition rransac.hpp:48
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
#define pcl_lrint(x)
Definition pcl_macros.h:254