매우 심플하게 만든 A* 알고리즘.
이전에 C#으로 만든걸 C++ 로 옴겨서 테스트용으로 작성했다.
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 | #pragma once
#include "PathNode.h"
#include <vector>
#include <stack>
using namespace std;
#define MAP_SIZE_X 10
#define MAP_SIZE_Y 10
class CustomAStar
{
public:
CustomAStar();
~CustomAStar();
void SetGoalNode(int x, int y);
//길찾기를 시작합니다. ( 시작 노드 위치를 인자로 설정.)
stack<PathNode*>* PathFinding(int x, int y);
// 장애물을 지정합니다.
void SetObstacle(int x, int y);
private:
PathNode* pathMap[MAP_SIZE_X][MAP_SIZE_Y];
vector<PathNode*> openList;
vector<PathNode*> closedList;
vector<Vector2> fourDir;
stack<PathNode*> navigatePath;
PathNode* curNode;
PathNode* goalNode;
void InitMap();
void InitPathFinding();
void CurNodeToClosedList();
bool IsInClosedList(int searchPosX, int searchPosY);
bool IsInOpenList(int searchPosX, int searchPosY);
bool IsGoalInOpenList();
void SetOpenList();
void SearchAdjacentNodes(PathNode* selectNode);
void ExtractNavigatePath();
void SetStartNode(int x, int y);
PathNode* SelectLowCostPath();
};
|
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 "CustomAStar.h"
CustomAStar::CustomAStar()
{
fourDir.push_back(Vector2(0, 1));
fourDir.push_back(Vector2(0, -1));
fourDir.push_back(Vector2(-1, 0));
fourDir.push_back(Vector2(1, 0));
InitMap();
}
CustomAStar::~CustomAStar()
{
}
void CustomAStar::SetStartNode(int x, int y)
{
curNode = pathMap[x][y];
curNode->SetParentNode(nullptr);
curNode->CalcHValue(goalNode);
openList.push_back(curNode);
}
void CustomAStar::SetGoalNode(int x, int y)
{
goalNode = pathMap[x][y];
goalNode->isGoalNode = true;
}
stack<PathNode*>* CustomAStar::PathFinding(int x, int y)
{
InitPathFinding();
SetStartNode(x, y);
while ((openList.size() != 0) && (!IsGoalInOpenList()))
{
SetOpenList();
PathNode* selectNode = SelectLowCostPath();
if (selectNode != nullptr) SearchAdjacentNodes(selectNode);
}
ExtractNavigatePath();
return &navigatePath;
}
void CustomAStar::SetObstacle(int x, int y)
{
closedList.push_back(pathMap[x][y]);
}
// pathMap을 초기화 합니다.
void CustomAStar::InitMap()
{
for(int x = 0; x < MAP_SIZE_X; x++)
for (int y = 0; y < MAP_SIZE_Y; y++) {
PathNode* node = new PathNode();
node->SetPosition(x, y);
pathMap[x][y] = node;
pathMap[x][y]->isGoalNode = false;
}
}
// 길찾기 전에 호출되어 초기화를 진행합니다.
void CustomAStar::InitPathFinding()
{
openList.clear();
closedList.clear();
}
void CustomAStar::CurNodeToClosedList()
{
closedList.push_back(curNode);
vector<PathNode*>::iterator it = openList.begin();
while (it != openList.end()) {
if ((*it)->GetPosition() == curNode->GetPosition()) {
openList.erase(it);
break;
}
it++;
}
}
bool CustomAStar::IsInClosedList(int searchPosX, int searchPosY)
{
bool isFind = false;
for each (auto node in closedList){
if ((node->GetPosition().x == searchPosX) &&
(node->GetPosition().y == searchPosY))
isFind = true;
}
return isFind;
}
bool CustomAStar::IsInOpenList(int searchPosX, int searchPosY)
{
bool isFind = false;
for each (auto node in openList){
if ((node->GetPosition().x == searchPosX) &&
(node->GetPosition().y == searchPosY))
isFind = true;
}
return isFind;
}
bool CustomAStar::IsGoalInOpenList()
{
bool isFind = false;
for each (auto node in openList) {
if (node->isGoalNode)
isFind = true;
}
return isFind;
}
void CustomAStar::SetOpenList()
{
for each(Vector2 pos in fourDir){
int searchPosX = curNode->GetPosition().x + pos.x;
int searchPosY = curNode->GetPosition().y + pos.y;
if ((searchPosX >= 0 && searchPosX < MAP_SIZE_X) && (searchPosY >= 0 && searchPosY < MAP_SIZE_Y)){
if ((!IsInClosedList(searchPosX, searchPosY)) && (!IsInOpenList(searchPosX, searchPosY))){
pathMap[searchPosX][searchPosY]->SetParentNode(curNode);
pathMap[searchPosX][searchPosY]->CalcHValue(goalNode);
pathMap[searchPosX][searchPosY]->CalcGValue();
openList.push_back(pathMap[searchPosX][searchPosY]);
}
}
}
CurNodeToClosedList();
}
void CustomAStar::SearchAdjacentNodes(PathNode * selectNode)
{
for each(Vector2 pos in fourDir){
int searchPosX = selectNode->GetPosition().x;
int searchPosY = selectNode->GetPosition().y;
if ((searchPosX >= 0 && searchPosX < MAP_SIZE_X) && (searchPosY >= 0 && searchPosY < MAP_SIZE_Y)){
if (!IsInClosedList(searchPosX, searchPosY)){
if (!IsInOpenList(searchPosX, searchPosY)){
openList.push_back(pathMap[searchPosX][searchPosY]);
pathMap[searchPosX][searchPosY]->SetParentNode(curNode);
pathMap[searchPosX][searchPosY]->CalcHValue(goalNode);
pathMap[searchPosX][searchPosY]->CalcGValue();
}
else{
if ((pathMap[searchPosX][searchPosY]->GetGValue() < selectNode->GetGValue())){
selectNode->SetParentNode(pathMap[searchPosX][searchPosY]);
selectNode->CalcHValue(goalNode);
selectNode->CalcGValue();
}
}
}
}
}
}
void CustomAStar::ExtractNavigatePath()
{
PathNode* path = goalNode;
while (path->GetParentNode() != nullptr){
navigatePath.push(path);
path = path->GetParentNode();
}
}
PathNode* CustomAStar::SelectLowCostPath()
{
int minCost = 0;
PathNode* lowCostPath = nullptr;
if (openList.empty()) return nullptr;
minCost = openList[0]->GetGValue() + openList[0]->GetHValue();
lowCostPath = openList[0];
for (int i = 1; i < openList.size(); i++){
int cost = openList[i]->GetGValue() + openList[i]->GetHValue();
if (minCost > cost){
minCost = cost;
lowCostPath = openList[i];
}
}
vector<PathNode*>::iterator it = openList.begin();
while (it != openList.end()) {
if ((*it)->GetPosition() == lowCostPath->GetPosition()) {
openList.erase(it);
break;
}
it++;
}
closedList.push_back(lowCostPath);
curNode = lowCostPath;
return lowCostPath;
}
|
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 | #pragma once
#include <math.h>
struct Vector2
{
public:
int x;
int y;
Vector2(int _x, int _y) {
x = _x;
y = _y;
}
Vector2() {
// 디폴트.
}
bool operator ==(const Vector2& vec) const {
if ((x == vec.x) && (y == vec.y)) return true;
else return false;
}
};
class PathNode
{
public:
PathNode();
~PathNode();
void CalcGValue();
void CalcHValue(PathNode* goal);
Vector2 GetPosition();
void SetPosition(int x, int y);
void SetPosition(Vector2 pos);
PathNode* GetParentNode();
int GetGValue();
int GetHValue();
void SetParentNode(PathNode* node);
bool isGoalNode;
private:
PathNode* parentNode = nullptr;
int gValue = 0;
int hValue = 0;
Vector2 position;
};
|
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 | #include "PathNode.h"
PathNode::PathNode()
{
}
PathNode::~PathNode()
{
}
void PathNode::CalcGValue()
{
if (parentNode == nullptr) {
gValue = 0;
return;
}
int diffX = position.x - parentNode->GetPosition().x;
int diffY = position.y - parentNode->GetPosition().y;
if (((diffX == -1) && (diffY == 1))
|| ((diffX == 1) && (diffY == 1))
|| ((diffX == -1) && (diffY == -1))
|| ((diffX == 1) && (diffY == -1))){
gValue = (abs(diffX) + abs(diffY)) * 14 + parentNode->GetGValue();
}
else{
gValue = (abs(diffX) + abs(diffY)) * 10 + parentNode->GetGValue();
}
}
void PathNode::CalcHValue(PathNode* goal)
{
int diffX = goal->GetPosition().x - position.x;
int diffY = goal->GetPosition().y - position.y;
hValue = (abs(diffX) + abs(diffY)) * 10;
}
Vector2 PathNode::GetPosition()
{
return position;
}
void PathNode::SetPosition(int x, int y)
{
position.x = x;
position.y = y;
}
void PathNode::SetPosition(Vector2 pos)
{
position = pos;
}
PathNode* PathNode::GetParentNode()
{
return parentNode;
}
int PathNode::GetGValue()
{
return gValue;
}
int PathNode::GetHValue()
{
return hValue;
}
void PathNode::SetParentNode(PathNode * node)
{
parentNode = node;
}
|
댓글 없음:
댓글 쓰기