mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
improved code added unit test
This commit is contained in:
@@ -24,10 +24,14 @@ namespace omath::pathfinding
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::expected<const NavigationVertex, std::string> GetClossestVertex(const Vector3& point) const;
|
std::expected<Vector3, std::string> GetClossestVertex(const Vector3& point) const;
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const std::vector<Vector3>& GetNeighbors(const Vector3& vertex) const;
|
||||||
|
|
||||||
|
std::list<NavigationVertex> m_vertexes;
|
||||||
|
|
||||||
|
std::unordered_map<Vector3, std::vector<Vector3>> m_verTextMap;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
// Created by Vlad on 28.07.2024.
|
// Created by Vlad on 28.07.2024.
|
||||||
//
|
//
|
||||||
#include "omath/pathfinding/Astar.h"
|
#include "omath/pathfinding/Astar.h"
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
@@ -10,8 +12,7 @@ namespace omath::pathfinding
|
|||||||
{
|
{
|
||||||
struct PathNode final
|
struct PathNode final
|
||||||
{
|
{
|
||||||
Vector3 cameFrom;
|
std::optional<Vector3> cameFrom;
|
||||||
NavigationVertex const* navVertex;
|
|
||||||
float gCost = 0.f;
|
float gCost = 0.f;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -21,26 +22,38 @@ namespace omath::pathfinding
|
|||||||
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.GetClossestVertex(start).value();
|
const auto startVertex = navMesh.GetClossestVertex(start).value();
|
||||||
const auto& endVertex = navMesh.GetClossestVertex(end).value();
|
const auto endVertex = navMesh.GetClossestVertex(end).value();
|
||||||
|
|
||||||
openList.emplace(startVertex.origin, PathNode{startVertex.origin, &startVertex, 0.f});
|
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
||||||
|
|
||||||
while (!openList.empty())
|
while (!openList.empty())
|
||||||
{
|
{
|
||||||
const auto [cord, node] = *std::ranges::min_element(openList,
|
const auto perfectVertex = *std::ranges::min_element(openList,
|
||||||
[&endVertex](const auto& a, const auto& b) -> bool
|
[&endVertex](const auto& a, const auto& b) -> bool
|
||||||
{
|
{
|
||||||
const auto aCost = a.second.gCost + a.second.navVertex->origin.DistTo(endVertex.origin);
|
const auto aCost = a.second.gCost + a.first.DistTo(endVertex);
|
||||||
const auto bCost = b.second.gCost + b.second.navVertex->origin.DistTo(endVertex.origin);
|
const auto bCost = b.second.gCost + b.first.DistTo(endVertex);
|
||||||
return aCost < bCost;
|
return aCost < bCost;
|
||||||
});
|
});
|
||||||
|
|
||||||
openList.erase(cord);
|
closedList.emplace(perfectVertex);
|
||||||
|
openList.erase(perfectVertex.first);
|
||||||
|
|
||||||
for (const auto& neighbor : node.navVertex->connections)
|
for (const auto& neighbor : navMesh.GetNeighbors(perfectVertex.first))
|
||||||
if (!closedList.contains(neighbor->origin))
|
if (!closedList.contains(neighbor))
|
||||||
closedList.emplace(neighbor->origin, PathNode{cord, neighbor, neighbor->origin.DistTo(cord) + node.gCost});
|
openList.emplace(neighbor, PathNode{perfectVertex.first, neighbor.DistTo(perfectVertex.first) + perfectVertex.second.gCost});
|
||||||
|
|
||||||
|
|
||||||
|
if (perfectVertex.first != endVertex)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::vector<Vector3> path = {};
|
||||||
|
|
||||||
|
for (std::optional current = perfectVertex.first; current; current = closedList.at(*current).cameFrom )
|
||||||
|
path.push_back(current.value());
|
||||||
|
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
|||||||
@@ -2,3 +2,25 @@
|
|||||||
// Created by Vlad on 28.07.2024.
|
// Created by Vlad on 28.07.2024.
|
||||||
//
|
//
|
||||||
#include "omath/pathfinding/NavigationMesh.h"
|
#include "omath/pathfinding/NavigationMesh.h"
|
||||||
|
|
||||||
|
namespace omath::pathfinding
|
||||||
|
{
|
||||||
|
std::expected<Vector3, std::string> NavigationMesh::GetClossestVertex(const Vector3 &point) const
|
||||||
|
{
|
||||||
|
const auto res = std::ranges::min_element(m_verTextMap,
|
||||||
|
[&point](const auto& a, const auto& b)
|
||||||
|
{
|
||||||
|
return a.first.DistTo(point) < b.first.DistTo(point);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res == m_verTextMap.cend())
|
||||||
|
return std::unexpected("Failed to get clossest point");
|
||||||
|
|
||||||
|
return res->first;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Vector3>& NavigationMesh::GetNeighbors(const Vector3 &vertex) const
|
||||||
|
{
|
||||||
|
return m_verTextMap.at(vertex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
|||||||
file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||||
|
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
add_executable(unit-tests UnitTestPrediction.cpp UnitTestMatrix.cpp)
|
add_executable(unit-tests UnitTestPrediction.cpp UnitTestMatrix.cpp UnitTestAstar.cpp)
|
||||||
|
|
||||||
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
||||||
|
|
||||||
|
|||||||
17
tests/UnitTestAstar.cpp
Normal file
17
tests/UnitTestAstar.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 18.08.2024.
|
||||||
|
//
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <omath/pathfinding/Astar.h>
|
||||||
|
|
||||||
|
|
||||||
|
TEST(UnitTestAstar, FindingRightPath)
|
||||||
|
{
|
||||||
|
omath::pathfinding::NavigationMesh mesh;
|
||||||
|
|
||||||
|
mesh.m_verTextMap[{0.f, 0.f, 0.f}] = {{0.f, 1.f, 0.f}};
|
||||||
|
mesh.m_verTextMap[{0.f, 1.f, 0.f}] = {{0.f, 2.f, 0.f}};
|
||||||
|
mesh.m_verTextMap[{0.f, 2.f, 0.f}] = {{0.f, 3.f, 0.f}};
|
||||||
|
mesh.m_verTextMap[{0.f, 3.f, 0.f}] = {};
|
||||||
|
omath::pathfinding::Astar::FindPath({}, {0.f, 3.f, 0.f}, mesh);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user