From 714a68e8b141fac71a2892089979acd905ed316d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 6 Jun 2024 07:34:57 +0200 Subject: w --- src/RTree-search.hpp | 12 -------- src/hole-cut.cpp | 81 +++++++++++++++++++++++++++++++------------------ src/hole.hpp | 16 +++++----- src/object.cpp | 1 + src/rect-intersects.hpp | 14 +++++++++ src/search.cpp | 1 + 6 files changed, 76 insertions(+), 49 deletions(-) create mode 100644 src/rect-intersects.hpp (limited to 'src') diff --git a/src/RTree-search.hpp b/src/RTree-search.hpp index 47c53592..4b824cd6 100644 --- a/src/RTree-search.hpp +++ b/src/RTree-search.hpp @@ -1,18 +1,6 @@ #pragma once #include "compat/assert.hpp" #include "RTree.h" -#include -#include - -namespace floormat { - -constexpr inline bool rect_intersects(Vector2 min1, Vector2 max1, Vector2 min2, Vector2 max2) -{ - return min1.x() < max2.x() && max1.x() > min2.x() && - min1.y() < max2.y() && max1.y() > min2.y(); -} - -} // namespace floormat RTREE_TEMPLATE template diff --git a/src/hole-cut.cpp b/src/hole-cut.cpp index 8110510a..51d1a8af 100644 --- a/src/hole-cut.cpp +++ b/src/hole-cut.cpp @@ -8,12 +8,16 @@ #pragma GCC diagnostic ignored "-Wswitch-default" //#pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wbitwise-instead-of-logical" +#endif namespace floormat { namespace { -using bbox = cut_rectangle_result::bbox; -using rect = cut_rectangle_result::rect; +template using crr = cut_rectangle_result; +template using bbox = typename cut_rectangle_result::bbox; +template using rect = typename cut_rectangle_result::rect; enum shift : uint8_t { __ = 0, x0 = 1 << 0, x1 = 1 << 1, y0 = 1 << 2, y1 = 1 << 3, }; enum class location : uint8_t { R0, R1, H0, H1, }; @@ -32,6 +36,8 @@ struct element std::array array; }; +template using Vec2ʹ = VectorTypeFor<2, T>; + constexpr element make_element(uint8_t s) { // ReSharper disable CppIdenticalOperandsInBinaryExpression @@ -114,12 +120,13 @@ constexpr element make_element(uint8_t s) } // NOLINTEND(*-simplify) // ReSharper restore CppIdenticalOperandsInBinaryExpression - std::unreachable(); + fm_assert(false); } constexpr auto elements = map(make_element, iota_array); -constexpr auto get_value_from_coord(Vector2i r0, Vector2i r1, Vector2i h0, Vector2i h1, coords c) +template +constexpr auto get_value_from_coord(Vec2ʹ r0, Vec2ʹ r1, Vec2ʹ h0, Vec2ʹ h1, coords c) { const auto xs = std::array{ r0.x(), r1.x(), h0.x(), h1.x(), }; const auto ys = std::array{ r0.y(), r1.y(), h0.y(), h1.y(), }; @@ -127,37 +134,29 @@ constexpr auto get_value_from_coord(Vector2i r0, Vector2i r1, Vector2i h0, Vecto const auto x1 = xs[(uint8_t)c.x1]; const auto y0 = ys[(uint8_t)c.y0]; const auto y1 = ys[(uint8_t)c.y1]; - return rect{ Vector2i{x0, y0}, Vector2i{x1, y1} }; + return rect{ {x0, y0}, {x1, y1} }; } +template [[nodiscard]] -constexpr bool -check_empty(Vector2i r0, Vector2i r1, Vector2i h0, Vector2i h1, Vector2ub input_bb, Vector2ub hole_bb) +constexpr bool check_empty(Vec2ʹ r0, Vec2ʹ r1, Vec2ʹ h0, Vec2ʹ h1) { - bool iempty = Vector2ui{input_bb}.product() == 0; - bool hempty = Vector2ui{hole_bb}.product() == 0; + bool iempty = r0.x() == r1.x() | r0.y() == r1.y(); + bool hempty = h0.x() == h1.x() | h0.y() == h1.y(); bool empty_before_x = h1.x() <= r0.x(); bool empty_after_x = h0.x() >= r1.x(); bool empty_before_y = h1.y() <= r0.y(); bool empty_after_y = h0.y() >= r1.y(); - return iempty | hempty | empty_before_x | empty_after_x | empty_before_y | empty_after_y; } -constexpr cut_rectangle_result cut_rectangleʹ(bbox input, bbox hole) +template +constexpr cut_rectangle_result cut_rectangle(Vec2ʹ r0, Vec2ʹ r1, Vec2ʹ h0, Vec2ʹ h1) { - auto ihalf = Vector2i{input.bbox_size/2}; - auto r0 = input.position - ihalf; - auto r1 = input.position + Vector2i{input.bbox_size} - ihalf; - - auto hhalf = Vector2i{hole.bbox_size/2}; - auto h0 = hole.position - hhalf; - auto h1 = hole.position + Vector2i{hole.bbox_size} - hhalf; - - if (check_empty(r0, r1, h0, h1, input.bbox_size, hole.bbox_size)) + if (check_empty(r0, r1, h0, h1)) return { .size = 1, - .array = {{ rect { r0, r1 }, }}, + .array = {{ { r0, r1 }, }}, }; const bool sx = h0.x() <= r0.x(); @@ -169,23 +168,45 @@ constexpr cut_rectangle_result cut_rectangleʹ(bbox input, bbox hole) CORRADE_ASSUME(val < 16); const auto elt = elements[val]; const auto sz = elt.size; - cut_rectangle_result res = { + cut_rectangle_result res = { .size = sz, .array = {}, }; - CORRADE_ASSUME(sz <= 8); - - for (auto i = 0u; i < sz; i++) - res.array[i] = get_value_from_coord(r0, r1, h0, h1, elt.array[i]); + for (auto i = 0u; i < 8; i++) + res.array[i] = get_value_from_coord(r0, r1, h0, h1, elt.array[i]); return res; } -} // namespace - -cut_rectangle_result cut_rectangle(bbox input, bbox hole) +template +constexpr cut_rectangle_result cut_rectangle(bbox input, bbox hole) { - return cut_rectangleʹ(input, hole); + using Vec2 = Vec2ʹ; + + auto ihalf = Vec2{input.bbox_size/2}; + auto r0 = input.position - ihalf; + auto r1 = input.position + Vec2{input.bbox_size} - ihalf; + + auto hhalf = Vec2{hole.bbox_size/2}; + auto h0 = hole.position - hhalf; + auto h1 = hole.position + Vec2{hole.bbox_size} - hhalf; + + return cut_rectangle(r0, r1, h0, h1); } + +} // namespace + +template struct cut_rectangle_result; +template struct cut_rectangle_result; + +template crr cut_rectangle_result::cut(bbox input, bbox hole) { return cut_rectangle(input, hole); } +template crr cut_rectangle_result::cut(Vec2 r0, Vec2 r1, Vec2 h0, Vec2 h1) { return cut_rectangle(r0, r1, h0, h1); } + +template cut_rectangle_result cut_rectangle_result::cut(bbox input, bbox hole); +template cut_rectangle_result cut_rectangle_result::cut(bbox input, bbox hole); + +template cut_rectangle_result cut_rectangle_result::cut(Vector2i r0, Vector2i r1, Vector2i h0, Vector2i h1); +template cut_rectangle_result cut_rectangle_result::cut(Vector2 r0, Vector2 r1, Vector2 h0, Vector2 h1); + } // namespace floormat diff --git a/src/hole.hpp b/src/hole.hpp index 6ec1c666..384a5044 100644 --- a/src/hole.hpp +++ b/src/hole.hpp @@ -1,6 +1,7 @@ #pragma once #include "object.hpp" #include +#include namespace floormat { @@ -54,17 +55,18 @@ private: void mark_chunk_modified(); }; -struct cut_rectangle_result +template +struct cut_rectangle_result // todo rename { - struct bbox { Vector2i position; Vector2ub bbox_size; }; - struct rect { Vector2i min, max; }; + using Vec2 = VectorTypeFor<2, T>; + struct bbox { Vec2 position; Vector2ub bbox_size; }; + struct rect { Vec2 min, max; }; + + static cut_rectangle_result cut(bbox input, bbox hole); + static cut_rectangle_result cut(Vec2 r0, Vec2 r1, Vec2 h0, Vec2 h1); uint8_t size = 0; std::array array; - - operator ArrayView() const; }; -cut_rectangle_result cut_rectangle(cut_rectangle_result::bbox input, cut_rectangle_result::bbox hole); - } // namespace floormat diff --git a/src/object.cpp b/src/object.cpp index 72906320..a3df2ef9 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -4,6 +4,7 @@ #include "rotation.inl" #include "anim-atlas.hpp" #include "src/RTree-search.hpp" +#include "rect-intersects.hpp" #include "src/timer.hpp" #include "compat/debug.hpp" #include "compat/exception.hpp" diff --git a/src/rect-intersects.hpp b/src/rect-intersects.hpp new file mode 100644 index 00000000..25d838d6 --- /dev/null +++ b/src/rect-intersects.hpp @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace floormat { + +template +requires (std::is_same_v || std::is_same_v) +constexpr bool rect_intersects(Math::Vector<2, T> min1, Math::Vector<2, T> max1, Math::Vector<2, T> min2, Math::Vector<2, T> max2) +{ + return min1.data()[0] < max2.data()[0] && max1.data()[0] > min2.data()[0] && + min1.data()[1] < max2.data()[1] && max1.data()[1] > min2.data()[1]; +} + +} // namespace floormat diff --git a/src/search.cpp b/src/search.cpp index 68ec0cce..220e3869 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -6,6 +6,7 @@ #include "world.hpp" #include "pass-mode.hpp" #include "RTree-search.hpp" +#include "rect-intersects.hpp" #include "compat/array-size.hpp" #include "compat/function2.hpp" #include -- cgit v1.2.3