From b4f4917aec6d3e164511578a721a99e59b8ff565 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 5 Jul 2026 05:53:28 +0300 Subject: [PATCH] added constexpr support for radar --- include/omath/algorithm/radar.hpp | 15 +++++++++++++-- tests/general/unit_test_projection.cpp | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/omath/algorithm/radar.hpp b/include/omath/algorithm/radar.hpp index 4f27d47..6c59ff9 100644 --- a/include/omath/algorithm/radar.hpp +++ b/include/omath/algorithm/radar.hpp @@ -10,14 +10,25 @@ namespace omath::algorithm template requires std::is_floating_point_v [[nodiscard]] - Vector2 world_to_radar(const Camera& camera, const Vector3& position, const FloatingType scale) + constexpr Vector2 world_to_radar(const Camera& camera, const Vector3& position, const FloatingType scale) { const auto look_at_angles = camera.calc_look_at_angles(position); const auto current_angles = camera.get_view_angles(); + if consteval + { + const auto right_yaw = current_angles.yaw + - camera.calc_look_at_angles(camera.get_origin() + camera.get_abs_right()).yaw + - decltype(current_angles.yaw)::from_degrees(90); + const auto sign = right_yaw.cos() < 0 ? -1.f : 1.f; + + const auto yaw = current_angles.yaw - look_at_angles.yaw - decltype(current_angles.yaw)::from_degrees(90); + return omath::Vector2(static_cast(yaw.cos()) * sign, static_cast(yaw.sin())) + * (camera.get_origin().distance_to(position) * scale); + } static const auto sign = [&camera, ¤t_angles] { - auto right_yaw = current_angles.yaw + const auto right_yaw = current_angles.yaw - camera.calc_look_at_angles(camera.get_origin() + camera.get_abs_right()).yaw - decltype(current_angles.yaw)::from_degrees(90); return right_yaw.cos() < 0 ? -1.f : 1.f; diff --git a/tests/general/unit_test_projection.cpp b/tests/general/unit_test_projection.cpp index 3085dc2..c4b275e 100644 --- a/tests/general/unit_test_projection.cpp +++ b/tests/general/unit_test_projection.cpp @@ -1,6 +1,7 @@ // // Created by Vlad on 27.08.2024. // +#include "omath/algorithm/radar.hpp" #include "omath/engines/unity_engine/camera.hpp" #include #include @@ -1283,8 +1284,9 @@ TEST(UnitTestProjection, TriangleFarToSideCulled) TEST(UnitTestProjection, TriangleStraddlingFrustumNotCulled) { constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f); - const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f); + constexpr auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f); + constexpr auto position = omath::algorithm::world_to_radar(cam, {-1.f, 0.f, 0.f}, 0.1f); // Large triangle with vertices on both sides of the frustum — should not be culled const omath::Triangle> tri{{100.f, 0.f, 0.f}, {100.f, 5000.f, 0.f}, {100.f, 0.f, 5000.f}}; EXPECT_FALSE(cam.is_culled_by_frustum(tri));