mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-08 14:02:06 +00:00
Compare commits
3 Commits
v5.4.0
...
d7ec571a7a
| Author | SHA1 | Date | |
|---|---|---|---|
| d7ec571a7a | |||
| 235917008c | |||
| 71e256d033 |
@@ -4,17 +4,28 @@
|
||||
|
||||
#pragma once
|
||||
#include "omath/linear_algebra/vector3.hpp"
|
||||
#include <array>
|
||||
|
||||
namespace omath::primitives
|
||||
{
|
||||
enum class UpAxis { X, Y, Z };
|
||||
enum class UpAxis
|
||||
{
|
||||
X,
|
||||
Y,
|
||||
Z
|
||||
};
|
||||
|
||||
template<class Type>
|
||||
template<class Type, UpAxis Up = UpAxis::Y>
|
||||
struct Aabb final
|
||||
{
|
||||
Vector3<Type> min;
|
||||
Vector3<Type> max;
|
||||
|
||||
[[nodiscard]]
|
||||
static consteval UpAxis get_up_axis()
|
||||
{
|
||||
return Up;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<Type> center() const noexcept
|
||||
{
|
||||
@@ -27,7 +38,6 @@ namespace omath::primitives
|
||||
return (max - min) / static_cast<Type>(2);
|
||||
}
|
||||
|
||||
template<UpAxis Up = UpAxis::Y>
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<Type> top() const noexcept
|
||||
{
|
||||
@@ -42,7 +52,6 @@ namespace omath::primitives
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
template<UpAxis Up = UpAxis::Y>
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<Type> bottom() const noexcept
|
||||
{
|
||||
@@ -57,11 +66,22 @@ namespace omath::primitives
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::array<Vector3<Type>, 8> vertices() const noexcept
|
||||
{
|
||||
return {
|
||||
Vector3<Type>{min.x, min.y, min.z}, Vector3<Type>{max.x, min.y, min.z},
|
||||
Vector3<Type>{min.x, max.y, min.z}, Vector3<Type>{max.x, max.y, min.z},
|
||||
Vector3<Type>{min.x, min.y, max.z}, Vector3<Type>{max.x, min.y, max.z},
|
||||
Vector3<Type>{min.x, max.y, max.z}, Vector3<Type>{max.x, max.y, max.z},
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool is_collide(const Aabb& other) const noexcept
|
||||
{
|
||||
return min.x <= other.max.x && max.x >= other.min.x &&
|
||||
min.y <= other.max.y && max.y >= other.min.y &&min.z <= other.max.z && max.z >= other.min.z;
|
||||
return min.x <= other.max.x && max.x >= other.min.x && min.y <= other.max.y && max.y >= other.min.y
|
||||
&& min.z <= other.max.z && max.z >= other.min.z;
|
||||
}
|
||||
};
|
||||
} // namespace omath::primitives
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace omath::hud
|
||||
class CanvasBox final
|
||||
{
|
||||
public:
|
||||
CanvasBox() = default;
|
||||
CanvasBox(Vector2<float> top, Vector2<float> bottom, float ratio = 4.f);
|
||||
|
||||
[[nodiscard("You have to use array")]]
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
#include "canvas_box.hpp"
|
||||
#include "entity_overlay_widgets.hpp"
|
||||
#include "hud_renderer_interface.hpp"
|
||||
#include "omath/3d_primitives/aabb.hpp"
|
||||
#include "omath/linear_algebra/vector2.hpp"
|
||||
#include "omath/utility/color.hpp"
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
namespace omath::hud
|
||||
{
|
||||
class EntityOverlay final
|
||||
@@ -57,13 +57,17 @@ namespace omath::hud
|
||||
float offset = 5.f);
|
||||
|
||||
// ── Labels ───────────────────────────────────────────────────────
|
||||
EntityOverlay& add_right_label(const Color& color, float offset, widget::Outlined outlined, const std::string_view& text);
|
||||
EntityOverlay& add_right_label(const Color& color, float offset, widget::Outlined outlined,
|
||||
const std::string_view& text);
|
||||
|
||||
EntityOverlay& add_left_label(const Color& color, float offset, widget::Outlined outlined, const std::string_view& text);
|
||||
EntityOverlay& add_left_label(const Color& color, float offset, widget::Outlined outlined,
|
||||
const std::string_view& text);
|
||||
|
||||
EntityOverlay& add_top_label(const Color& color, float offset, widget::Outlined outlined, std::string_view text);
|
||||
EntityOverlay& add_top_label(const Color& color, float offset, widget::Outlined outlined,
|
||||
std::string_view text);
|
||||
|
||||
EntityOverlay& add_bottom_label(const Color& color, float offset, widget::Outlined outlined, std::string_view text);
|
||||
EntityOverlay& add_bottom_label(const Color& color, float offset, widget::Outlined outlined,
|
||||
std::string_view text);
|
||||
|
||||
EntityOverlay& add_centered_top_label(const Color& color, float offset, widget::Outlined outlined,
|
||||
const std::string_view& text);
|
||||
@@ -72,24 +76,24 @@ namespace omath::hud
|
||||
const std::string_view& text);
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_right_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_right_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
return add_right_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_left_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_left_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
return add_left_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_top_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_top_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
return add_top_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
@@ -112,8 +116,9 @@ namespace omath::hud
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_centered_bottom_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_centered_bottom_label(const Color& color, const float offset,
|
||||
const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
{
|
||||
return add_centered_bottom_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
@@ -164,6 +169,44 @@ namespace omath::hud
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Camera, class Aabb>
|
||||
static std::expected<EntityOverlay, std::string>
|
||||
from_aabb(const Camera& camera, const Aabb& aabb, const float aspect,
|
||||
const std::shared_ptr<HudRendererInterface>& renderer)
|
||||
{
|
||||
Vector3<float> top;
|
||||
Vector3<float> bottom;
|
||||
bool has_projected_vertex = false;
|
||||
|
||||
for (const auto& vertex: aabb.vertices())
|
||||
{
|
||||
if (auto projected = camera.world_to_screen_unclipped(vertex))
|
||||
{
|
||||
if (!has_projected_vertex)
|
||||
{
|
||||
top = *projected;
|
||||
bottom = *projected;
|
||||
has_projected_vertex = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (projected->y < top.y)
|
||||
top = *projected;
|
||||
if (projected->y > bottom.y)
|
||||
bottom = *projected;
|
||||
}
|
||||
}
|
||||
|
||||
if (!has_projected_vertex)
|
||||
return std::unexpected("");
|
||||
|
||||
auto center = camera.world_to_screen_unclipped(aabb.center());
|
||||
if (!center)
|
||||
return std::unexpected("");
|
||||
|
||||
return EntityOverlay{{center->x, top.y}, {center->x, bottom.y}, aspect, renderer};
|
||||
}
|
||||
|
||||
private:
|
||||
// optional<W> dispatch — enables when() conditional widgets
|
||||
template<typename W>
|
||||
|
||||
@@ -119,11 +119,11 @@ namespace
|
||||
switch (axis)
|
||||
{
|
||||
case omath::primitives::UpAxis::X:
|
||||
return aabb.top<omath::primitives::UpAxis::X>();
|
||||
return omath::primitives::Aabb<float, omath::primitives::UpAxis::X>{aabb.min, aabb.max}.top();
|
||||
case omath::primitives::UpAxis::Y:
|
||||
return aabb.top<omath::primitives::UpAxis::Y>();
|
||||
return aabb.top();
|
||||
case omath::primitives::UpAxis::Z:
|
||||
return aabb.top<omath::primitives::UpAxis::Z>();
|
||||
return omath::primitives::Aabb<float, omath::primitives::UpAxis::Z>{aabb.min, aabb.max}.top();
|
||||
}
|
||||
std::unreachable();
|
||||
}
|
||||
@@ -133,11 +133,11 @@ namespace
|
||||
switch (axis)
|
||||
{
|
||||
case omath::primitives::UpAxis::X:
|
||||
return aabb.bottom<omath::primitives::UpAxis::X>();
|
||||
return omath::primitives::Aabb<float, omath::primitives::UpAxis::X>{aabb.min, aabb.max}.bottom();
|
||||
case omath::primitives::UpAxis::Y:
|
||||
return aabb.bottom<omath::primitives::UpAxis::Y>();
|
||||
return aabb.bottom();
|
||||
case omath::primitives::UpAxis::Z:
|
||||
return aabb.bottom<omath::primitives::UpAxis::Z>();
|
||||
return omath::primitives::Aabb<float, omath::primitives::UpAxis::Z>{aabb.min, aabb.max}.bottom();
|
||||
}
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "omath/3d_primitives/aabb.hpp"
|
||||
|
||||
using UpAxis = omath::primitives::UpAxis;
|
||||
using AABB = omath::primitives::Aabb<float>;
|
||||
using AABBZ = omath::primitives::Aabb<float, UpAxis::Z>;
|
||||
using Vec3 = omath::Vector3<float>;
|
||||
|
||||
// --- center() ---
|
||||
@@ -65,14 +67,12 @@ TEST(AabbTests, ExtentsOfDegenerateBox)
|
||||
EXPECT_FLOAT_EQ(e.z, 0.f);
|
||||
}
|
||||
|
||||
using UpAxis = omath::primitives::UpAxis;
|
||||
|
||||
// --- top() ---
|
||||
|
||||
TEST(AabbTests, TopYUpSymmetricBox)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto t = box.top<UpAxis::Y>();
|
||||
constexpr auto t = box.top();
|
||||
EXPECT_FLOAT_EQ(t.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(t.y, 2.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 0.f);
|
||||
@@ -81,7 +81,7 @@ TEST(AabbTests, TopYUpSymmetricBox)
|
||||
TEST(AabbTests, TopYUpOffsetBox)
|
||||
{
|
||||
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto t = box.top<UpAxis::Y>();
|
||||
constexpr auto t = box.top();
|
||||
EXPECT_FLOAT_EQ(t.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(t.y, 10.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 4.f);
|
||||
@@ -89,8 +89,8 @@ TEST(AabbTests, TopYUpOffsetBox)
|
||||
|
||||
TEST(AabbTests, TopZUpSymmetricBox)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto t = box.top<UpAxis::Z>();
|
||||
constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto t = box.top();
|
||||
EXPECT_FLOAT_EQ(t.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(t.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 3.f);
|
||||
@@ -98,8 +98,8 @@ TEST(AabbTests, TopZUpSymmetricBox)
|
||||
|
||||
TEST(AabbTests, TopZUpOffsetBox)
|
||||
{
|
||||
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto t = box.top<UpAxis::Z>();
|
||||
constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto t = box.top();
|
||||
EXPECT_FLOAT_EQ(t.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(t.y, 7.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 6.f);
|
||||
@@ -108,7 +108,8 @@ TEST(AabbTests, TopZUpOffsetBox)
|
||||
TEST(AabbTests, TopDefaultIsYUp)
|
||||
{
|
||||
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}};
|
||||
EXPECT_EQ(box.top(), box.top<UpAxis::Y>());
|
||||
EXPECT_EQ(AABB::get_up_axis(), UpAxis::Y);
|
||||
EXPECT_FLOAT_EQ(box.top().y, 4.f);
|
||||
}
|
||||
|
||||
// --- bottom() ---
|
||||
@@ -116,7 +117,7 @@ TEST(AabbTests, TopDefaultIsYUp)
|
||||
TEST(AabbTests, BottomYUpSymmetricBox)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto b = box.bottom<UpAxis::Y>();
|
||||
constexpr auto b = box.bottom();
|
||||
EXPECT_FLOAT_EQ(b.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(b.y, -2.f);
|
||||
EXPECT_FLOAT_EQ(b.z, 0.f);
|
||||
@@ -125,7 +126,7 @@ TEST(AabbTests, BottomYUpSymmetricBox)
|
||||
TEST(AabbTests, BottomYUpOffsetBox)
|
||||
{
|
||||
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto b = box.bottom<UpAxis::Y>();
|
||||
constexpr auto b = box.bottom();
|
||||
EXPECT_FLOAT_EQ(b.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(b.y, 4.f);
|
||||
EXPECT_FLOAT_EQ(b.z, 4.f);
|
||||
@@ -133,8 +134,8 @@ TEST(AabbTests, BottomYUpOffsetBox)
|
||||
|
||||
TEST(AabbTests, BottomZUpSymmetricBox)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto b = box.bottom<UpAxis::Z>();
|
||||
constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto b = box.bottom();
|
||||
EXPECT_FLOAT_EQ(b.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(b.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(b.z, -3.f);
|
||||
@@ -142,8 +143,8 @@ TEST(AabbTests, BottomZUpSymmetricBox)
|
||||
|
||||
TEST(AabbTests, BottomZUpOffsetBox)
|
||||
{
|
||||
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto b = box.bottom<UpAxis::Z>();
|
||||
constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto b = box.bottom();
|
||||
EXPECT_FLOAT_EQ(b.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(b.y, 7.f);
|
||||
EXPECT_FLOAT_EQ(b.z, 2.f);
|
||||
@@ -152,14 +153,33 @@ TEST(AabbTests, BottomZUpOffsetBox)
|
||||
TEST(AabbTests, BottomDefaultIsYUp)
|
||||
{
|
||||
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}};
|
||||
EXPECT_EQ(box.bottom(), box.bottom<UpAxis::Y>());
|
||||
EXPECT_EQ(AABB::get_up_axis(), UpAxis::Y);
|
||||
EXPECT_FLOAT_EQ(box.bottom().y, 0.f);
|
||||
}
|
||||
|
||||
TEST(AabbTests, TopAndBottomAreSymmetric)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
EXPECT_FLOAT_EQ(box.top<UpAxis::Y>().y, -box.bottom<UpAxis::Y>().y);
|
||||
EXPECT_FLOAT_EQ(box.top<UpAxis::Z>().z, -box.bottom<UpAxis::Z>().z);
|
||||
constexpr AABBZ z_up_box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
EXPECT_FLOAT_EQ(box.top().y, -box.bottom().y);
|
||||
EXPECT_FLOAT_EQ(z_up_box.top().z, -z_up_box.bottom().z);
|
||||
}
|
||||
|
||||
// --- vertices() ---
|
||||
|
||||
TEST(AabbTests, VerticesReturnsAllCorners)
|
||||
{
|
||||
constexpr AABB box{{1.f, 2.f, 3.f}, {4.f, 6.f, 8.f}};
|
||||
constexpr auto v = box.vertices();
|
||||
|
||||
EXPECT_EQ(v[0], Vec3(1.f, 2.f, 3.f));
|
||||
EXPECT_EQ(v[1], Vec3(4.f, 2.f, 3.f));
|
||||
EXPECT_EQ(v[2], Vec3(1.f, 6.f, 3.f));
|
||||
EXPECT_EQ(v[3], Vec3(4.f, 6.f, 3.f));
|
||||
EXPECT_EQ(v[4], Vec3(1.f, 2.f, 8.f));
|
||||
EXPECT_EQ(v[5], Vec3(4.f, 2.f, 8.f));
|
||||
EXPECT_EQ(v[6], Vec3(1.f, 6.f, 8.f));
|
||||
EXPECT_EQ(v[7], Vec3(4.f, 6.f, 8.f));
|
||||
}
|
||||
|
||||
// --- is_collide() ---
|
||||
|
||||
Reference in New Issue
Block a user