From db99a1ecfc880c7cd9cd4621c81badb52376ec82 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 30 Jun 2026 01:32:45 +0300 Subject: [PATCH] improved stuff --- include/omath/hud/entity_overlay.hpp | 27 +++++++++++++----- tests/general/unit_test_entity_overlay.cpp | 33 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 tests/general/unit_test_entity_overlay.cpp diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index 4e57cd5..a7cb574 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -8,10 +8,18 @@ #include "omath/3d_primitives/aabb.hpp" #include "omath/linear_algebra/vector2.hpp" #include "omath/utility/color.hpp" +#include #include #include +#include namespace omath::hud { + enum class EntityOverlayError + { + NO_PROJECTED_VERTEX, + CENTER_PROJECTION_FAILED, + }; + class EntityOverlay final { public: @@ -170,15 +178,18 @@ namespace omath::hud } template - static std::expected + [[nodiscard]] + static std::expected from_aabb(const Camera& camera, const Aabb& aabb, const float aspect, const std::shared_ptr& renderer) { - Vector3 top; - Vector3 bottom; + using ProjectedVector = std::invoke_result_t; + + ProjectedVector top; + ProjectedVector bottom; bool has_projected_vertex = false; - for (const auto& vertex: aabb.vertices()) + for (const auto& vertex : aabb.vertices()) { if (auto projected = camera.world_to_screen_unclipped(vertex)) { @@ -198,13 +209,15 @@ namespace omath::hud } if (!has_projected_vertex) - return std::unexpected(""); + return std::unexpected(EntityOverlayError::NO_PROJECTED_VERTEX); auto center = camera.world_to_screen_unclipped(aabb.center()); if (!center) - return std::unexpected(""); + return std::unexpected(EntityOverlayError::CENTER_PROJECTION_FAILED); - return EntityOverlay{{center->x, top.y}, {center->x, bottom.y}, aspect, renderer}; + const auto center_x = static_cast(center->x); + return EntityOverlay{ + {center_x, static_cast(top.y)}, {center_x, static_cast(bottom.y)}, aspect, renderer}; } private: diff --git a/tests/general/unit_test_entity_overlay.cpp b/tests/general/unit_test_entity_overlay.cpp new file mode 100644 index 0000000..c18329f --- /dev/null +++ b/tests/general/unit_test_entity_overlay.cpp @@ -0,0 +1,33 @@ +// +// Created by orange on 30.06.2026. +// +#include +#include +#include +#include +#include + +TEST(EntityOverlayTests, FromAabbSupportsDoubleCameraCoordinates) +{ + constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f); + const omath::unreal_engine::Camera camera{{0.0, 0.0, 0.0}, {}, {1920.f, 1080.f}, fov, 0.01, 1000.0}; + const omath::primitives::Aabb aabb{{90.0, -1.0, -1.0}, {110.0, 1.0, 1.0}}; + + const auto overlay = omath::hud::EntityOverlay::from_aabb(camera, aabb, 0.5f, + std::shared_ptr{}); + + ASSERT_TRUE(overlay.has_value()); +} + +TEST(EntityOverlayTests, FromAabbReturnsErrorCodeIfNoVertexProjects) +{ + constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f); + const omath::unreal_engine::Camera camera{{0.0, 0.0, 0.0}, {}, {1920.f, 1080.f}, fov, 0.01, 1000.0}; + const omath::primitives::Aabb aabb{{-110.0, -1.0, -1.0}, {-90.0, 1.0, 1.0}}; + + const auto overlay = omath::hud::EntityOverlay::from_aabb(camera, aabb, 0.5f, + std::shared_ptr{}); + + ASSERT_FALSE(overlay.has_value()); + EXPECT_EQ(overlay.error(), omath::hud::EntityOverlayError::NO_PROJECTED_VERTEX); +}