-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinder.cpp
275 lines (228 loc) · 6.84 KB
/
pathfinder.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
#include "pathfinder.h"
#include <iostream>
#include <vector>
#include <utility>
#include <QDebug>
#include <queue>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <limits>
#include "environment.h"
bool operator == (GridLocation a, GridLocation b) {
return a.x == b.x && a.y == b.y;
}
bool operator != (GridLocation a, GridLocation b) {
return !(a == b);
}
bool operator <(GridLocation a, GridLocation b) {
return a.priority<b.priority;
}
bool operator >(GridLocation a, GridLocation b) {
return a.priority>b.priority;
}
bool Pathfinder::passable(GridLocation& grid)
{
return !std::isinf(grid.value);
}
bool Pathfinder::isValidPosition(GridLocation grid)
{
return !getNeighbors(grid).empty();
}
std::vector<GridLocation> Pathfinder::getPath() const
{
return path;
}
float Pathfinder::getWeight() const
{
return weight;
}
void Pathfinder::setWeight(float value)
{
weight = value;
}
std::vector<GridLocation> Pathfinder::getMap() const
{
return map;
}
GridLocation Pathfinder::getMystart() const
{
return mystart;
}
void Pathfinder::setMystart(const GridLocation &value)
{
mystart = value;
}
GridLocation Pathfinder::getGoal() const
{
return goal;
}
//set the
void Pathfinder:: setStart(int x,int y)
{
mystart=map.at(x+y*cols);
}
//set the value of tile to infinity
void Pathfinder::setInf(unsigned long index)
{
map.at(index).value=std::numeric_limits<float>::infinity();
}
void Pathfinder::setGoal(int x, int y)
{
goal = map.at(x+y*cols);
}
void Pathfinder::setGoal(const GridLocation &value)
{
goal = value;
}
void Pathfinder::setGcost(float value)
{
Gcost = value;
}
void Pathfinder::setValue(unsigned long index, float val)
{
map.at(index).value=val;
}
//initialise the map
Pathfinder::Pathfinder(std::vector<std::unique_ptr<Environment>>& mmap,int mwidth, int mlength):cols{mwidth},rows{mlength}
{
// initialise the map and store in a vector
for(int i=0;i<mlength;i++)
{
for(int j=0;j<mwidth;j++)
{
int xPos=mmap.at(i*mwidth+j)->getXPos();
int yPos=mmap.at(i*mwidth+j)->getYPos();
float value=mmap.at(i*mwidth+j)->getValue();
//std::vector<unsigned long>vector(4);
GridLocation grid{0,0,xPos,yPos,value,0,0};
map.push_back(grid);
}
}
}
//initialise the map
Pathfinder::Pathfinder(std::vector<std::unique_ptr<Tile>>& mmap,int mwidth,int mlength):cols{mwidth},rows{mlength}
{
for(int i=0;i<mlength;i++)
{
for(int j=0;j<mwidth;j++)
{
int xPos=mmap.at(i*mwidth+j)->getXPos();
int yPos=mmap.at(i*mwidth+j)->getYPos();
float value=mmap.at(i*mwidth+j)->getValue();
//std::vector<unsigned long>vector(4);
GridLocation grid{0,0,xPos,yPos,value,0,0};
map.push_back(grid);
}
}
}
Pathfinder::~Pathfinder()
{}
// use a_star algorithm to search path
//find valid path --> return true
//no valid path--> return false
bool Pathfinder::find()
{
path.clear();
std::priority_queue<GridLocation,std::vector<GridLocation>,std::greater<GridLocation>> frontier;
//check if it is a valid destination
if(!isValidPosition(goal)){
qDebug()<<"the goal is not reachable, exiting! Please choose a valid position";
return false;
}
//check if it is a valid start
if(!isValidPosition(mystart)){
qDebug()<< "Failed to proceed: the starting point is surrounded by walls, exiting! Please choose a valid position";
return false;
}
frontier.push(mystart);
// start the main loop for the search
while (!frontier.empty()) {
GridLocation current=frontier.top();
frontier.pop();
unsigned long index_current=(unsigned long)current.x+current.y*cols;
if(current==goal) //check the exit condition
{
//qDebug()<<"find goal" ;
//qDebug()<<"x= "<<current.x<< " y= "<<current.y ;
//qDebug()<<"x: "<<map[current.come_from].x<<" y: "<< map[current.come_from].y ;
return true;
}
std::vector<unsigned long> neighbors=getNeighbors(current);
//iterate the all the elements in the neighbors
for(auto index:neighbors)
{
GridLocation * next=& map[index];
float new_cost=current.cost+current.value-next->value+Gcost;
float heuristic=abs(goal.x-next->x)+abs(goal.y-next->y);
float priority =new_cost+weight*heuristic;
if(next->visited==0||(next->visited==1&&new_cost<next->cost))
{
next->cost=new_cost;//update the minimum total cost from start to this gridlocation
next->come_from=index_current; // update the previous gridlocation of this point
next->priority=priority;
frontier.push(map[index]);
if(next->visited==0)next->visited=1;
}
}
}
return false;
}
bool Pathfinder::find(std::unique_ptr<Environment>& start, std::unique_ptr<Environment>& end)
{
mystart=GridLocation{0,0,start->getXPos(),start->getYPos(),start->getValue(),0,0};
goal=GridLocation{0,0,end->getXPos(),end->getYPos(),end->getValue(),0,0};
return find();
}
//generate the path for the search
//clear the info of previous search
void Pathfinder::generatePath()
{
GridLocation current=map[goal.x+goal.y*cols];
path.push_back(map[goal.x+goal.y*cols]);
while(current!=mystart)
{
path.push_back(map[current.come_from]);
//qDebug()<<"x= "<<current.x<< " y= "<< current.y<<" came from "<<current.come_from ;
current=map[current.come_from];
}
std::reverse(path.begin(),path.end());
//clear all info about previous search
for (auto & gl:map)
{
gl.come_from=0;
gl.cost=0;
gl.visited=0;
}
}
std::vector<unsigned long> Pathfinder:: getNeighbors(GridLocation current)
{
std::vector<unsigned long> neighbors;
int xPos=current.x;
int yPos=current.y;
//check if current GridLocation is in the first column
if(xPos>=1)
{
if(passable(map[yPos*cols+xPos-1]))
neighbors.push_back((unsigned long)(yPos*cols+xPos-1));
}
//check if current GridLocation is in the last column
if(xPos<cols-1)
{
if(passable(map[yPos*cols+xPos+1]))
neighbors.push_back((unsigned long)(yPos*cols+xPos+1));
}
//check if current GridLocation is in the first row
if(yPos>=1)
{
if(passable(map[yPos*cols+xPos-cols]))
neighbors.push_back((unsigned long)(yPos-1)*cols+xPos);
}
//check if current GridLocation is in the last row
if(yPos<rows-1)
{
if(passable(map[yPos*cols+xPos+cols]))
neighbors.push_back((unsigned long)((yPos+1)*cols+xPos));
}
return neighbors;
}