rmsac.hpp
Go to the documentation of this file.
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: rmsac.hpp 6144 2012-07-04 22:06:28Z rusu $
00035  *
00036  */
00037 
00038 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
00039 #define PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
00040 
00041 #include <pcl/sample_consensus/rmsac.h>
00042 
00044 template <typename PointT> bool
00045 pcl::RandomizedMEstimatorSampleConsensus<PointT>::computeModel (int debug_verbosity_level)
00046 {
00047   // Warn and exit if no threshold was set
00048   if (threshold_ == std::numeric_limits<double>::max())
00049   {
00050     PCL_ERROR ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] No threshold set!\n");
00051     return (false);
00052   }
00053 
00054   iterations_ = 0;
00055   double d_best_penalty = std::numeric_limits<double>::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   std::set<int> indices_subset;
00063 
00064   int n_inliers_count = 0;
00065   unsigned skipped_count = 0;
00066   // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
00067   const unsigned max_skip = max_iterations_ * 10;
00068   
00069   // Number of samples to try randomly
00070   size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
00071 
00072   // Iterate
00073   while (iterations_ < k && skipped_count < max_skip)
00074   {
00075     // Get X samples which satisfy the model criteria
00076     sac_model_->getSamples (iterations_, selection);
00077 
00078     if (selection.empty ()) break;
00079 
00080     // Search for inliers in the point cloud for the current plane model M
00081     if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
00082     {
00083       //iterations_++;
00084       ++ skipped_count;
00085       continue;
00086     }
00087 
00088     // RMSAC addon: verify a random fraction of the data
00089     // Get X random samples which satisfy the model criterion
00090     this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
00091 
00092     if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
00093     {
00094       // Unfortunately we cannot "continue" after the first iteration, because k might not be set, while iterations gets incremented
00095       if (k != 1.0)
00096       {
00097         ++iterations_;
00098         continue;
00099       }
00100     }
00101 
00102     double d_cur_penalty = 0;
00103     // Iterate through the 3d points and calculate the distances from them to the model
00104     sac_model_->getDistancesToModel (model_coefficients, distances);
00105 
00106     if (distances.empty () && k > 1.0)
00107       continue;
00108 
00109     for (size_t i = 0; i < distances.size (); ++i)
00110       d_cur_penalty += (std::min) (distances[i], threshold_);
00111 
00112     // Better match ?
00113     if (d_cur_penalty < d_best_penalty)
00114     {
00115       d_best_penalty = d_cur_penalty;
00116 
00117       // Save the current model/coefficients selection as being the best so far
00118       model_              = selection;
00119       model_coefficients_ = model_coefficients;
00120 
00121       n_inliers_count = 0;
00122       // Need to compute the number of inliers for this model to adapt k
00123       for (size_t i = 0; i < distances.size (); ++i)
00124         if (distances[i] <= threshold_)
00125           n_inliers_count++;
00126 
00127       // Compute the k parameter (k=log(z)/log(1-w^n))
00128       double w = static_cast<double> (n_inliers_count) / static_cast<double>(sac_model_->getIndices ()->size ());
00129       double p_no_outliers = 1 - pow (w, static_cast<double> (selection.size ()));
00130       p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);       // Avoid division by -Inf
00131       p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers);   // Avoid division by 0.
00132       k = log (1 - probability_) / log (p_no_outliers);
00133     }
00134 
00135     ++iterations_;
00136     if (debug_verbosity_level > 1)
00137       PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (ceil (k)), d_best_penalty);
00138     if (iterations_ > max_iterations_)
00139     {
00140       if (debug_verbosity_level > 0)
00141         PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
00142       break;
00143     }
00144   }
00145 
00146   if (model_.empty ())
00147   {
00148     if (debug_verbosity_level > 0)
00149       PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
00150     return (false);
00151   }
00152 
00153   // Iterate through the 3d points and calculate the distances from them to the model again
00154   sac_model_->getDistancesToModel (model_coefficients_, distances);
00155   std::vector<int> &indices = *sac_model_->getIndices ();
00156   if (distances.size () != indices.size ())
00157   {
00158     PCL_ERROR ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
00159     return (false);
00160   }
00161 
00162   inliers_.resize (distances.size ());
00163   // Get the inliers for the best model found
00164   n_inliers_count = 0;
00165   for (size_t i = 0; i < distances.size (); ++i)
00166     if (distances[i] <= threshold_)
00167       inliers_[n_inliers_count++] = indices[i];
00168 
00169   // Resize the inliers vector
00170   inliers_.resize (n_inliers_count);
00171 
00172   if (debug_verbosity_level > 0)
00173     PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
00174 
00175   return (true);
00176 }
00177 
00178 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
00179 
00180 #endif    // PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
00181 


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:17:42