diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-26 13:58:27 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-26 15:12:25 +0200 |
commit | 30f06fac317668c7da977edf13085a0e0e9df964 (patch) | |
tree | 57ca6692d772f98ecbdda2e0d739a69e125f8d52 /src/hole.cpp | |
parent | 0978386d9938a7d3f205ec4419bd5213d5f0a579 (diff) |
wa
Diffstat (limited to 'src/hole.cpp')
-rw-r--r-- | src/hole.cpp | 62 |
1 files changed, 37 insertions, 25 deletions
diff --git a/src/hole.cpp b/src/hole.cpp index 5551b780..396d6d44 100644 --- a/src/hole.cpp +++ b/src/hole.cpp @@ -13,6 +13,7 @@ namespace floormat { namespace { using bbox = cut_rectangle_result::bbox; +using rect = 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, }; @@ -33,6 +34,8 @@ struct element constexpr element make_element(uint8_t s) { + // ReSharper disable CppIdenticalOperandsInBinaryExpression + // NOLINTBEGIN(*-simplify) switch (s) { using enum location; @@ -110,11 +113,38 @@ constexpr element make_element(uint8_t s) {R0, H0, H0, H1}, }}}; } - fm_assert(false); + // NOLINTEND(*-simplify) + // ReSharper restore CppIdenticalOperandsInBinaryExpression + std::unreachable(); } constexpr auto elements = map(make_element, iota_array<uint8_t, 16>); +constexpr auto get_value_from_coord(Vector2i r0, Vector2i r1, Vector2i h0, Vector2i 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(), }; + const auto x0 = xs[(uint8_t)c.x0]; + 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} }; +} + +[[nodiscard]] +constexpr bool +check_empty(Vector2i r0, Vector2i r1, Vector2i h0, Vector2i h1, Vector2ub input_bb, Vector2ub hole_bb) +{ + bool iempty = Vector2ui{input_bb}.product() == 0; + bool hempty = Vector2ui{hole_bb}.product() == 0; + 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) { auto ihalf = Vector2i{input.bbox_size/2}; @@ -125,17 +155,8 @@ constexpr cut_rectangle_result cut_rectangleʹ(bbox input, bbox hole) auto h0 = hole.position - hhalf; auto h1 = hole.position + Vector2i{hole.bbox_size} - hhalf; - { - bool iempty = Vector2ui{input.bbox_size}.product() == 0; - bool hempty = Vector2ui{hole.bbox_size}.product() == 0; - 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(); - - if (iempty | hempty | empty_before_x | empty_after_x | empty_before_y | empty_after_y) [[unlikely]] - return { 0, {} }; - } + if (check_empty(r0, r1, h0, h1, input.bbox_size, hole.bbox_size)) + return {0, {}}; const bool sx = h0.x() <= r0.x(); const bool ex = h1.x() >= r1.x(); @@ -150,20 +171,11 @@ constexpr cut_rectangle_result cut_rectangleʹ(bbox input, bbox hole) .array = {}, }; - const Int xs[4] = { r0.x(), r1.x(), h0.x(), h1.x(), }; - const Int ys[4] = { r0.y(), r1.y(), h0.y(), h1.y(), }; - - for (auto i = 0uz; i < elt.size; i++) - { - const auto e = elt.array[i]; - - const auto x0 = xs[(uint8_t)e.x0]; - const auto x1 = xs[(uint8_t)e.x1]; - const auto y0 = ys[(uint8_t)e.y0]; - const auto y1 = ys[(uint8_t)e.y1]; + const auto sz = elt.size; + CORRADE_ASSUME(sz <= 8); - res.array[i] = { {x0, y0}, {x1, y1}, }; - } + for (auto i = 0u; i < sz; i++) + res.array[i] = get_value_from_coord(r0, r1, h0, h1, elt.array[i]); return res; } |