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