use raygui

This commit is contained in:
Fabien Freling 2022-02-15 13:17:14 +01:00
parent 8d91216f24
commit 81463d9eb0
9 changed files with 4589 additions and 83 deletions

View file

@ -2,6 +2,15 @@
The subject is available here: [Test Algo](./test_algo.pdf) The subject is available here: [Test Algo](./test_algo.pdf)
## TODO
- [ ] document
- [ ] dumb packing
- [X] add raygui
- [ ] add box in gui by pressing down
- [ ] delete box in gui by clicking
- [ ] wrap stb_rect_pack?
- [ ] change bbox api to origin + size
## Installation ## Installation
## Notes ## Notes
@ -36,6 +45,12 @@ I use 2 external libraries for better visualization:
I don't rely on them for the algorithm implementation and the core of the I don't rely on them for the algorithm implementation and the core of the
exercise doesn't rely on external libraries. exercise doesn't rely on external libraries.
To avoid name collision, I created my own namespace `freling`.
## References ## References
- [Exploring rectangle packing algorithms](https://www.david-colson.com/2020/03/10/exploring-rect-packing.html)
- [A Skyline-Based Heuristic for the 2D Rectangular Strip Packing Problem](https://www.researchgate.net/publication/221049934_A_Skyline-Based_Heuristic_for_the_2D_Rectangular_Strip_Packing_Problem)
- [New Improvements in Optimal Rectangle Packing](https://www.ijcai.org/Proceedings/09/Papers/092.pdf)
[1]: https://en.wikipedia.org/wiki/Packing_problems#Packing_of_rectangles [1]: https://en.wikipedia.org/wiki/Packing_problems#Packing_of_rectangles

View file

@ -3,6 +3,9 @@ exe_cli := "./packing_cli"
exe_gui := "./packing_gui" exe_gui := "./packing_gui"
build_sh := "build.sh" build_sh := "build.sh"
params := "lenna.png 0 0 64 64 100 100 200 164 80 200 150 420"
build-raylib: build-raylib:
cd src/3rd-party/raylib-4.0.0/src && \ cd src/3rd-party/raylib-4.0.0/src && \
make CC=clang \ make CC=clang \
@ -17,10 +20,10 @@ build-gui: build-raylib
tup {{exe_gui}} tup {{exe_gui}}
run-cli: build-cli run-cli: build-cli
{{exe_cli}} lenna.png 0 0 64 64 100 100 200 164 80 200 150 420 {{exe_cli}} {{params}}
run-gui: build-gui run-gui: build-gui
nixGL {{exe_gui}} lenna.png 0 0 64 64 100 100 200 164 80 200 150 420 nixGL {{exe_gui}} {{params}}
generate-build: generate-build:
git clean -xf src/ git clean -xf src/
@ -28,7 +31,7 @@ generate-build:
tup generate {{build_sh}} tup generate {{build_sh}}
debug: build-cli debug: build-cli
lldb {{exe_cli}} lldb {{exe_cli}} {{params}}
archive: generate-build archive: generate-build
git archive --add-file={{build_sh}} --output={{name}}.zip --prefix={{name}}/ HEAD git archive --add-file={{build_sh}} --output={{name}}.zip --prefix={{name}}/ HEAD

4441
src/3rd-party/raylib-4.0.0/src/raygui.h vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,7 @@
#include "bounding_box.h" #include "bounding_box.h"
namespace freling {
int BoundingBox::width() const { int BoundingBox::width() const {
return right - left; return right - left;
} }
@ -16,3 +18,5 @@ bool BoundingBox::operator==(const BoundingBox& b) const {
return left == b.left and top == b.top and right == b.right and return left == b.left and top == b.top and right == b.right and
bottom == b.bottom; bottom == b.bottom;
} }
} // namespace freling

View file

@ -2,6 +2,8 @@
#include <cstdint> #include <cstdint>
namespace freling {
struct BoundingBox { struct BoundingBox {
uint32_t left; uint32_t left;
uint32_t top; uint32_t top;
@ -14,3 +16,5 @@ struct BoundingBox {
bool operator==(const BoundingBox& b) const; bool operator==(const BoundingBox& b) const;
}; };
} // namespace freling

View file

@ -32,18 +32,18 @@ int main(int argc, const char* argv[]) {
} }
int i = 2; int i = 2;
std::vector<BoundingBox> bboxes; std::vector<freling::BoundingBox> bboxes;
bboxes.reserve((argc - 2) / 4); bboxes.reserve((argc - 2) / 4);
while (i < argc) { while (i < argc) {
const uint32_t x1 = atoi(argv[i]); const uint32_t x1 = atoi(argv[i]);
const uint32_t y1 = atoi(argv[i + 1]); const uint32_t y1 = atoi(argv[i + 1]);
const uint32_t x2 = atoi(argv[i + 2]); const uint32_t x2 = atoi(argv[i + 2]);
const uint32_t y2 = atoi(argv[i + 3]); const uint32_t y2 = atoi(argv[i + 3]);
bboxes.push_back(BoundingBox({x1, y1, x2, y2})); bboxes.push_back(freling::BoundingBox({x1, y1, x2, y2}));
i += 4; i += 4;
} }
Frame regions = pack(*in_frame, bboxes); std::optional<Frame> regions = pack(*in_frame, bboxes);
return 0; return 0;
} }

View file

@ -5,9 +5,12 @@
#include <optional> #include <optional>
#include <vector> #include <vector>
namespace raylib {
#include <raylib.h> #include <raylib.h>
} #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-result"
#define RAYGUI_IMPLEMENTATION
#include <raygui.h>
#pragma clang diagnostic pop
#include "bounding_box.h" #include "bounding_box.h"
#include "frame.h" #include "frame.h"
@ -17,44 +20,40 @@ namespace raylib {
namespace fs = std::filesystem; namespace fs = std::filesystem;
struct Button { struct Button {
raylib::Rectangle rect; Rectangle rect;
std::string text; std::string text;
bool hover = false; bool hover = false;
bool pressed() { bool pressed() {
hover = hover = CheckCollisionPointRec(GetMousePosition(), rect);
raylib::CheckCollisionPointRec(raylib::GetMousePosition(), rect); return hover and IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
return hover and
raylib::IsMouseButtonReleased(raylib::MOUSE_BUTTON_LEFT);
}; };
void draw() const { void draw() const {
raylib::DrawRectangleRec(rect, raylib::LIGHTGRAY); DrawRectangleRec(rect, LIGHTGRAY);
raylib::DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLUE);
raylib::BLUE); DrawText(text.c_str(),
raylib::DrawText(text.c_str(), (rect.x + rect.width / 2 - MeasureText(text.c_str(), 10) / 2),
(rect.x + rect.width / 2 - rect.y + 11, 10, DARKBLUE);
raylib::MeasureText(text.c_str(), 10) / 2),
rect.y + 11, 10, raylib::DARKBLUE);
}; };
}; };
void draw(const BoundingBox& box, void draw(const freling::BoundingBox& box,
const raylib::Vector2& offset, const Vector2& offset,
const raylib::Color& color) { const Color& color) {
const raylib::Rectangle rect = {offset.x + box.left, offset.y + box.top, const Rectangle rect = {offset.x + box.left, offset.y + box.top,
static_cast<float>(box.right - box.left), static_cast<float>(box.right - box.left),
static_cast<float>(box.bottom - box.top)}; static_cast<float>(box.bottom - box.top)};
raylib::DrawRectangleRec(rect, raylib::ColorAlpha(color, 0.3)); DrawRectangleRec(rect, ColorAlpha(color, 0.3));
raylib::DrawRectangleLinesEx(rect, 3, color); DrawRectangleLinesEx(rect, 3, color);
} }
raylib::Image image_from_frame(const Frame& frame) { Image image_from_frame(const Frame& frame) {
raylib::Image image = {0}; Image image = {0};
const int format = raylib::PIXELFORMAT_UNCOMPRESSED_R8G8B8; const int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8;
const unsigned int size = const unsigned int size =
raylib::GetPixelDataSize(frame.width, frame.height, format); GetPixelDataSize(frame.width, frame.height, format);
image.data = RL_MALLOC(size); image.data = RL_MALLOC(size);
memcpy(image.data, frame.data, size); memcpy(image.data, frame.data, size);
@ -87,7 +86,7 @@ int main(int argc, const char* argv[]) {
return 1; return 1;
} }
std::vector<BoundingBox> bboxes; std::vector<freling::BoundingBox> bboxes;
bboxes.reserve((argc - 2) / 4); bboxes.reserve((argc - 2) / 4);
int i = 2; int i = 2;
while (i < argc) { while (i < argc) {
@ -95,54 +94,60 @@ int main(int argc, const char* argv[]) {
const uint32_t y1 = atoi(argv[i + 1]); const uint32_t y1 = atoi(argv[i + 1]);
const uint32_t x2 = atoi(argv[i + 2]); const uint32_t x2 = atoi(argv[i + 2]);
const uint32_t y2 = atoi(argv[i + 3]); const uint32_t y2 = atoi(argv[i + 3]);
bboxes.push_back(BoundingBox({x1, y1, x2, y2})); bboxes.push_back(freling::BoundingBox({x1, y1, x2, y2}));
i += 4; i += 4;
} }
const std::vector<raylib::Color> bbox_colors = {raylib::RED, raylib::GREEN, const std::vector<Color> bbox_colors = {RED, GREEN, BLUE};
raylib::BLUE};
raylib::Vector2 win_size = {800, 450}; Vector2 win_size = {800, 450};
const int padding = 5; const int padding = 5;
const raylib::Vector2 in_offset = {padding, 40}; const Vector2 in_offset = {padding, 40};
raylib::InitWindow(win_size.x, win_size.y, "netatmo - rect packing"); InitWindow(win_size.x, win_size.y, "netatmo - rect packing");
raylib::SetTargetFPS(60); SetTargetFPS(60);
raylib::Image in_img = image_from_frame(*in_frame); Image in_img = image_from_frame(*in_frame);
raylib::Texture2D in_texture = raylib::LoadTextureFromImage(in_img); Texture2D in_texture = LoadTextureFromImage(in_img);
raylib::Image pack_img; Image pack_img;
raylib::Texture2D pack_texture; Texture2D pack_texture;
bool packed = false; bool packed = false;
win_size.x = std::max(win_size.x, in_offset.x + in_texture.width + padding); win_size.x = std::max(win_size.x, in_offset.x + in_texture.width + padding);
win_size.y = win_size.y =
std::max(win_size.y, in_offset.y + in_texture.height + padding); std::max(win_size.y, in_offset.y + in_texture.height + padding);
raylib::SetWindowSize(win_size.x, win_size.y); SetWindowSize(win_size.x, win_size.y);
const raylib::Vector2 b_size = {150, 30}; const Vector2 b_size = {150, 30};
Button b_add_box = {{padding, padding, b_size.x, b_size.y}, "Add box"}; Button b_add_box = {{padding, padding, b_size.x, b_size.y}, "Add box"};
Button b_pack = {{padding * 2 + b_size.x, padding, b_size.x, b_size.y}, Button b_pack = {{padding * 2 + b_size.x, padding, b_size.x, b_size.y},
"Pack rects"}; "Pack rects"};
while (!raylib::WindowShouldClose()) { while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
// //
// Update // Buttons
// //
if (b_add_box.pressed()) { if (GuiButton((Rectangle){padding, padding, b_size.x, b_size.y},
bboxes.push_back(BoundingBox({0, 0, 64, 64})); "Add Box")) {
bboxes.push_back(freling::BoundingBox({0, 0, 64, 64}));
} }
if (b_pack.pressed()) {
Frame f_packed = pack(*in_frame, bboxes); if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x,
b_size.y},
"Pack Rects")) {
std::optional<Frame> f_packed = pack(*in_frame, bboxes);
if (packed) { if (packed) {
raylib::UnloadTexture(pack_texture); UnloadTexture(pack_texture);
raylib::UnloadImage(pack_img); UnloadImage(pack_img);
} }
pack_img = image_from_frame(f_packed); pack_img = image_from_frame(*f_packed);
pack_texture = raylib::LoadTextureFromImage(pack_img); pack_texture = LoadTextureFromImage(pack_img);
packed = true; packed = true;
win_size.x = win_size.x =
@ -153,23 +158,15 @@ int main(int argc, const char* argv[]) {
in_offset.y + std::max(in_texture.height, pack_texture.height) + in_offset.y + std::max(in_texture.height, pack_texture.height) +
padding); padding);
raylib::SetWindowSize(win_size.x, win_size.y); SetWindowSize(win_size.x, win_size.y);
} }
// //
// Draw
//
raylib::BeginDrawing();
raylib::ClearBackground(raylib::RAYWHITE);
b_add_box.draw();
b_pack.draw();
// Input image // Input image
raylib::DrawTexture(in_texture, in_offset.x, in_offset.y, //
raylib::WHITE); DrawTexture(in_texture, in_offset.x, in_offset.y, WHITE);
raylib::DrawRectangleLines(in_offset.x, in_offset.y, in_texture.width, DrawRectangleLines(in_offset.x, in_offset.y, in_texture.width,
in_texture.height, raylib::BLACK); in_texture.height, BLACK);
int c = 0; int c = 0;
for (const auto& b : bboxes) { for (const auto& b : bboxes) {
@ -178,27 +175,27 @@ int main(int argc, const char* argv[]) {
++c; ++c;
}; };
//
// Packed image // Packed image
//
if (packed) { if (packed) {
const int offset_x = in_offset.x + in_texture.width + padding; const int offset_x = in_offset.x + in_texture.width + padding;
raylib::DrawTexture(pack_texture, offset_x, in_offset.y, DrawTexture(pack_texture, offset_x, in_offset.y, WHITE);
raylib::WHITE); DrawRectangleLines(offset_x, in_offset.y, pack_texture.width,
raylib::DrawRectangleLines(offset_x, in_offset.y, pack_texture.height, BLACK);
pack_texture.width, pack_texture.height,
raylib::BLACK);
} }
raylib::EndDrawing(); EndDrawing();
} }
// Cleanup // Cleanup
raylib::UnloadTexture(in_texture); UnloadTexture(in_texture);
raylib::UnloadImage(in_img); UnloadImage(in_img);
if (packed) { if (packed) {
raylib::UnloadTexture(pack_texture); UnloadTexture(pack_texture);
raylib::UnloadImage(pack_img); UnloadImage(pack_img);
} }
raylib::CloseWindow(); CloseWindow();
return 0; return 0;
} }

View file

@ -1,10 +1,40 @@
#include "pack.h" #include "pack.h"
#include <algorithm> #include <algorithm>
#include <cassert>
#include <cmath> #include <cmath>
#include <cstring>
#include <iostream> #include <iostream>
Frame pack(const Frame& in_frame, const std::vector<BoundingBox>& bboxes) { namespace freling {
void blit(const Frame& in_frame,
const BoundingBox& in_box,
Frame& out_frame,
const BoundingBox& out_box) {
assert(in_box.width() == out_box.width());
assert(in_box.height() == out_box.height());
const int data_width = in_box.width() * sizeof(Pixel);
const int in_row_size = in_frame.width * sizeof(Pixel);
const int out_row_size = out_frame.width * sizeof(Pixel);
for (unsigned int i = 0; i < in_box.height(); ++i) {
const int in_offset =
in_box.left * sizeof(Pixel) + (i + in_box.top) * in_row_size;
const int out_offset =
out_box.left * sizeof(Pixel) + (i + out_box.top) * out_row_size;
memcpy(out_frame.data + out_offset, in_frame.data + in_offset,
data_width);
}
}
std::optional<Frame> pack(const Frame& in_frame,
const std::vector<BoundingBox>& bboxes) {
if (bboxes.size() == 0) {
std::cerr << "No bounding box, cannot pack.\n";
return {};
}
// We sort the bounding boxes by maximum area // We sort the bounding boxes by maximum area
std::vector<BoundingBox> sorted_bboxes = bboxes; std::vector<BoundingBox> sorted_bboxes = bboxes;
std::sort(sorted_bboxes.begin(), sorted_bboxes.end(), std::sort(sorted_bboxes.begin(), sorted_bboxes.end(),
@ -38,6 +68,12 @@ Frame pack(const Frame& in_frame, const std::vector<BoundingBox>& bboxes) {
std::cout << "maximum image dimention: " << in_min_dim << " x " std::cout << "maximum image dimention: " << in_min_dim << " x "
<< in_min_dim << "\n"; << in_min_dim << "\n";
// TODO const auto& largest = sorted_bboxes[0];
BoundingBox out_largest = {0, 0, static_cast<uint32_t>(largest.width()),
static_cast<uint32_t>(largest.height())};
Frame packed_frame(largest.width(), largest.height());
blit(in_frame, largest, packed_frame, out_largest);
return Frame(in_frame); return Frame(in_frame);
} }
} // namespace freling

View file

@ -1,8 +1,14 @@
#pragma once #pragma once
#include <optional>
#include <vector> #include <vector>
#include "bounding_box.h" #include "bounding_box.h"
#include "frame.h" #include "frame.h"
Frame pack(const Frame& in_frame, const std::vector<BoundingBox>& bboxes); namespace freling {
std::optional<Frame> pack(const Frame& in_frame,
const std::vector<BoundingBox>& bboxes);
}