forked from DEIS-Tools/Swarm-Robotics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
370 lines (334 loc) · 12.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "gnuplot.h"
#include "map_structure.h"
#include "stratego.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <sys/resource.h>
#define THRESHOLD_OF_HARD_POINT 65
#define DEFAULT_MAP_PATH "../Data/"
//If there are less then MAX_NOISINESS points in area it will be considered
//as noisiness, thus will be ignored
#define MAX_NOISINESS 3
//Used to find all the adj points. The bigger the value the wider
//range is going to be used
#define POINT_BELONGS 8
//Retrieves data such as height, width and other parameters of the map;
//In the future there might be more data needed to be extracted from the map file
std::string readFromFile(std::string fileName, int &width, int &height);
//Given width and height extracts all points of the map
void harvestData(std::vector<std::vector<pair<int, bool>>> &result,
const std::string& data);
//Function responsible for extraction of each Figure in the map
std::vector<Figure> extractFigures(std::vector<std::vector<pair<int, bool>>> &data);
//Using GNUplot and Plotting folder data draws the final map
void drawWorld();
//Clean ups the Plotting folder data
void cleanResultFiles();
//Main method for parsing and creating the map
void initializeMap();
//Used for path configuration, for text user interface
void setupPaths();
Map_Structure &sMap = Map_Structure::get_instance();
std::string custom_UPPAAL_PATH, custom_MAP_PATH;
int width = -1, height = -1;
int main() {
const rlim_t kStackSize = 100 * 1024 * 1024; // min stack size = 100 MB
struct rlimit rl{};
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
bool initializedMap = false;
int choice;
while (true) {
std::cout << "Choose option 1-6" << std::endl;
std::cout << "1. Setup paths" << std::endl;
std::cout
<< "2. Initialize the map (Generates the static analysis of the map)"
<< std::endl;
std::cout << "3. Request for station plan" << std::endl;
std::cout << "4. Request for waypoint plan" << std::endl;
std::cout << "5. Display map" << std::endl;
std::cout << "6. Exit" << std::endl;
std::cin >> choice;
switch (choice) {
case 1:
setupPaths();
break;
case 2:
try {
initializeMap();
}
catch (std::exception& e) {
std::cout << "error: " << e.what() << std::endl;
}
initializedMap = true;
break;
case 3:
if (initializedMap) {
try {
std::cout << stratego::getSingleTrace(stratego::queryType::stations,
custom_UPPAAL_PATH.empty() ? DEFAULT_UPPAAL_PATH : custom_MAP_PATH)
<< std::endl;
}
catch (std::runtime_error &e) {
std::cout << "error: " << e.what() << std::endl;
}
}
else
std::cout << "Please initialize map first" << std::endl;
break;
case 4:
if (initializedMap) {
try {
std::cout << stratego::getSingleTrace(stratego::queryType::waypoints,
custom_UPPAAL_PATH.empty() ? DEFAULT_UPPAAL_PATH : custom_MAP_PATH)
<< std::endl;
}
catch (std::exception& e) {
std::cout << "error: " << e.what() << std::endl;
}
}
else
std::cout << "Please initialize map first" << std::endl;
break;
case 5:
if (initializedMap) {
//After the initialization of the map was completed, one may invoke the drawing of the map
drawWorld();
}
else std::cout << "Please initialize map first" << std::endl;
break;
case 6:
return 0;
default:
std::cout << "Please select a proper choice 1-6" <<std::endl;
break;
}
}
}
void setupPaths(){
int choice;
regex regexp("^(~/|/)([[a-zA-Z_0-9]+/]*)+$");
smatch m;
while (true){
std::cout << "Choose option 1-4" << std::endl;
std::cout << "1. Set Uppaal Stratego 64-bit path" << std::endl;
std::cout << "2. Set map path"<< std::endl;
std::cout << "3. Use default paths" << std::endl;
std::cout << "4. Back" << std::endl;
std::cin >> choice;
std::cout << "choice: "<<choice<<std::endl;
switch (choice) {
case 1:
std::cout<<"Enter Uppaal path (Default:~/Desktop/uppaalStratego/)" <<std::endl;
cin >> custom_UPPAAL_PATH;
while(!regex_match(custom_UPPAAL_PATH, regexp)){
std::cout<< "Incorrect path, please try again" <<std::endl;
cin >> custom_UPPAAL_PATH;
}
std::cout << "New Uppaal path: " << custom_UPPAAL_PATH <<std::endl;
break;
case 2:
std::cout<<"Enter map and station folder path (Default:.../mapConversion/Data/)" <<std::endl;
std::cout<<"Map file name: data.txt" <<std::endl;
std::cout<<"Station file name: points.json" <<std::endl;
while(!regex_match(custom_MAP_PATH, regexp)){
std::cout<< "Incorrect path, please try again" <<std::endl;
cin >> custom_MAP_PATH;
}
std::cout << "New Map folder path: " << custom_MAP_PATH <<std::endl;
break;
case 3:
custom_MAP_PATH = "";
custom_UPPAAL_PATH = "";
std::cout<< "The paths have been chosen to be default ones" <<std::endl;
std::cout <<"Uppaal path:"<< DEFAULT_UPPAAL_PATH <<std::endl;
std::cout <<"Map folder path:"<< DEFAULT_MAP_PATH <<std::endl;
break;
case 4:
return;
default:
std::cout << "Please select a proper choice 1-4" <<std::endl;
break;
}
}
}
void initializeMap() {
try{
cleanResultFiles();
// gathers data from file and cleans it up
std::string data = readFromFile(custom_MAP_PATH.empty()? DEFAULT_MAP_PATH :custom_MAP_PATH , width, height);
std::cout << "width: " << width << std::endl;
std::cout << "height: " << height << std::endl;
std::vector<std::vector<pair<int, bool>>> matrix(height);
// harvest the data from string into 2D vector
harvestData(matrix, data);
// std::cout << "x matrix: " << matrix.back().size()<<std::endl;
// std::cout << "y matrix: " << matrix.size()<<std::endl;
sMap.initializeStations(DEFAULT_MAP_PATH);
std::cout << "initializeStations complete" << std::endl;
extractFigures(matrix);
std::cout << "extractFigures complete" << std::endl;
sMap.collectAllWayPoints();
std::cout << "collectAllWayPoints complete" << std::endl;
sMap.setAllPossibleLines(width*height);
std::cout << "setAllPossibleLines complete" << std::endl;
//perform sorting of all the lines for the creation of static config
sMap.sortLines();
//Invokes the creation of static_config file
sMap.createStaticJSON("../config");
}
catch(std::exception& e){
throw e;
}
}
void cleanResultFilesHelper(const std::string& path){
std::ofstream ofs;
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
if(ofs.fail()) throw std::runtime_error("Failed opening file: " + path);
ofs.close();
}
void cleanResultFiles() {
try{
if (mkdir("../Plotting", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
{
if( errno != EEXIST ) {
throw std::runtime_error("cannot create Plotting folder");
}
}
cleanResultFilesHelper("../Plotting/offset.dat");
cleanResultFilesHelper("../Plotting/figures.dat");
cleanResultFilesHelper("../Plotting/stations.dat");
cleanResultFilesHelper("../Plotting/lines.dat");
}
catch(std::exception& e){
throw e;
}
}
void harvestData(std::vector<std::vector<pair<int, bool>>> &result,
const std::string& data) { // stores data in result
std::stringstream ss(data);
int counter = 1, counter2 = 0;
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
result[counter2].push_back(make_pair(stoi(substr), false));
if (counter++ == width) {
counter2++;
counter = 1;
}
}
//plots the corners of the map
ofstream file;
file.open("../Plotting/figures.dat", std::ios_base::app);
if(file.fail())
throw std::runtime_error("Failed opening file: /Plotting/figures.dat");
file << result[0].size() << " " << 0 << "\n";
file << 0 << " " << result.size() << "\n";
file << 0 << " " << 0 << "\n";
file.close();
}
std::string readFromFile(std::string fileName, int &width, int &height) {
std::string line, full;
std::size_t pos;
std::string fileLoc = fileName+"data.txt";
std::ifstream file(fileLoc);
if(file.fail()) throw std::runtime_error("Failed opening file: " + fileLoc);
// read from file
while (getline(file, line)) {
// if line consist of width of height harvest the values
if (line.find("width: ") != std::string::npos) {
pos = line.find("width: ") + 7;
width = stoi(line.substr(pos, line.size()));
}
if (line.find("height: ") != std::string::npos) {
pos = line.find("height: ") + 8;
height = stoi(line.substr(pos, line.size()));
}
full += line;
}
file.close();
if(width == -1 || height == -1) throw std::runtime_error("Map file does not consist of width or height");
// clean up data
pos = full.find("data: [") + 7;
full = full.substr(pos, full.size());
pos = full.find(']');
full.erase(pos, full.size());
full.erase(std::remove_if(full.begin(), full.end(), ::isspace), full.end());
full.erase(std::remove(full.begin(), full.end(), '\n'), full.end());
return full;
}
void identifyAdjPoints(int row, int column, std::shared_ptr<Figure> &fig,
std::vector<std::vector<pair<int, bool>>> &data) {
if ((data[row][column].first > THRESHOLD_OF_HARD_POINT || data[row][column].first ==-1) && !data[row][column].second) {
//if (data[row][column].first > THRESHOLD_OF_HARD_POINT && !data[row][column].second) {
//Point point = Point(row, column, 0, Type::realCorner, "", fig);
// std::cout << fig->getPoints().max_size() <<std::endl;
fig->addPoint(Point(row, column, 0, Type::realCorner, " ", fig));
data[row][column].second = true;
for (int i = -POINT_BELONGS; i <= POINT_BELONGS; i++) {
for (int j = -POINT_BELONGS; j <= POINT_BELONGS; j++) {
if (column + i >= 0 && row + j >= 0 && column + i < width && row + j < height) {
//recursively call the method with different values
//in order to see if there exist more points belonging to this figure
identifyAdjPoints(row + j, column + i, fig, data);
}
}
}
}
}
std::vector<Figure>
extractFigures(std::vector<std::vector<pair<int, bool>>> &data) {
bool once = true;
std::vector<Figure> figures;
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data[i].size(); j++) {
if ((data[i][j].first > THRESHOLD_OF_HARD_POINT || data[i][j].first ==-1) && !data[i][j].second) {
//if (data[i][j].first > THRESHOLD_OF_HARD_POINT && !data[i][j].second) {
std::shared_ptr<Figure> fig(new Figure());
//Whenever we find first hard point, we are creating the new Figure Object for it
//and scout the nearest area in order to harvest the rest of the points which belong to the figure
identifyAdjPoints(i, j, fig, data);
//If figure contains of less then MAX_NOISINESS then we ignore this figure
if(fig->getPoints().size() >MAX_NOISINESS){
if(!once) {
//After harvesting the figure we find it's convex hull
fig->convexHull();
//Finally save the new figure
sMap.figures.push_back(fig);
}
once = false;
}
}
}
}
return figures;
}
void drawWorld() {
GnuplotPipe gp;
std::string command =
"set style line 2 lc rgb 'black' pt 5\n set style line 3 lc rgb 'red' pt "
"5\n set style line 4 lc rgb 'blue' pt 5\n set xrange [0:";
command += std::to_string(width);
command += "] \n set yrange [0:";
command += std::to_string(height);
command += "] \n plot '../Plotting/figures.dat' with points ls 2 , '../Plotting/offset.dat' with "
"points ls 3, '../Plotting/stations.dat' with points ls 4, '../Plotting/lines.dat' "
"using 1:2 with linespoints";
gp.sendLine(command);
gp.sendEndOfData();
}