-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlocalmap.cpp
164 lines (156 loc) · 6.18 KB
/
localmap.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
#include "localmap.h"
inline double euclid_dist(Cell a, Cell b) {
return sqrt(abs(a.x - b.x) * abs(a.x - b.x) + abs(a.y - b.y) * abs(a.y - b.y));
}
LocalMap::LocalMap() { Map(); }
LocalMap::~LocalMap() {}
bool LocalMap::lineOfSight(Cell a1, Cell a2, const Map &map, bool cutcorners)
{
int i1 = a1.y;
int j1 = a1.x;
int i2 = a2.y;
int j2 = a2.x;
int delta_i = std::abs(i1 - i2);
int delta_j = std::abs(j1 - j2);
int step_i = (i1 < i2 ? 1 : -1);
int step_j = (j1 < j2 ? 1 : -1);
int error = 0;
int i = i1 + step_i;
int j = j1 + step_j;
if(delta_i == 0) {
for(; j != j2; j += step_j)
if(map.CellIsObstacle(Cell(j, i)))
return false;
return true;
}
else if(delta_j == 0) {
for(; i != i2; i += step_i)
if(map.CellIsObstacle(Cell(j, i)))
return false;
return true;
}
if(cutcorners) {
if (delta_i > delta_j) {
for (; i != i2; i += step_i) {
if (map.CellIsObstacle(Cell(j, i)))
return false;
error += delta_j;
if ((error << 1) > delta_i) {
if(((error << 1) - delta_j) < delta_i && map.CellIsObstacle(Cell(j, i+step_i)))
return false;
else if(((error << 1) - delta_j) > delta_i && map.CellIsObstacle(Cell(j+step_j, i)))
return false;
j += step_j;
error -= delta_i;
}
}
}
else {
for (; j != j2; j += step_j) {
if (map.CellIsObstacle(Cell(j, i)))
return false;
error += delta_i;
if ((error << 1) > delta_j) {
if(((error << 1) - delta_i) < delta_j && map.CellIsObstacle(Cell(j+step_j, i)))
return false;
else if(((error << 1) - delta_i) > delta_j && map.CellIsObstacle(Cell(j, i+step_i)))
return false;
i += step_i;
error -= delta_j;
}
}
}
}
else {
int sep_value = delta_i*delta_i + delta_j*delta_j;
if(delta_i > delta_j) {
for(; i != i2; i += step_i) {
if(map.CellIsObstacle(Cell(j, i)))
return false;
if(map.CellIsObstacle(Cell(j + step_j, i)))
return false;
error += delta_j;
if(error >= delta_i) {
if(((error << 1) - delta_i - delta_j)*((error << 1) - delta_i - delta_j) < sep_value)
if(map.CellIsObstacle(Cell(j, i + step_i)))
return false;
if((3*delta_i - ((error << 1) - delta_j))*(3*delta_i - ((error << 1) - delta_j)) < sep_value)
if(map.CellIsObstacle(Cell(j + 2*step_j, i)))
return false;
j += step_j;
error -= delta_i;
}
}
if(map.CellIsObstacle(Cell(j, i)))
return false;
}
else {
for(; j != j2; j += step_j) {
if(map.CellIsObstacle(Cell(j, i)))
return false;
if(map.CellIsObstacle(Cell(j, i + step_i)))
return false;
error += delta_i;
if(error >= delta_j) {
if(((error << 1) - delta_i - delta_j)*((error << 1) - delta_i - delta_j) < (delta_i*delta_i + delta_j*delta_j))
if(map.CellIsObstacle(Cell(j + step_j, i)))
return false;
if((3*delta_j - ((error << 1) - delta_i))*(3*delta_j - ((error << 1) - delta_i)) < (delta_i*delta_i + delta_j*delta_j))
if(map.CellIsObstacle(Cell(j, i + 2*step_i)))
return false;
i += step_i;
error -= delta_j;
}
}
if(map.CellIsObstacle(Cell(j, i)))
return false;
}
}
return true;
}
bool LocalMap::GetLocalMap(const Map &_map, Cell nstart, double radius) {
height = _map.height;
width = _map.width;
start = _map.start;
goal = _map.goal;
CellSize = _map.CellSize;
BuildGrid();
for (int y = static_cast<int>(nstart.y - radius); y < static_cast<int>(nstart.y + radius); ++y) {
for (int x = static_cast<int>(nstart.x - radius); x < static_cast<int>(nstart.x + radius); ++x) {
if (CellOnGrid(Cell(x, y)) && euclid_dist(start, Cell(x,y)) < radius) {
//if (lineOfSight(Cell(x, y), nstart, _map, _map.algorithm_info.cutcorners)) Grid[y][x] = _map[y][x];
//else Grid[y][x] = CN_GC_NOOBS;
Grid[y][x] = _map[y][x];
}
}
}
return true;
}
Changes LocalMap::UpdateMap(const Map& _map, Cell new_start, double radius) {
Changes change;
for (int y = static_cast<int>(new_start.y - radius); y < static_cast<int>(new_start.y + radius); ++y) {
for (int x = static_cast<int>(new_start.x - radius); x < static_cast<int>(new_start.x + radius); ++x) {
if (CellOnGrid(Cell(x, y)) && euclid_dist(new_start, Cell(x,y)) < radius) {
/*if (lineOfSight(Cell(x, y), new_start, _map, _map.algorithm_info.cutcorners)) {
if (Grid[y][x] != _map[y][x]) {
if (_map[y][x] == CN_GC_NOOBS) change.cleared.push_back(Cell(x, y));
else change.occupied.push_back(Cell(x, y));
Grid[y][x] = _map[y][x];
}
}*/
if (Grid[y][x] != _map[y][x]) {
if (_map[y][x] == CN_GC_NOOBS) change.cleared.push_back(Cell(x, y));
else change.occupied.push_back(Cell(x, y));
Grid[y][x] = _map[y][x];
}
}
}
}
/*for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
std::cout << Grid[i][j] << " ";
}
std::cout << std::endl;
}*/
return change;
}