spanning_tree.cpp
Go to the documentation of this file.
1 #include "lib/spanning_tree.h"
2 
4 {
5 }
6 
8 {
9  return mst_edges;
10 }
11 
12 geometry_msgs::PoseArray spanning_tree::get_tree ()
13 {
14  geometry_msgs::PoseArray path;
15  vector<geometry_msgs::Pose> poses;
16  geometry_msgs::Pose pose;
17 
18  for (auto e : mst_edges) {
19  // from
20  pose.position.x = e.from % map.info.width * map.info.resolution + map.info.origin.position.x;
21  pose.position.y = e.from / map.info.width * map.info.resolution + map.info.origin.position.y;
22 
23  // orientation
24  double dx = e.to % map.info.width * map.info.resolution + map.info.origin.position.x - pose.position.x;
25  double dy = e.to / map.info.width * map.info.resolution + map.info.origin.position.y - pose.position.y;
26  tf2::Quaternion direction;
27  direction.setRPY(0, 0, atan2(dy, dx));
28  pose.orientation = tf2::toMsg(direction);
29 
30  poses.push_back(pose);
31  }
32 
33  path.poses = poses;
34  path.header.stamp = Time::now();
35  path.header.frame_id = "local_origin_ned";
36  return path;
37 }
38 
39 void spanning_tree::initialize_graph (nav_msgs::OccupancyGrid gridmap, bool connect4)
40 {
41  // initialize map
42  map = gridmap;
43  int rows = gridmap.info.height;
44  int cols = gridmap.info.width;
45 
46  // empty containers
47  nodes.clear();
48  nodes.resize(rows*cols);
49  edges = priority_queue<edge, vector<edge>, compare_edge>();
50 
51  // iterate all rows and columns
52  for (int i=0; i<rows; i++) {
53  for (int j=0; j<cols; j++) {
54  // found a vertex
55  if (map.data[i*cols+j] == 0) {
56  // check von neumann neighborhood for connected vertices
57  if (i>0 && map.data[(i-1)*cols+j] == 0) {
58  add_edge(i*cols+j, (i-1)*cols+j, 1);
59  }
60  if (i<rows-1 && map.data[(i+1)*cols+j] == 0) {
61  add_edge(i*cols+j, (i+1)*cols+j, 1);
62  }
63  if (j>0 && map.data[i*cols+j-1] == 0) {
64  add_edge(i*cols+j, i*cols+j-1, 1);
65  }
66  if (j<cols-1 && map.data[i*cols+j+1] == 0) {
67  add_edge(i*cols+j, i*cols+j+1, 1);
68  }
69 
70  // check moore neighborhood for connected vertices
71  if (!connect4) {
72  if (i>0 && j>0 && map.data[(i-1)*cols+j-1] == 0) {
73  add_edge(i*cols+j, (i-1)*cols+j-1, 1);
74  }
75  if (i<rows-1 && j<cols-1 && map.data[(i+1)*cols+j+1] == 0) {
76  add_edge(i*cols+j, (i+1)*cols+j+1, 1);
77  }
78  if (i<rows-1 && j>0 && map.data[(i+1)*cols+j-1] == 0) {
79  add_edge(i*cols+j, (i+1)*cols+j-1, 1);
80  }
81  if (i>0 && j<cols-1 && map.data[(i-1)*cols+j+1] == 0) {
82  add_edge(i*cols+j, (i-1)*cols+j+1, 1);
83  }
84  }
85  }
86  }
87  }
88 }
89 
91 {
92  mst_edges.clear();
93 
94  while (!edges.empty()) {
95  // select shortest edge
96  edge edge = edges.top();
97 
98  // edge connects different sets
99  if (nodes[edge.from] != nodes[edge.to]) {
100  // combine the two sets
101  unordered_set<int> s(nodes[edge.from].begin(), nodes[edge.from].end());
102  for (auto v : nodes[edge.to])
103  s.insert(v);
104  for (auto v : s)
105  nodes[v] = s;
106 
107  // add edge to mst
108  mst_edges.push_back(edge);
109  }
110 
111  // remove edge from source tree
112  edges.pop();
113  }
114 }
115 
116 void spanning_tree::add_edge (int from, int to, int cost)
117 {
118  // add edge to priority queue
119  edges.push(edge(from, to, cost));
120 
121  // create singleton set for both vertices
122  if (nodes[from].size() == 0) {
123  nodes[from].insert(from);
124  }
125  if (nodes[to].size() == 0) {
126  nodes[to].insert(to);
127  }
128 }
129 
130 bool spanning_tree::different_sets (int a, int b)
131 {
132  return nodes[a] != nodes[b];
133 }
nav_msgs::OccupancyGrid map
The grid map that needs to be covered by the MST.
Definition: spanning_tree.h:86
vector< edge > mst_edges
Edges in Minimal-Spanning Tree.
Definition: spanning_tree.h:81
void add_edge(int from, int to, int cost)
Add an edge to the tree.
void construct()
Generate the MST using Kruskal&#39;s algorithm.
nav_msgs::OccupancyGrid gridmap
The grid map representing the environment to be covered.
Definition: coverage_path.h:69
A struct that provides the comparison of edge objects.
Definition: edge.h:46
vector< edge > get_mst_edges()
Get the edges of the MST.
int to
The ending vertex of the edge.
Definition: edge.h:35
A class for representing edges.
Definition: edge.h:9
geometry_msgs::Pose pose
Current position of the CPS.
Definition: coverage_path.h:39
int from
The starting vertex of the edge.
Definition: edge.h:30
spanning_tree()
Constructor.
mst_path path
The coverage path.
Definition: coverage_path.h:79
geometry_msgs::PoseArray get_tree()
Get the generated MST for visualization.
vector< unordered_set< int > > nodes
The sets of connected vertices.
Definition: spanning_tree.h:71
void initialize_graph(nav_msgs::OccupancyGrid gridmap, bool connect4=true)
Initialize the internal tree structure from a given grid map.
priority_queue< edge, vector< edge >, compare_edge > edges
Priority queue of edge objects sorted by cost.
Definition: spanning_tree.h:76
bool different_sets(int a, int b)
Check whether two vertices are in different sets.


coverage_path
Author(s): Micha Sende
autogenerated on Thu Oct 31 2019 10:37:30