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: msac.hpp 3280 2011-11-30 18:31:35Z gedikli $ 00035 * 00036 */ 00037 00038 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_ 00039 #define PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_ 00040 00041 #include "pcl/sample_consensus/msac.h" 00042 00044 template <typename PointT> bool 00045 pcl::MEstimatorSampleConsensus<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::MEstimatorSampleConsensus::computeModel] No threshold set!\n"); 00051 return (false); 00052 } 00053 00054 iterations_ = 0; 00055 double d_best_penalty = DBL_MAX; 00056 double k = 1.0; 00057 00058 std::vector<int> best_model; 00059 std::vector<int> selection; 00060 Eigen::VectorXf model_coefficients; 00061 std::vector<double> distances; 00062 00063 int n_inliers_count = 0; 00064 unsigned skipped_count = 0; 00065 // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters! 00066 const unsigned max_skip = max_iterations_ * 10; 00067 00068 // Iterate 00069 while (iterations_ < k && skipped_count < max_skip) 00070 { 00071 // Get X samples which satisfy the model criteria 00072 sac_model_->getSamples (iterations_, selection); 00073 00074 if (selection.empty ()) break; 00075 00076 // Search for inliers in the point cloud for the current plane model M 00077 if (!sac_model_->computeModelCoefficients (selection, model_coefficients)) 00078 { 00079 //iterations_++; 00080 ++ skipped_count; 00081 continue; 00082 } 00083 00084 double d_cur_penalty = 0; 00085 // Iterate through the 3d points and calculate the distances from them to the model 00086 sac_model_->getDistancesToModel (model_coefficients, distances); 00087 00088 if (distances.empty () && k > 1.0) 00089 continue; 00090 00091 for (size_t i = 0; i < distances.size (); ++i) 00092 d_cur_penalty += (std::min) (distances[i], threshold_); 00093 00094 // Better match ? 00095 if (d_cur_penalty < d_best_penalty) 00096 { 00097 d_best_penalty = d_cur_penalty; 00098 00099 // Save the current model/coefficients selection as being the best so far 00100 model_ = selection; 00101 model_coefficients_ = model_coefficients; 00102 00103 n_inliers_count = 0; 00104 // Need to compute the number of inliers for this model to adapt k 00105 for (size_t i = 0; i < distances.size (); ++i) 00106 if (distances[i] <= threshold_) 00107 ++n_inliers_count; 00108 00109 // Compute the k parameter (k=log(z)/log(1-w^n)) 00110 double w = (double)((double)n_inliers_count / (double)sac_model_->getIndices ()->size ()); 00111 double p_no_outliers = 1.0 - pow (w, (double)selection.size ()); 00112 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf 00113 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0. 00114 k = log (1.0 - probability_) / log (p_no_outliers); 00115 } 00116 00117 ++iterations_; 00118 if (debug_verbosity_level > 1) 00119 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, (int)ceil (k), d_best_penalty); 00120 if (iterations_ > max_iterations_) 00121 { 00122 if (debug_verbosity_level > 0) 00123 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n"); 00124 break; 00125 } 00126 } 00127 00128 if (model_.empty ()) 00129 { 00130 if (debug_verbosity_level > 0) 00131 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n"); 00132 return (false); 00133 } 00134 00135 // Iterate through the 3d points and calculate the distances from them to the model again 00136 sac_model_->getDistancesToModel (model_coefficients_, distances); 00137 std::vector<int> &indices = *sac_model_->getIndices (); 00138 00139 if (distances.size () != indices.size ()) 00140 { 00141 PCL_ERROR ("[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", (unsigned long)distances.size (), (unsigned long)indices.size ()); 00142 return (false); 00143 } 00144 00145 inliers_.resize (distances.size ()); 00146 // Get the inliers for the best model found 00147 n_inliers_count = 0; 00148 for (size_t i = 0; i < distances.size (); ++i) 00149 if (distances[i] <= threshold_) 00150 inliers_[n_inliers_count++] = indices[i]; 00151 00152 // Resize the inliers vector 00153 inliers_.resize (n_inliers_count); 00154 00155 if (debug_verbosity_level > 0) 00156 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", (unsigned long)model_.size (), n_inliers_count); 00157 00158 return (true); 00159 } 00160 00161 #define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<T>; 00162 00163 #endif // PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_