qHexWalker 0.0.1
Hexagonal Grid Pathfinding & Maze Visualization on Interactive Maps
Loading...
Searching...
No Matches
astar.h
Go to the documentation of this file.
1
12#ifndef Q_HEX_WALKER_ASTAR_H
13#define Q_HEX_WALKER_ASTAR_H
14
45class H3AStar final : public QObject {
46 Q_OBJECT
47 Q_DISABLE_COPY_MOVE(H3AStar)
48
49public:
54 explicit H3AStar(QObject *parent = nullptr);
55
59 ~H3AStar() override;
60
75 std::vector<H3Index> findShortestPath(H3Index start, H3Index end);
76
85 void setBlockedCells(const std::unordered_set<H3Index> &blocked);
86
87signals:
95 void newCell(H3Index h3Index);
96
97private:
99 static constexpr uint16_t MAX_CELLS_RES_3{41'162};
100
102 static constexpr int MAX_NEIGHBORS{6};
103
108 struct Node {
109 H3Index cell{};
110 double gScore{};
111 double fScore{};
112
118 bool operator>(const Node &other) const { return fScore > other.fScore; }
119 };
120
125 struct H3IndexHash {
126 std::size_t operator()(const H3Index &index) const { return std::hash<uint64_t>()(index); }
127 };
128
130 std::unordered_set<H3Index> blockedCells{};
131
139 std::vector<H3Index> findPathAtResolution3(H3Index start, H3Index end, const LatLng &endCoord);
140
149 std::vector<H3Index> refineEndSegmentGradual(H3Index prevInPath, H3Index parentEnd, H3Index originalEnd,
150 int endRes);
151
159 std::vector<H3Index> refineStartSegmentGradual(H3Index originalStart, H3Index nextInPath, int startRes) const;
160
170 std::vector<H3Index> refinePath(const std::vector<H3Index> &pathRes3, H3Index originalStart, H3Index originalEnd,
171 int startRes, int endRes);
172
176 static H3Index findBoundaryCellInDirection(const std::vector<H3Index> &cells, H3Index from, H3Index direction);
177
181 std::vector<H3Index> findLocalPathAtResolution(H3Index start, H3Index end, H3Index limitParent) const;
182
189 static std::vector<H3Index> getChildrenAtResolution(H3Index parent, int resolution);
190
196 static std::array<H3Index, MAX_NEIGHBORS> getNeighbors(H3Index cell);
197
204 static double getDistanceBetweenCells(H3Index cell1, H3Index cell2);
205
212 static double heuristic(H3Index cell, const LatLng &targetCoord);
213
221 static std::vector<H3Index> reconstructPath(const std::unordered_map<H3Index, H3Index, H3IndexHash> &previous,
222 H3Index start, H3Index end);
223
227 static H3Index cellToParentRes3(H3Index index);
228
232 static H3Index cellToChildRes3(H3Index index);
233};
234
235#endif // Q_HEX_WALKER_ASTAR_H
Bidirectional A* pathfinding algorithm for H3 hexagonal cells.
Definition astar.h:45
std::vector< H3Index > findShortestPath(H3Index start, H3Index end)
Finds the shortest path between two H3 cells.
Definition astar.cpp:12
static H3Index cellToParentRes3(H3Index index)
Converts any cell to its parent at resolution 3.
Definition astar.cpp:573
std::vector< H3Index > refinePath(const std::vector< H3Index > &pathRes3, H3Index originalStart, H3Index originalEnd, int startRes, int endRes)
Refines entire coarse path to target resolution.
Definition astar.cpp:413
static H3Index findBoundaryCellInDirection(const std::vector< H3Index > &cells, H3Index from, H3Index direction)
Finds boundary cell in a direction for path refinement.
Definition astar.cpp:448
std::vector< H3Index > refineEndSegmentGradual(H3Index prevInPath, H3Index parentEnd, H3Index originalEnd, int endRes)
Refines path segment near the end point to target resolution.
Definition astar.cpp:322
void setBlockedCells(const std::unordered_set< H3Index > &blocked)
Sets cells that cannot be traversed (obstacles).
Definition astar.cpp:10
static double heuristic(H3Index cell, const LatLng &targetCoord)
Heuristic function for A* (distance to target).
Definition astar.cpp:542
static constexpr int MAX_NEIGHBORS
Maximum neighbors for a hexagonal cell (always 6).
Definition astar.h:102
~H3AStar() override
Destructor.
static H3Index cellToChildRes3(H3Index index)
Converts resolution 3 cell to child at original resolution.
Definition astar.cpp:580
std::vector< H3Index > findLocalPathAtResolution(H3Index start, H3Index end, H3Index limitParent) const
Finds local path within a parent cell boundary.
Definition astar.cpp:256
static constexpr uint16_t MAX_CELLS_RES_3
Maximum number of cells at resolution 3 (~41K cells globally).
Definition astar.h:99
std::vector< H3Index > findPathAtResolution3(H3Index start, H3Index end, const LatLng &endCoord)
Finds path at resolution 3 using bidirectional A*.
Definition astar.cpp:74
static std::array< H3Index, MAX_NEIGHBORS > getNeighbors(H3Index cell)
Gets the 6 neighbors of a hexagonal cell.
Definition astar.cpp:504
std::vector< H3Index > refineStartSegmentGradual(H3Index originalStart, H3Index nextInPath, int startRes) const
Refines path segment near the start point to target resolution.
Definition astar.cpp:373
static std::vector< H3Index > getChildrenAtResolution(H3Index parent, int resolution)
Gets all children of a cell at specified resolution.
Definition astar.cpp:485
std::unordered_set< H3Index > blockedCells
Set of blocked (impassable) cells.
Definition astar.h:130
static std::vector< H3Index > reconstructPath(const std::unordered_map< H3Index, H3Index, H3IndexHash > &previous, H3Index start, H3Index end)
Reconstructs path from the visited nodes map.
Definition astar.cpp:551
static double getDistanceBetweenCells(H3Index cell1, H3Index cell2)
Calculates geographic distance between two cells.
Definition astar.cpp:525
Hash function for H3Index in unordered containers.
Definition astar.h:125
std::size_t operator()(const H3Index &index) const
Definition astar.h:126
Internal node structure for A* priority queue.
Definition astar.h:108
double gScore
Cost from start to this node.
Definition astar.h:110
bool operator>(const Node &other) const
Comparison operator for priority queue (min-heap).
Definition astar.h:118
H3Index cell
The H3 cell index.
Definition astar.h:109
double fScore
Estimated total cost (gScore + heuristic).
Definition astar.h:111