mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-08 05:52:07 +00:00
improved stuff
This commit is contained in:
@@ -8,10 +8,18 @@
|
|||||||
#include "omath/3d_primitives/aabb.hpp"
|
#include "omath/3d_primitives/aabb.hpp"
|
||||||
#include "omath/linear_algebra/vector2.hpp"
|
#include "omath/linear_algebra/vector2.hpp"
|
||||||
#include "omath/utility/color.hpp"
|
#include "omath/utility/color.hpp"
|
||||||
|
#include <expected>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <type_traits>
|
||||||
namespace omath::hud
|
namespace omath::hud
|
||||||
{
|
{
|
||||||
|
enum class EntityOverlayError
|
||||||
|
{
|
||||||
|
NO_PROJECTED_VERTEX,
|
||||||
|
CENTER_PROJECTION_FAILED,
|
||||||
|
};
|
||||||
|
|
||||||
class EntityOverlay final
|
class EntityOverlay final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -170,12 +178,15 @@ namespace omath::hud
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class Camera, class Aabb>
|
template<class Camera, class Aabb>
|
||||||
static std::expected<EntityOverlay, std::string>
|
[[nodiscard]]
|
||||||
|
static std::expected<EntityOverlay, EntityOverlayError>
|
||||||
from_aabb(const Camera& camera, const Aabb& aabb, const float aspect,
|
from_aabb(const Camera& camera, const Aabb& aabb, const float aspect,
|
||||||
const std::shared_ptr<HudRendererInterface>& renderer)
|
const std::shared_ptr<HudRendererInterface>& renderer)
|
||||||
{
|
{
|
||||||
Vector3<float> top;
|
using ProjectedVector = std::invoke_result_t<decltype(&Aabb::center), const Aabb&>;
|
||||||
Vector3<float> bottom;
|
|
||||||
|
ProjectedVector top;
|
||||||
|
ProjectedVector bottom;
|
||||||
bool has_projected_vertex = false;
|
bool has_projected_vertex = false;
|
||||||
|
|
||||||
for (const auto& vertex : aabb.vertices())
|
for (const auto& vertex : aabb.vertices())
|
||||||
@@ -198,13 +209,15 @@ namespace omath::hud
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!has_projected_vertex)
|
if (!has_projected_vertex)
|
||||||
return std::unexpected("");
|
return std::unexpected(EntityOverlayError::NO_PROJECTED_VERTEX);
|
||||||
|
|
||||||
auto center = camera.world_to_screen_unclipped(aabb.center());
|
auto center = camera.world_to_screen_unclipped(aabb.center());
|
||||||
if (!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<float>(center->x);
|
||||||
|
return EntityOverlay{
|
||||||
|
{center_x, static_cast<float>(top.y)}, {center_x, static_cast<float>(bottom.y)}, aspect, renderer};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 30.06.2026.
|
||||||
|
//
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <omath/3d_primitives/aabb.hpp>
|
||||||
|
#include <omath/engines/unreal_engine/camera.hpp>
|
||||||
|
#include <omath/hud/entity_overlay.hpp>
|
||||||
|
|
||||||
|
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<double> 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<omath::hud::HudRendererInterface>{});
|
||||||
|
|
||||||
|
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<double> 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<omath::hud::HudRendererInterface>{});
|
||||||
|
|
||||||
|
ASSERT_FALSE(overlay.has_value());
|
||||||
|
EXPECT_EQ(overlay.error(), omath::hud::EntityOverlayError::NO_PROJECTED_VERTEX);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user