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 
13 #include <mrpt/system/CTicTac.h>
15 #include <mrpt/system/filesystem.h>
17 #include <mrpt/poses/CPose2D.h>
18 #include <iostream>
19 
20 using namespace mrpt;
21 using namespace mrpt::maps;
22 using namespace mrpt::nav;
23 using namespace mrpt::serialization;
24 using namespace mrpt::img;
25 using namespace mrpt::math;
26 using namespace mrpt::poses;
27 using namespace mrpt::io;
28 using namespace mrpt::system;
29 using namespace std;
30 
31 #include <mrpt/examples_config.h>
32 
33 string myGridMap(
34  MRPT_EXAMPLES_BASE_DIRECTORY +
35  string("../share/mrpt/datasets/2006-MalagaCampus.gridmap.gz"));
36 
37 // ------------------------------------------------------
38 // TestPathPlanning
39 // ------------------------------------------------------
40 void TestPathPlanning()
41 {
42  // Load the gridmap:
43  COccupancyGridMap2D gridmap;
44 
46  THROW_EXCEPTION_FMT("Map file '%s' not found", myGridMap.c_str());
47 
48  printf("Loading gridmap...");
49  {
51  auto arch = archiveFrom(f);
52  arch >> gridmap;
53  }
54  printf(
55  "Done! %f x %f m\n", gridmap.getXMax() - gridmap.getXMin(),
56  gridmap.getYMax() - gridmap.getYMin());
57 
58  // Find path:
59  PlannerSimple2D pathPlanning;
60  pathPlanning.robotRadius = 0.30f;
61 
62  std::deque<TPoint2D> thePath;
63  bool notFound;
64  CTicTac tictac;
65 
66  CPose2D origin(20, -110, 0);
67  CPose2D target(90, 40, 0);
68 
69  cout << "Origin: " << origin << endl;
70  cout << "Target: " << target << endl;
71 
72  cout << "Searching path...";
73  cout.flush();
74  tictac.Tic();
75 
76  pathPlanning.computePath(
77  gridmap, origin, target, thePath, notFound, 100.0f /* Max. distance */);
78 
79  double t = tictac.Tac();
80  cout << "Done in " << t * 1000 << " ms" << endl;
81 
82  printf("Path found: %s\n", notFound ? "NO" : "YES");
83  printf("Path has %u steps\n", (unsigned)thePath.size());
84 
85  // Save result:
86  CImage img;
87  gridmap.getAsImage(img, false, true); // Force a RGB image
88 
89  // Draw the path:
90  // ---------------------
91  int R = round(pathPlanning.robotRadius / gridmap.getResolution());
92 
93  for (std::deque<TPoint2D>::const_iterator it = thePath.begin();
94  it != thePath.end(); ++it)
95  img.drawCircle(
96  gridmap.x2idx(it->x), gridmap.getSizeY() - 1 - gridmap.y2idx(it->y),
97  R, TColor(0, 0, 255));
98 
99  img.cross(
100  gridmap.x2idx(origin.x()),
101  gridmap.getSizeY() - 1 - gridmap.y2idx(origin.y()),
102  TColor(0x20, 0x20, 0x20), '+', 10);
103  img.cross(
104  gridmap.x2idx(target.x()),
105  gridmap.getSizeY() - 1 - gridmap.y2idx(target.y()),
106  TColor(0x50, 0x50, 0x50), 'x', 10);
107 
108  const std::string dest = "path_planning.png";
109  cout << "Saving output to: " << dest << endl;
110  img.saveToFile(dest);
111  printf("Done\n");
112 
113 #if MRPT_HAS_WXWIDGETS
114  mrpt::gui::CDisplayWindow win("Computed path");
115  win.showImage(img.scaleHalf().scaleHalf());
116 
117  win.waitForKey();
118 #endif
119 }
120 
121 int main(int argc, char** argv)
122 {
123  try
124  {
126  return 0;
127  }
128  catch (exception& e)
129  {
130  cout << "MRPT exception caught: " << e.what() << endl;
131  return -1;
132  }
133  catch (...)
134  {
135  printf("Another exception!!");
136  return -1;
137  }
138 }
filesystem.h
TestPathPlanning
void TestPathPlanning()
Definition: vision_stereo_rectify/test.cpp:40
COccupancyGridMap2D.h
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::nav::PlannerSimple2D
Searches for collision-free path in 2D occupancy grids for holonomic circular robots.
Definition: PlannerSimple2D.h:33
mrpt::io
Definition: img/CImage.h:22
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::nav
Definition: CAbstractHolonomicReactiveMethod.h:23
mrpt::maps::COccupancyGridMap2D::getAsImage
void getAsImage(mrpt::img::CImage &img, bool verticalFlip=false, bool forceRGB=false, bool tricolor=false) const
Returns the grid as a 8-bit graylevel image, where each pixel is a cell (output image is RGB only if ...
Definition: COccupancyGridMap2D_getAs.cpp:29
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
R
const float R
Definition: CKinematicChain.cpp:138
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CPose2D.h
mrpt::system::fileExists
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
mrpt::maps::COccupancyGridMap2D::getXMax
float getXMax() const
Returns the "x" coordinate of right side of grid map.
Definition: COccupancyGridMap2D.h:302
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::maps::COccupancyGridMap2D::getSizeY
unsigned int getSizeY() const
Returns the vertical size of grid map in cells count.
Definition: COccupancyGridMap2D.h:298
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::maps::COccupancyGridMap2D::getYMax
float getYMax() const
Returns the "y" coordinate of bottom side of grid map.
Definition: COccupancyGridMap2D.h:306
mrpt::img
Definition: CCanvas.h:17
mrpt::io::CFileGZInputStream
Transparently opens a compressed "gz" file and reads uncompressed data from it.
Definition: io/CFileGZInputStream.h:26
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::poses::CPose2D
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:40
mrpt::maps::COccupancyGridMap2D::y2idx
int y2idx(float y) const
Definition: COccupancyGridMap2D.h:314
mrpt::maps::COccupancyGridMap2D::x2idx
int x2idx(float x) const
Transform a coordinate value into a cell index.
Definition: COccupancyGridMap2D.h:310
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
mrpt::maps::COccupancyGridMap2D::getYMin
float getYMin() const
Returns the "y" coordinate of top side of grid map.
Definition: COccupancyGridMap2D.h:304
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
mrpt::serialization
Definition: aligned_serialization.h:14
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
myGridMap
string myGridMap(MRPT_EXAMPLES_BASE_DIRECTORY+string("../share/mrpt/datasets/2006-MalagaCampus.gridmap.gz"))
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:561
mrpt::maps::COccupancyGridMap2D::getResolution
float getResolution() const
Returns the resolution of the grid map.
Definition: COccupancyGridMap2D.h:308
img
GLint GLvoid * img
Definition: glext.h:3763
CTicTac.h
mrpt::maps::COccupancyGridMap2D::getXMin
float getXMin() const
Returns the "x" coordinate of left side of grid map.
Definition: COccupancyGridMap2D.h:300
CFileGZInputStream.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
PlannerSimple2D.h
mrpt::maps::COccupancyGridMap2D
A class for storing an occupancy grid map.
Definition: COccupancyGridMap2D.h:62
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::nav::PlannerSimple2D::robotRadius
float robotRadius
The aproximate robot radius used in the planification.
Definition: PlannerSimple2D.h:54
mrpt::maps
Definition: CBeacon.h:24
CArchive.h
mrpt::nav::PlannerSimple2D::computePath
void computePath(const mrpt::maps::COccupancyGridMap2D &theMap, const mrpt::poses::CPose2D &origin, const mrpt::poses::CPose2D &target, std::deque< mrpt::math::TPoint2D > &path, bool &notFound, float maxSearchPathLength=-1) const
This method compute the optimal path for a circular robot, in the given occupancy grid map,...
Definition: PlannerSimple2D.cpp:32
CDisplayWindow.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



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