Improve Templates name and readme

Signed-off-by: Bensuperpc <bensuperpc@gmail.com>
This commit is contained in:
Bensuperpc 2024-06-21 21:28:11 +02:00
parent 9a3c3cdd04
commit f10202da96
No known key found for this signature in database
GPG Key ID: EED195F75454E328
3 changed files with 83 additions and 69 deletions

View File

@ -1,6 +1,6 @@
# astar # 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). 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).
@ -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] Debug mode in template argument and lambda function
* [x] Support direct access and not access to the map * [x] Support direct access and not access to the map
* [x] Unit tests and benchmarks * [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 # How to use it

View File

@ -18,22 +18,22 @@
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
template <typename T> template <typename CoordinateType>
concept ArithmeticType = std::is_arithmetic<T>::value; concept ArithmeticType = std::is_arithmetic<CoordinateType>::value;
template <typename T> template <typename CoordinateType>
concept IntegerType = std::is_integral<T>::value; concept IntegerType = std::is_integral<CoordinateType>::value;
template <typename T> template <typename CoordinateType>
concept FloatingPointType = std::is_floating_point<T>::value; concept FloatingPointType = std::is_floating_point<CoordinateType>::value;
namespace AStar { namespace AStar {
template <IntegerType T = int32_t> template <IntegerType CoordinateType = int32_t>
class Vec2 { class Vec2 {
public: public:
Vec2() = default; 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); } bool operator==(const Vec2& pos) const noexcept { return (x == pos.x && y == pos.y); }
Vec2 operator=(const Vec2& pos) noexcept { Vec2 operator=(const Vec2& pos) noexcept {
@ -49,25 +49,25 @@ class Vec2 {
size_t operator()(const Vec2& pos) const noexcept { return std::hash<size_t>()(pos.x ^ (pos.y << 4)); } size_t operator()(const Vec2& pos) const noexcept { return std::hash<size_t>()(pos.x ^ (pos.y << 4)); }
}; };
T x = 0; CoordinateType x = 0;
T y = 0; CoordinateType y = 0;
}; };
typedef Vec2<int32_t> Vec2i; typedef Vec2<int32_t> Vec2i;
template <IntegerType T = uint32_t> template <IntegerType CoordinateType = uint32_t>
class Node { class Node {
public: public:
explicit Node() : pos(Vec2i(0, 0)), parentNode(nullptr) {} 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, 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) {} : 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 { struct hash {
size_t operator()(const Node* node) const noexcept { return std::hash<size_t>()(node->pos.x ^ (node->pos.y << 4)); } size_t operator()(const Node* node) const noexcept { return std::hash<size_t>()(node->pos.x ^ (node->pos.y << 4)); }
}; };
T pathCost = 0; CoordinateType pathCost = 0;
T heuristicCost = 0; CoordinateType heuristicCost = 0;
Vec2i pos = {0, 0}; Vec2i pos = {0, 0};
Node* parentNode = nullptr; Node* parentNode = nullptr;
}; };
@ -109,7 +109,7 @@ static constexpr uint32_t dijkstra([[maybe_unused]] const Vec2i& source,
} }
}; // namespace Heuristic }; // namespace Heuristic
template <IntegerType T = uint32_t, bool enableDebug = false> template <IntegerType CoordinateType = uint32_t, bool enableDebug = false>
class AStarVirtual { class AStarVirtual {
public: public:
explicit AStarVirtual() explicit AStarVirtual()
@ -117,8 +117,8 @@ class AStarVirtual {
_directionsCount(4), _directionsCount(4),
_heuristicWeight(10), _heuristicWeight(10),
_mouvemementCost(10), _mouvemementCost(10),
_debugCurrentNode([](Node<T>*) {}), _debugCurrentNode([](Node<CoordinateType>*) {}),
_debugOpenNode([](Node<T>*) {}) { _debugOpenNode([](Node<CoordinateType>*) {}) {
_directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; _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) { void setHeuristic(const std::function<uint32_t(Vec2i, Vec2i, uint32_t)>& heuristic) {
@ -143,23 +143,23 @@ class AStarVirtual {
std::vector<Vec2i>& getDirections() noexcept { return _directions; } std::vector<Vec2i>& getDirections() noexcept { return _directions; }
void setDebugCurrentNode(const std::function<void(Node<T>*)>& debugCurrentNode) noexcept { _debugCurrentNode = debugCurrentNode; } void setDebugCurrentNode(const std::function<void(Node<CoordinateType>*)>& debugCurrentNode) noexcept { _debugCurrentNode = debugCurrentNode; }
void setDebugOpenNode(const std::function<void(Node<T>*)>& debugOpenNode) noexcept { _debugOpenNode = debugOpenNode; } void setDebugOpenNode(const std::function<void(Node<CoordinateType>*)>& debugOpenNode) noexcept { _debugOpenNode = debugOpenNode; }
protected: protected:
std::function<uint32_t(Vec2i, Vec2i, uint32_t)> _heuristicFunction; std::function<uint32_t(Vec2i, Vec2i, uint32_t)> _heuristicFunction;
std::vector<Vec2i> _directions; std::vector<Vec2i> _directions;
size_t _directionsCount; size_t _directionsCount;
T _heuristicWeight; CoordinateType _heuristicWeight;
size_t _mouvemementCost = 10; size_t _mouvemementCost = 10;
// Only used if enableDebug is true // Only used if enableDebug is true
std::function<void(Node<T>*)> _debugCurrentNode; std::function<void(Node<CoordinateType>*)> _debugCurrentNode;
std::function<void(Node<T>*)> _debugOpenNode; std::function<void(Node<CoordinateType>*)> _debugOpenNode;
}; };
template <IntegerType T = uint32_t, bool enableDebug = false> template <IntegerType CoordinateType = uint32_t, bool enableDebug = false>
class AStar final : public AStarVirtual<T, enableDebug> { class AStar final : public AStarVirtual<CoordinateType, enableDebug> {
public: public:
explicit AStar() {} explicit AStar() {}
@ -168,23 +168,23 @@ class AStar final : public AStarVirtual<T, enableDebug> {
return {}; return {};
} }
Node<T>* currentNode = nullptr; Node<CoordinateType>* currentNode = nullptr;
auto compareFn = [](const Node<T>* a, const Node<T>* b) { return a->getTotalCost() > b->getTotalCost(); }; auto compareFn = [](const Node<CoordinateType>* a, const Node<CoordinateType>* b) { return a->getTotalCost() > b->getTotalCost(); };
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)> openNodeVecPQueue = std::priority_queue<Node<CoordinateType>*, std::vector<Node<CoordinateType>*>, decltype(compareFn)> openNodeVecPQueue =
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)>(compareFn); 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<CoordinateType>*, Vec2i::hash> openNodeMap;
std::unordered_map<Vec2i, Node<T>*, Vec2i::hash> closedNodeMap; 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()}); openNodeMap.insert({source, openNodeVecPQueue.top()});
while (!openNodeVecPQueue.empty()) { while (!openNodeVecPQueue.empty()) {
currentNode = openNodeVecPQueue.top(); currentNode = openNodeVecPQueue.top();
if constexpr (enableDebug) { if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugCurrentNode(currentNode); AStarVirtual<CoordinateType, enableDebug>::_debugCurrentNode(currentNode);
} }
if (currentNode->pos == target) { if (currentNode->pos == target) {
@ -195,8 +195,8 @@ class AStar final : public AStarVirtual<T, enableDebug> {
openNodeMap.erase(currentNode->pos); openNodeMap.erase(currentNode->pos);
closedNodeMap.insert({currentNode->pos, currentNode}); closedNodeMap.insert({currentNode->pos, currentNode});
for (size_t i = 0; i < AStarVirtual<T, enableDebug>::_directionsCount; ++i) { for (size_t i = 0; i < AStarVirtual<CoordinateType, enableDebug>::_directionsCount; ++i) {
Vec2i newPos = currentNode->pos + AStarVirtual<T, enableDebug>::_directions[i]; Vec2i newPos = currentNode->pos + AStarVirtual<CoordinateType, enableDebug>::_directions[i];
if (_obstacles.contains(newPos)) { if (_obstacles.contains(newPos)) {
continue; continue;
@ -210,14 +210,14 @@ class AStar final : public AStarVirtual<T, enableDebug> {
continue; continue;
} }
T nextCost = currentNode->pathCost + AStarVirtual<T, enableDebug>::_mouvemementCost; CoordinateType nextCost = currentNode->pathCost + AStarVirtual<CoordinateType, enableDebug>::_mouvemementCost;
Node<T>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr; Node<CoordinateType>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr;
if (nextNode == nullptr) { if (nextNode == nullptr) {
nextNode = new Node<T>(newPos, currentNode); nextNode = new Node<CoordinateType>(newPos, currentNode);
nextNode->pathCost = nextCost; nextNode->pathCost = nextCost;
nextNode->heuristicCost = static_cast<T>(AStarVirtual<T, enableDebug>::_heuristicFunction( nextNode->heuristicCost = static_cast<CoordinateType>(AStarVirtual<CoordinateType, enableDebug>::_heuristicFunction(
nextNode->pos, target, AStarVirtual<T, enableDebug>::_heuristicWeight)); nextNode->pos, target, AStarVirtual<CoordinateType, enableDebug>::_heuristicWeight));
openNodeVecPQueue.push(nextNode); openNodeVecPQueue.push(nextNode);
openNodeMap.insert({nextNode->pos, nextNode}); openNodeMap.insert({nextNode->pos, nextNode});
} else if (nextCost < nextNode->pathCost) { } else if (nextCost < nextNode->pathCost) {
@ -226,7 +226,7 @@ class AStar final : public AStarVirtual<T, enableDebug> {
} }
if constexpr (enableDebug) { if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugOpenNode(nextNode); AStarVirtual<CoordinateType, enableDebug>::_debugOpenNode(nextNode);
} }
} }
} }
@ -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 // 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> template <IntegerType CoordinateType = uint32_t, bool enableDebug = false, IntegerType MapElementType = uint32_t>
class AStarFast final : public AStarVirtual<T, enableDebug> { class AStarFast final : public AStarVirtual<CoordinateType, enableDebug> {
public: 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 // 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) { if (target.x < 0 || target.x >= worldSize.x || target.y < 0 || target.y >= worldSize.y) {
return {}; return {};
} }
Node<T>* currentNode = nullptr; Node<CoordinateType>* currentNode = nullptr;
auto compareFn = [](const Node<T>* a, const Node<T>* b) { return a->getTotalCost() > b->getTotalCost(); }; auto compareFn = [](const Node<CoordinateType>* a, const Node<CoordinateType>* b) { return a->getTotalCost() > b->getTotalCost(); };
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)> openNodeVecPQueue = std::priority_queue<Node<CoordinateType>*, std::vector<Node<CoordinateType>*>, decltype(compareFn)> openNodeVecPQueue =
std::priority_queue<Node<T>*, std::vector<Node<T>*>, decltype(compareFn)>(compareFn); 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<CoordinateType>*, Vec2i::hash> openNodeMap;
std::unordered_map<Vec2i, Node<T>*, Vec2i::hash> closedNodeMap; 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()}); openNodeMap.insert({source, openNodeVecPQueue.top()});
while (!openNodeVecPQueue.empty()) { while (!openNodeVecPQueue.empty()) {
currentNode = openNodeVecPQueue.top(); currentNode = openNodeVecPQueue.top();
if constexpr (enableDebug) { if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugCurrentNode(currentNode); AStarVirtual<CoordinateType, enableDebug>::_debugCurrentNode(currentNode);
} }
if (currentNode->pos == target) { if (currentNode->pos == target) {
@ -301,8 +301,8 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
openNodeMap.erase(currentNode->pos); openNodeMap.erase(currentNode->pos);
closedNodeMap.insert({currentNode->pos, currentNode}); closedNodeMap.insert({currentNode->pos, currentNode});
for (size_t i = 0; i < AStarVirtual<T, enableDebug>::_directionsCount; ++i) { for (size_t i = 0; i < AStarVirtual<CoordinateType, enableDebug>::_directionsCount; ++i) {
Vec2i newPos = currentNode->pos + AStarVirtual<T, enableDebug>::_directions[i]; Vec2i newPos = currentNode->pos + AStarVirtual<CoordinateType, enableDebug>::_directions[i];
if (_isObstacleFunction(map[newPos.x + newPos.y * worldSize.x])) { if (_isObstacleFunction(map[newPos.x + newPos.y * worldSize.x])) {
continue; continue;
@ -316,13 +316,13 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
continue; continue;
} }
T nextCost = currentNode->pathCost + AStarVirtual<T, enableDebug>::_mouvemementCost; CoordinateType nextCost = currentNode->pathCost + AStarVirtual<CoordinateType, enableDebug>::_mouvemementCost;
Node<T>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr; Node<CoordinateType>* nextNode = openNodeMap.find(newPos) != openNodeMap.end() ? openNodeMap[newPos] : nullptr;
if (nextNode == nullptr) { if (nextNode == nullptr) {
nextNode = new Node<T>(newPos, currentNode); nextNode = new Node<CoordinateType>(newPos, currentNode);
nextNode->pathCost = nextCost; nextNode->pathCost = nextCost;
nextNode->heuristicCost = static_cast<T>(AStarVirtual<T, enableDebug>::_heuristicFunction( nextNode->heuristicCost = static_cast<CoordinateType>(AStarVirtual<CoordinateType, enableDebug>::_heuristicFunction(
nextNode->pos, target, AStarVirtual<T, enableDebug>::_heuristicWeight)); nextNode->pos, target, AStarVirtual<CoordinateType, enableDebug>::_heuristicWeight));
openNodeVecPQueue.push(nextNode); openNodeVecPQueue.push(nextNode);
openNodeMap.insert({nextNode->pos, nextNode}); openNodeMap.insert({nextNode->pos, nextNode});
} else if (nextCost < nextNode->pathCost) [[likely]] { } else if (nextCost < nextNode->pathCost) [[likely]] {
@ -331,7 +331,7 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
} }
if constexpr (enableDebug) { if constexpr (enableDebug) {
AStarVirtual<T, enableDebug>::_debugOpenNode(nextNode); AStarVirtual<CoordinateType, enableDebug>::_debugOpenNode(nextNode);
} }
} }
} }
@ -355,11 +355,11 @@ class AStarFast final : public AStarVirtual<T, enableDebug> {
return path; return path;
} }
void setObstacle(const std::function<bool(U)>& isObstacleFunction) noexcept { _isObstacleFunction = isObstacleFunction; } void setObstacle(const std::function<bool(MapElementType)>& isObstacleFunction) noexcept { _isObstacleFunction = isObstacleFunction; }
std::function<bool(U)>& getObstacle() noexcept { return _isObstacleFunction; } std::function<bool(MapElementType)>& getObstacle() noexcept { return _isObstacleFunction; }
private: private:
std::function<bool(U)> _isObstacleFunction; std::function<bool(MapElementType)> _isObstacleFunction;
}; };
} // namespace AStar } // namespace AStar

View File

@ -18,7 +18,7 @@ static void DoSetup([[maybe_unused]] const benchmark::State& state) {}
static void DoTeardown([[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) { static void astar_bench(benchmark::State& state) {
auto range = state.range(0); auto range = state.range(0);
@ -47,7 +47,7 @@ static void astar_bench(benchmark::State& state) {
std::vector<uint8_t> blocks = std::vector<uint8_t>(mapWidth * mapHeight, 0); std::vector<uint8_t> blocks = std::vector<uint8_t>(mapWidth * mapHeight, 0);
benchmark::DoNotOptimize(blocks); benchmark::DoNotOptimize(blocks);
AStar::AStar<T, false> pathFinder; AStar::AStar<CoordinateType, false> pathFinder;
benchmark::DoNotOptimize(pathFinder); benchmark::DoNotOptimize(pathFinder);
pathFinder.setWorldSize({mapWidth, mapHeight}); pathFinder.setWorldSize({mapWidth, mapHeight});
pathFinder.setHeuristic(AStar::Heuristic::euclidean); pathFinder.setHeuristic(AStar::Heuristic::euclidean);
@ -104,7 +104,7 @@ BENCHMARK(astar_bench<uint32_t>)
->UseRealTime() ->UseRealTime()
->Repetitions(repetitions); ->Repetitions(repetitions);
template <IntegerType T> template <IntegerType CoordinateType>
static void astar_bench_fast(benchmark::State& state) { static void astar_bench_fast(benchmark::State& state) {
auto range = state.range(0); auto range = state.range(0);
@ -133,7 +133,7 @@ static void astar_bench_fast(benchmark::State& state) {
std::vector<uint32_t> blocks = std::vector<uint32_t>(mapWidth * mapHeight, 0); std::vector<uint32_t> blocks = std::vector<uint32_t>(mapWidth * mapHeight, 0);
benchmark::DoNotOptimize(blocks); benchmark::DoNotOptimize(blocks);
AStar::AStarFast<T, false, uint32_t> pathFinder; AStar::AStarFast<CoordinateType, false, uint32_t> pathFinder;
benchmark::DoNotOptimize(pathFinder); benchmark::DoNotOptimize(pathFinder);
pathFinder.setHeuristic(AStar::Heuristic::euclidean); pathFinder.setHeuristic(AStar::Heuristic::euclidean);
pathFinder.setDiagonalMovement(true); pathFinder.setDiagonalMovement(true);