Support Forum       G3D Web Page     
Classes | Public Types | Public Member Functions | List of all members
G3D::Pathfinder< Node, HashFunc > Class Template Referenceabstract


Finds good paths between nodes in an arbitrary directed graph. More...

Classes

class  NodeOrNull
 
class  PriorityQueue
 An inefficient implementation of a priority queue. More...
 
class  Step
 Used by findPath. More...
 

Public Types

typedef SmallArray< Node, 6 > NodeList
 
typedef Array< Node > Path
 
typedef Table< Node, StepStepTable
 

Public Member Functions

virtual ~Pathfinder ()
 
virtual float costOfEdge (const Node &A, const Node &B) const
 Returns the exact cost of traversing the directed edge from A to B, which are two nodes known to be neighbors. More...
 
virtual float estimateCost (const Node &A, const Node &B) const =0
 Returns an estimate of the cost of traversing from A to B. More...
 
virtual bool findPath (const Node &start, const Node &goal, Path &path, StepTable &bestPathTo) const
 Finds a good path from start to goal, and returns it as a list of nodes to visit. More...
 
bool findPath (const Node &start, const Node &goal, Path &path) const
 
virtual void getNeighbors (const Node &N, NodeList &neighbors) const =0
 Identifies all nodes (directionally) adjacent to N. More...
 

Detailed Description

template<typename Node, class HashFunc = HashTrait<Node>>
class G3D::Pathfinder< Node, HashFunc >


Finds good paths between nodes in an arbitrary directed graph.

Subclass and override estimateCost(), costOfEdge(), and getNeighbors().

Parameters
Nodemust support hashCode (or provide a HashFunc) and operator== (see G3D::Table). Two Nodes must be == if and only if they describe the same location in the graph. For a regular grid, use a class like G3D::Point2int32. For an arbitrary graph, Node may be a pointer or shared_ptr to a node, or an arbitrary unique ID. Node should be a relatively small object because it will be copied a lot during Pathfinding.
Referenced Code: Ported from Morgan McGuire's Javascript implementation
at http://codeheartjs.com/examples/pathfinding/findPath.js

Example:

#include <G3D/G3D.h>
class Map : public Pathfinder<Point2int32> {
protected:
shared_ptr<Image> m_grid;
bool isOpen(const Point2int32 P) const {
return (m_grid->get<Color1>(P, WrapMode::CLAMP).value <= 0.5f);
}
void appendIfOpen(const Point2int32 P, NodeList& neighborArray) const {
if (isOpen(P)) { neighborArray.append(P); }
}
public:
Map(const String& filename) {
m_grid = Image::fromFile(filename);
}
virtual float estimateCost(const Point2int32& A, const Point2int32& B) const override {
// Manhattan distance
return abs(A.x - B.x) + abs(A.y - B.y);
}
virtual void getNeighbors(const Point2int32& A, NodeList& neighborArray) const override {
neighborArray.clear();
appendIfOpen(A + Point2int32(-1, 0), neighborArray);
appendIfOpen(A + Point2int32(+1, 0), neighborArray);
appendIfOpen(A + Point2int32( 0, -1), neighborArray);
appendIfOpen(A + Point2int32( 0, +1), neighborArray);
}
void drawPath(const Point2int32& A, const Point2int32& B, const Path& path, const String& filename) const {
// Draw the path
const shared_ptr<Image>& image = m_grid->clone();
printf("Path has length %d\n", path.length());
for (int i = 0; i < path.length(); ++i) {
image->set(path[i], Color3::yellow());
}
image->set(A, Color3::green());
image->set(B, Color3::red());
image->save(filename);
}
};
int main(int argc, char** argv) {
Map map("test.png"); Point2int32 A(10, 10), B(20, 4);
//Map map("4x4.png"); Point2int32 A(1, 1), B(2, 2);
//Map map("6x6.png"); Point2int32 A(1, 1), B(4, 4);
Map::Path path;
Map::StepTable bestPathTo;
if (map.findPath(A, B, path, bestPathTo)) {
map.drawPath(A, B, path, "result.png");
} else {
printf("No path found!\n");
}
debugPrintf("bestPathTo:\n");
for (Map::StepTable::Iterator it = bestPathTo.begin(); it.hasMore(); ++it) {
printf(" %s <- %s\n", it->value.to.toString().c_str(), it->value.from.isNull() ? "nullptr" : it->value.from.node().toString().c_str());
}
return 0;
}

Member Typedef Documentation

◆ NodeList

template<typename Node , class HashFunc = HashTrait<Node>>
typedef SmallArray<Node, 6> G3D::Pathfinder< Node, HashFunc >::NodeList

◆ Path

template<typename Node , class HashFunc = HashTrait<Node>>
typedef Array<Node> G3D::Pathfinder< Node, HashFunc >::Path

◆ StepTable

template<typename Node , class HashFunc = HashTrait<Node>>
typedef Table<Node, Step> G3D::Pathfinder< Node, HashFunc >::StepTable

Constructor & Destructor Documentation

◆ ~Pathfinder()

template<typename Node , class HashFunc = HashTrait<Node>>
virtual G3D::Pathfinder< Node, HashFunc >::~Pathfinder ( )
inlinevirtual

Member Function Documentation

◆ costOfEdge()

template<typename Node , class HashFunc = HashTrait<Node>>
virtual float G3D::Pathfinder< Node, HashFunc >::costOfEdge ( const Node &  A,
const Node &  B 
) const
inlinevirtual

Returns the exact cost of traversing the directed edge from A to B, which are two nodes known to be neighbors.

The default implementation returns 1.0f for all pairs.

Referenced by G3D::Pathfinder< Node, HashFunc >::findPath().

◆ estimateCost()

template<typename Node , class HashFunc = HashTrait<Node>>
virtual float G3D::Pathfinder< Node, HashFunc >::estimateCost ( const Node &  A,
const Node &  B 
) const
pure virtual

Returns an estimate of the cost of traversing from A to B.

Return finf() if B is known to not be reachable from A.

Referenced by G3D::Pathfinder< Node, HashFunc >::findPath().

◆ findPath() [1/2]

template<typename Node , class HashFunc = HashTrait<Node>>
virtual bool G3D::Pathfinder< Node, HashFunc >::findPath ( const Node &  start,
const Node &  goal,
Path path,
StepTable bestPathTo 
) const
inlinevirtual

Finds a good path from start to goal, and returns it as a list of nodes to visit.

Returns null if there is no path.

The default implementation uses the A* algorithm.

For visualization purposes, findPathBestPathTo contains information about the other explored paths when the function returns.

Parameters
bestPathToMaps each Node to the Step on the best known path to that Node. Provided for visualization purposes. Cleared at the start. Note that the overloaded version of findPath() does not require a StepTable.
Returns
True if a path was found, otherwise false

Referenced by G3D::Pathfinder< Node, HashFunc >::findPath().

◆ findPath() [2/2]

template<typename Node , class HashFunc = HashTrait<Node>>
bool G3D::Pathfinder< Node, HashFunc >::findPath ( const Node &  start,
const Node &  goal,
Path path 
) const
inline

◆ getNeighbors()

template<typename Node , class HashFunc = HashTrait<Node>>
virtual void G3D::Pathfinder< Node, HashFunc >::getNeighbors ( const Node &  N,
NodeList neighbors 
) const
pure virtual

Identifies all nodes (directionally) adjacent to N.

First clears neighbors.

Referenced by G3D::Pathfinder< Node, HashFunc >::findPath().


documentation generated on Wed Nov 24 2021 08:01:59 using doxygen 1.8.15