dl
dl_astar.h
浏览该文件的文档.
1
12
13#pragma once
14
15#include <array>
16#include <vector>
17#include <map>
18#include <list>
19#include <algorithm>
20#include <cassert>
21
22#include "io/dl_log.h"
23#include "base/dl_string.h"
24
25namespace dl
26{
27
28
29 constexpr bool ASTAR_SHOW_LOG = false;
30
32template<typename T, typename COST>
33class AStar
34{
35public:
36 // 点类型
37 using AStarPoint = T;
38 // 代价类型(float、int等)
39 using AStarCost = COST;
40 using FuncCost = std::function<AStarCost(const AStarPoint&, const AStarPoint&)>;
41 using FuncLink = std::function<void(AStar*, const AStarPoint&)>;
42
43 //节点
45 {
46 public:
48 AStarNode* _parent; //父节点(为空代表起点)
49 AStarCost _cost; //前往这个节点 的代价值
50 AStarCost _total; //总代价 (cost + 估计代价)
51 bool _open; //是否在 Open表
52 bool _closed; //是否在 Close表
53 };
54
55 inline static bool AStarNodeCompare(AStarNode* n1, AStarNode* n2)
56 {
57 return n1->_total > n2->_total;
58 }
59
60 //优先队列
62 {
63 public:
65 {
66 AStarNode* node = _heap.front();
67 std::pop_heap(_heap.begin(), _heap.end(), AStarNodeCompare);
68 _heap.pop_back();
69 return node;
70 }
71 void Push(AStarNode* node)
72 {
73 _heap.push_back(node);
74 std::push_heap(_heap.begin(), _heap.end(), AStarNodeCompare);
75 }
77 {
78 for (auto iter = _heap.begin(); iter != _heap.end(); ++iter)
79 {
80 if ((*iter)->_xy == node->_xy)
81 {
82 std::push_heap(_heap.begin(), iter + 1, AStarNodeCompare);
83 return;
84 }
85 }
86 }
87 bool IsEmpty()
88 {
89 return _heap.empty();
90 }
91 void Clear()
92 {
93 _heap.clear();
94 }
95 private:
96 std::vector<AStarNode*> _heap;
97 };
98
99 inline AStarPoint AbsPoint(const AStarPoint& a, const AStarPoint& b)
100 {
101 AStarPoint ret{
102 a.x < b.x ? (b.x - a.x) : (a.x - b.x),
103 a.y < b.y ? (b.y - a.y) : (a.y - b.y)
104 };
105 return ret;
106 }
107
112 inline AStarCost AStarGetH(const AStarPoint& p0, const AStarPoint& p1)
113 {
114 constexpr AStarCost ASTAR_K0 = 10;
115 constexpr AStarCost ASTAR_K1 = 14;
116
117 AStarPoint d = AbsPoint(p0, p1);
118 return ASTAR_K0 * (d.x + d.y) - (std::min)(d.x, d.y) * (2 * ASTAR_K0 - ASTAR_K1);
119 }
120
121
131 const AStarPoint& size,
132 FuncCost func_g,
133 FuncCost func_h = nullptr,
134 FuncLink func_link = nullptr,
135 size_t max_node = 10000) :
136 _funcG{ func_g },
137 _funcH{ func_h },
138 _funcLink{ func_link },
139 _maxNode{ max_node },
140 _bDiagonal{ true },
141 _size{}
142 {
143 _arrNodeBank = new AStarNode[_maxNode];
144 assert(_funcG);
145 if (_funcH == nullptr)
146 {
147 _funcH = std::bind(&AStar::AStarGetH, this, std::placeholders::_1, std::placeholders::_2);
148 }
149 }
150
151 void SetSize(const AStarPoint& size)
152 {
153 _size = size;
154 }
155
165 bool Search(const AStarPoint& source, const AStarPoint& target, bool target_near,
166 std::list<AStarPoint>& list_path,
167 std::list<AStarPoint>* fail_path = nullptr)
168 {
169 if (ASTAR_SHOW_LOG)
170 log_msg("{} -> {}", source, target);
171 //清空
172 _bankCur = 0;
173 _masterNode.clear();
174 _queueOpen.Clear();
175 //起点插入Open表
176 AStarNode* node_start = _get_node(source);
177 node_start->_open = true;
178 node_start->_closed = false;
179 node_start->_cost = 0;
180 node_start->_total = _funcH(source, target);
181 node_start->_parent = NULL;
182 _queueOpen.Push(node_start);
183
184 _nodeBest = nullptr;
185 _xyTarget = target;
186 while (!_queueOpen.IsEmpty())
187 {
188 //取出 消耗值 最小的节点
189 _nodeBest = _queueOpen.Pop();
190 if (ASTAR_SHOW_LOG)
191 log_info("当前最佳节点为:{}", _nodeBest->_xy);
192 //是目标节点
193 if (_nodeBest->_xy == target
194 || (target_near && _is_link_node(_nodeBest->_xy, target)))
195 {
196 //构造路径(包含首尾节点)
197 list_path.clear();
198 while (_nodeBest)
199 {
200 list_path.push_front(_nodeBest->_xy);
201 _nodeBest = _nodeBest->_parent;
202 }
203 return true;
204 }
205
206 //由于_check_connecting 会增加节点,所以提前判断,并预留一些
207 if (_bankCur + 32 >= _maxNode)
208 {
209 if (fail_path)
210 {
211 //构造路径(包含首尾节点)
212 fail_path->clear();
213 while (_nodeBest)
214 {
215 fail_path->push_front(_nodeBest->_xy);
216 _nodeBest = _nodeBest->_parent;
217 }
218 }
219 return false;
220 }
221 //图的连接
222 if (_funcLink)
223 _funcLink(this, _nodeBest->_xy);
224 else
225 _link_node(_nodeBest->_xy);
226
227 //
228 _nodeBest->_closed = true;
229 }
230
231 if (fail_path)
232 {
233 //构造路径(包含首尾节点)
234 while (_nodeBest)
235 {
236 fail_path->push_front(_nodeBest->_xy);
237 _nodeBest = _nodeBest->_parent;
238 }
239 }
240 return false;
241 }
242
243
249 void AddNode(const AStarPoint& xy, AStarCost k_cost)
250 {
251 if (_nodeBest->_parent == nullptr ||
252 _nodeBest->_parent->_xy != xy)
253 {
254 // 代价
255 AStarCost g = _funcG(_nodeBest->_xy, xy);
256 // 如果代价 等于0,说明此节点不可通行
257 if (g == 0)
258 return;
259
260 AStarNode* actual_node = _get_node(xy);
261
262 // 不在close表
263 if (actual_node->_closed == false)
264 {
265 if (actual_node->_open)
266 {
267 // 上一个点代价 + 移到自己的代价
268 AStarCost new_cost = _nodeBest->_cost + g * k_cost;
269 // 总代价,自己代价 + 估计代价
270 AStarCost new_total = new_cost + _funcH(xy, _xyTarget);
271
272 //新的 代价更小 才刷新
273 if (new_total < actual_node->_total)
274 {
275 if (ASTAR_SHOW_LOG)
276 log_info("更新节点:{},{}->{}", actual_node->_xy, actual_node->_cost, new_cost);
277
278 actual_node->_parent = _nodeBest;
279 actual_node->_cost = new_cost;
280 actual_node->_total = new_total;
281 _queueOpen.UpdateNode(actual_node);
282 }
283 }
284 else
285 {//不在open表,则加入
286 actual_node->_parent = _nodeBest;
287 actual_node->_cost = _nodeBest->_cost + g * k_cost;
288 actual_node->_total = actual_node->_cost + _funcH(xy, _xyTarget);
289
290 _queueOpen.Push(actual_node);
291 actual_node->_open = true;
292
293 if (ASTAR_SHOW_LOG)
294 log_info("增加节点:{},{}", actual_node->_xy, actual_node->_cost);
295 }
296 }
297 }
298 }
299
303 void SetLinkDiagonal(bool diagonal)
304 {
305 assert(_funcLink == nullptr);
306 _bDiagonal = diagonal;
307 }
308
312 void SetFuncLink(FuncLink func_link)
313 {
314 _funcLink = func_link;
315 }
316
317
319 {
320 delete[] _arrNodeBank;
321 }
322private:
323 AStarNode* _get_node(AStarPoint xy)
324 {
325 //如果在 主列表 直接返回,如果没有则从节点银行构造一个
326 auto& iter_y = _masterNode[xy.y];
327 AStarNode*& node = iter_y[xy.x];
328
329 if (node)
330 {
331 return node;
332 }
333 else
334 {
335 //此处会越界
336 if (_bankCur >= _maxNode)
337 {
338 //cout << "寻路的节点超过上限!" <<;
339 return nullptr;
340 }
341
342 node = &_arrNodeBank[_bankCur++];
343 node->_xy = xy;
344 node->_open = false;
345 node->_closed = false;
346 return node;
347 }
348 }
352 void _link_node(const AStarPoint& p)
353 {
354 constexpr AStarCost ASTAR_K0 = 10;
355 constexpr AStarCost ASTAR_K1 = 14;
356
357 if (_size.x == 0)
358 {
359 AddNode({ p.x + 1, p.y }, ASTAR_K0);
360 AddNode({ p.x - 1, p.y }, ASTAR_K0);
361 AddNode({ p.x, p.y + 1 }, ASTAR_K0);
362 AddNode({ p.x, p.y - 1 }, ASTAR_K0);
363 if (_bDiagonal)
364 {
365 AddNode({ p.x + 1, p.y + 1 }, ASTAR_K1);
366 AddNode({ p.x + 1, p.y - 1 }, ASTAR_K1);
367 AddNode({ p.x - 1, p.y - 1 }, ASTAR_K1);
368 AddNode({ p.x - 1, p.y + 1 }, ASTAR_K1);
369 }
370 }
371 else
372 {
373 if (p.x + 1 < _size.w)
374 AddNode({ p.x + 1, p.y }, ASTAR_K0);
375 if (p.x > 0)
376 AddNode({ p.x - 1, p.y }, ASTAR_K0);
377 if (p.y + 1 < _size.h)
378 AddNode({ p.x, p.y + 1 }, ASTAR_K0);
379 if (p.y > 0)
380 AddNode({ p.x, p.y - 1}, ASTAR_K0);
381
382 if (_bDiagonal)
383 {
384 if (p.x + 1 < _size.w && p.y + 1 < _size.h)
385 AddNode({ p.x + 1, p.y + 1 }, ASTAR_K1);
386 if (p.x + 1 < _size.w && p.y > 0)
387 AddNode({ p.x + 1, p.y - 1 }, ASTAR_K1);
388 if (p.x > 0 && p.y > 0)
389 AddNode({ p.x - 1, p.y - 1 }, ASTAR_K1);
390 if (p.x > 0 && p.y + 1 < _size.h)
391 AddNode({ p.x - 1, p.y + 1 }, ASTAR_K1);
392 }
393 }
394 }
398 bool _is_link_node(const AStarPoint& p0, const AStarPoint& p1)
399 {
400 AStarPoint p = AbsPoint(p0, p1);
401 if (_bDiagonal)
402 {
403 return p.x <= 1 && p.y <= 1;
404 }
405 else
406 return p.x + p.y <= 1;
407 }
408
409 // 主列表{y, x}
410 std::map<unsigned, std::map<unsigned, AStarNode*>> _masterNode;
411 // Open表
412 AStarPriorityQueue _queueOpen;
413 // 节点银行
414 AStarNode* _arrNodeBank;
415 // 每次搜索前置0
416 size_t _bankCur;
417 // 遍历节点上限(也是节点银行大小)
418 size_t _maxNode;
419
420 // 是否可斜穿
421 bool _bDiagonal;
422 // 不为0则检查越界
423 AStarPoint _size;
424
425 //回调函数
426 FuncCost _funcG;
427 FuncCost _funcH;
428 FuncLink _funcLink;
429
430 //临时缓存
431 AStarNode* _nodeBest;
432 AStarPoint _xyTarget;
433};
434}
void UpdateNode(AStarNode *node)
void Push(AStarNode *node)
AStarCost AStarGetH(const AStarPoint &p0, const AStarPoint &p1)
默认获取H方法
void AddNode(const AStarPoint &xy, AStarCost k_cost)
添加节点(在func_link回调函数使用)
AStarPoint AbsPoint(const AStarPoint &a, const AStarPoint &b)
void SetFuncLink(FuncLink func_link)
修改连接函数
std::function< void(AStar *, const AStarPoint &)> FuncLink
static bool AStarNodeCompare(AStarNode *n1, AStarNode *n2)
AStar(const AStarPoint &size, FuncCost func_g, FuncCost func_h=nullptr, FuncLink func_link=nullptr, size_t max_node=10000)
定制一个A星寻路
std::function< AStarCost(const AStarPoint &, const AStarPoint &)> FuncCost
bool Search(const AStarPoint &source, const AStarPoint &target, bool target_near, std::list< AStarPoint > &list_path, std::list< AStarPoint > *fail_path=nullptr)
搜索
void SetSize(const AStarPoint &size)
void SetLinkDiagonal(bool diagonal)
设置是否对角线可走(使用默认连接函数才有效)
日志系统
#define log_msg(...)
#define log_info(...)
字符串相关
constexpr bool ASTAR_SHOW_LOG