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 #include <mrpt/graphs/dijkstra.h>
13 #include <mrpt/system/CTicTac.h>
14 #include <mrpt/random.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace mrpt::graphs;
20 using namespace mrpt::poses;
21 using namespace mrpt::math;
22 using namespace mrpt::gui;
23 using namespace mrpt::random;
24 using namespace mrpt::system;
25 using namespace std;
26 
27 // The type of my Dijkstra problem:
29  CMyDijkstra; // See other options in mrpt::poses::CNetworkOfPoses<>
30 
31 // Before MRPT 0.9.5 it was:
32 // typedef CDijkstra<CNetworkOfPoses2D::edge_t> CMyDijkstra;
33 
34 // adds a new edge to the graph. The edge is annotated with the relative
35 // position of the two nodes
36 void addEdge(
37  TNodeID from, TNodeID to,
39  CNetworkOfPoses2D& graph_links)
40 {
41  CPose2D p = real_poses.find(to)->second - real_poses.find(from)->second;
42  graph_links.insertEdge(from, to, p);
43 }
44 
45 // weight is the distance between two nodes.
46 double myDijkstraWeight(
47  const CMyDijkstra::graph_t& g, const TNodeID from, const TNodeID to,
48  const CMyDijkstra::edge_t& edge)
49 {
50  // return 1; // Topological distance
51  return edge.norm(); // Metric distance
52 }
53 
54 // ------------------------------------------------------
55 // TestDijkstra
56 // ------------------------------------------------------
57 void TestDijkstra()
58 {
59  CTicTac tictac;
60  CNetworkOfPoses2D graph_links;
61  CNetworkOfPoses2D::global_poses_t optimal_poses, optimal_poses_dijkstra;
63 
65 
66  // ----------------------------
67  // Create a random graph:
68  // ----------------------------
69  const size_t N_VERTEX = 20;
70  const double DIST_THRES = 10;
71  const double NODES_XY_MAX = 15;
72 
73  vector<float> xs, ys;
74 
75  for (size_t j = 0; j < N_VERTEX; j++)
76  {
77  CPose2D p(
78  getRandomGenerator().drawUniform(-NODES_XY_MAX, NODES_XY_MAX),
79  getRandomGenerator().drawUniform(-NODES_XY_MAX, NODES_XY_MAX),
80  getRandomGenerator().drawUniform(-M_PI, M_PI));
81  real_poses[j] = p;
82 
83  // for the figure:
84  xs.push_back(p.x());
85  ys.push_back(p.y());
86  }
87 
88  // Add some edges
89  for (size_t i = 0; i < N_VERTEX; i++)
90  {
91  for (size_t j = 0; j < N_VERTEX; j++)
92  {
93  if (i == j) continue;
94  if (real_poses[i].distanceTo(real_poses[j]) < DIST_THRES)
95  addEdge(i, j, real_poses, graph_links);
96  }
97  }
98 
99  // ----------------------------
100  // Dijkstra
101  // ----------------------------
102  tictac.Tic();
103  const size_t SOURCE_NODE = 0;
104 
105  CMyDijkstra myDijkstra(graph_links, SOURCE_NODE, &myDijkstraWeight);
106 
107  cout << "Dijkstra took " << tictac.Tac() * 1e3 << " ms for "
108  << graph_links.edges.size() << " edges." << endl;
109 
110  // Demo of getting the tree representation of
111  // the graph & visit its nodes:
112  // ---------------------------------------------------------
113  CMyDijkstra::tree_graph_t graphAsTree;
114  myDijkstra.getTreeGraph(graphAsTree);
115 
116  // Text representation of the tree:
117  cout << "TREE:\n" << graphAsTree.getAsTextDescription() << endl;
118 
119  struct CMyVisitor : public CMyDijkstra::tree_graph_t::Visitor
120  {
121  virtual void OnVisitNode(
122  const TNodeID parent,
123  const CMyDijkstra::tree_graph_t::TEdgeInfo& edge_to_child,
124  const size_t depth_level) override
125  {
126  cout << string(depth_level * 3, ' ');
127  cout << edge_to_child.id << endl;
128  }
129  };
130 
131  CMyVisitor myVisitor;
132 
133  cout << "Depth-first traverse of graph:\n";
134  cout << SOURCE_NODE << endl;
135  graphAsTree.visitDepthFirst(SOURCE_NODE, myVisitor);
136 
137  cout << endl << "Breadth-first traverse of graph:\n";
138  cout << SOURCE_NODE << endl;
139  graphAsTree.visitBreadthFirst(SOURCE_NODE, myVisitor);
140 
141  // ----------------------------
142  // Display results graphically:
143  // ----------------------------
144  CDisplayWindowPlots win("Dijkstra example");
145 
146  win.hold_on();
147  win.axis_equal();
148 
149  for (TNodeID i = 0; i < N_VERTEX && win.isOpen(); i++)
150  {
151  if (i == SOURCE_NODE) continue;
152 
154 
155  myDijkstra.getShortestPathTo(i, path);
156 
157  cout << "to " << i << " -> #steps= " << path.size() << endl;
158 
159  win.setWindowTitle(
160  format(
161  "Dijkstra path %u->%u", static_cast<unsigned int>(SOURCE_NODE),
162  static_cast<unsigned int>(i)));
163 
164  win.clf();
165 
166  // plot all edges:
167  for (CNetworkOfPoses2D::iterator e = graph_links.begin();
168  e != graph_links.end(); ++e)
169  {
170  const CPose2D& p1 = real_poses[e->first.first];
171  const CPose2D& p2 = real_poses[e->first.second];
172 
173  vector<float> X(2);
174  vector<float> Y(2);
175  X[0] = p1.x();
176  Y[0] = p1.y();
177  X[1] = p2.x();
178  Y[1] = p2.y();
179  win.plot(X, Y, "k1");
180  }
181 
182  // Draw the shortest path:
183  for (CMyDijkstra::edge_list_t::const_iterator a = path.begin();
184  a != path.end(); ++a)
185  {
186  const CPose2D& p1 = real_poses[a->first];
187  const CPose2D& p2 = real_poses[a->second];
188 
189  vector<float> X(2);
190  vector<float> Y(2);
191  X[0] = p1.x();
192  Y[0] = p1.y();
193  X[1] = p2.x();
194  Y[1] = p2.y();
195  win.plot(X, Y, "g3");
196  }
197 
198  // Draw All nodes:
199  win.plot(xs, ys, ".b7");
200  win.axis_fit(true);
201 
202  cout << "Press any key to show next shortest path, close window to "
203  "end...\n";
204  win.waitForKey();
205  }
206 
207  win.clear();
208 }
209 
210 int main()
211 {
212  try
213  {
214  TestDijkstra();
215  return 0;
216  }
217  catch (exception& e)
218  {
219  cout << "MRPT exception caught: " << e.what() << endl;
220  return -1;
221  }
222  catch (...)
223  {
224  printf("Another exception!!");
225  return -1;
226  }
227 }
addEdge
void addEdge(TNodeID from, TNodeID to, const mrpt::aligned_std_map< TNodeID, CPose2D > &real_poses, CNetworkOfPoses2D &graph_links)
Definition: vision_stereo_rectify/test.cpp:36
CNetworkOfPoses.h
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::graphs::CDirectedGraph::insertEdge
void insertEdge(TNodeID from_nodeID, TNodeID to_nodeID, const edge_t &edge_value)
Insert an edge (from -> to) with the given edge value.
Definition: CDirectedGraph.h:123
TestDijkstra
void TestDijkstra()
Definition: vision_stereo_rectify/test.cpp:57
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::graphs::CDirectedGraph::begin
iterator begin()
Definition: CDirectedGraph.h:106
mrpt::graphs::CDirectedTree::visitDepthFirst
void visitDepthFirst(const TNodeID vroot, Visitor &user_visitor, const size_t root_depth_level=0) const
Depth-first visit of all children nodes of a given root (itself excluded from the visit),...
Definition: CDirectedTree.h:122
mrpt::graphs::TNodeID
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
Definition: TNodeID.h:17
mrpt::aligned_std_map
std::map< KEY, VALUE, std::less< KEY >, mrpt::aligned_allocator_cpp11< std::pair< const KEY, VALUE > >> aligned_std_map
Definition: aligned_std_map.h:17
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
g
GLubyte g
Definition: glext.h:6279
aligned_std_map.h
mrpt::graphs::CDirectedGraph::edges
edges_map_t edges
The public member with the directed edges in the graph.
Definition: CDirectedGraph.h:100
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::graphs::CDirectedTree::getAsTextDescription
std::string getAsTextDescription() const
Return a text representation of the tree spanned in a depth-first view, as in this example:
Definition: CDirectedTree.h:174
dijkstra.h
mrpt::graphs::CDijkstra::graph_t
TYPE_GRAPH graph_t
The type of the graph, typically a mrpt::graphs::CDirectedGraph<> or any other derived class.
Definition: dijkstra.h:157
random.h
mrpt::graphs::CDirectedTree::visitBreadthFirst
void visitBreadthFirst(const TNodeID vroot, Visitor &user_visitor, const size_t root_depth_level=0) const
Breadth-first visit of all children nodes of a given root (itself excluded from the visit),...
Definition: CDirectedTree.h:144
mrpt::graphs::CDirectedTree
A special kind of graph in the form of a tree with directed edges and optional edge annotations of te...
Definition: CDirectedTree.h:54
mrpt::random::CRandomGenerator::randomize
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Definition: RandomGenerator.cpp:32
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
mrpt::graphs::CDijkstra::edge_t
typename graph_t::edge_t edge_t
The type of edge data in graph_t.
Definition: dijkstra.h:159
mrpt::graphs::CDijkstra::edge_list_t
std::list< TPairNodeIDs > edge_list_t
A list of edges used to describe a path on the graph.
Definition: dijkstra.h:161
mrpt::poses::CPoseOrPoint::y
double y() const
Definition: CPoseOrPoint.h:144
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
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::poses::CPoseOrPoint::x
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
mrpt::graphs::CDirectedGraph< CPOSE, mrpt::graphs::detail::edge_annotations_empty >::iterator
typename edges_map_t::iterator iterator
Definition: CDirectedGraph.h:92
mrpt::graphs
Abstract graph and tree data structures, plus generic graph algorithms.
Definition: CAStarAlgorithm.h:18
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:31
mrpt::graphs::CNetworkOfPoses
A directed graph of pose constraints, with edges being the relative poses between pairs of nodes iden...
Definition: CNetworkOfPoses.h:122
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
CTicTac.h
mrpt::graphs::CNetworkOfPoses::global_poses_t
typename MAPS_IMPLEMENTATION::template map< mrpt::graphs::TNodeID, global_pose_t > global_poses_t
A map from pose IDs to their global coordinate estimates, without uncertainty (the "most-likely value...
Definition: CNetworkOfPoses.h:229
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::graphs::CDijkstra
The Dijkstra algorithm for finding the shortest path between a given source node in a (weighted) dire...
Definition: dijkstra.h:102
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:17
mrpt::graphs::CDirectedGraph::end
iterator end()
Definition: CDirectedGraph.h:108
CDisplayWindowPlots.h
myDijkstraWeight
double myDijkstraWeight(const CMyDijkstra::graph_t &g, const TNodeID from, const TNodeID to, const CMyDijkstra::edge_t &edge)
Definition: vision_stereo_rectify/test.cpp:46
CMyDijkstra
CDijkstra< CNetworkOfPoses2D > CMyDijkstra
Definition: vision_stereo_rectify/test.cpp:29
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279



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