From a33ee638b9715d606594ebbce93402a223bfac06 Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 30 Nov 2024 03:37:25 +0300 Subject: [PATCH] added more unit tests --- include/omath/Angles.hpp | 35 +++++++++++++++--- include/omath/collision/LineTracer.hpp | 1 + include/omath/engines/opengl.hpp | 8 ++--- include/omath/pathfinding/NavigationMesh.hpp | 6 ++++ tests/CMakeLists.txt | 2 ++ tests/engines/UnitTestOpenGL.cpp | 4 +-- tests/general/UnitTestAngles.cpp | 38 ++++++++++++++++++++ 7 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 tests/general/UnitTestAngles.cpp diff --git a/include/omath/Angles.hpp b/include/omath/Angles.hpp index 9e557fd..0dc6d61 100644 --- a/include/omath/Angles.hpp +++ b/include/omath/Angles.hpp @@ -7,12 +7,39 @@ namespace omath::angles { - [[nodiscard]] constexpr float RadiansToDegrees(const float radiands) + template + requires std::is_floating_point_v + [[nodiscard]] constexpr float RadiansToDegrees(const type& radians) { - return radiands * (180.f / std::numbers::pi_v); + return radians * (type(180) / std::numbers::pi_v); } - [[nodiscard]] constexpr float DegreesToRadians(const float degrees) + + template + requires std::is_floating_point_v + [[nodiscard]] constexpr float DegreesToRadians(const type& degrees) { - return degrees * (std::numbers::pi_v / 180.f); + return degrees * (std::numbers::pi_v / type(180)); + } + + template + requires std::is_floating_point_v + [[nodiscard]] type HorizontalFovToVertical(const type& horFov, const type& aspect) + { + const auto fovRad = DegreesToRadians(horFov); + + const auto vertFov = type(2) * std::atan(std::tan(fovRad / type(2)) / aspect); + + return RadiansToDegrees(vertFov); + } + + template + requires std::is_floating_point_v + [[nodiscard]] type VerticalFovToHorizontal(const type& vertFov, const type& aspect) + { + const auto fovRad = DegreesToRadians(vertFov); + + const auto horFov = type(2) * std::atan(std::tan(fovRad / type(2)) * aspect); + + return RadiansToDegrees(horFov); } } diff --git a/include/omath/collision/LineTracer.hpp b/include/omath/collision/LineTracer.hpp index 1de6d1e..fc1d7fa 100644 --- a/include/omath/collision/LineTracer.hpp +++ b/include/omath/collision/LineTracer.hpp @@ -26,6 +26,7 @@ namespace omath::collision public: LineTracer() = delete; + [[nodiscard]] static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle); diff --git a/include/omath/engines/opengl.hpp b/include/omath/engines/opengl.hpp index 6bed17e..bcda994 100644 --- a/include/omath/engines/opengl.hpp +++ b/include/omath/engines/opengl.hpp @@ -22,10 +22,10 @@ namespace omath::opengl { return { - {right.x, up.x, -forward.x, 0}, - {right.y, up.y, -forward.y, 0}, - {right.z, up.z, -forward.z, 0}, - {-cam_origin.x, -cam_origin.y, -cam_origin.z, 1}, + {right.x, up.x, -forward.x, 0}, + {right.y, up.y, -forward.y, 0}, + {right.z, up.z, -forward.z, 0}, + {-cam_origin.x, -cam_origin.y, -cam_origin.z, 1}, }; } diff --git a/include/omath/pathfinding/NavigationMesh.hpp b/include/omath/pathfinding/NavigationMesh.hpp index e7aceb4..525cbc4 100644 --- a/include/omath/pathfinding/NavigationMesh.hpp +++ b/include/omath/pathfinding/NavigationMesh.hpp @@ -12,6 +12,12 @@ namespace omath::pathfinding { + + enum Error + { + + }; + class NavigationMesh final { public: diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6d8d48e..109cdc7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,10 +15,12 @@ add_executable(unit-tests general/UnitTestColor.cpp general/UnitTestVector4.cpp general/UnitTestLineTrace.cpp + general/UnitTestAngles.cpp engines/UnitTestOpenGL.cpp engines/UnitTestUnityEngine.cpp engines/UnitTestSourceEngine.cpp + ) target_link_libraries(unit-tests PRIVATE gtest gtest_main omath glm) diff --git a/tests/engines/UnitTestOpenGL.cpp b/tests/engines/UnitTestOpenGL.cpp index 0c44e4a..2cb6889 100644 --- a/tests/engines/UnitTestOpenGL.cpp +++ b/tests/engines/UnitTestOpenGL.cpp @@ -5,8 +5,8 @@ #include #include #include -#include -#include +#include +#include #include #include "glm/ext/matrix_clip_space.hpp" diff --git a/tests/general/UnitTestAngles.cpp b/tests/general/UnitTestAngles.cpp new file mode 100644 index 0000000..324ce4d --- /dev/null +++ b/tests/general/UnitTestAngles.cpp @@ -0,0 +1,38 @@ +// +// Created by Orange on 11/30/2024. +// +#include +#include + + +TEST(UnitTestAngles, RadiansToDeg) +{ + constexpr float rad = 67; + + EXPECT_NEAR(omath::angles::RadiansToDegrees(rad), 3838.82f, 0.01f); +} + +TEST(UnitTestAngles, DegreesToRadians) +{ + constexpr float degree = 90; + + EXPECT_NEAR(omath::angles::DegreesToRadians(degree), 1.5708f, 0.01f); +} + +TEST(UnitTestAngles, HorizontalFovToVerical) +{ + constexpr float hFov = 90; + constexpr float aspectRation = 16.0f / 9.0f; + const auto verticalFov = omath::angles::HorizontalFovToVertical(hFov, aspectRation); + + EXPECT_NEAR(verticalFov, 58.71f, 0.01f); +} + +TEST(UnitTestAngles, VerticalToHorizontal) +{ + constexpr float vFov = 58.71; + constexpr float aspectRation = 16.0f / 9.0f; + const auto horizontalFov = omath::angles::VerticalFovToHorizontal(vFov, aspectRation); + + EXPECT_NEAR(horizontalFov, 89.99f, 0.01f); +} \ No newline at end of file