Go to the documentation of this file.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
00039
00040
00041 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
00042 #define PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
00043
00044 #include <pcl/sample_consensus/msac.h>
00045
00047 template <typename PointT> bool
00048 pcl::MEstimatorSampleConsensus<PointT>::computeModel (int debug_verbosity_level)
00049 {
00050
00051 if (threshold_ == std::numeric_limits<double>::max())
00052 {
00053 PCL_ERROR ("[pcl::MEstimatorSampleConsensus::computeModel] No threshold set!\n");
00054 return (false);
00055 }
00056
00057 iterations_ = 0;
00058 double d_best_penalty = std::numeric_limits<double>::max();
00059 double k = 1.0;
00060
00061 std::vector<int> best_model;
00062 std::vector<int> selection;
00063 Eigen::VectorXf model_coefficients;
00064 std::vector<double> distances;
00065
00066 int n_inliers_count = 0;
00067 unsigned skipped_count = 0;
00068
00069 const unsigned max_skip = max_iterations_ * 10;
00070
00071
00072 while (iterations_ < k && skipped_count < max_skip)
00073 {
00074
00075 sac_model_->getSamples (iterations_, selection);
00076
00077 if (selection.empty ()) break;
00078
00079
00080 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
00081 {
00082
00083 ++ skipped_count;
00084 continue;
00085 }
00086
00087 double d_cur_penalty = 0;
00088
00089 sac_model_->getDistancesToModel (model_coefficients, distances);
00090
00091 if (distances.empty () && k > 1.0)
00092 continue;
00093
00094 for (size_t i = 0; i < distances.size (); ++i)
00095 d_cur_penalty += (std::min) (distances[i], threshold_);
00096
00097
00098 if (d_cur_penalty < d_best_penalty)
00099 {
00100 d_best_penalty = d_cur_penalty;
00101
00102
00103 model_ = selection;
00104 model_coefficients_ = model_coefficients;
00105
00106 n_inliers_count = 0;
00107
00108 for (size_t i = 0; i < distances.size (); ++i)
00109 if (distances[i] <= threshold_)
00110 ++n_inliers_count;
00111
00112
00113 double w = static_cast<double> (n_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
00114 double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
00115 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
00116 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
00117 k = log (1.0 - probability_) / log (p_no_outliers);
00118 }
00119
00120 ++iterations_;
00121 if (debug_verbosity_level > 1)
00122 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (ceil (k)), d_best_penalty);
00123 if (iterations_ > max_iterations_)
00124 {
00125 if (debug_verbosity_level > 0)
00126 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
00127 break;
00128 }
00129 }
00130
00131 if (model_.empty ())
00132 {
00133 if (debug_verbosity_level > 0)
00134 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
00135 return (false);
00136 }
00137
00138
00139 sac_model_->getDistancesToModel (model_coefficients_, distances);
00140 std::vector<int> &indices = *sac_model_->getIndices ();
00141
00142 if (distances.size () != indices.size ())
00143 {
00144 PCL_ERROR ("[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
00145 return (false);
00146 }
00147
00148 inliers_.resize (distances.size ());
00149
00150 n_inliers_count = 0;
00151 for (size_t i = 0; i < distances.size (); ++i)
00152 if (distances[i] <= threshold_)
00153 inliers_[n_inliers_count++] = indices[i];
00154
00155
00156 inliers_.resize (n_inliers_count);
00157
00158 if (debug_verbosity_level > 0)
00159 PCL_DEBUG ("[pcl::MEstimatorSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
00160
00161 return (true);
00162 }
00163
00164 #define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<T>;
00165
00166 #endif // PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_