Compare commits

..

8 Commits

Author SHA1 Message Date
244d01c313 Merge pull request #27 from orange-cpp/u/orange-cpp/unity-build-support
added unity build support
2025-02-22 23:33:03 +03:00
e31ffac103 added unity build support 2025-02-22 23:32:29 +03:00
ae87257adf Merge pull request #26 from orange-cpp/u/orange-cpp/improved-astar
Improved A*
2025-02-22 23:06:33 +03:00
906f5099d1 improvement 2025-02-16 10:57:03 +03:00
96e4e1c9d6 added new method added concept for mat type 2025-02-16 10:06:04 +03:00
872dbe146f Merge pull request #25 from orange-cpp/u/orange-cpp/hotfix
improved cmake+hotfix of clang support
2025-01-17 16:59:30 +03:00
e0dcb65e3f improved cmake+hotfix of clang support 2025-01-17 16:56:47 +03:00
d0c532df39 hot fix 2025-01-06 05:36:27 +03:00
9 changed files with 113 additions and 31 deletions

View File

@@ -4,15 +4,12 @@ project(omath VERSION 1.0.1)
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
set(CMAKE_CXX_STANDARD 26)
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)
option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF) option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
if (OMATH_BUILD_AS_SHARED_LIBRARY) if (OMATH_BUILD_AS_SHARED_LIBRARY)
add_library(omath SHARED source/Vector3.cpp) add_library(omath SHARED source/Vector3.cpp)
else() else()
@@ -22,6 +19,17 @@ else()
include/omath/engines/OpenGL/Camera.hpp) include/omath/engines/OpenGL/Camera.hpp)
endif() 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) target_compile_definitions(omath PUBLIC OMATH_EXPORT)
add_subdirectory(source) add_subdirectory(source)

View File

@@ -22,6 +22,13 @@ namespace omath
COLUMN_MAJOR COLUMN_MAJOR
}; };
template<typename M1, typename M2>
concept MatTemplateEqual =
(M1::rows == M2::rows) && (M1::columns == M2::columns) &&
std::is_same_v<typename M1::value_type, typename M2::value_type> &&
(M1::store_type == M2::store_type);
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR> template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
requires std::is_arithmetic_v<Type> requires std::is_arithmetic_v<Type>
class Mat final class Mat final

View File

@@ -9,6 +9,7 @@
#include "omath/Vector2.hpp" #include "omath/Vector2.hpp"
#include "omath/Angle.hpp" #include "omath/Angle.hpp"
#include <expected> #include <expected>
#include <immintrin.h>
namespace omath namespace omath
@@ -228,6 +229,14 @@ namespace omath
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::FromRadians(std::acos(Dot(other) / bottom)); return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::FromRadians(std::acos(Dot(other) / bottom));
} }
[[nodiscard]] bool IsPerpendicular(const Vector3& other) const
{
if (const auto angle = AngleBetween(other))
return angle->AsDegrees() == 90.f;
return false;
}
[[nodiscard]] constexpr float Sum2D() const [[nodiscard]] constexpr float Sum2D() const
{ {
return Vector2::Sum(); return Vector2::Sum();
@@ -235,7 +244,7 @@ namespace omath
[[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const; [[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const;
[[nodiscard]] std::tuple<float, float, float> AsTuple() const [[nodiscard]] constexpr std::tuple<float, float, float> AsTuple() const
{ {
return std::make_tuple(x, y, z); return std::make_tuple(x, y, z);
} }

View File

@@ -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);
}; };
} }

View File

@@ -24,7 +24,7 @@ namespace omath::projection
return m_width / m_height; return m_width / m_height;
} }
}; };
using FieldOfView = const Angle<float, 0.f, 180.f, AngleFlags::Clamped>; using FieldOfView = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
template<class Mat4x4Type, class ViewAnglesType> template<class Mat4x4Type, class ViewAnglesType>
class Camera class Camera

View File

@@ -9,7 +9,7 @@ namespace omath::opengl
{ {
Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
const Angle<float, 0, 180, AngleFlags::Clamped>& fov, const float near, const float far) : const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far) projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
{ {
} }

View File

@@ -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::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> 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 aCost = a.second.gCost + a.first.DistTo(endVertex);
const auto bCost = b.second.gCost + b.first.DistTo(endVertex);
return aCost < bCost;
});
closedList.emplace(perfectVertex); const auto current = currentIt->first;
openList.erase(perfectVertex.first); const auto currentNode = currentIt->second;
for (const auto& neighbor : navMesh.GetNeighbors(perfectVertex.first)) if (current == endVertex)
if (!closedList.contains(neighbor)) return ReconstructFinalPath(closedList, current);
openList.emplace(neighbor, PathNode{perfectVertex.first, neighbor.DistTo(perfectVertex.first) + perfectVertex.second.gCost});
if (perfectVertex.first != endVertex) closedList.emplace(current, currentNode);
continue; openList.erase(currentIt);
std::vector<Vector3> path = {}; for (const auto& neighbor: navMesh.GetNeighbors(current))
{
if (closedList.contains(neighbor))
continue;
for (std::optional current = perfectVertex.first; current; current = closedList.at(*current).cameFrom ) const float tentativeGCost = currentNode.gCost + neighbor.DistTo(current);
path.push_back(current.value());
return path; const auto openIt = openList.find(neighbor);
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
openList[neighbor] = PathNode{current, tentativeGCost};
}
} }
return {}; return {};
} }
} } // namespace omath::pathfinding

View File

@@ -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)

View File

@@ -395,6 +395,12 @@ TEST_F(UnitTestVector3, AngleBeatween)
EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).AngleBetween({0.0f, 0.0f, 1.0f}).has_value()); EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).AngleBetween({0.0f, 0.0f, 1.0f}).has_value());
} }
TEST_F(UnitTestVector3, IsPerpendicular)
{
EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).IsPerpendicular({1, 0 ,0}), true);
EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).IsPerpendicular({0.0f, 0.0f, 1.0f}), false);
EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).IsPerpendicular({0.0f, 0.0f, 1.0f}));
}
// Static assertions (compile-time checks) // Static assertions (compile-time checks)
static_assert(Vector3(1.0f, 2.0f, 3.0f).LengthSqr() == 14.0f, "LengthSqr should be 14"); static_assert(Vector3(1.0f, 2.0f, 3.0f).LengthSqr() == 14.0f, "LengthSqr should be 14");