mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-14 15:33:26 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 244d01c313 | |||
| e31ffac103 | |||
| ae87257adf | |||
| 906f5099d1 |
@@ -4,8 +4,6 @@ project(omath VERSION 1.0.1)
|
|||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
|
|
||||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
|
||||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
|
||||||
|
|
||||||
option(OMATH_BUILD_TESTS "Build unit tests" ON)
|
option(OMATH_BUILD_TESTS "Build unit tests" ON)
|
||||||
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
||||||
@@ -22,8 +20,14 @@ else()
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
set_target_properties(omath PROPERTIES
|
set_target_properties(omath PROPERTIES
|
||||||
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
UNITY_BUILD ON
|
||||||
|
UNITY_BUILD_BATCH_SIZE 20
|
||||||
CXX_STANDARD 23
|
CXX_STANDARD 23
|
||||||
CXX_STANDARD_REQUIRED ON)
|
CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
|
||||||
target_compile_features(omath PUBLIC cxx_std_23)
|
target_compile_features(omath PUBLIC cxx_std_23)
|
||||||
|
|
||||||
target_compile_definitions(omath PUBLIC OMATH_EXPORT)
|
target_compile_definitions(omath PUBLIC OMATH_EXPORT)
|
||||||
|
|||||||
@@ -9,11 +9,17 @@
|
|||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
{
|
{
|
||||||
|
struct PathNode;
|
||||||
class Astar final
|
class Astar final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
||||||
|
private:
|
||||||
|
[[nodiscard]]
|
||||||
|
static std::vector<Vector3> ReconstructFinalPath(const std::unordered_map<Vector3, PathNode>& closedList, const Vector3& current);
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
static auto GetPerfectNode(const std::unordered_map<Vector3, PathNode>& openList, const Vector3& endVertex);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,10 @@
|
|||||||
//
|
//
|
||||||
#include "omath/pathfinding/Astar.hpp"
|
#include "omath/pathfinding/Astar.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
@@ -18,45 +18,83 @@ namespace omath::pathfinding
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<Vector3> Astar::ReconstructFinalPath(const std::unordered_map<Vector3, PathNode>& closedList,
|
||||||
|
const Vector3& current)
|
||||||
|
{
|
||||||
|
std::vector<Vector3> path;
|
||||||
|
std::optional currentOpt = current;
|
||||||
|
|
||||||
|
while (currentOpt)
|
||||||
|
{
|
||||||
|
path.push_back(*currentOpt);
|
||||||
|
|
||||||
|
auto it = closedList.find(*currentOpt);
|
||||||
|
|
||||||
|
if (it == closedList.end())
|
||||||
|
break;
|
||||||
|
|
||||||
|
currentOpt = it->second.cameFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ranges::reverse(path);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
auto Astar::GetPerfectNode(const std::unordered_map<Vector3, PathNode>& openList, const Vector3& endVertex)
|
||||||
|
{
|
||||||
|
return std::ranges::min_element(openList,
|
||||||
|
[&endVertex](const auto& a, const auto& b)
|
||||||
|
{
|
||||||
|
const float fA = a.second.gCost + a.first.DistTo(endVertex);
|
||||||
|
const float fB = b.second.gCost + b.first.DistTo(endVertex);
|
||||||
|
return fA < fB;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Vector3> Astar::FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh)
|
std::vector<Vector3> Astar::FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh)
|
||||||
{
|
{
|
||||||
std::unordered_map<Vector3, PathNode> closedList;
|
std::unordered_map<Vector3, PathNode> closedList;
|
||||||
std::unordered_map<Vector3, PathNode> openList;
|
std::unordered_map<Vector3, PathNode> openList;
|
||||||
|
|
||||||
const auto startVertex = navMesh.GetClosestVertex(start).value();
|
auto maybeStartVertex = navMesh.GetClosestVertex(start);
|
||||||
const auto endVertex = navMesh.GetClosestVertex(end).value();
|
auto maybeEndVertex = navMesh.GetClosestVertex(end);
|
||||||
|
|
||||||
|
if (!maybeStartVertex || !maybeEndVertex)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const auto startVertex = maybeStartVertex.value();
|
||||||
|
const auto endVertex = maybeEndVertex.value();
|
||||||
|
|
||||||
|
|
||||||
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
||||||
|
|
||||||
while (!openList.empty())
|
while (!openList.empty())
|
||||||
{
|
{
|
||||||
const auto perfectVertex = *std::ranges::min_element(openList,
|
auto currentIt = GetPerfectNode(openList, endVertex);
|
||||||
[&endVertex](const auto& a, const auto& b) -> bool
|
|
||||||
|
const auto current = currentIt->first;
|
||||||
|
const auto currentNode = currentIt->second;
|
||||||
|
|
||||||
|
if (current == endVertex)
|
||||||
|
return ReconstructFinalPath(closedList, current);
|
||||||
|
|
||||||
|
|
||||||
|
closedList.emplace(current, currentNode);
|
||||||
|
openList.erase(currentIt);
|
||||||
|
|
||||||
|
for (const auto& neighbor: navMesh.GetNeighbors(current))
|
||||||
{
|
{
|
||||||
const auto aCost = a.second.gCost + a.first.DistTo(endVertex);
|
if (closedList.contains(neighbor))
|
||||||
const auto bCost = b.second.gCost + b.first.DistTo(endVertex);
|
|
||||||
return aCost < bCost;
|
|
||||||
});
|
|
||||||
|
|
||||||
closedList.emplace(perfectVertex);
|
|
||||||
openList.erase(perfectVertex.first);
|
|
||||||
|
|
||||||
for (const auto& neighbor : navMesh.GetNeighbors(perfectVertex.first))
|
|
||||||
if (!closedList.contains(neighbor))
|
|
||||||
openList.emplace(neighbor, PathNode{perfectVertex.first, neighbor.DistTo(perfectVertex.first) + perfectVertex.second.gCost});
|
|
||||||
|
|
||||||
|
|
||||||
if (perfectVertex.first != endVertex)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
std::vector<Vector3> path = {};
|
const float tentativeGCost = currentNode.gCost + neighbor.DistTo(current);
|
||||||
|
|
||||||
for (std::optional current = perfectVertex.first; current; current = closedList.at(*current).cameFrom )
|
const auto openIt = openList.find(neighbor);
|
||||||
path.push_back(current.value());
|
|
||||||
|
|
||||||
return path;
|
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
|
||||||
|
openList[neighbor] = PathNode{current, tentativeGCost};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
} // namespace omath::pathfinding
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
project(unit-tests)
|
project(unit-tests)
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
|
||||||
|
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
add_executable(unit-tests
|
add_executable(unit-tests
|
||||||
@@ -26,6 +25,15 @@ add_executable(unit-tests
|
|||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set_target_properties(unit-tests PROPERTIES
|
||||||
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
UNITY_BUILD ON
|
||||||
|
UNITY_BUILD_BATCH_SIZE 20
|
||||||
|
CXX_STANDARD 23
|
||||||
|
CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
|
||||||
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
||||||
|
|
||||||
gtest_discover_tests(unit-tests)
|
gtest_discover_tests(unit-tests)
|
||||||
Reference in New Issue
Block a user