Compare commits

...

2 Commits

Author SHA1 Message Date
orange d7ec571a7a patch 2026-06-29 10:14:03 +03:00
orange 235917008c removed stff 2026-06-29 09:54:34 +03:00
3 changed files with 36 additions and 30 deletions
+8 -5
View File
@@ -8,7 +8,12 @@
namespace omath::primitives namespace omath::primitives
{ {
enum class UpAxis { X, Y, Z }; enum class UpAxis
{
X,
Y,
Z
};
template<class Type, UpAxis Up = UpAxis::Y> template<class Type, UpAxis Up = UpAxis::Y>
struct Aabb final struct Aabb final
@@ -33,7 +38,6 @@ namespace omath::primitives
return (max - min) / static_cast<Type>(2); return (max - min) / static_cast<Type>(2);
} }
template<UpAxis Up = UpAxis::Y>
[[nodiscard]] [[nodiscard]]
constexpr Vector3<Type> top() const noexcept constexpr Vector3<Type> top() const noexcept
{ {
@@ -48,7 +52,6 @@ namespace omath::primitives
std::unreachable(); std::unreachable();
} }
template<UpAxis Up = UpAxis::Y>
[[nodiscard]] [[nodiscard]]
constexpr Vector3<Type> bottom() const noexcept constexpr Vector3<Type> bottom() const noexcept
{ {
@@ -77,8 +80,8 @@ namespace omath::primitives
[[nodiscard]] [[nodiscard]]
constexpr bool is_collide(const Aabb& other) const noexcept constexpr bool is_collide(const Aabb& other) const noexcept
{ {
return min.x <= other.max.x && max.x >= other.min.x && return min.x <= other.max.x && max.x >= other.min.x && min.y <= other.max.y && max.y >= other.min.y
min.y <= other.max.y && max.y >= other.min.y &&min.z <= other.max.z && max.z >= other.min.z; && min.z <= other.max.z && max.z >= other.min.z;
} }
}; };
} // namespace omath::primitives } // namespace omath::primitives
+6 -6
View File
@@ -119,11 +119,11 @@ namespace
switch (axis) switch (axis)
{ {
case omath::primitives::UpAxis::X: 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: case omath::primitives::UpAxis::Y:
return aabb.top<omath::primitives::UpAxis::Y>(); return aabb.top();
case omath::primitives::UpAxis::Z: 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(); std::unreachable();
} }
@@ -133,11 +133,11 @@ namespace
switch (axis) switch (axis)
{ {
case omath::primitives::UpAxis::X: 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: case omath::primitives::UpAxis::Y:
return aabb.bottom<omath::primitives::UpAxis::Y>(); return aabb.bottom();
case omath::primitives::UpAxis::Z: 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(); std::unreachable();
} }
+21 -18
View File
@@ -4,7 +4,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "omath/3d_primitives/aabb.hpp" #include "omath/3d_primitives/aabb.hpp"
using UpAxis = omath::primitives::UpAxis;
using AABB = omath::primitives::Aabb<float>; using AABB = omath::primitives::Aabb<float>;
using AABBZ = omath::primitives::Aabb<float, UpAxis::Z>;
using Vec3 = omath::Vector3<float>; using Vec3 = omath::Vector3<float>;
// --- center() --- // --- center() ---
@@ -65,14 +67,12 @@ TEST(AabbTests, ExtentsOfDegenerateBox)
EXPECT_FLOAT_EQ(e.z, 0.f); EXPECT_FLOAT_EQ(e.z, 0.f);
} }
using UpAxis = omath::primitives::UpAxis;
// --- top() --- // --- top() ---
TEST(AabbTests, TopYUpSymmetricBox) TEST(AabbTests, TopYUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; 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.x, 0.f);
EXPECT_FLOAT_EQ(t.y, 2.f); EXPECT_FLOAT_EQ(t.y, 2.f);
EXPECT_FLOAT_EQ(t.z, 0.f); EXPECT_FLOAT_EQ(t.z, 0.f);
@@ -81,7 +81,7 @@ TEST(AabbTests, TopYUpSymmetricBox)
TEST(AabbTests, TopYUpOffsetBox) TEST(AabbTests, TopYUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; 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.x, 2.f);
EXPECT_FLOAT_EQ(t.y, 10.f); EXPECT_FLOAT_EQ(t.y, 10.f);
EXPECT_FLOAT_EQ(t.z, 4.f); EXPECT_FLOAT_EQ(t.z, 4.f);
@@ -89,8 +89,8 @@ TEST(AabbTests, TopYUpOffsetBox)
TEST(AabbTests, TopZUpSymmetricBox) TEST(AabbTests, TopZUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto t = box.top<UpAxis::Z>(); constexpr auto t = box.top();
EXPECT_FLOAT_EQ(t.x, 0.f); EXPECT_FLOAT_EQ(t.x, 0.f);
EXPECT_FLOAT_EQ(t.y, 0.f); EXPECT_FLOAT_EQ(t.y, 0.f);
EXPECT_FLOAT_EQ(t.z, 3.f); EXPECT_FLOAT_EQ(t.z, 3.f);
@@ -98,8 +98,8 @@ TEST(AabbTests, TopZUpSymmetricBox)
TEST(AabbTests, TopZUpOffsetBox) TEST(AabbTests, TopZUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto t = box.top<UpAxis::Z>(); constexpr auto t = box.top();
EXPECT_FLOAT_EQ(t.x, 2.f); EXPECT_FLOAT_EQ(t.x, 2.f);
EXPECT_FLOAT_EQ(t.y, 7.f); EXPECT_FLOAT_EQ(t.y, 7.f);
EXPECT_FLOAT_EQ(t.z, 6.f); EXPECT_FLOAT_EQ(t.z, 6.f);
@@ -108,7 +108,8 @@ TEST(AabbTests, TopZUpOffsetBox)
TEST(AabbTests, TopDefaultIsYUp) TEST(AabbTests, TopDefaultIsYUp)
{ {
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}}; 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() --- // --- bottom() ---
@@ -116,7 +117,7 @@ TEST(AabbTests, TopDefaultIsYUp)
TEST(AabbTests, BottomYUpSymmetricBox) TEST(AabbTests, BottomYUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; 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.x, 0.f);
EXPECT_FLOAT_EQ(b.y, -2.f); EXPECT_FLOAT_EQ(b.y, -2.f);
EXPECT_FLOAT_EQ(b.z, 0.f); EXPECT_FLOAT_EQ(b.z, 0.f);
@@ -125,7 +126,7 @@ TEST(AabbTests, BottomYUpSymmetricBox)
TEST(AabbTests, BottomYUpOffsetBox) TEST(AabbTests, BottomYUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; 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.x, 2.f);
EXPECT_FLOAT_EQ(b.y, 4.f); EXPECT_FLOAT_EQ(b.y, 4.f);
EXPECT_FLOAT_EQ(b.z, 4.f); EXPECT_FLOAT_EQ(b.z, 4.f);
@@ -133,8 +134,8 @@ TEST(AabbTests, BottomYUpOffsetBox)
TEST(AabbTests, BottomZUpSymmetricBox) TEST(AabbTests, BottomZUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto b = box.bottom<UpAxis::Z>(); constexpr auto b = box.bottom();
EXPECT_FLOAT_EQ(b.x, 0.f); EXPECT_FLOAT_EQ(b.x, 0.f);
EXPECT_FLOAT_EQ(b.y, 0.f); EXPECT_FLOAT_EQ(b.y, 0.f);
EXPECT_FLOAT_EQ(b.z, -3.f); EXPECT_FLOAT_EQ(b.z, -3.f);
@@ -142,8 +143,8 @@ TEST(AabbTests, BottomZUpSymmetricBox)
TEST(AabbTests, BottomZUpOffsetBox) TEST(AabbTests, BottomZUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto b = box.bottom<UpAxis::Z>(); constexpr auto b = box.bottom();
EXPECT_FLOAT_EQ(b.x, 2.f); EXPECT_FLOAT_EQ(b.x, 2.f);
EXPECT_FLOAT_EQ(b.y, 7.f); EXPECT_FLOAT_EQ(b.y, 7.f);
EXPECT_FLOAT_EQ(b.z, 2.f); EXPECT_FLOAT_EQ(b.z, 2.f);
@@ -152,14 +153,16 @@ TEST(AabbTests, BottomZUpOffsetBox)
TEST(AabbTests, BottomDefaultIsYUp) TEST(AabbTests, BottomDefaultIsYUp)
{ {
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}}; 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) TEST(AabbTests, TopAndBottomAreSymmetric)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; 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); constexpr AABBZ z_up_box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
EXPECT_FLOAT_EQ(box.top<UpAxis::Z>().z, -box.bottom<UpAxis::Z>().z); EXPECT_FLOAT_EQ(box.top().y, -box.bottom().y);
EXPECT_FLOAT_EQ(z_up_box.top().z, -z_up_box.bottom().z);
} }
// --- vertices() --- // --- vertices() ---