FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
astar/astarvisualization.cpp at master · anthoturc/astar · GitHub
anthoturc
/
astar
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
astar
/
astarvisualization.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
293 lines (241 loc) · 7.88 KB
Breadcrumbs
astar
/
astarvisualization.cpp
Copy path
File metadata and controls
293 lines (241 loc) · 7.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
astarvisualization.hpp
"
#
include
<
queue
>
#
include
<
unistd.h
>
#
include
<
vector
>
#
include
<
algorithm
>
astarvisualization::astarvisualization
(sf::RenderWindow& w)
: visualization(w), source_(
nullptr
), dest_(
nullptr
), runningAStar_(
false
), dragging_(
false
) {}
astarvisualization::~astarvisualization
() {}
int
astarvisualization::manhattan_heuristic
(astarnode* n1, astarnode* n2)
{
return
COST_MULTIPLIER
*(
std::abs
(n1->
getCol
() - n2->
getCol
())
+
std::abs
(n1->
getRow
() - n2->
getRow
())
);
}
void
astarvisualization::search
()
{
/*
8 directions of each nodes neighbors
*/
#
if
DIAGONALS_ALLOWED
std::vector<std::vector<
int
>> dirs {{-
1
,
0
}, {-
1
, -
1
}, {-
1
,
1
}, {
0
,
1
}, {
0
, -
1
}, {
1
, -
1
}, {
1
,
0
}, {
1
,
1
}};
#
else
std::vector<std::vector<
int
>> dirs {{-
1
,
0
}, {
0
,
1
}, {
0
, -
1
}, {
1
,
0
}};
#
endif
//
DIAGONALS_ALLOWED
std::priority_queue<astarnode *, std::vector<astarnode *>, comp> open;
std::vector<astarnode *> inopen;
source_->
setGCost
(
0
);
source_->
setHCost
(
INT_MAX
);
dest_->
setGCost
(
INT_MAX
);
dest_->
setHCost
(
0
);
open.
push
(source_);
inopen.
push_back
(source_);
while
(!open.
empty
()) {
//
note this will be infinite if the destination is unreachable
astarnode * curr = open.
top
();
open.
pop
();
if
(curr != source_ && curr != dest_)
curr->
r_
->
setFillColor
(
VISITED_COLOR
);
#
if
FAST_RENDER
#
else
w_.
clear
();
drawGrid
();
w_.
display
();
#
endif
//
FAST_RENDER
if
(curr == dest_)
break
;
int
row = curr->
getRow
();
int
col = curr->
getCol
();
int
currGCost = curr->
getGCost
();
for
(
auto
d : dirs) {
int
neighborRow = row + d[
0
];
int
neighborCol = col + d[
1
];
if
(
validNeighbor
(neighborRow, neighborCol)) {
astarnode * neighbor = (astarnode *)grid_[neighborRow][neighborCol];
if
(neighbor->
isObstacle
())
continue
;
//
calculate the g-cost for the neighboring node
int
gcost =
CARDINAL_COST
;
#
if
DIAGONALS_ALLOWED
bool
isDiagonal = (d[
0
] ==
1
&& d[
1
] ==
1
) || (d[
0
] ==
1
&& d[
1
] == -
1
)
|| (d[
0
] == -
1
&& d[
1
] == -
1
) || (d[
0
] == -
1
&& d[
1
] ==
1
);
if
(isDiagonal) {
gcost =
DIAGNOAL_COST
;
}
#
endif
//
DIAGNOALS_ALLOWED
gcost += currGCost;
auto
itOpen =
std::find
(inopen.
begin
(), inopen.
end
(), neighbor);
if
(itOpen == inopen.
end
() || gcost < neighbor->
getGCost
()) {
int
hcost =
manhattan_heuristic
(neighbor, dest_);
//
set costs
neighbor->
setPredecessor
(curr);
neighbor->
setGCost
(gcost);
neighbor->
setHCost
(hcost);
if
(neighbor != dest_)
neighbor->
r_
->
setFillColor
(
NEIGHBOR_FOUND_COLOR
);
//
add to open
open.
push
(neighbor);
inopen.
push_back
(neighbor);
}
}
}
#
if
FAST_RENDER
#
else
w_.
clear
();
drawGrid
();
w_.
display
();
usleep
(
SLEEP_TIME
/
2
);
#
endif
//
FAST_RENDER
}
w_.
clear
();
runningAStar_ =
false
;
if
(!dest_->
getPredecessor
()) {
dest_->
r_
->
setFillColor
(
PATH_NOT_FOUND_COLOR
);
drawGrid
();
w_.
display
();
return
;
}
astarnode * tmp = dest_;
while
(tmp) {
if
(tmp != dest_ && tmp != source_)
tmp->
r_
->
setFillColor
(
PATH_COLOR
);
tmp = tmp->
getPredecessor
();
drawGrid
();
usleep
(
SLEEP_TIME
);
//
sleep for 1 sec
w_.
display
();
}
}
void
astarvisualization::initGrid
()
{
int
numRows = H/
BOX_H
;
int
numCols = W/
BOX_W
;
grid_.
resize
(numRows, std::vector<searchnode *>(numCols));
for
(
int
row =
0
; row < numRows; ++row) {
for
(
int
col =
0
; col < numCols; ++col) {
int
xpos = row *
BOX_SZ
;
int
ypos = col *
BOX_SZ
;
sf::RectangleShape * r =
new
sf::RectangleShape
(
sf::Vector2f
(
BOX_W
,
BOX_H
));
r->
setOutlineColor
(sf::Color::Black);
r->
setOutlineThickness
(
BORDER_THICKNESS
);
r->
setFillColor
(
DEFAULT_COLOR
);
r->
setPosition
((
float
)xpos, (
float
)ypos);
grid_[row][col] =
new
astarnode
();
grid_[row][col]->
r_
= r;
grid_[row][col]->
setRow
(row);
grid_[row][col]->
setCol
(col);
}
}
}
void
astarvisualization::resetGrid
()
{
source_ = dest_ =
nullptr
;
dragging_ = runningAStar_ =
false
;
for
(
auto
row : grid_) {
for
(
auto
node : row) {
astarnode * tmp = (astarnode *)node;
tmp->
setGCost
(
INT_MAX
);
tmp->
setHCost
(
INT_MAX
);
tmp->
setObstacle
(
false
);
tmp->
setPredecessor
(
nullptr
);
tmp->
r_
->
setFillColor
(
DEFAULT_COLOR
);
}
}
}
void
astarvisualization::run
()
{
initGrid
();
while
(w_.
isOpen
()) {
handleWindowEvents
();
w_.
clear
();
drawGrid
();
if
(!runningAStar_) {
w_.
display
();
}
else
{
search
();
}
}
}
void
astarvisualization::handleWindowEvents
()
{
sf::Event e;
while
(w_.
pollEvent
(e)) {
switch
(e.
type
) {
case
sf::Event::Closed:
w_.
close
();
break
;
case
sf::Event::MouseButtonPressed:
if
(runningAStar_)
break
;
if
(!source_ || !dest_)
handleMouseDownEvent
(e);
else
dragging_ =
true
;
break
;
case
sf::Event::MouseMoved:
if
(runningAStar_)
break
;
if
(dragging_ && source_ && dest_)
//
source and dest must be set before obstacles
setObstacle
(e);
break
;
case
sf::Event::MouseButtonReleased:
if
(runningAStar_)
break
;
dragging_ =
false
;
break
;
case
sf::Event::KeyPressed:
handleKeyPress
(e);
break
;
default
:
//
TODO: add other events here
break
;
}
}
}
void
astarvisualization::handleKeyPress
(sf::Event& e)
{
if
(e.
key
.
code
==
RUN_KEY
) {
if
(source_ && dest_) {
runningAStar_ =
true
;
}
}
else
if
(e.
key
.
code
==
RESET_KEY
) {
resetGrid
();
}
}
void
astarvisualization::handleMouseDownEvent
(sf::Event& e)
{
int
xpos = e.
mouseButton
.
x
;
int
ypos = e.
mouseButton
.
y
;
if
(xpos <
0
|| xpos >= W)
return
;
if
(ypos <
0
|| ypos >= H)
return
;
if
(!dragging_) {
if
(!source_ && e.
mouseButton
.
button
== sf::Mouse::Left) {
//
source node set
source_ =
setNodeColor
(xpos, ypos,
SRC_COLOR
);
}
else
if
(!dest_ && e.
mouseButton
.
button
== sf::Mouse::Right) {
//
destination node set
dest_ =
setNodeColor
(xpos, ypos,
DEST_COLOR
);
}
}
}
astarnode *
astarvisualization::setNodeColor
(
int
xpos,
int
ypos,
const
sf::Color& c)
{
int
row = xpos/
BOX_SZ
;
int
col = ypos/
BOX_SZ
;
grid_[row][col]->
r_
->
setFillColor
(c);
return
(astarnode *)grid_[row][col];
}
void
astarvisualization::setObstacle
(sf::Event& e)
{
int
xpos = e.
mouseMove
.
x
;
int
ypos = e.
mouseMove
.
y
;
if
(xpos >= W || xpos <
0
)
return
;
if
(ypos >= H || ypos <
0
)
return
;
astarnode * obstacle =
setNodeColor
(xpos, ypos, sf::Color::Black);
if
(obstacle == source_) {
setNodeColor
(xpos, ypos,
SRC_COLOR
);
return
;
}
else
if
(obstacle == dest_) {
setNodeColor
(xpos, ypos,
DEST_COLOR
);
}
obstacle->
setObstacle
(
true
);
}
Back
|
FazBrowse Home
|
New Git URL