MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /**
11  * Execute the iterating closest point algorithm (ICP) on a hardcoded pair of
12  * laser data. The algorithm computes the transformation (translation and
13  * rotation) for aligning the 2 sets of laser scans and plots the
14  */
15 
16 #include <mrpt/gui.h>
18 #include <mrpt/math/utils.h>
21 #include <mrpt/poses/CPose2D.h>
22 #include <mrpt/poses/CPosePDF.h>
24 #include <mrpt/slam/CICP.h>
25 
26 #include <fstream>
27 #include <iostream>
28 
29 using namespace mrpt;
30 using namespace mrpt::slam;
31 using namespace mrpt::maps;
32 using namespace mrpt::obs;
33 using namespace mrpt::math;
34 using namespace mrpt::poses;
35 using namespace std;
36 
37 bool skip_window = false;
38 int ICP_method = (int)icpClassic;
39 
40 // ------------------------------------------------------
41 // TestICP
42 // ------------------------------------------------------
43 void TestICP()
44 {
45  CSimplePointsMap m1, m2;
46  float runningTime;
47  CICP::TReturnInfo info;
48  CICP ICP;
49 
50  // Load scans:
53 
56 
57  // Build the points maps from the scans:
58  m1.insertObservation(scan1);
59  m2.insertObservation(scan2);
60 
61 #if MRPT_HAS_PCL
62  cout << "Saving map1.pcd and map2.pcd in PCL format...\n";
63  m1.savePCDFile("map1.pcd", false);
64  m2.savePCDFile("map2.pcd", false);
65 #endif
66 
67  // -----------------------------------------------------
68 
69  /**
70  * user-set parameters for the icp algorithm.
71  * For a full list of the available options check the online tutorials
72  * page:
73  * https://www.mrpt.org/Iterative_Closest_Point_(ICP)_and_other_matching_algorithms
74  */
75 
76  // select which algorithm version to use
77  // ICP.options.ICP_algorithm = icpLevenbergMarquardt;
78  // ICP.options.ICP_algorithm = icpClassic;
80 
81  // configuration options for the icp algorithm
82  ICP.options.maxIterations = 100;
83  ICP.options.thresholdAng = DEG2RAD(10.0f);
84  ICP.options.thresholdDist = 0.75f;
85  ICP.options.ALFA = 0.5f;
86  ICP.options.smallestThresholdDist = 0.05f;
87  ICP.options.doRANSAC = false;
88 
89  ICP.options.dumpToConsole();
90  // -----------------------------------------------------
91 
92  /**
93  * Scans alignment procedure.
94  * Given an initial guess (initialPose) and the maps to be aligned, the
95  * algorithm returns the probability density function (pdf) of the alignment
96  * Additional arguments are provided to investigate the performance of the
97  * algorithm
98  */
99  CPose2D initialPose(0.8f, 0.0f, (float)DEG2RAD(0.0f));
100 
101  CPosePDF::Ptr pdf =
102  ICP.Align(&m1, &m2, initialPose, &runningTime, (void*)&info);
103 
104  printf(
105  "ICP run in %.02fms, %d iterations (%.02fms/iter), %.01f%% goodness\n "
106  "-> ",
107  runningTime * 1000, info.nIterations,
108  runningTime * 1000.0f / info.nIterations, info.goodness * 100);
109 
110  cout << "Mean of estimation: " << pdf->getMeanVal() << endl << endl;
111 
112  CPosePDFGaussian gPdf;
113  gPdf.copyFrom(*pdf);
114 
115  cout << "Covariance of estimation: " << endl << gPdf.cov << endl;
116 
117  cout << " std(x): " << sqrt(gPdf.cov(0, 0)) << endl;
118  cout << " std(y): " << sqrt(gPdf.cov(1, 1)) << endl;
119  cout << " std(phi): " << RAD2DEG(sqrt(gPdf.cov(2, 2))) << " (deg)" << endl;
120 
121  // cout << "Covariance of estimation (MATLAB format): " << endl <<
122  // gPdf.cov.inMatlabFormat() << endl;
123 
124  /**
125  * Save the results for potential postprocessing (in txt and in matlab
126  * format)
127  */
128  cout << "-> Saving reference map as scan1.txt" << endl;
129  m1.save2D_to_text_file("scan1.txt");
130 
131  cout << "-> Saving map to align as scan2.txt" << endl;
132  m2.save2D_to_text_file("scan2.txt");
133 
134  cout << "-> Saving transformed map to align as scan2_trans.txt" << endl;
135  CSimplePointsMap m2_trans = m2;
136  m2_trans.changeCoordinatesReference(gPdf.mean);
137  m2_trans.save2D_to_text_file("scan2_trans.txt");
138 
139  cout << "-> Saving MATLAB script for drawing 2D ellipsoid as view_ellip.m"
140  << endl;
142  COV22.setSize(2, 2);
143  CVectorFloat MEAN2D(2);
144  MEAN2D[0] = gPdf.mean.x();
145  MEAN2D[1] = gPdf.mean.y();
146  {
147  ofstream f("view_ellip.m");
148  f << math::MATLAB_plotCovariance2D(COV22, MEAN2D, 3.0f);
149  }
150 
151 // If we have 2D windows, use'em:
152 #if MRPT_HAS_WXWIDGETS
153  /**
154  * Plotting the icp results:
155  * Aligned maps + transformation uncertainty ellipsis
156  */
157  if (!skip_window)
158  {
159  gui::CDisplayWindowPlots win("ICP results");
160 
161  // Reference map:
162  vector<float> map1_xs, map1_ys, map1_zs;
163  m1.getAllPoints(map1_xs, map1_ys, map1_zs);
164  win.plot(map1_xs, map1_ys, "b.3", "map1");
165 
166  // Translated map:
167  vector<float> map2_xs, map2_ys, map2_zs;
168  m2_trans.getAllPoints(map2_xs, map2_ys, map2_zs);
169  win.plot(map2_xs, map2_ys, "r.3", "map2");
170 
171  // Uncertainty
172  win.plotEllipse(MEAN2D[0], MEAN2D[1], COV22, 3.0, "b2", "cov");
173 
174  win.axis(-1, 10, -6, 6);
175  win.axis_equal();
176 
177  cout << "Close the window to exit" << endl;
178  win.waitForKey();
179  }
180 #endif
181 }
182 
183 int main(int argc, char** argv)
184 {
185  try
186  {
187  skip_window = (argc > 2);
188  if (argc > 1)
189  {
190  ICP_method = atoi(argv[1]);
191  }
192 
193  TestICP();
194 
195  return 0;
196  }
197  catch (exception& e)
198  {
199  cout << "MRPT exception caught: " << e.what() << endl;
200  return -1;
201  }
202  catch (...)
203  {
204  printf("Another exception!!");
205  return -1;
206  }
207 }
void copyFrom(const CPosePDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
bool save2D_to_text_file(const std::string &file) const
Save to a text file.
Definition: CPointsMap.cpp:74
std::string MATLAB_plotCovariance2D(const CMatrixFloat &cov22, const CVectorFloat &mean, const float &stdCount, const std::string &style=std::string("b"), const size_t &nEllipsePoints=30)
Generates a string with the MATLAB commands required to plot an confidence interval (ellipse) for a 2...
Definition: math.cpp:357
CPose2D mean
The mean value.
double RAD2DEG(const double x)
Radians to degrees.
void changeCoordinatesReference(const mrpt::poses::CPose2D &b)
Replace each point by (pose compounding operator).
Definition: CPointsMap.cpp:553
Template for column vectors of dynamic size, compatible with Eigen.
Create a GUI window and display plots with MATLAB-like interfaces and commands.
TICPAlgorithm ICP_algorithm
The algorithm to use (default: icpClassic).
Definition: CICP.h:84
mrpt::poses::CPosePDF::Ptr Align(const mrpt::maps::CMetricMap *m1, const mrpt::maps::CMetricMap *m2, const mrpt::poses::CPose2D &grossEst, float *runningTime=nullptr, void *info=nullptr)
The method for aligning a pair of metric maps, aligning only 2D + orientation.
Several implementations of ICP (Iterative closest point) algorithms for aligning two point maps or a ...
Definition: CICP.h:64
double DEG2RAD(const double x)
Degrees to radians.
void example2DRangeScan(mrpt::obs::CObservation2DRangeScan &s, int i=0)
Example 2D lidar scans (form a venerable SICK LMS200).
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans. ...
STL namespace.
double smallestThresholdDist
The size for threshold such that iterations will stop, since it is considered precise enough...
Definition: CICP.h:120
TConfigParams options
The options employed by the ICP align.
Definition: CICP.h:180
CMatrixDynamic< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
mrpt::math::CMatrixDouble33 cov
The 3x3 covariance matrix.
This base provides a set of functions for maths stuff.
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
void getAllPoints(VECTOR &xs, VECTOR &ys, VECTOR &zs, size_t decimation=1) const
Returns a copy of the 2D/3D points as a std::vector of float coordinates.
Definition: CPointsMap.h:557
unsigned int maxIterations
Maximum number of iterations to run.
Definition: CICP.h:101
This namespace contains representation of robot actions and observations.
bool doRANSAC
Perform a RANSAC step, mrpt::tfest::se2_l2_robust(), after the ICP convergence, to obtain a better es...
Definition: CICP.h:133
virtual bool savePCDFile(const std::string &filename, bool save_as_binary) const
Save the point cloud as a PCL PCD file, in either ASCII or binary format (requires MRPT built against...
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
mrpt::gui::CDisplayWindow3D::Ptr win
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
TICPAlgorithm
The ICP algorithm selection, used in mrpt::slam::CICP::options mrpt_slam_grp.
Definition: CICP.h:19
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:39
double ALFA
The scale factor for thresholds everytime convergence is achieved.
Definition: CICP.h:117
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
The ICP algorithm return information.
Definition: CICP.h:190
double thresholdDist
Initial threshold distance for two points to become a correspondence.
Definition: CICP.h:114
void dumpToConsole() const
Just like dumpToTextStream() but sending the text to the console (std::cout)
This template class provides the basic functionality for a general 2D any-size, resizable container o...
bool insertObservation(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D *robotPose=nullptr)
Insert the observation information into this map.
Definition: CMetricMap.cpp:93
CMatrixDynamic< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019