From 13188615ed86b71bce15130648c48b06a55c4cf0 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 27 Aug 2024 10:56:56 +0300 Subject: [PATCH] added unit test --- include/omath/projection/Camera.h | 1 + source/Vector3.cpp | 21 +++++++++++---------- source/projection/Camera.cpp | 11 +++++++++++ tests/CMakeLists.txt | 2 +- tests/UnitTestProjection.cpp | 15 +++++++++++++++ 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 tests/UnitTestProjection.cpp diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.h index 8f25902..f86c8b6 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.h @@ -15,6 +15,7 @@ namespace omath::projection class Camera { public: + Camera(const Vector3& position, const Vector3& viewAngles, const Vector3& viewPort, float fov, float near, float far); void SetViewAngles(const Vector3& viewAngles); [[nodiscard]] const Vector3& GetViewAngles() const; diff --git a/source/Vector3.cpp b/source/Vector3.cpp index 35466f2..39d5757 100644 --- a/source/Vector3.cpp +++ b/source/Vector3.cpp @@ -238,10 +238,12 @@ namespace omath const auto cosRoll = std::cos(angles::DegreesToRadians(roll)); const auto sinRoll = std::sin(angles::DegreesToRadians(roll)); - - return {-sinRoll*sinPitch*cosYaw + -cosRoll*-sinYaw, - -sinRoll*sinPitch*sinYaw + -cosRoll*cosYaw, - -sinRoll*cosPitch}; + // Right vector calculation + return { + cosYaw * cosRoll - sinYaw * sinPitch * sinRoll, // X component + sinRoll * cosPitch, // Y component + sinYaw * cosRoll + cosYaw * sinPitch * sinRoll // Z component + }; } Vector3 Vector3::UpVector(float pitch, float yaw, float roll) @@ -255,12 +257,11 @@ namespace omath const auto cosRoll = std::cos(angles::DegreesToRadians(roll)); const auto sinRoll = std::sin(angles::DegreesToRadians(roll)); - - return - { - cosRoll*sinPitch*cosYaw+-sinRoll*-sinYaw, - cosRoll*sinPitch*cosYaw+-sinRoll*-sinYaw, - cosRoll*cosPitch, + // Up vector calculation + return { + -sinRoll * cosYaw + cosRoll * sinPitch * sinYaw, // X component + cosRoll * cosPitch, // Y component + -sinRoll * sinYaw - cosRoll * sinPitch * cosYaw // Z component }; } diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index 984c414..dac1ffe 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -7,6 +7,17 @@ namespace omath::projection { + Camera::Camera(const Vector3 &position, const Vector3 &viewAngles, const Vector3 &viewPort, const float fov, const float near, + const float far) + { + m_origin = position; + m_viewAngles = viewAngles; + m_viewPort = viewPort; + m_fieldOfView = fov; + m_nearPlaneDistance = near; + m_farPlaneDistance = far; + } + Matrix Camera::GetViewMatrix() const { return GetOrientationMatrix() * GetTranslationMatrix(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9781a8b..f47be28 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) include(GoogleTest) -add_executable(unit-tests UnitTestPrediction.cpp UnitTestMatrix.cpp UnitTestAstar.cpp) +add_executable(unit-tests UnitTestPrediction.cpp UnitTestMatrix.cpp UnitTestAstar.cpp UnitTestProjection.cpp) target_link_libraries(unit-tests PRIVATE gtest gtest_main omath) diff --git a/tests/UnitTestProjection.cpp b/tests/UnitTestProjection.cpp new file mode 100644 index 0000000..df7eaeb --- /dev/null +++ b/tests/UnitTestProjection.cpp @@ -0,0 +1,15 @@ +// +// Created by Vlad on 27.08.2024. +// +#include +#include +#include +#include + +TEST(UnitTestProjection, IsPointOnScreen) +{ + const omath::projection::Camera camera({}, {90, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 120, 10, 100); + + const auto proj = camera.WorldToScreen({0,0,15}); + EXPECT_TRUE(proj.has_value()); +} \ No newline at end of file