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