-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder2D.cpp
More file actions
268 lines (208 loc) · 5.26 KB
/
Copy pathPathFinder2D.cpp
File metadata and controls
268 lines (208 loc) · 5.26 KB
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
#include "PathFinder2D.h"
void PathFinder2D::EraseLengthArray() {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
fLengthToPoint[j][i] = cols * rows + 1;
}
PathFinder2D::PathFinder2D(int cols, int rows, int *firstObstacleMapCell) {
// set nessesary parameters
this->cols = cols;
this->rows = rows;
iObstacleMap = firstObstacleMapCell;
// create map of length
fLengthToPoint = new float*[rows];
for (int i = 0; i < rows; i++) {
fLengthToPoint[i] = new float[cols];
}
for (int y = 0; y < rows; y++)
for (int x = 0; x < cols; x++)
fLengthToPoint[x][y] = rows * cols + 1;
bWasHere = new bool* [rows];
for (int i = 0; i < rows; i++) {
bWasHere[i] = new bool[cols];
}
for (int y = 0; y < rows; y++)
for (int x = 0; x < cols; x++)
bWasHere[x][y] = false;
}
PathFinder2D::~PathFinder2D() {
for (int i = 0; i < rows; i++) {
delete[]fLengthToPoint[i];
}
delete[]fLengthToPoint;
for (int i = 0; i < rows; i++) {
delete[]bWasHere[i];
}
delete[]bWasHere;
}
// can we use neigbors without errors?
inline bool PathFinder2D::PointInMap(int x, int y, int dx, int dy) {
if (x + dx >= cols) {
return false;
}
if (x + dx < 0) {
return false;
}
if (y + dy >= rows) {
return false;
}
if (y + dy < 0) {
return false;
}
if (GetObstacleMap((x + dx), (y + dy)) == -1) {
return false;
}
if (dx == 0 && dy == 0) {
return false;
}
return true;
}
template<class Q>
void PathFinder2D::ClearQueue(Q& q) {
q = Q();
}
std::vector<PathFinder2D::Point>PathFinder2D::FindPath(int fromX, int fromY,
int toX, int toY) {
// value is valid
if (fromX >= cols)
fromX = cols - 1;
if (fromY >= rows)
fromX = rows - 1;
if (fromX < 0)
fromX = 0;
if (fromY < 0)
fromY = 0;
if (toX >= cols)
toX = cols - 1;
if (toY >= rows)
toX = rows - 1;
if (toX < 0)
toX = 0;
if (toY < 0)
toY = 0;
iEndX = toX;
iEndY = toY;
iStartX = fromX;
iStartY = fromY;
EraseLengthArray();
ClearQueue(qPointsToScan);
qPointsToScan.push(Point(fromX, fromY, this));
fLengthToPoint[fromX][fromY] = 0;
while (!qPointsToScan.empty()) {
Point p = qPointsToScan.top();
if (p.x == toX && p.y == toY) {
return CreatePath();
}
qPointsToScan.pop();
float fCurLength = fLengthToPoint[p.x][p.y];
bWasHere[p.x][p.y] = true;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++) {
if (PointInMap(p.x, p.y, i, j)) {
float fCost = 1.0f;
// diagonal movement cost 1.5
if (abs(i) == abs(j))
fCost = 1.5f;
if (bWasHere[p.x + i][p.y + j])
// if new movement not better than older
if (fCurLength + fCost >=
fLengthToPoint[p.x + i][p.y + j]) {
continue;
}
fLengthToPoint[p.x + i][p.y + j] = fCurLength + fCost;
bWasHere[p.x + i][p.y + j] = true;
qPointsToScan.push(Point(p.x + i, p.y + j, this));
}
}
}
std::vector<Point>s;
return s;
}
float** PathFinder2D::GetLenghtToPointMap() {
return fLengthToPoint;
}
PathFinder2D::Point::Point(PathFinder2D *ref) {
x = 0;
y = 0;
this->ref = ref;
}
PathFinder2D::Point::Point() {
x = 0;
y = 0;
this->ref = NULL;
}
PathFinder2D::Point::Point(int x, int y, PathFinder2D *ref) {
this->x = x;
this->y = y;
this->ref = ref;
}
bool PathFinder2D::Point:: operator > (const Point& cmp) const {
// check distance to destination
int a1 = (x - ref->GetEndX()) * (x - ref->GetEndX());
int b1 = (y - ref->GetEndY()) * (y - ref->GetEndY());
float d1 = std::sqrt(a1 + b1) + ref->GetLengthToPoint(x, y);
int a2 = (cmp.x - ref->GetEndX()) * (cmp.x - ref->GetEndX());
int b2 = (cmp.y - ref->GetEndY()) * (cmp.y - ref->GetEndY());
float d2 = std::sqrt(a2 + b2) + ref->GetLengthToPoint(cmp.x, cmp.y);
if (d1 > d2)
return false;
return true;
}
bool PathFinder2D::Point:: operator < (const Point& cmp) const {
// check distance to destination
int a1 = (x - ref->GetEndX()) * (x - ref->GetEndX());
int b1 = (y - ref->GetEndY()) * (y - ref->GetEndY());
float d1 = std::sqrt(a1 + b1) + ref->GetLengthToPoint(x, y);
int a2 = (cmp.x - ref->GetEndX()) * (cmp.x - ref->GetEndX());
int b2 = (cmp.y - ref->GetEndY()) * (cmp.y - ref->GetEndY());
float d2 = std::sqrt(a2 + b2) + ref->GetLengthToPoint(cmp.x, cmp.y);
if (d1 > d2)
return true;
return false;
}
int PathFinder2D::GetEndX() {
return iEndX;
}
int PathFinder2D::GetEndY() {
return iEndY;
}
void PathFinder2D::SetObstacleMap(int x, int y, int value) {
iObstacleMap[y * cols + x] = value;
}
int PathFinder2D::GetObstacleMap(int x, int y) {
return iObstacleMap[y * cols + x];
}
float PathFinder2D::GetLengthToPoint(int x, int y) {
return fLengthToPoint[x][y];
}
std::vector<PathFinder2D::Point>PathFinder2D::CreatePath() {
// finding a path
PathFinder2D::Point p;
p.x = iEndX;
p.y = iEndY;
PathFinder2D::Point tmp;
std::vector<PathFinder2D::Point>path;
path.push_back(p);
int n = 0;
while (n < cols * rows) {
float min = fLengthToPoint[p.x][p.y];
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++) {
if (PointInMap(p.x, p.y, i, j)) {
if (min > fLengthToPoint[p.x + i][p.y + j]) {
tmp.x = p.x + i;
tmp.y = p.y + j;
min = fLengthToPoint[p.x + i][p.y + j];
}
}
}
if(p.x==tmp.x && p.y==tmp.y) break;
p = tmp;
path.push_back(p);
if (p.x == iStartX && p.y == iStartY)
return path;
n++;
}
path.clear();
return path;
}