-
Notifications
You must be signed in to change notification settings - Fork 55
/
a-star.hpp
138 lines (115 loc) · 3.23 KB
/
a-star.hpp
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
//
// Created by huangxh on 19-9-16.
//
#ifndef BCD_PLANNER_A_STAR_H
#define BCD_PLANNER_A_STAR_H
#endif //BCD_PLANNER_A_STAR_H
#include <vector>
#include <map>
#include <deque>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
cv::Mat map;
class Point2D
{
public:
Point2D(int x_pos, int y_pos)
{
x = x_pos;
y = y_pos;
}
int x;
int y;
};
struct MapPoint
{
double cost = 0.0;
double occupancy = 1.0;
Point2D prev_position = Point2D(INT_MAX, INT_MAX);
bool costComputed = false;
};
std::map<Point2D, MapPoint> cost_map;
std::vector<Point2D> GetNeighbors(Point2D position)
{
std::vector<Point2D> neighbors = {
Point2D(position.x-1, position.y-1),
Point2D(position.x, position.y-1),
Point2D(position.x+1, position.y-1),
Point2D(position.x-1, position.y),
Point2D(position.x+1, position.y),
Point2D(position.x-1, position.y+1),
Point2D(position.x, position.y+1),
Point2D(position.x+1, position.y+1)
};
return neighbors;
}
void BuildOccupancyMap() // CV_32FC3
{
for(int i = 0; i < map.rows; i++)
{
for(int j = 0; j < map.cols; j++)
{
if(map.at<cv::Vec3f>(i,j) == cv::Vec3f(255,255,255))
{
cost_map[Point2D(j,i)].occupancy = INT_MAX;
}
else
{
cost_map[Point2D(j,i)].occupancy = 1.0;
}
}
}
}
void BuildCostMap(Point2D start)
{
std::deque<Point2D> task_list = {start};
cost_map[start].costComputed = true;
std::vector<Point2D> neighbors;
std::vector<Point2D> candidates;
while(!task_list.empty())
{
candidates.clear();
neighbors = GetNeighbors(task_list.front());
for(auto neighbor:neighbors)
{
if(!cost_map[neighbor].costComputed)
{
candidates.emplace_back(neighbor);
cost_map[neighbor].cost = cost_map[task_list.front()].cost + 1.0;
cost_map[neighbor].prev_position = task_list.front();
cost_map[neighbor].costComputed = true;
}
}
task_list.pop_front();
task_list.insert(task_list.end(), candidates.begin(), candidates.end());
}
}
std::deque<Point2D> FindShortestPath(Point2D start, Point2D end)
{
std::deque<Point2D> path = {end};
Point2D curr_positon = Point2D(end.x, end.y);
int prev_x = cost_map[curr_positon].prev_position.x;
int prev_y = cost_map[curr_positon].prev_position.y;
while(prev_x != start.x && prev_y != start.y)
{
path.emplace_front(Point2D(prev_x,prev_y));
curr_positon = Point2D(prev_x, prev_y);
prev_x = cost_map[curr_positon].prev_position.x;
prev_y = cost_map[curr_positon].prev_position.y;
}
path.emplace_front(start);
return path;
}
void ResetCostMap()
{
for(int i = 0; i < map.rows; i++)
{
for(int j = 0; j < map.cols; j++)
{
cost_map[Point2D(j,i)].cost = 0.0;
cost_map[Point2D(j,i)].prev_position = Point2D(INT_MAX, INT_MAX);
cost_map[Point2D(j,i)].costComputed = false;
}
}
}