-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·139 lines (124 loc) · 3.74 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
#include "visualizer.h"
#include "planner.h"
#include <fstream>
#include <string>
#include <vector>
#include <exception>
#include <algorithm>
#include <numeric>
#ifdef _MSC_VER
static const char* PATH_SEP = "\\";
#else
static const char* PATH_SEP = "/";
#endif
// Bits used in the overrides image bytes
enum OverrideFlags
{
OF_RIVER_MARSH = 0x10,
OF_INLAND = 0x20,
OF_WATER_BASIN = 0x40
};
// Some constants
enum {
IMAGE_DIM = 2048, // Width and height of the elevation and overrides image
ROVER_X = 159,
ROVER_Y = 1520,
BACHELOR_X = 1303,
BACHELOR_Y = 85,
WEDDING_X = 1577,
WEDDING_Y = 1294
};
std::ifstream::pos_type fileSize(const std::string& filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
if (!in.good())
{
throw std::exception();
}
return in.tellg();
}
std::vector<uint8_t> loadFile(const std::string& filename, size_t expectedFileSize)
{
size_t fsize = fileSize(filename);
if (fsize != expectedFileSize)
{
throw std::exception();
}
std::vector<uint8_t> data(fsize);
std::ifstream ifile(filename, std::ifstream::binary);
if (!ifile.good())
{
throw std::exception();
}
ifile.read((char*)&data[0], fsize);
return data;
}
bool donut(int x, int y, int x1, int y1)
{
int dx = x - x1;
int dy = y - y1;
int r2 = dx * dx + dy * dy;
return r2 >= 150 && r2 <= 400;
}
bool scatter(int x, int y, std::vector<std::pair<int, int>>& path)
{
return std::any_of(path.begin(), path.end(), [x,y](std::pair<int,int> a){
const int dxa = x - a.first;
const int dya = y - a.second;
const int r2a = dxa * dxa + dya * dya;
return r2a < 100;});
}
int main(int argc, char** argv)
{
const size_t expectedFileSize = IMAGE_DIM * IMAGE_DIM;
// Address assets relative to application location
std::string anchor = std::string(".") + PATH_SEP;
std::string pname = argv[0];
auto lastpos = pname.find_last_of("/\\");
if (lastpos != std::string::npos)
{
anchor = pname.substr(0, lastpos) + PATH_SEP;
}
auto elevation = loadFile(anchor + "assets" + PATH_SEP + "elevation.data", expectedFileSize);
auto overrides = loadFile(anchor + "assets" + PATH_SEP + "overrides.data", expectedFileSize);
Planner planner;
planner.run(overrides, elevation, {ROVER_X, ROVER_Y, BACHELOR_X, BACHELOR_Y, WEDDING_X, WEDDING_Y});
auto result = planner.get_result();
std::ofstream of("pic_dijkstra.bmp", std::ofstream::binary);
visualizer::writeBMP(
of,
&elevation[0],
IMAGE_DIM,
IMAGE_DIM,
[&] (size_t x, size_t y, uint8_t elevation) {
// Marks interesting positions on the map
if (donut(x, y, ROVER_X, ROVER_Y) ||
donut(x, y, BACHELOR_X, BACHELOR_Y) ||
donut(x, y, WEDDING_X, WEDDING_Y))
{
return uint8_t(visualizer::IPV_PATH);
}
if (scatter(x, y, result))
{
return uint8_t(visualizer::IPV_PATH);
}
// Signifies water
if ((overrides[y * IMAGE_DIM + x] & (OF_WATER_BASIN | OF_RIVER_MARSH)) ||
elevation == 0)
{
return uint8_t(visualizer::IPV_WATER);
}
// Signifies normal ground color
if (elevation < visualizer::IPV_ELEVATION_BEGIN)
{
elevation = visualizer::IPV_ELEVATION_BEGIN;
}
return elevation;
});
of.flush();
#if __APPLE__
auto res = system("open pic.bmp");
(void)res;
#endif
return 0;
}