Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /**
11  * icp3d
12  * Execute an Iterative Closest Point algorithm using two 3D point clouds.
13  */
14 
15 #include <mrpt/slam/CICP.h>
16 
17 #include <mrpt/poses/CPose3DPDF.h>
22 #include <mrpt/opengl/CSphere.h>
24 #include <mrpt/opengl/CDisk.h>
26 #include <iostream>
27 
28 using namespace std;
29 using namespace mrpt;
30 using namespace mrpt::gui;
31 using namespace mrpt::opengl;
32 using namespace mrpt::poses;
33 using namespace mrpt::slam;
34 using namespace mrpt::maps;
35 using namespace mrpt::obs;
36 
37 // Increase this values to get more precision. It will also increase run time.
38 const size_t HOW_MANY_YAWS = 120;
39 const size_t HOW_MANY_PITCHS = 120;
40 
41 // The scans of the 3D object, taken from 2 different places:
42 vector<CObservation2DRangeScan> sequence_scans1, sequence_scans2;
43 
44 // The two origins for the 3D scans
45 CPose3D viewpoint1(-0.3, 0.7, 3, DEG2RAD(5), DEG2RAD(80), DEG2RAD(3));
46 CPose3D viewpoint2(0.5, -0.2, 2.6, DEG2RAD(-5), DEG2RAD(100), DEG2RAD(-7));
47 
48 CPose3D SCAN2_POSE_ERROR(0.15, -0.07, 0.10, -0.03, 0.1, 0.1);
49 
50 /**
51  * Generate 3 objects to work with - 1 sphere, 2 disks
52  */
54 {
55  CSphere::Ptr sph = mrpt::make_aligned_shared<CSphere>(0.5);
56  sph->setLocation(0, 0, 0);
57  sph->setColor(1, 0, 0);
58  world->insert(sph);
59 
60  CDisk::Ptr pln = mrpt::make_aligned_shared<opengl::CDisk>();
61  pln->setDiskRadius(2);
62  pln->setPose(CPose3D(0, 0, 0, 0, DEG2RAD(5), DEG2RAD(5)));
63  pln->setColor(0.8, 0, 0);
64  world->insert(pln);
65 
66  {
67  CDisk::Ptr pln = mrpt::make_aligned_shared<opengl::CDisk>();
68  pln->setDiskRadius(2);
69  pln->setPose(CPose3D(0, 0, 0, DEG2RAD(30), DEG2RAD(-20), DEG2RAD(-2)));
70  pln->setColor(0.9, 0, 0);
71  world->insert(pln);
72  }
73 }
74 
75 void test_icp3D()
76 {
77  // Create the reference objects:
78  COpenGLScene::Ptr scene1 = mrpt::make_aligned_shared<COpenGLScene>();
79  COpenGLScene::Ptr scene2 = mrpt::make_aligned_shared<COpenGLScene>();
80  COpenGLScene::Ptr scene3 = mrpt::make_aligned_shared<COpenGLScene>();
81 
83  mrpt::make_aligned_shared<CGridPlaneXY>(-20, 20, -20, 20, 0, 1);
84  plane1->setColor(0.3, 0.3, 0.3);
85  scene1->insert(plane1);
86  scene2->insert(plane1);
87  scene3->insert(plane1);
88 
89  CSetOfObjects::Ptr world = mrpt::make_aligned_shared<CSetOfObjects>();
90  generateObjects(world);
91  scene1->insert(world);
92 
93  // Perform the 3D scans:
95  mrpt::make_aligned_shared<CAngularObservationMesh>();
97  mrpt::make_aligned_shared<CAngularObservationMesh>();
98 
99  cout << "Performing ray-tracing..." << endl;
100  CAngularObservationMesh::trace2DSetOfRays(
101  scene1, viewpoint1, aom1,
102  CAngularObservationMesh::TDoubleRange::CreateFromAperture(
104  CAngularObservationMesh::TDoubleRange::CreateFromAperture(
105  M_PI, HOW_MANY_YAWS));
106  CAngularObservationMesh::trace2DSetOfRays(
107  scene1, viewpoint2, aom2,
108  CAngularObservationMesh::TDoubleRange::CreateFromAperture(
110  CAngularObservationMesh::TDoubleRange::CreateFromAperture(
111  M_PI, HOW_MANY_YAWS));
112  cout << "Ray-tracing done" << endl;
113 
114  // Put the viewpoints origins:
115  {
117  origin1->setPose(viewpoint1);
118  origin1->setScale(0.6);
119  scene1->insert(origin1);
120  scene2->insert(origin1);
121  }
122  {
124  origin2->setPose(viewpoint2);
125  origin2->setScale(0.6);
126  scene1->insert(origin2);
127  scene2->insert(origin2);
128  }
129 
130  // Show the scanned points:
131  CSimplePointsMap M1, M2;
132 
133  aom1->generatePointCloud(&M1);
134  aom2->generatePointCloud(&M2);
135 
136  // Create the wrongly-localized M2:
137  CSimplePointsMap M2_noisy;
138  M2_noisy = M2;
140 
141  CSetOfObjects::Ptr PTNS1 = mrpt::make_aligned_shared<CSetOfObjects>();
142  CSetOfObjects::Ptr PTNS2 = mrpt::make_aligned_shared<CSetOfObjects>();
143 
144  M1.renderOptions.color = mrpt::img::TColorf(1, 0, 0);
145  M1.getAs3DObject(PTNS1);
146 
147  M2_noisy.renderOptions.color = mrpt::img::TColorf(0, 0, 1);
148  M2_noisy.getAs3DObject(PTNS2);
149 
150  scene2->insert(PTNS1);
151  scene2->insert(PTNS2);
152 
153  // --------------------------------------
154  // Do the ICP-3D
155  // --------------------------------------
156  float run_time;
157  CICP icp;
158  CICP::TReturnInfo icp_info;
159 
160  icp.options.thresholdDist = 0.40;
161  icp.options.thresholdAng = 0;
162 
163  std::vector<double> xs, ys, zs;
164  M1.getAllPoints(xs, ys, ys);
165  cout << "Size of xs in M1: " << xs.size() << endl;
166  M2.getAllPoints(xs, ys, ys);
167  cout << "Size of xs in M2: " << xs.size() << endl;
168 
169  CPose3DPDF::Ptr pdf = icp.Align3D(
170  &M2_noisy, // Map to align
171  &M1, // Reference map
172  CPose3D(), // Initial gross estimate
173  &run_time, &icp_info);
174 
175  CPose3D mean = pdf->getMeanVal();
176 
177  cout << "ICP run took " << run_time << " secs." << endl;
178  cout << "Goodness: " << 100 * icp_info.goodness
179  << "% , # of iterations= " << icp_info.nIterations
180  << " Quality: " << icp_info.quality << endl;
181  cout << "ICP output: mean= " << mean << endl;
182  cout << "Real displacement: " << SCAN2_POSE_ERROR << endl;
183 
184  // Aligned maps:
185  CSetOfObjects::Ptr PTNS2_ALIGN = mrpt::make_aligned_shared<CSetOfObjects>();
186 
188  M2_noisy.getAs3DObject(PTNS2_ALIGN);
189 
190  scene3->insert(PTNS1);
191  scene3->insert(PTNS2_ALIGN);
192 
193  // Show in Windows:
194  CDisplayWindow3D window("ICP-3D demo: scene", 500, 500);
195  CDisplayWindow3D window2("ICP-3D demo: UNALIGNED scans", 500, 500);
196  CDisplayWindow3D window3("ICP-3D demo: ICP-ALIGNED scans", 500, 500);
197 
198  window.setPos(10, 10);
199  window2.setPos(530, 10);
200  window3.setPos(10, 520);
201 
202  window.get3DSceneAndLock() = scene1;
203  window.unlockAccess3DScene();
204 
205  window2.get3DSceneAndLock() = scene2;
206  window2.unlockAccess3DScene();
207 
208  window3.get3DSceneAndLock() = scene3;
209  window3.unlockAccess3DScene();
210 
211  std::this_thread::sleep_for(20ms);
212  window.forceRepaint();
213  window2.forceRepaint();
214 
215  window.setCameraElevationDeg(15);
216  window.setCameraAzimuthDeg(90);
217  window.setCameraZoom(15);
218 
219  window2.setCameraElevationDeg(15);
220  window2.setCameraAzimuthDeg(90);
221  window2.setCameraZoom(15);
222 
223  window3.setCameraElevationDeg(15);
224  window3.setCameraAzimuthDeg(90);
225  window3.setCameraZoom(15);
226 
227  cout << "Press any key to exit..." << endl;
228  window.waitForKey();
229 }
230 
231 int main()
232 {
233  try
234  {
235  test_icp3D();
236  return 0;
237  }
238  catch (exception& e)
239  {
240  cout << "Error: " << e.what() << '.' << endl;
241  return -1;
242  }
243  catch (...)
244  {
245  cout << "Unknown Error.\n";
246  return -1;
247  }
248 }
DEG2RAD
#define DEG2RAD
Definition: core/include/mrpt/core/bits_math.h:59
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
mrpt::maps::CPointsMap::getAs3DObject
virtual void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map.
Definition: CPointsMap.cpp:803
mrpt::maps::CPointsMap::changeCoordinatesReference
void changeCoordinatesReference(const mrpt::poses::CPose2D &b)
Replace each point by (pose compounding operator).
Definition: CPointsMap.cpp:574
mrpt::slam::CICP::options
TConfigParams options
The options employed by the ICP align.
Definition: CICP.h:182
sequence_scans1
vector< CObservation2DRangeScan > sequence_scans1
Definition: vision_stereo_rectify/test.cpp:42
mrpt::slam::CICP::TReturnInfo::goodness
float goodness
A goodness measure for the alignment, it is a [0,1] range indicator of percentage of correspondences.
Definition: CICP.h:199
mrpt::slam::CICP::TConfigParams::thresholdDist
double thresholdDist
Initial threshold distance for two points to become a correspondence.
Definition: CICP.h:116
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
generateObjects
void generateObjects(CSetOfObjects::Ptr &world)
Definition: vision_stereo_rectify/test.cpp:107
CICP.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::maps::CPointsMap::getAllPoints
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:536
mrpt::opengl::CAngularObservationMesh::Ptr
std::shared_ptr< CAngularObservationMesh > Ptr
Definition: CAngularObservationMesh.h:46
stock_objects.h
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
CDisplayWindow3D.h
mrpt::slam::CICP::TReturnInfo
The ICP algorithm return information.
Definition: CICP.h:192
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::slam::CICP::TReturnInfo::quality
float quality
A measure of the 'quality' of the local minimum of the sqr.
Definition: CICP.h:203
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
viewpoint2
CPose3D viewpoint2(0.5, -0.2, 2.6, DEG2RAD(-5), DEG2RAD(100), DEG2RAD(-7))
sequence_scans2
vector< CObservation2DRangeScan > sequence_scans2
Definition: vision_stereo_rectify/test.cpp:42
CDisk.h
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
CAngularObservationMesh.h
mrpt::maps::CSimplePointsMap
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans.
Definition: CSimplePointsMap.h:31
mrpt::slam::CICP::TConfigParams::thresholdAng
double thresholdAng
Definition: CICP.h:116
mrpt::maps::CPointsMap::renderOptions
TRenderOptions renderOptions
Definition: CPointsMap.h:316
mrpt::slam::CICP
Several implementations of ICP (Iterative closest point) algorithms for aligning two point maps or a ...
Definition: CICP.h:67
mrpt::opengl::CSetOfObjects::Ptr
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:30
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
HOW_MANY_YAWS
const size_t HOW_MANY_YAWS
Definition: vision_stereo_rectify/test.cpp:55
mrpt::slam::CMetricMapsAlignmentAlgorithm::Align3D
mrpt::poses::CPose3DPDF::Ptr Align3D(const mrpt::maps::CMetricMap *m1, const mrpt::maps::CMetricMap *m2, const mrpt::poses::CPose3D &grossEst, float *runningTime=nullptr, void *info=nullptr)
The method for aligning a pair of metric maps, aligning the full 6D pose.
Definition: CMetricMapsAlignmentAlgorithm.cpp:36
mrpt::opengl::stock_objects::CornerXYZ
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
Definition: StockObjects.cpp:209
test_icp3D
void test_icp3D()
Definition: vision_stereo_rectify/test.cpp:75
mean
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
Definition: eigen_plugins.h:427
SCAN2_POSE_ERROR
CPose3D SCAN2_POSE_ERROR(0.15, -0.07, 0.10, -0.03, 0.1, 0.1)
mrpt::opengl::CDisk::Ptr
std::shared_ptr< CDisk > Ptr
Definition: CDisk.h:35
mrpt::poses::CPose3DPDF::Ptr
std::shared_ptr< CPose3DPDF > Ptr
Definition: CPose3DPDF.h:45
CPose3DPDF.h
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::slam::CICP::TReturnInfo::nIterations
unsigned short nIterations
The number of executed iterations until convergence.
Definition: CICP.h:196
mrpt::maps
Definition: CBeacon.h:24
mrpt::maps::CPointsMap::TRenderOptions::color
mrpt::img::TColorf color
Color of points.
Definition: CPointsMap.h:312
HOW_MANY_PITCHS
const size_t HOW_MANY_PITCHS
Definition: vision_stereo_rectify/test.cpp:56
CSimplePointsMap.h
CGridPlaneXY.h
mrpt::slam
Definition: CMultiMetricMapPDF.h:27
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
viewpoint1
CPose3D viewpoint1(-0.3, 0.7, 3, DEG2RAD(5), DEG2RAD(80), DEG2RAD(3))
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
CObservation2DRangeScan.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST