Point Cloud Library (PCL)
1.3.1
|
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2009, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * $Id: rransac.hpp 3280 2011-11-30 18:31:35Z gedikli $ 00035 * 00036 */ 00037 00038 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_ 00039 #define PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_ 00040 00041 #include "pcl/sample_consensus/rransac.h" 00042 00044 template <typename PointT> bool 00045 pcl::RandomizedRandomSampleConsensus<PointT>::computeModel (int debug_verbosity_level) 00046 { 00047 // Warn and exit if no threshold was set 00048 if (threshold_ == DBL_MAX) 00049 { 00050 PCL_ERROR ("[pcl::RandomizedRandomSampleConsensus::computeModel] No threshold set!\n"); 00051 return (false); 00052 } 00053 00054 iterations_ = 0; 00055 int n_best_inliers_count = -INT_MAX; 00056 double k = 1.0; 00057 00058 std::vector<int> selection; 00059 Eigen::VectorXf model_coefficients; 00060 std::set<int> indices_subset; 00061 00062 int n_inliers_count = 0; 00063 unsigned skipped_count = 0; 00064 // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters! 00065 const unsigned max_skip = max_iterations_ * 10; 00066 00067 // Number of samples to try randomly 00068 size_t fraction_nr_points = pcl_lrint (sac_model_->getIndices ()->size () * fraction_nr_pretest_ / 100.0); 00069 00070 // Iterate 00071 while (iterations_ < k && skipped_count < max_skip) 00072 { 00073 // Get X samples which satisfy the model criteria 00074 sac_model_->getSamples (iterations_, selection); 00075 00076 if (selection.empty ()) break; 00077 00078 // Search for inliers in the point cloud for the current plane model M 00079 if (!sac_model_->computeModelCoefficients (selection, model_coefficients)) 00080 { 00081 //iterations_++; 00082 ++ skipped_count; 00083 continue; 00084 } 00085 00086 // RRANSAC addon: verify a random fraction of the data 00087 // Get X random samples which satisfy the model criterion 00088 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset); 00089 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_)) 00090 { 00091 // Unfortunately we cannot "continue" after the first iteration, because k might not be set, while iterations gets incremented 00092 if (k > 1.0) 00093 { 00094 ++iterations_; 00095 continue; 00096 } 00097 } 00098 00099 // Select the inliers that are within threshold_ from the model 00100 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_); 00101 00102 // Better match ? 00103 if (n_inliers_count > n_best_inliers_count) 00104 { 00105 n_best_inliers_count = n_inliers_count; 00106 00107 // Save the current model/inlier/coefficients selection as being the best so far 00108 model_ = selection; 00109 model_coefficients_ = model_coefficients; 00110 00111 // Compute the k parameter (k=log(z)/log(1-w^n)) 00112 double w = (double)((double)n_inliers_count / (double)sac_model_->getIndices ()->size ()); 00113 double p_no_outliers = 1 - pow (w, (double)selection.size ()); 00114 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf 00115 p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0. 00116 k = log (1 - probability_) / log (p_no_outliers); 00117 } 00118 00119 ++iterations_; 00120 00121 if (debug_verbosity_level > 1) 00122 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Trial %d out of %d: %d inliers (best is: %d so far).\n", iterations_, (int)ceil (k), n_inliers_count, n_best_inliers_count); 00123 if (iterations_ > max_iterations_) 00124 { 00125 if (debug_verbosity_level > 0) 00126 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n"); 00127 break; 00128 } 00129 } 00130 00131 if (debug_verbosity_level > 0) 00132 PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", (unsigned long)model_.size (), n_best_inliers_count); 00133 00134 if (model_.empty ()) 00135 { 00136 inliers_.clear (); 00137 return (false); 00138 } 00139 00140 // Get the set of inliers that correspond to the best model found so far 00141 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_); 00142 return (true); 00143 } 00144 00145 #define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<T>; 00146 00147 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_ 00148