Skip to content

Commit a33ee63

Browse files
committed
added more unit tests
1 parent 6a9a51b commit a33ee63

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

include/omath/Angles.hpp

+31-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,39 @@
77

88
namespace omath::angles
99
{
10-
[[nodiscard]] constexpr float RadiansToDegrees(const float radiands)
10+
template<class type>
11+
requires std::is_floating_point_v<type>
12+
[[nodiscard]] constexpr float RadiansToDegrees(const type& radians)
1113
{
12-
return radiands * (180.f / std::numbers::pi_v<float>);
14+
return radians * (type(180) / std::numbers::pi_v<type>);
1315
}
14-
[[nodiscard]] constexpr float DegreesToRadians(const float degrees)
16+
17+
template<class type>
18+
requires std::is_floating_point_v<type>
19+
[[nodiscard]] constexpr float DegreesToRadians(const type& degrees)
20+
{
21+
return degrees * (std::numbers::pi_v<type> / type(180));
22+
}
23+
24+
template<class type>
25+
requires std::is_floating_point_v<type>
26+
[[nodiscard]] type HorizontalFovToVertical(const type& horFov, const type& aspect)
1527
{
16-
return degrees * (std::numbers::pi_v<float> / 180.f);
28+
const auto fovRad = DegreesToRadians(horFov);
29+
30+
const auto vertFov = type(2) * std::atan(std::tan(fovRad / type(2)) / aspect);
31+
32+
return RadiansToDegrees(vertFov);
33+
}
34+
35+
template<class type>
36+
requires std::is_floating_point_v<type>
37+
[[nodiscard]] type VerticalFovToHorizontal(const type& vertFov, const type& aspect)
38+
{
39+
const auto fovRad = DegreesToRadians(vertFov);
40+
41+
const auto horFov = type(2) * std::atan(std::tan(fovRad / type(2)) * aspect);
42+
43+
return RadiansToDegrees(horFov);
1744
}
1845
}

include/omath/collision/LineTracer.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace omath::collision
2626
public:
2727
LineTracer() = delete;
2828

29+
2930
[[nodiscard]]
3031
static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle);
3132

include/omath/engines/opengl.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ namespace omath::opengl
2222
{
2323
return
2424
{
25-
{right.x, up.x, -forward.x, 0},
26-
{right.y, up.y, -forward.y, 0},
27-
{right.z, up.z, -forward.z, 0},
28-
{-cam_origin.x, -cam_origin.y, -cam_origin.z, 1},
25+
{right.x, up.x, -forward.x, 0},
26+
{right.y, up.y, -forward.y, 0},
27+
{right.z, up.z, -forward.z, 0},
28+
{-cam_origin.x, -cam_origin.y, -cam_origin.z, 1},
2929
};
3030
}
3131

include/omath/pathfinding/NavigationMesh.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
namespace omath::pathfinding
1414
{
15+
16+
enum Error
17+
{
18+
19+
};
20+
1521
class NavigationMesh final
1622
{
1723
public:

tests/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ add_executable(unit-tests
1515
general/UnitTestColor.cpp
1616
general/UnitTestVector4.cpp
1717
general/UnitTestLineTrace.cpp
18+
general/UnitTestAngles.cpp
1819

1920
engines/UnitTestOpenGL.cpp
2021
engines/UnitTestUnityEngine.cpp
2122
engines/UnitTestSourceEngine.cpp
23+
2224
)
2325

2426
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath glm)

tests/engines/UnitTestOpenGL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <gtest/gtest.h>
66
#include <omath/Matrix.hpp>
77
#include <print>
8-
#include <omath/engines/opengl.hpp>
9-
#include <omath/engines/source.hpp>
8+
#include <omath/engines/OpenGL.hpp>
9+
#include <omath/engines/Source.hpp>
1010
#include <glm/glm.hpp>
1111

1212
#include "glm/ext/matrix_clip_space.hpp"

tests/general/UnitTestAngles.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Created by Orange on 11/30/2024.
3+
//
4+
#include <gtest/gtest.h>
5+
#include <omath/Angles.hpp>
6+
7+
8+
TEST(UnitTestAngles, RadiansToDeg)
9+
{
10+
constexpr float rad = 67;
11+
12+
EXPECT_NEAR(omath::angles::RadiansToDegrees(rad), 3838.82f, 0.01f);
13+
}
14+
15+
TEST(UnitTestAngles, DegreesToRadians)
16+
{
17+
constexpr float degree = 90;
18+
19+
EXPECT_NEAR(omath::angles::DegreesToRadians(degree), 1.5708f, 0.01f);
20+
}
21+
22+
TEST(UnitTestAngles, HorizontalFovToVerical)
23+
{
24+
constexpr float hFov = 90;
25+
constexpr float aspectRation = 16.0f / 9.0f;
26+
const auto verticalFov = omath::angles::HorizontalFovToVertical(hFov, aspectRation);
27+
28+
EXPECT_NEAR(verticalFov, 58.71f, 0.01f);
29+
}
30+
31+
TEST(UnitTestAngles, VerticalToHorizontal)
32+
{
33+
constexpr float vFov = 58.71;
34+
constexpr float aspectRation = 16.0f / 9.0f;
35+
const auto horizontalFov = omath::angles::VerticalFovToHorizontal(vFov, aspectRation);
36+
37+
EXPECT_NEAR(horizontalFov, 89.99f, 0.01f);
38+
}

0 commit comments

Comments
 (0)