-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
278 lines (221 loc) · 8.24 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <unordered_set>
#include <queue>
#include <functional>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <fstream>
#include "include/json.hpp"
#include "include/Map2D.h"
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#define FACTOR 50
using json = nlohmann::json;
// This class define the single cell
class GridLocation {
public:
int x, y;
inline bool operator==(const GridLocation& other) const {
return x == other.x && y == other.y;
}
inline bool operator!=(const GridLocation& other) const {
return x != other.x || y != other.y;
}
inline bool operator<(const GridLocation& other) const {
return x < other.x && y < other.y;
}
inline bool operator>(const GridLocation& other) const {
return x > other.x || y > other.y;
}
GridLocation(int x_, int y_) : x(x_), y(y_) {};
GridLocation() : x(0), y(0) {};
};
namespace std {
// implement hash function so we can put GridLocation into an unordered_set
template <> struct hash<GridLocation> {
typedef GridLocation argument_type;
typedef std::size_t result_type;
std::size_t operator()(const GridLocation& id) const noexcept {
return std::hash<int>()(id.x ^ (id.y << 4));
}
};
}
class SquareGrid {
private:
int size_x, size_y;
std::unordered_set<GridLocation> walls;
public:
// Eight connected map
const std::array<GridLocation, 8> DIRS = {GridLocation{1, 0}, GridLocation{0, -1}, GridLocation{-1, 0}, GridLocation{0, 1},
GridLocation{1, 1}, GridLocation{-1, -1}, GridLocation{-1, 1}, GridLocation{1, -1}};
SquareGrid(int size_x_, int size_y_) : size_x(size_x_), size_y(size_y_) {}
// check if a cell is in the bounds of the map
bool in_bounds(GridLocation id) const {
return 0 <= id.x && id.x < size_x
&& 0 <= id.y && id.y < size_y;
}
// check if a cell is a wall
bool passable(GridLocation id) const {
return walls.find(id) == walls.end();
}
// create a new wall
void add_wall(int x, int y) {
walls.emplace(x, y);
}
// return the list of free cells around a specific one
std::vector<GridLocation> neighbors(GridLocation id) const {
std::vector<GridLocation> results;
for (GridLocation dir : DIRS) {
GridLocation next{id.x + dir.x, id.y + dir.y};
if (in_bounds(next) && passable(next)) {
results.push_back(next);
}
}
if ((id.x + id.y) % 2 == 0) {
// aesthetic improvement on square grids
std::reverse(results.begin(), results.end());
}
return results;
}
// cost to move from a cell to another as the euclidean distance
double cost(GridLocation from_node, GridLocation to_node) const {
return std::hypot((from_node.x - to_node.x), (from_node.y - to_node.y));
}
// print an ASCII grid
void draw_grid(std::vector<GridLocation> path) {
char map[size_x][size_y];
for(int x = 0; x < size_x; x++) {
for(int y=0; y < size_y; y++) {
map[x][y]='.';
}
}
for (const auto& elem : walls) {
map[elem.x][elem.y] = '#';
}
for (const auto& elem : path) {
map[elem.x][elem.y] = '@';
}
for(int x = 0; x < size_x; x++) {
for(int y=0; y < size_y; y++) {
std::cout<<map[x][y];
}
std::cout<<std::endl;
}
}
// visualise the map with openCV
void draw_map(std::vector<GridLocation> path) {
char sim_window[] = "Simulator";
cv::Mat sim_image(int(std::ceil(size_x * FACTOR)),
int(std::ceil(size_y * FACTOR)),
CV_8UC3, cv::Scalar( 255, 255, 255 ));
for (const auto& elem : walls) {
rectangle(sim_image, cv::Point(elem.x * FACTOR, elem.y * FACTOR), cv::Point((elem.x + 1) * FACTOR, (elem.y + 1) * FACTOR),
cv::Scalar( 0, 0, 0 ), cv::FILLED, cv::LINE_8);
}
for (const auto& elem : path) {
circle(sim_image, cv::Point(int(elem.x * FACTOR + FACTOR/2), int(elem.y * FACTOR + FACTOR/2)),
5, cv::Scalar(255, 0, 0), cv::FILLED, cv::LINE_8);
}
cv::imshow(sim_window,sim_image);
cv::waitKey(0);
}
};
template<typename T, typename priority_t>
struct PriorityQueue {
typedef std::pair<T, priority_t> PQElement;
std::priority_queue<PQElement, std::vector<PQElement>, std::greater<PQElement>> elements;
inline bool empty() const {
return elements.empty();
}
inline void put(T item, priority_t priority) {
elements.emplace(item, priority);
}
priority_t get() {
priority_t best_item = elements.top().second;
elements.pop();
return best_item;
}
};
// It's ya boi Dijkstra
template<typename Location, typename Graph>
void dijkstra_search(Graph graph, Location start, Location goal,
std::unordered_map<Location, Location>& came_from,
std::unordered_map<Location, double>& cost_so_far) {
PriorityQueue<double, Location> frontier;
frontier.put(0, start);
std::cout<<"start "<<start.x<<" "<<start.y<<std::endl;
std::cout<<"goal "<<goal.x<<" "<<goal.y<<std::endl;
came_from[start] = start;
cost_so_far[start] = 0;
while (!frontier.empty()) {
Location current = frontier.get();
if (current == goal) {
break;
}
for (Location next : graph.neighbors(current)) {
double new_cost = cost_so_far[current] + graph.cost(current, next);
if (cost_so_far.find(next) == cost_so_far.end() || new_cost < cost_so_far[next]) {
cost_so_far[next] = new_cost;
came_from[next] = current;
frontier.put(new_cost, next);
}
}
}
}
template<typename Location>
std::vector<Location> reconstruct_path(Location start, Location goal, std::unordered_map<Location, Location> came_from) {
std::vector<Location> path;
Location current = goal;
while (current != start) {
path.push_back(current);
current = came_from[current];
}
path.push_back(start); // optional
std::reverse(path.begin(), path.end());
return path;
}
int main(int argc, char **argv) {
// TODO file location is hardcoded
std::string filename = "/home/gianluca/locations.json";
std::ifstream file(filename);
json map_location;
file >> map_location;
// TODO check the JSON file, map image location is hardcoded and an absolute path
// Open the image of the map from the JSON and get the size
cv::Mat image = cv::imread(map_location["map"]["location"], cv::IMREAD_GRAYSCALE);
Map2D tmap(map_location["map"]["occupiedThresh"],
map_location["map"]["freeThresh"],
image.cols,
image.rows);
// load the value of each pixel in a vector
for(auto it = image.begin<uchar>(); it != image.end<uchar>(); ++it) {
tmap.cells.push_back(*it);
}
SquareGrid local_map(tmap.cellSizeX, tmap.cellSizeY);
// if a pixel is above a specific threshold, save it as a wall in the gridmap
std::cout<<"Creating walls"<<std::endl;
int index = 0;
for(int y = 0; y < tmap.cellSizeX; y++){
for(int x = 0; x < tmap.cellSizeY; x++) {
if (((255 - tmap.cells[index]) / 255.0) > tmap.occupiedThresh) {
local_map.add_wall(x, y);
}
index++;
}
}
// define start and goal position
GridLocation start(map_location["start"][0], map_location["start"][1]);
GridLocation goal(map_location["goal"][0], map_location["goal"][1]);
std::unordered_map<GridLocation, GridLocation> came_from;
std::unordered_map<GridLocation, double> cost_so_far;
std::cout<<"plan"<<std::endl;
dijkstra_search(local_map, start, goal, came_from, cost_so_far);
std::vector<GridLocation> path = reconstruct_path(start, goal, came_from);
std::cout<<"done"<<std::endl;
//show result
local_map.draw_map(path);
return true;
}