Add files

Signed-off-by: Bensuperpc <bensuperpc@gmail.com>
This commit is contained in:
2024-01-29 21:08:29 +01:00
commit 7d81e22900
68 changed files with 4264 additions and 0 deletions

27
example/CMakeLists.txt Normal file
View File

@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.14)
project(astarExamples CXX)
include(../cmake/project-is-top-level.cmake)
include(../cmake/folders.cmake)
if(PROJECT_IS_TOP_LEVEL)
find_package(astar REQUIRED)
endif()
add_custom_target(run-examples)
function(add_example NAME)
add_executable("${NAME}" "${NAME}.cpp")
target_link_libraries("${NAME}" PRIVATE astar::astar)
target_compile_features("${NAME}" PRIVATE cxx_std_20)
add_custom_target("run_${NAME}" COMMAND "${NAME}" VERBATIM)
add_dependencies("run_${NAME}" "${NAME}")
add_dependencies(run-examples "run_${NAME}")
endfunction()
add_example(basic_example)
add_example(debug_example)
add_example(basic_fast_example)
add_folders(Example)

31
example/basic_example.cpp Normal file
View File

@ -0,0 +1,31 @@
#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), it it equal to 0, then the path is not found
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;
}

36
example/basic_fast_example.cpp Executable file
View File

@ -0,0 +1,36 @@
#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;
}

43
example/debug_example.cpp Normal file
View File

@ -0,0 +1,43 @@
#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;
}