00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
00039 #define PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
00040
00041 #include "pcl/sample_consensus/ransac.h"
00042
00044 template <typename PointT> bool
00045 pcl::RandomSampleConsensus<PointT>::computeModel (int debug_verbosity_level)
00046 {
00047
00048 if (threshold_ == DBL_MAX)
00049 {
00050 ROS_ERROR ("[pcl::RandomSampleConsensus::computeModel] No threshold set!");
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> inliers;
00059 std::vector<int> selection;
00060 Eigen::VectorXf model_coefficients;
00061
00062 int n_inliers_count = 0;
00063
00064
00065 while (iterations_ < k)
00066 {
00067
00068 sac_model_->getSamples (iterations_, selection);
00069
00070 if (selection.empty ())
00071 {
00072 ROS_ERROR ("[pcl::RandomSampleConsensus::computeModel] No samples could be selected!");
00073 break;
00074 }
00075
00076
00077 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
00078 {
00079
00080 continue;
00081 }
00082
00083
00084 sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
00085
00086
00087
00088 n_inliers_count = inliers.size ();
00089
00090
00091 if (n_inliers_count > n_best_inliers_count)
00092 {
00093 n_best_inliers_count = n_inliers_count;
00094
00095
00096 inliers_ = inliers;
00097 model_ = selection;
00098 model_coefficients_ = model_coefficients;
00099
00100
00101 double w = (double)((double)n_best_inliers_count / (double)sac_model_->getIndices ()->size ());
00102 double p_no_outliers = 1.0 - pow (w, (double)selection.size ());
00103 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
00104 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
00105 k = log (1.0 - probability_) / log (p_no_outliers);
00106 }
00107
00108 ++iterations_;
00109 if (debug_verbosity_level > 1)
00110 ROS_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Trial %d out of %f: %d inliers (best is: %d so far).", iterations_, k, n_inliers_count, n_best_inliers_count);
00111 if (iterations_ > max_iterations_)
00112 {
00113 if (debug_verbosity_level > 0)
00114 ROS_DEBUG ("[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.");
00115 break;
00116 }
00117 }
00118
00119 if (debug_verbosity_level > 0)
00120 ROS_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Model: %zu size, %d inliers.", model_.size (), n_best_inliers_count);
00121
00122 if (model_.empty ())
00123 {
00124 inliers_.clear ();
00125 return (false);
00126 }
00127
00128
00129
00130 return (true);
00131 }
00132
00133 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class pcl::RandomSampleConsensus<T>;
00134
00135 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
00136