-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha_star.cpp
103 lines (85 loc) · 3.29 KB
/
a_star.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
/**
* @file a_star.cpp
* @author ShieldQiQi
* @brief Contains the AStar class
*/
#include <cmath>
#include "a_star.h"
extern AStar new_a_star;
using namespace std;
extern std::vector<std::vector<int>> grid;
extern std::vector<std::vector<int>> costGrid;
extern MainWindow *w;
void CALLBACK TimerProcAstar(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
{
Q_UNUSED(hWnd);Q_UNUSED(nMsg);Q_UNUSED(nTimerid);Q_UNUSED(dwTime);
const std::vector<Node> motion = GetMotion();
// Main loop
if(!new_a_star.doneFlag)
{
Node current = new_a_star.open_list_.top();
new_a_star.open_list_.pop();
current.id_ = current.x_ * new_a_star.n + current.y_;
// find if current is the goal node
if (CompareCoordinates(current, new_a_star.goal_))
{
new_a_star.closed_list_.push_back(current);
grid[current.x_][current.y_] = 5;
//return new_a_star.closed_list_;
new_a_star.doneFlag = true;
KillTimer(NULL, 1);
}
grid[current.x_][current.y_] = 5;
w->update();
// find all the neighbours and update their f(n)=g(n)+h(n)
for (const auto& m : motion)
{
Node new_point;
new_point = current + m;
// update id and parent id
new_point.id_ = new_a_star.n * new_point.x_ + new_point.y_;
new_point.pid_ = current.id_;
// the difference between Dijkstar and A-star Algorithm
// f(n)=g(n)+h(n) where the g(n)=cost while h(n)=Manhattan distance of current node and goal node
new_point.h_cost_ = std::abs(new_point.x_ - new_a_star.goal_.x_) + std::abs(new_point.y_ - new_a_star.goal_.y_);
if (CompareCoordinates(new_point, new_a_star.goal_)){
new_a_star.open_list_.push(new_point);
break;
}
// Check boundaries
if (checkOutsideBoundary(new_point, new_a_star.n)) {
continue;}
// obstacle or visited
if (grid[new_point.x_][new_point.y_] == 1 || grid[new_point.x_][new_point.y_] == 6) {
continue;}
if(costGrid[new_point.x_][new_point.y_]>new_point.cost_)
{
// check the new_point if is already in open_list_
// if new_point in open_list then
new_a_star.open_list_.push(new_point);
grid[new_point.x_][new_point.y_] = 2;
costGrid[new_point.x_][new_point.y_] = new_point.cost_;
// otherwise, update the cost of the previus one
// Todo, not a serious problem, but would be better if is done
}
}
new_a_star.closed_list_.push_back(current);
grid[current.x_][current.y_] = 6;
}else{
new_a_star.closed_list_.clear();
Node no_path_node(-1, -1, -1, -1, -1, -1);
new_a_star.closed_list_.push_back(no_path_node);
new_a_star.doneFlag = true;
KillTimer(NULL, 1);
}
}
std::vector<Node> AStar::a_star(std::vector<std::vector<int>>& grid, const Node& start_in, const Node& goal_in)
{
start_ = start_in;
goal_ = goal_in;
n = grid.size();
// Get possible motions
open_list_.push(start_);
SetTimer(NULL, 1, 50, (TIMERPROC)TimerProcAstar);
return closed_list_;
}