Skip to content

Commit

Permalink
Improve Templates name and readme
Browse files Browse the repository at this point in the history
Signed-off-by: Bensuperpc <[email protected]>
  • Loading branch information
bensuperpc committed Jun 21, 2024
1 parent 9a3c3cd commit f10202d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 69 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# astar

Fast and easy to use header only 2D astar algorithm library in C++20.
Fast and easy to use standalone header only 2D astar algorithm library in C++20.

I made it for learning how the astar algorithm works, try to make the fastest, tested and configurable as possible for my needs (future games and works).

Expand All @@ -24,6 +24,20 @@ It is an [astar algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm), t
* [x] Debug mode in template argument and lambda function
* [x] Support direct access and not access to the map
* [x] Unit tests and benchmarks
* [ ] Working CI (WIP)

### Heuristic function

You can set the heuristic function to calculate the distance between two points and return the cost.

| Heuristic | C++ Function | Description |
|-----------|--------------|-------------|
| euclidean | AStar::Heuristic::euclidean | Default |
| manhattan | AStar::Heuristic::manhattan | |
| octagonal | AStar::Heuristic::octagonal | |
| chebyshev | AStar::Heuristic::chebyshev | |
| euclideanNoSQR | AStar::Heuristic::euclideanNoSQR | |
| dijkstra | AStar::Heuristic::dijkstra | Always return 0 |

# How to use it

Expand Down
128 changes: 64 additions & 64 deletions include/astar/astar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
#include <unordered_set>
#include <vector>

template <typename T>
concept ArithmeticType = std::is_arithmetic<T>::value;
template <typename CoordinateType>
concept ArithmeticType = std::is_arithmetic<CoordinateType>::value;

template <typename T>
concept IntegerType = std::is_integral<T>::value;
template <typename CoordinateType>
concept IntegerType = std::is_integral<CoordinateType>::value;

template <typename T>
concept FloatingPointType = std::is_floating_point<T>::value;
template <typename CoordinateType>
concept FloatingPointType = std::is_floating_point<CoordinateType>::value;

namespace AStar {

template <IntegerType T = int32_t>
template <IntegerType CoordinateType = int32_t>
class Vec2 {
public:
Vec2() = default;
Vec2(T x_, T y_) : x(x_), y(y_) {}
Vec2(CoordinateType x_, CoordinateType y_) : x(x_), y(y_) {}

bool operator==(const Vec2& pos) const noexcept { return (x == pos.x && y == pos.y); }
Vec2 operator=(const Vec2& pos) noexcept {
Expand All @@ -49,25 +49,25 @@ class Vec2 {
size_t operator()(const Vec2& pos) const noexcept { return std::hash<size_t>()(pos.x ^ (pos.y << 4)); }
};

T x = 0;
T y = 0;
CoordinateType x = 0;
CoordinateType y = 0;
};
typedef Vec2<int32_t> Vec2i;

template <IntegerType T = uint32_t>
template <IntegerType CoordinateType = uint32_t>
class Node {
public:
explicit Node() : pos(Vec2i(0, 0)), parentNode(nullptr) {}
explicit Node(const Vec2i& pos, Node* parent = nullptr) : pos(pos), parentNode(parent) {}
explicit Node(const Vec2i& pos, const T pathCost, const T heuristicCost, Node* parent = nullptr)
explicit Node(const Vec2i& pos, const CoordinateType pathCost, const CoordinateType heuristicCost, Node* parent = nullptr)
: pathCost(pathCost), heuristicCost(heuristicCost), pos(pos), parentNode(parent) {}
inline T getTotalCost() const noexcept { return pathCost + heuristicCost; }
inline CoordinateType getTotalCost() const noexcept { return pathCost + heuristicCost; }
struct hash {
size_t operator()(const Node* node) const noexcept { return std::hash<size_t>()(node->pos.x ^ (node->pos.y << 4)); }
};

T pathCost = 0;
T heuristicCost = 0;
CoordinateType pathCost = 0;
CoordinateType heuristicCost = 0;
Vec2i pos = {0, 0};
Node* parentNode = nullptr;
};
Expand Down Expand Up @@ -109,16 +109,16 @@ static constexpr uint32_t dijkstra([[maybe_unused]] const Vec2i& source,
}
}; // namespace Heuristic

template <IntegerType T = uint32_t, bool enableDebug = false>
template <IntegerType CoordinateType = uint32_t, bool enableDebug = false>
class AStarVirtual {
public:
explicit AStarVirtual()
: _heuristicFunction(&Heuristic::euclidean),
_directionsCount(4),
_heuristicWeight(10),
_mouvemementCost(10),
_debugCurrentNode([](Node<T>*) {}),
_debugOpenNode([](Node<T>*) {}) {
_debugCurrentNode([](Node<CoordinateType>*) {}),
_debugOpenNode([](Node<CoordinateType>*) {}) {
_directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
}
void setHeuristic(const std::function<uint32_t(Vec2i, Vec2i, uint32_t)>& heuristic) {
Expand All @@ -143,23 +143,23 @@ class AStarVirtual {

std::vector<Vec2i>& getDirections() noexcept { return _directions; }

void setDebugCurrentNode(const std::function<void(Node<T>*)>& debugCurrentNode) noexcept { _debugCurrentNode = debugCurrentNode; }
void setDebugOpenNode(const std::function<void(Node<T>*)>& debugOpenNode) noexcept { _debugOpenNode = debugOpenNode; }
void setDebugCurrentNode(const std::function<void(Node<CoordinateType>*)>& debugCurrentNode) noexcept { _debugCurrentNode = debugCurrentNode; }
void setDebugOpenNode(const std::function<void(Node<CoordinateType>*)>& debugOpenNode) noexcept { _debugOpenNode = debugOpenNode; }

protected:
std::function<uint32_t(Vec2i, Vec2i, uint32_t)> _heuristicFunction;
std::vector<Vec2i> _directions;
size_t _directionsCount;
T _heuristicWeight;
CoordinateType _heuristicWeight;
size_t _mouvemementCost = 10;

// Only used if enableDebug is true
std::function<void(Node<T>*)> _debugCurrentNode;
std::function<void(Node<T>*)> _debugOpenNode;
std::function<void(Node<CoordinateType>*)> _debugCurrentNode;
std::function<void(Node<CoordinateType>*)> _debugOpenNode;
};

template <IntegerType T = uint32_t, bool enableDebug = false>
class AStar final : public AStarVirtual<T, enableDebug> {
template <IntegerType CoordinateType = uint32_t, bool enableDebug = false>
class AStar final : public AStarVirtual<CoordinateType, enableDebug> {
public:
explicit AStar() {}

Expand All @@ -168,23 +168,23 @@ class AStar final : public AStarVirtual<T, enableDebug> {
return {};
}

Node<T>* currentNode = nullptr;
Node<CoordinateType>* currentNode = nullptr;

auto compareFn = [](const Node<T>* a, const Node<T>* b) { return a->getTotalCost() > b->getTotalCost(); };
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)> openNodeVecPQueue =
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)>(compareFn);
auto compareFn = [](const Node<CoordinateType>* a, const Node<CoordinateType>* b) { return a->getTotalCost() > b->getTotalCost(); };
std::priority_queue<Node<CoordinateType>*, std::vector<Node<CoordinateType>*>, decltype(compareFn)> openNodeVecPQueue =
std::priority_queue<Node<CoordinateType>*, std::vector<Node<CoordinateType>*>, decltype(compareFn)>(compareFn);

std::unordered_map<Vec2i, Node<T>*, Vec2i::hash> openNodeMap;
std::unordered_map<Vec2i, Node<T>*, Vec2i::hash> closedNodeMap;
std::unordered_map<Vec2i, Node<CoordinateType>*, Vec2i::hash> openNodeMap;
std::unordered_map<Vec2i, Node<CoordinateType>*, Vec2i::hash> closedNodeMap;

openNodeVecPQueue.push(new Node<T>(source));
openNodeVecPQueue.push(new Node<CoordinateType>(source));
openNodeMap.insert({source, openNodeVecPQueue.top()});

while (!openNodeVecPQueue.empty()) {
currentNode = openNodeVecPQueue.top();

if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugCurrentNode(currentNode);
AStarVirtual<CoordinateType, enableDebug>::_debugCurrentNode(currentNode);
}

if (currentNode->pos == target) {
Expand All @@ -195,8 +195,8 @@ class AStar final : public AStarVirtual<T, enableDebug> {
openNodeMap.erase(currentNode->pos);
closedNodeMap.insert({currentNode->pos, currentNode});

for (size_t i = 0; i < AStarVirtual<T, enableDebug>::_directionsCount; ++i) {
Vec2i newPos = currentNode->pos + AStarVirtual<T, enableDebug>::_directions[i];
for (size_t i = 0; i < AStarVirtual<CoordinateType, enableDebug>::_directionsCount; ++i) {
Vec2i newPos = currentNode->pos + AStarVirtual<CoordinateType, enableDebug>::_directions[i];

if (_obstacles.contains(newPos)) {
continue;
Expand All @@ -210,14 +210,14 @@ class AStar final : public AStarVirtual<T, enableDebug> {
continue;
}

T nextCost = currentNode->pathCost + AStarVirtual<T, enableDebug>::_mouvemementCost;
Node<T>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr;
CoordinateType nextCost = currentNode->pathCost + AStarVirtual<CoordinateType, enableDebug>::_mouvemementCost;
Node<CoordinateType>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr;

if (nextNode == nullptr) {
nextNode = new Node<T>(newPos, currentNode);
nextNode = new Node<CoordinateType>(newPos, currentNode);
nextNode->pathCost = nextCost;
nextNode->heuristicCost = static_cast<T>(AStarVirtual<T, enableDebug>::_heuristicFunction(
nextNode->pos, target, AStarVirtual<T, enableDebug>::_heuristicWeight));
nextNode->heuristicCost = static_cast<CoordinateType>(AStarVirtual<CoordinateType, enableDebug>::_heuristicFunction(
nextNode->pos, target, AStarVirtual<CoordinateType, enableDebug>::_heuristicWeight));
openNodeVecPQueue.push(nextNode);
openNodeMap.insert({nextNode->pos, nextNode});
} else if (nextCost < nextNode->pathCost) {
Expand All @@ -226,7 +226,7 @@ class AStar final : public AStarVirtual<T, enableDebug> {
}

if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugOpenNode(nextNode);
AStarVirtual<CoordinateType, enableDebug>::_debugOpenNode(nextNode);
}
}
}
Expand Down Expand Up @@ -264,33 +264,33 @@ class AStar final : public AStarVirtual<T, enableDebug> {
};

// Fast AStar are faster than normal AStar but use more ram and direct access to the map
template <IntegerType T = uint32_t, bool enableDebug = false, IntegerType U = uint32_t>
class AStarFast final : public AStarVirtual<T, enableDebug> {
template <IntegerType CoordinateType = uint32_t, bool enableDebug = false, IntegerType MapElementType = uint32_t>
class AStarFast final : public AStarVirtual<CoordinateType, enableDebug> {
public:
explicit AStarFast() : _isObstacleFunction([](U value) { return value == 1; }) {}
explicit AStarFast() : _isObstacleFunction([](MapElementType value) { return value == 1; }) {}

// Same as AStar::findPath() but use direct access to the map
std::vector<Vec2i> findPath(const Vec2i& source, const Vec2i& target, const std::vector<U>& map, const Vec2i& worldSize) {
std::vector<Vec2i> findPath(const Vec2i& source, const Vec2i& target, const std::vector<MapElementType>& map, const Vec2i& worldSize) {
if (target.x < 0 || target.x >= worldSize.x || target.y < 0 || target.y >= worldSize.y) {
return {};
}

Node<T>* currentNode = nullptr;
Node<CoordinateType>* currentNode = nullptr;

auto compareFn = [](const Node<T>* a, const Node<T>* b) { return a->getTotalCost() > b->getTotalCost(); };
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)> openNodeVecPQueue =
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)>(compareFn);
std::unordered_map<Vec2i, Node<T>*, Vec2i::hash> openNodeMap;
std::unordered_map<Vec2i, Node<T>*, Vec2i::hash> closedNodeMap;
auto compareFn = [](const Node<CoordinateType>* a, const Node<CoordinateType>* b) { return a->getTotalCost() > b->getTotalCost(); };
std::priority_queue<Node<CoordinateType>*, std::vector<Node<CoordinateType>*>, decltype(compareFn)> openNodeVecPQueue =
std::priority_queue<Node<CoordinateType>*, std::vector<Node<CoordinateType>*>, decltype(compareFn)>(compareFn);
std::unordered_map<Vec2i, Node<CoordinateType>*, Vec2i::hash> openNodeMap;
std::unordered_map<Vec2i, Node<CoordinateType>*, Vec2i::hash> closedNodeMap;

openNodeVecPQueue.push(new Node<T>(source));
openNodeVecPQueue.push(new Node<CoordinateType>(source));
openNodeMap.insert({source, openNodeVecPQueue.top()});

while (!openNodeVecPQueue.empty()) {
currentNode = openNodeVecPQueue.top();

if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugCurrentNode(currentNode);
AStarVirtual<CoordinateType, enableDebug>::_debugCurrentNode(currentNode);
}

if (currentNode->pos == target) {
Expand All @@ -301,8 +301,8 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
openNodeMap.erase(currentNode->pos);
closedNodeMap.insert({currentNode->pos, currentNode});

for (size_t i = 0; i < AStarVirtual<T, enableDebug>::_directionsCount; ++i) {
Vec2i newPos = currentNode->pos + AStarVirtual<T, enableDebug>::_directions[i];
for (size_t i = 0; i < AStarVirtual<CoordinateType, enableDebug>::_directionsCount; ++i) {
Vec2i newPos = currentNode->pos + AStarVirtual<CoordinateType, enableDebug>::_directions[i];

if (_isObstacleFunction(map[newPos.x + newPos.y * worldSize.x])) {
continue;
Expand All @@ -316,13 +316,13 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
continue;
}

T nextCost = currentNode->pathCost + AStarVirtual<T, enableDebug>::_mouvemementCost;
Node<T>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr;
CoordinateType nextCost = currentNode->pathCost + AStarVirtual<CoordinateType, enableDebug>::_mouvemementCost;
Node<CoordinateType>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr;
if (nextNode == nullptr) {
nextNode = new Node<T>(newPos, currentNode);
nextNode = new Node<CoordinateType>(newPos, currentNode);
nextNode->pathCost = nextCost;
nextNode->heuristicCost = static_cast<T>(AStarVirtual<T, enableDebug>::_heuristicFunction(
nextNode->pos, target, AStarVirtual<T, enableDebug>::_heuristicWeight));
nextNode->heuristicCost = static_cast<CoordinateType>(AStarVirtual<CoordinateType, enableDebug>::_heuristicFunction(
nextNode->pos, target, AStarVirtual<CoordinateType, enableDebug>::_heuristicWeight));
openNodeVecPQueue.push(nextNode);
openNodeMap.insert({nextNode->pos, nextNode});
} else if (nextCost < nextNode->pathCost) [[likely]] {
Expand All @@ -331,7 +331,7 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
}

if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugOpenNode(nextNode);
AStarVirtual<CoordinateType, enableDebug>::_debugOpenNode(nextNode);
}
}
}
Expand All @@ -355,11 +355,11 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {

return path;
}
void setObstacle(const std::function<bool(U)>& isObstacleFunction) noexcept { _isObstacleFunction = isObstacleFunction; }
std::function<bool(U)>& getObstacle() noexcept { return _isObstacleFunction; }
void setObstacle(const std::function<bool(MapElementType)>& isObstacleFunction) noexcept { _isObstacleFunction = isObstacleFunction; }
std::function<bool(MapElementType)>& getObstacle() noexcept { return _isObstacleFunction; }

private:
std::function<bool(U)> _isObstacleFunction;
std::function<bool(MapElementType)> _isObstacleFunction;
};

} // namespace AStar
8 changes: 4 additions & 4 deletions test/source/benchmark/astar_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void DoSetup([[maybe_unused]] const benchmark::State& state) {}

static void DoTeardown([[maybe_unused]] const benchmark::State& state) {}

template <IntegerType T>
template <IntegerType CoordinateType>
static void astar_bench(benchmark::State& state) {
auto range = state.range(0);

Expand Down Expand Up @@ -47,7 +47,7 @@ static void astar_bench(benchmark::State& state) {
std::vector<uint8_t> blocks = std::vector<uint8_t>(mapWidth * mapHeight, 0);
benchmark::DoNotOptimize(blocks);

AStar::AStar<T, false> pathFinder;
AStar::AStar<CoordinateType, false> pathFinder;
benchmark::DoNotOptimize(pathFinder);
pathFinder.setWorldSize({mapWidth, mapHeight});
pathFinder.setHeuristic(AStar::Heuristic::euclidean);
Expand Down Expand Up @@ -104,7 +104,7 @@ BENCHMARK(astar_bench<uint32_t>)
->UseRealTime()
->Repetitions(repetitions);

template <IntegerType T>
template <IntegerType CoordinateType>
static void astar_bench_fast(benchmark::State& state) {
auto range = state.range(0);

Expand Down Expand Up @@ -133,7 +133,7 @@ static void astar_bench_fast(benchmark::State& state) {
std::vector<uint32_t> blocks = std::vector<uint32_t>(mapWidth * mapHeight, 0);
benchmark::DoNotOptimize(blocks);

AStar::AStarFast<T, false, uint32_t> pathFinder;
AStar::AStarFast<CoordinateType, false, uint32_t> pathFinder;
benchmark::DoNotOptimize(pathFinder);
pathFinder.setHeuristic(AStar::Heuristic::euclidean);
pathFinder.setDiagonalMovement(true);
Expand Down

0 comments on commit f10202d

Please sign in to comment.