![]() |
hivemind 1.0.0
|
Abstract graph interface optimized for path-finding. More...
#include <graph.h>
Public Types | |
using | NodePtr = std::shared_ptr< Node< T > > |
Helper alias to make code more readable. | |
Public Member Functions | |
virtual std::vector< NodePtr > | GetNeighbors (NodePtr node)=0 |
Collects all neighbor nodes of node . | |
virtual double | GetCost (NodePtr a, NodePtr b)=0 |
Returns the cost between a and b . | |
virtual bool | HasLineOfSight (NodePtr a, NodePtr b)=0 |
Determines if there is a direct line of sight between node a and node b . | |
virtual void | ResetNodes (void)=0 |
Resets all local and global goals and parent relationships of all nodes. | |
void | SolveAStar (NodePtr start, NodePtr goal) |
Finds cheapest path from start to goal . | |
void | PostSmooth (NodePtr start, NodePtr goal) |
Simplifies the path from start to goal . | |
Abstract graph interface optimized for path-finding.
T | Type of user data to store in each node |
This interface is designed to be flexible and scalable. The sub-classes are required to implement a few methods, such as Graph::GetNeighbors and Graph::GetCost for the A* path-finding algorithm to work.
using Routemaker::Graph< T >::NodePtr = std::shared_ptr<Node<T> > |
|
pure virtual |
Returns the cost between a
and b
.
Implemented by sub-classes of Graph. The a* path-finding algorithm uses cost to efficiently find the best path between two nodes. In order to do this, it requires some method of calculating the cost of moving between any two nodes. It is up to the sub-class to define how this is calulated. An example of this cost may be the euclidean distance between two nodes.
a
and node b
. Implemented in Routemaker::Routemaker.
|
pure virtual |
Collects all neighbor nodes of node
.
Implemented by sub-classes of Graph. The neighbor relationship between nodes define the edges of the graph. It is up to the subclass to define these relationships. For a 2D grid, the neighbors would simply be the nodes directly to the north, south, east and west, in addition to the corners between them. For a road network, the relationships may be more complex.
node | A pointer to the node from which to collect all neighbors |
node
Implemented in Routemaker::Routemaker.
|
pure virtual |
Determines if there is a direct line of sight between node a
and node b
.
Implemented by sub-classes of Graph. The Graph::PostSmooth method traverses the already found path through the A* path-finding algorithm and simplifies it by using this method. In a graph representing a 2D grid, a Bresenham implementation or ray-casting can be used to determine line of sight.
Implemented in Routemaker::Routemaker.
void Routemaker::Graph< T >::PostSmooth | ( | NodePtr | start, |
NodePtr | goal | ||
) |
Simplifies the path from start
to goal
.
start | Pointer to the start node of the path |
goal | Pointer to the end node of the path |
Should be run on the same nodes as Graph::SolveAStar, and should only be called after Graph::SolveAStar has finished.
|
pure virtual |
Resets all local and global goals and parent relationships of all nodes.
Implemented by sub-classes of Graph. In order to be able to re-use the same graph for several A* searches, the Graph::SolveAStar method needs to be able to reset all the nodes. As this interface does not contain the actual collection of nodes, this needs to be implemented in the sub-classes.
Implemented in Routemaker::Routemaker.
void Routemaker::Graph< T >::SolveAStar | ( | NodePtr | start, |
NodePtr | goal | ||
) |
Finds cheapest path from start
to goal
.
start | Pointer to the node to start the path from |
goal | Pointer to the node to find the path to |
Using the A* algorithm, this method explores the graph's nodes and updates their local and global goals, their visited flags, as well as their parent relationships.
When the algorithm finishes, given that a path exists between the nodes, the cheapest path between them is defined by the parent relationships. The path can be extracted by starting at the goal
and following the Node::Parent pointers until start
is reached, saving each node in a list and reversing it at the end.