-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.cpp
192 lines (176 loc) · 3.93 KB
/
AStar.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
#include "AStar.h"
AStar::AStar()
{
area.reserve(600);
markedPoints.reserve(4800);
}
AStar::~AStar()
{
map = NULL;
}
void AStar::setMapAndPoint(vector<vector<vector<int>>>* _map, const Point3 & _p1, const Point3 & _p2)
{
map = _map;
from = _p1;
to = _p2;
}
bool AStar::getPath(vector<Point3>& _path, int &_pathLength)
{
lastPathLength = _path.size();
area.resize(1);
markedPoints.resize(0);
area[0].setParameters(from, &to, map);
markedPoints.push_back(from);
SearchArea _tmpArea;
unsigned int _index = 0;
while (area[_index].nextPoint != to)
{
// 如果是死路,恢复path,返回false
if (area[_index].isDeadPath)
{
clearPathMarkInMap(_index + 1);
return false;
}
markedPoints.push_back(area[_index].nextPoint);
_tmpArea.setParameters(area[_index].nextPoint, &to, map);
area.push_back(_tmpArea);
_index++;
}
markedPoints.push_back(area[_index].nextPoint);
_path.resize(lastPathLength + _index + 1);
for (unsigned int i = lastPathLength; i < _path.size(); i++)
{
_path[i] = area[i - lastPathLength].nextPoint;
}
_pathLength = _path.size();
clearPathMarkInMap(_index + 2);
return true;
}
int AStar::getDistance(const Point3 & _p1, const Point3 & _p2)
{
int _distance;
_distance = int(pow(_p1.x - _p2.x, 2) + pow(_p1.y - _p2.y, 2));
return _distance;
}
void AStar::clearPathMarkInMap(const int &_length)
{
int _mark = -1;
for (int i = 0; i < _length; i++)
{
setMarkInMap(*map, markedPoints[i], _mark);
}
}
SearchPoint::SearchPoint(const Point3 & _p, Point3 * _to)
{
p = _p;
to = _to;
distance = getDistance(p, *to);
}
void SearchPoint::setParameters(const Point3 & _p, Point3 * _to)
{
p = _p;
to = _to;
distance = getDistance(p, *to);
}
int SearchPoint::getDistance(const Point3 & _from, const Point3 & _to)
{
int _distance;
_distance = int(pow(_from.x - _to.x, 2) + pow(_from.y - _to.y, 2));
return _distance;
}
SearchArea::SearchArea(const Point3 & _parentPoint, Point3 * _to, vector<vector<vector<int>>>* _map)
{
parentPoint = _parentPoint;
to = _to;
map = _map;
area.resize(0);
mapX = _map->size();
mapY = (*_map)[0].size();
isDeadPath = false;
area.reserve(8);
setMarkInMap(*map, parentPoint, CLOSE_FLAG);
getAreaPoints();
getNextPoint();
}
void SearchArea::setParameters(const Point3 & _parentPoint, Point3 * _to, vector<vector<vector<int>>>* _map)
{
parentPoint = _parentPoint;
to = _to;
map = _map;
area.resize(0);
mapX = _map->size();
mapY = (*_map)[0].size();
isDeadPath = false;
area.reserve(8);
setMarkInMap(*map, parentPoint, CLOSE_FLAG);
getAreaPoints();
getNextPoint();
}
void SearchArea::getAreaPoints()
{
Point3 _tmpPoint = parentPoint;
SearchPoint _tmpSearchPoint;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
_tmpPoint.x += i;
_tmpPoint.y += j;
if (isValidPoint(_tmpPoint))
{
if (_tmpPoint == parentPoint || getMarkInMap(*map, _tmpPoint) == CLOSE_FLAG)
{
_tmpPoint = parentPoint;
continue;
}
else
{
_tmpSearchPoint.setParameters(_tmpPoint, to);
area.push_back(_tmpSearchPoint);
}
}
_tmpPoint = parentPoint;
}
}
}
void SearchArea::getNextPoint()
{
int _minDistance = 999999;
unsigned int _minDistanceIndex;
bool _isGetPoint = false;
for (unsigned int i = 0; i < area.size(); i++)
{
if (area[i].distance < _minDistance)
{
_minDistance = area[i].distance;
_minDistanceIndex = i;
_isGetPoint = true;
}
}
if (_isGetPoint)
{
nextPoint = area[_minDistanceIndex].p;
setMarkInMap(*map, nextPoint, CLOSE_FLAG);
}
else
{
isDeadPath = true;
}
}
bool SearchArea::isValidPoint(Point3 & _p)
{
if (_p.x > mapX - 1 || _p.y > mapY - 1 || _p.x < 0 || _p.y < 0)
return false;
else if (getMarkInMap(*map, _p) <= -4)
return false;
else
return true;
}
void setMarkInMap(vector<vector<vector<int>>>& _map, const Point3 & _p, const int & _mark)
{
_map[_p.x][_p.y][_p.z] = _mark;
}
int getMarkInMap(vector<vector<vector<int>>>& _map, const Point3 & _p)
{
return _map[_p.x][_p.y][_p.z];
}