From 4447cd67d1036c5c696c8299bad11ec38b0777b2 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Wed, 16 Feb 2022 19:16:17 +0100 Subject: [PATCH] map regions in origin --- README.md | 2 +- src/a2.cpp | 8 ++++---- src/bounding_box.cpp | 4 ++++ src/bounding_box.h | 1 + src/main_gui.cpp | 39 ++++++++++++++++++++++++++++++++------- src/mapping.cpp | 28 ++++++++++++++++++++++++++++ src/mapping.h | 12 ++++++++++++ 7 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 src/mapping.cpp create mode 100644 src/mapping.h diff --git a/README.md b/README.md index 3cb1ca0..83a03e7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ The subject is available here: [Test Algo](./test_algo.pdf) - [X] delete box in gui by clicking - [ ] wrap stb_rect_pack? - [X] change bbox api to origin + size -- [ ] dummy A2 +- [X] dummy A2 ## Installation diff --git a/src/a2.cpp b/src/a2.cpp index fe50fdb..376e416 100644 --- a/src/a2.cpp +++ b/src/a2.cpp @@ -7,10 +7,10 @@ std::vector A2(const std::vector& regions) { for (int i = 0, size = regions.size(); i < size; ++i) { auto& box = updated_boxes[i]; const auto& region = regions[i]; - box.x = region.x + 2; - box.y = region.y + 2; - box.width = region.width - 4; - box.height = region.height - 4; + box.x = region.x + 10; + box.y = region.y + 10; + box.width = region.width - 20; + box.height = region.height - 20; } return updated_boxes; } diff --git a/src/bounding_box.cpp b/src/bounding_box.cpp index 0c69147..077038d 100644 --- a/src/bounding_box.cpp +++ b/src/bounding_box.cpp @@ -5,6 +5,10 @@ namespace freling { int BoundingBox::area() const { return width * height; } +bool BoundingBox::contains(int x, int y) const { + return this->x <= x and x <= this->x + this->width and this->y <= y and + y <= this->y + this->height; +} bool BoundingBox::operator==(const BoundingBox& b) const { return x == b.x and y == b.y and width == b.width and height == b.height; diff --git a/src/bounding_box.h b/src/bounding_box.h index cc75675..0e0cf66 100644 --- a/src/bounding_box.h +++ b/src/bounding_box.h @@ -11,6 +11,7 @@ struct BoundingBox { uint32_t height; int area() const; + bool contains(int x, int y) const; bool operator==(const BoundingBox& b) const; }; diff --git a/src/main_gui.cpp b/src/main_gui.cpp index 101b141..a315184 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -16,6 +16,7 @@ #include "a2.h" #include "bounding_box.h" #include "frame.h" +#include "mapping.h" #include "pack.h" #include "png.h" @@ -31,12 +32,12 @@ Rectangle rect_from_bbox(const freling::BoundingBox& box, return rect; } -void draw(const freling::BoundingBox& box, - const Vector2& offset, - const Color& color) { +void draw_resizable(const freling::BoundingBox& box, + const Vector2& offset, + const Color& color) { const Rectangle rect = rect_from_bbox(box, offset); DrawRectangleRec(rect, ColorAlpha(color, 0.3)); - DrawRectangleLinesEx(rect, 3, color); + DrawRectangleLinesEx(rect, 2, color); DrawTriangle( (Vector2){rect.x + rect.width - resize_handle, rect.y + rect.height}, (Vector2){rect.x + rect.width, rect.y + rect.height}, @@ -44,6 +45,13 @@ void draw(const freling::BoundingBox& box, color); } +void draw_region(const freling::BoundingBox& box, + const Vector2& offset, + const Color& color) { + const Rectangle rect = rect_from_bbox(box, offset); + DrawRectangleRec(rect, ColorAlpha(color, 0.8)); +} + Image image_from_frame(const Frame& frame) { Image image = {0}; @@ -97,6 +105,7 @@ int main(int argc, const char* argv[]) { YELLOW, ORANGE, PURPLE}; std::vector packed_bboxes; std::vector a2_bboxes; + std::vector a2_bboxes_origin; Vector2 win_size = {800, 450}; const int padding = 5; @@ -162,6 +171,7 @@ int main(int argc, const char* argv[]) { auto& box = bboxes[b_moving_idx]; box.x += mouse_delta.x; box.y += mouse_delta.y; + packed = false; } if (b_resize_idx != idx_unset) { auto& box = bboxes[b_resize_idx]; @@ -169,11 +179,13 @@ int main(int argc, const char* argv[]) { resize_handle); box.height = std::max(static_cast(box.height + mouse_delta.y), resize_handle); + packed = false; } if (b_delete_idx != idx_unset) { bboxes.erase(bboxes.begin() + b_delete_idx); b_delete_idx = idx_unset; } + a2_processed = a2_processed and packed; // // Draw @@ -192,6 +204,7 @@ int main(int argc, const char* argv[]) { if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x, b_size.y}, "Pack Rects")) { + a2_processed = false; std::optional f_packed = freling::pack(*in_frame, bboxes, packed_bboxes); @@ -219,6 +232,8 @@ int main(int argc, const char* argv[]) { b_size.x, b_size.y}, "A2")) { a2_bboxes = freling::A2(packed_bboxes); + a2_bboxes_origin = + freling::map_to_origin(bboxes, packed_bboxes, a2_bboxes); a2_processed = true; } @@ -232,10 +247,19 @@ int main(int argc, const char* argv[]) { int c = 0; for (const auto& b : bboxes) { const auto& color = bbox_colors[c % bbox_colors.size()]; - draw(b, in_offset, color); + draw_resizable(b, in_offset, color); ++c; }; + c = 0; + if (a2_processed) { + for (const auto& b : a2_bboxes_origin) { + const auto& color = bbox_colors[c % bbox_colors.size()]; + draw_region(b, in_offset, color); + ++c; + }; + } + // // Packed image // @@ -249,7 +273,8 @@ int main(int argc, const char* argv[]) { int c = 0; for (const auto& b : packed_bboxes) { const auto& color = bbox_colors[c % bbox_colors.size()]; - draw(b, pack_offset, color); + const Rectangle rect = rect_from_bbox(b, pack_offset); + DrawRectangleLinesEx(rect, 2, color); ++c; }; @@ -257,7 +282,7 @@ int main(int argc, const char* argv[]) { int c = 0; for (const auto& b : a2_bboxes) { const auto& color = bbox_colors[c % bbox_colors.size()]; - draw(b, pack_offset, color); + draw_region(b, pack_offset, color); ++c; }; } diff --git a/src/mapping.cpp b/src/mapping.cpp new file mode 100644 index 0000000..e91983e --- /dev/null +++ b/src/mapping.cpp @@ -0,0 +1,28 @@ +#include "mapping.h" + +namespace freling { + +std::vector map_to_origin( + const std::vector& origin_bboxes, + const std::vector& packed_bboxes, + const std::vector& a2_bboxes) { + std::vector mapped_bboxes(a2_bboxes.size()); + for (int i = 0, i_max = a2_bboxes.size(); i < i_max; ++i) { + const auto& a2_box = a2_bboxes[i]; + for (int j = 0, j_max = packed_bboxes.size(); j < j_max; ++j) { + const auto& packed_box = packed_bboxes[j]; + if (packed_box.contains(a2_box.x, a2_box.y)) { + const auto& origin_box = origin_bboxes[j]; + auto& mapped_box = mapped_bboxes[i]; + mapped_box.width = a2_box.width; + mapped_box.height = a2_box.height; + mapped_box.x = a2_box.x - packed_box.x + origin_box.x; + mapped_box.y = a2_box.y - packed_box.y + origin_box.y; + break; + } + } + } + return mapped_bboxes; +} + +} // namespace freling diff --git a/src/mapping.h b/src/mapping.h new file mode 100644 index 0000000..23d2da1 --- /dev/null +++ b/src/mapping.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "bounding_box.h" + +namespace freling { +std::vector map_to_origin( + const std::vector& origin_bboxes, + const std::vector& packed_bboxes, + const std::vector& a2_bboxes); +}