170 current->LocalGoal = 0.0;
171 current->GlobalGoal = GetCost(current, goal);
174 std::priority_queue<NodePtr, std::vector<NodePtr>,
177 return a->GlobalGoal > b->GlobalGoal;
180 notTested.push(start);
183 while (!notTested.empty()) {
186 while (!notTested.empty() && notTested.top()->Visited) {
192 if (notTested.empty()) {
196 current = notTested.top();
197 current->Visited =
true;
199 for (
auto neighbor : GetNeighbors(current)) {
201 if (!neighbor->Visited && !neighbor->Data.Occupied) {
202 notTested.push(neighbor);
209 double candidateGoal =
210 current->LocalGoal + GetCost(current, neighbor);
211 if (candidateGoal < neighbor->LocalGoal) {
212 neighbor->Parent = current;
213 neighbor->LocalGoal = candidateGoal;
214 neighbor->GlobalGoal =
215 neighbor->LocalGoal + GetCost(neighbor, goal);
234 NodePtr parent = current->Parent.lock();
235 while (current && parent && (current != start)) {
236 NodePtr grandParent = parent->Parent.lock();
240 if (HasLineOfSight(current, grandParent)) {
241 current->Parent = grandParent;
242 parent = grandParent;
246 parent = current->Parent.lock();
Abstract graph interface optimized for path-finding.
void PostSmooth(NodePtr start, NodePtr goal)
Simplifies the path from start to goal.
virtual void ResetNodes(void)=0
Resets all local and global goals and parent relationships of all nodes.
virtual std::vector< NodePtr > GetNeighbors(NodePtr node)=0
Collects all neighbor nodes of node.
virtual bool HasLineOfSight(NodePtr a, NodePtr b)=0
Determines if there is a direct line of sight between node a and node b.
void SolveAStar(NodePtr start, NodePtr goal)
Finds cheapest path from start to goal.
std::shared_ptr< Node< T > > NodePtr
Helper alias to make code more readable.
virtual double GetCost(NodePtr a, NodePtr b)=0
Returns the cost between a and b.
Represents a node in a Graph data structured made for path-finding.
T Data
Data stored in the the node.
std::weak_ptr< Node< T > > Parent
A non-owner pointer to the parent of the node.
bool Visited
Specifies if a given node has been visited during path-finding.
double LocalGoal
Represents the cost from the start node to this node.
double GlobalGoal
Represents the assumed cost from the start to the goal node through this node.