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)
|
||||
|
||||
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_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
||||
@@ -22,8 +20,14 @@ else()
|
||||
endif()
|
||||
|
||||
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_REQUIRED ON)
|
||||
|
||||
|
||||
target_compile_features(omath PUBLIC cxx_std_23)
|
||||
|
||||
target_compile_definitions(omath PUBLIC OMATH_EXPORT)
|
||||
|
||||
@@ -9,11 +9,17 @@
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct PathNode;
|
||||
class Astar final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
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 <algorithm>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
@@ -18,45 +18,83 @@ namespace omath::pathfinding
|
||||
};
|
||||
|
||||
|
||||
std::vector<Vector3> Astar::FindPath(const Vector3 &start, const Vector3 &end, const NavigationMesh &navMesh)
|
||||
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::unordered_map<Vector3, PathNode> closedList;
|
||||
std::unordered_map<Vector3, PathNode> openList;
|
||||
|
||||
const auto startVertex = navMesh.GetClosestVertex(start).value();
|
||||
const auto endVertex = navMesh.GetClosestVertex(end).value();
|
||||
auto maybeStartVertex = navMesh.GetClosestVertex(start);
|
||||
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});
|
||||
|
||||
while (!openList.empty())
|
||||
{
|
||||
const auto perfectVertex = *std::ranges::min_element(openList,
|
||||
[&endVertex](const auto& a, const auto& b) -> bool
|
||||
auto currentIt = GetPerfectNode(openList, endVertex);
|
||||
|
||||
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);
|
||||
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)
|
||||
if (closedList.contains(neighbor))
|
||||
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 )
|
||||
path.push_back(current.value());
|
||||
const auto openIt = openList.find(neighbor);
|
||||
|
||||
return path;
|
||||
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
|
||||
openList[neighbor] = PathNode{current, tentativeGCost};
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
enable_testing()
|
||||
|
||||
project(unit-tests)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
include(GoogleTest)
|
||||
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)
|
||||
|
||||
gtest_discover_tests(unit-tests)
|
||||
Reference in New Issue
Block a user