36 TPoint3D p1( allData(0,useIndices[0]),allData(1,useIndices[0]),allData(2,useIndices[0]) );
37 TPoint3D p2( allData(0,useIndices[1]),allData(1,useIndices[1]),allData(2,useIndices[1]) );
38 TPoint3D p3( allData(0,useIndices[2]),allData(1,useIndices[2]),allData(2,useIndices[2]) );
47 for (
size_t i=0;i<4;i++)
48 M(0,i)=T(plane.
coefs[i]);
62 const T distanceThreshold,
63 unsigned int & out_bestModelIndex,
67 out_bestModelIndex = 0;
73 plane.
coefs[0] = M(0,0);
74 plane.
coefs[1] = M(0,1);
75 plane.
coefs[2] = M(0,2);
76 plane.
coefs[3] = M(0,3);
78 const size_t N =
size(allData,2);
79 out_inlierIndices.clear();
80 out_inlierIndices.reserve(100);
81 for (
size_t i=0;i<N;i++)
83 const double d = plane.
distance(
TPoint3D( allData.get_unsafe(0,i),allData.get_unsafe(1,i),allData.get_unsafe(2,i) ) );
84 if (d<distanceThreshold)
85 out_inlierIndices.push_back(i);
107 template <
typename NUMTYPE>
109 const Eigen::Matrix<NUMTYPE,Eigen::Dynamic,1> &
x,
110 const Eigen::Matrix<NUMTYPE,Eigen::Dynamic,1> &
y,
111 const Eigen::Matrix<NUMTYPE,Eigen::Dynamic,1> &
z,
112 vector<pair<size_t,TPlane> > &out_detected_planes,
113 const double threshold,
114 const size_t min_inliers_for_valid_plane
121 out_detected_planes.clear();
128 remainingPoints.insertRow(0,
x);
129 remainingPoints.insertRow(1,
y);
130 remainingPoints.insertRow(2,
z);
142 ransac.setVerbosityLevel(mrpt::utils::LVL_INFO);
156 if (this_best_inliers.size()>=min_inliers_for_valid_plane)
159 out_detected_planes.push_back(
161 this_best_inliers.size(),
162 TPlane(
double(this_best_model(0,0)),
double(this_best_model(0,1)),
double(this_best_model(0,2)),
double(this_best_model(0,3)) )
165 out_detected_planes.rbegin()->second.unitarize();
168 remainingPoints.removeColumns(this_best_inliers);
182 #define EXPLICIT_INST_ransac_detect_3D_planes(_TYPE_) \ 183 template void BASE_IMPEXP mrpt::math::ransac_detect_3D_planes<_TYPE_>( \ 184 const Eigen::Matrix<_TYPE_,Eigen::Dynamic,1> &x, \ 185 const Eigen::Matrix<_TYPE_,Eigen::Dynamic,1> &y, \ 186 const Eigen::Matrix<_TYPE_,Eigen::Dynamic,1> &z, \ 187 vector<pair<size_t,TPlane> > &out_detected_planes, \ 188 const double threshold, \ 189 const size_t min_inliers_for_valid_plane); 193 #ifdef HAVE_LONG_DOUBLE 206 template <
typename T>
207 void ransac2Dline_fit(
214 TPoint2D p1( allData(0,useIndices[0]),allData(1,useIndices[0]) );
215 TPoint2D p2( allData(0,useIndices[1]),allData(1,useIndices[1]) );
224 for (
size_t i=0;i<3;i++)
225 M(0,i)=line.
coefs[i];
235 template <
typename T>
236 void ransac2Dline_distance(
239 const T distanceThreshold,
240 unsigned int & out_bestModelIndex,
243 out_inlierIndices.clear();
244 out_bestModelIndex = 0;
246 if (testModels.empty())
return;
248 ASSERTMSG_( testModels.size()==1,
format(
"Expected testModels.size()=1, but it's = %u",static_cast<unsigned int>(testModels.size()) ) )
254 line.
coefs[0] = M(0,0);
255 line.coefs[1] = M(0,1);
256 line.coefs[2] = M(0,2);
258 const size_t N =
size(allData,2);
259 out_inlierIndices.reserve(100);
260 for (
size_t i=0;i<N;i++)
262 const double d = line.distance(
TPoint2D( allData.get_unsafe(0,i),allData.get_unsafe(1,i) ) );
263 if (d<distanceThreshold)
264 out_inlierIndices.push_back(i);
270 template <
typename T>
271 bool ransac2Dline_degenerate(
285 template <
typename NUMTYPE>
287 const Eigen::Matrix<NUMTYPE,Eigen::Dynamic,1> &
x,
288 const Eigen::Matrix<NUMTYPE,Eigen::Dynamic,1> &
y,
289 std::vector<std::pair<size_t,TLine2D> > &out_detected_lines,
290 const double threshold,
291 const size_t min_inliers_for_valid_line
298 out_detected_lines.clear();
305 remainingPoints.insertRow(0,
x);
306 remainingPoints.insertRow(1,
y);
311 while (
size(remainingPoints,2)>=2)
317 ransac.setVerbosityLevel(mrpt::utils::LVL_INFO);
321 ransac2Dline_distance,
322 ransac2Dline_degenerate,
331 if (this_best_inliers.size()>=min_inliers_for_valid_line)
334 out_detected_lines.push_back(
336 this_best_inliers.size(),
337 TLine2D(
double(this_best_model(0,0)),
double(this_best_model(0,1)),
double(this_best_model(0,2)) )
340 out_detected_lines.rbegin()->second.unitarize();
343 remainingPoints.removeColumns(this_best_inliers);
355 #define EXPLICIT_INSTANT_ransac_detect_2D_lines(_TYPE_) \ 356 template void BASE_IMPEXP mrpt::math::ransac_detect_2D_lines<_TYPE_>( \ 357 const Eigen::Matrix<_TYPE_,Eigen::Dynamic,1> &x, \ 358 const Eigen::Matrix<_TYPE_,Eigen::Dynamic,1> &y, \ 359 std::vector<std::pair<size_t,TLine2D> > &out_detected_lines, \ 360 const double threshold, \ 361 const size_t min_inliers_for_valid_line ); \ 365 #ifdef HAVE_LONG_DOUBLE bool execute(const CMatrixTemplateNumeric< NUMTYPE > &data, TRansacFitFunctor fit_func, TRansacDistanceFunctor dist_func, TRansacDegenerateFunctor degen_func, const double distanceThreshold, const unsigned int minimumSizeSamplesToFit, mrpt::vector_size_t &out_best_inliers, CMatrixTemplateNumeric< NUMTYPE > &out_best_model, const double prob_good_sample=0.999, const size_t maxIter=2000) const
An implementation of the RANSAC algorithm for robust fitting of models to data.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
double distance(const TPoint3D &point) const
Distance to 3D point.
void BASE_IMPEXP ransac_detect_3D_planes(const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &x, const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &y, const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &z, std::vector< std::pair< size_t, TPlane > > &out_detected_planes, const double threshold, const size_t min_inliers_for_valid_plane=10)
Fit a number of 3-D planes to a given point cloud, automatically determining the number of existing p...
This base provides a set of functions for maths stuff.
bool ransac3Dplane_degenerate(const CMatrixDouble &planeCorresp, const mrpt::vector_size_t &useIndices)
Return "true" if the selected points are a degenerate (invalid) case.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define EXPLICIT_INST_ransac_detect_3D_planes(_TYPE_)
3D Plane, represented by its equation
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
double coefs[3]
Line coefficients, stored as an array: .
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double coefs[4]
Plane coefficients, stored as an array: .
A matrix of dynamic size.
std::vector< size_t > vector_size_t
void ransac3Dplane_distance(const CMatrixDouble &planeCorresp, const vector< CMatrixDouble > &testModels, const double distanceThreshold, unsigned int &out_bestModelIndex, mrpt::vector_size_t &out_inlierIndices)
void BASE_IMPEXP ransac_detect_2D_lines(const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &x, const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &y, std::vector< std::pair< size_t, TLine2D > > &out_detected_lines, const double threshold, const size_t min_inliers_for_valid_line=5)
Fit a number of 2-D lines to a given point cloud, automatically determining the number of existing li...
#define EXPLICIT_INSTANT_ransac_detect_2D_lines(_TYPE_)
A generic RANSAC implementation with models as matrices.
#define ASSERTMSG_(f, __ERROR_MSG)
void ransac3Dplane_fit(const CMatrixTemplateNumeric< T > &allData, const vector_size_t &useIndices, vector< CMatrixTemplateNumeric< T > > &fitModels)
2D line without bounds, represented by its equation .
std::vector< size_t > vector_size_t