Fast and easy to use header only 2D astar algorithm library in C++20
Go to file
Bensuperpc 9a3c3cdd04
Update example, header and screenshot
Signed-off-by: Bensuperpc <bensuperpc@gmail.com>
2024-02-13 00:08:10 +01:00
.github/workflows Add files 2024-01-29 21:08:29 +01:00
cmake Update example, header and screenshot 2024-02-13 00:08:10 +01:00
docs Add files 2024-01-29 21:08:29 +01:00
example Add files 2024-01-29 21:08:29 +01:00
include/astar Update example, header and screenshot 2024-02-13 00:08:10 +01:00
resources Update example, header and screenshot 2024-02-13 00:08:10 +01:00
test Update example, header and screenshot 2024-02-13 00:08:10 +01:00
tools Add files 2024-01-29 21:08:29 +01:00
.clang-format Add files 2024-01-29 21:08:29 +01:00
.clang-tidy Add files 2024-01-29 21:08:29 +01:00
.codespellrc Add files 2024-01-29 21:08:29 +01:00
.gitignore Add files 2024-01-29 21:08:29 +01:00
BUILDING.md Add files 2024-01-29 21:08:29 +01:00
CMakeLists.txt Add files 2024-01-29 21:08:29 +01:00
CMakePresets.json Add files 2024-01-29 21:08:29 +01:00
CMakeUserPresets.json Add files 2024-01-29 21:08:29 +01:00
CODE_OF_CONDUCT.md Add files 2024-01-29 21:08:29 +01:00
codespell.ignore-words.txt Add files 2024-01-29 21:08:29 +01:00
CONTRIBUTING.md Add files 2024-01-29 21:08:29 +01:00
HACKING.md Add files 2024-01-29 21:08:29 +01:00
LICENSE Add files 2024-01-29 21:08:29 +01:00
Makefile Add files 2024-01-29 21:08:29 +01:00
README.md Update example, header and screenshot 2024-02-13 00:08:10 +01:00

astar

Fast and easy to use 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).

How does it work

It is an astar algorithm, the main idea is to find the shortest path between two points in a grid/map.

Screenshots

astar

astar

Features

  • Header-only library C++20
  • Support 2D map
  • Support 3D map
  • Configurable heuristic function and movement cost
  • Configurable (diagonal and more) movement
  • Debug mode in template argument and lambda function
  • Support direct access and not access to the map
  • Unit tests and benchmarks

How to use it

This project is a header-only library and easy to use, just copy the include/astar folder in your project and include the astar/astar.hpp header or via CMake FetchContent_Declare.

Now you can use the Astar::Astar class to find the shortest path between two points in a grid.

#include <astar/astar.hpp>
#include <iostream>

auto main() -> int {
    // Create the template class with optional a type (e.g. uint32_t) and a boolean 
    // if you want enable debug mode (AStar::AStar<uint32_t, true>)
    AStar::AStar pathFinder;

    // Define the map size (width, height)
    pathFinder.setWorldSize({10, 10});

    // Set the heuristic function (manhattan, euclidean, octagonal etc...), it is optional, default is euclidean
    pathFinder.setHeuristic(AStar::Heuristic::manhattan);

    // if you want to enable diagonal movement, it is optional, default is false
    pathFinder.setDiagonalMovement(true);

    // Add a obstacle point (5, 5) and (5, 6)
    pathFinder.addObstacle({5, 5});
    pathFinder.addObstacle({5, 6});

    // Find the path from (0, 0) to (9, 9)
    auto path = pathFinder.findPath({0, 0}, {9, 9});

    // Print the path
    for (auto& p : path) {
        std::cout << p.x << " " << p.y << std::endl;
    }

    return 0;
}

Alternative version (direct access to the map)

You can use the alternative version of the library if you want astar have direct access to the map, this version is faster than the non-direct access version.

#include <astar/astar.hpp>
#include <iostream>

auto main() -> int {
    // Create the template class with optional a type (e.g. uint32_t) and a boolean
    // if you want enable debug mode (AStar::AStar<uint32_t, true>)
    AStar::AStarFast pathFinder;

    // Set the heuristic function (manhattan, euclidean, octagonal etc...), it is optional, default is euclidean
    pathFinder.setHeuristic(AStar::Heuristic::manhattan);

    // if you want to enable diagonal movement, it is optional, default is false
    pathFinder.setDiagonalMovement(true);

    // Create world 9x9 filled with 0
    std::vector<uint32_t> world(9 * 9, 0);

    // set lambda function to check if is an obstacle (value == 1)
    auto isObstacle = [](uint32_t value) -> bool { return value == 1; };
    pathFinder.setObstacle(isObstacle);

    // Add a obstacle point (5, 5) and (5, 6)
    world[5 + 5 * 9] = 1;
    world[5 + 6 * 9] = 1;

    // Find the path from (0, 0) to (9, 9), it it equal to 0, then the path is not found
    // This version of findPath() is faster due direct access to the world
    auto path = pathFinder.findPath({0, 0}, {9, 9}, world, {9, 9});

    // Print the path
    for (auto& p : path) {
        std::cout << p.x << " " << p.y << std::endl;
    }

    return 0;
}

Debug mode

You can enable the debug mode to call a lambda function when new node is visiting by the algorithm and when new node is added to the open list.

#include <iostream>

#include <astar/astar.hpp>

auto main() -> int {
    // Enable debug mode with template argument, this helps avoid performance issues on non-debug classes
    AStar::AStar<uint32_t, true> pathFinder;

    // Set lambda function to debug current node
    std::function<void(const AStar::Node<uint32_t>* node)> debugCurrentNode = [](const AStar::Node<uint32_t>* node) {
        std::cout << "Current node: " << node->pos.x << ", " << node->pos.y << std::endl;
    };
    pathFinder.setDebugCurrentNode(debugCurrentNode);

    // Set lambda function to debug open node
    std::function<void(const AStar::Node<uint32_t>* node)> debugOpenNode = [](const AStar::Node<uint32_t>* node) {
        std::cout << "Add to open list: " << node->pos.x << ", " << node->pos.y << std::endl;
    };
    pathFinder.setDebugOpenNode(debugOpenNode);

    // Define the map size (width, height)
    pathFinder.setWorldSize({10, 10});

    // Set the heuristic function (manhattan, euclidean, octagonal etc...), it is optional, default is euclidean
    pathFinder.setHeuristic(AStar::Heuristic::manhattan);

    // if you want to enable diagonal movement, it is optional, default is false
    pathFinder.setDiagonalMovement(true);

    // Add a obstacle point (5, 5) and (5, 6)
    pathFinder.addObstacle({5, 5});
    pathFinder.addObstacle({5, 6});

    // Find the path from (0, 0) to (9, 9)
    auto path = pathFinder.findPath({0, 0}, {9, 9});

    // Print the path
    for (auto& p : path) {
        std::cout << p.x << " " << p.y << std::endl;
    }

    return 0;
}

Building and installing

See the BUILDING document.

Contributing

See the CONTRIBUTING document.

Sources, references and ideas

You can find here the sources, references, libs and ideas that I have used to make this library.

Astar

Sources and references that I have used to make this library.

Bench others astar implementations

The list of others astar implementations that I have benchmarked to compare the performance of my implementation.

Libraries

Libraries used in this project.

Others

Licensing

LICENSE