From 8d91216f244f88f9cfc78aa20233bd7d65bc469c Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 14 Feb 2022 00:13:09 +0100 Subject: [PATCH] start packing --- justfile | 2 +- src/bounding_box.cpp | 18 ++++++++++++++++++ src/bounding_box.h | 6 ++++++ src/pack.cpp | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/bounding_box.cpp diff --git a/justfile b/justfile index 0a6d044..29e89ca 100644 --- a/justfile +++ b/justfile @@ -17,7 +17,7 @@ build-gui: build-raylib tup {{exe_gui}} run-cli: build-cli - {{exe_cli}} lenna.png 0 0 0 0 + {{exe_cli}} lenna.png 0 0 64 64 100 100 200 164 80 200 150 420 run-gui: build-gui nixGL {{exe_gui}} lenna.png 0 0 64 64 100 100 200 164 80 200 150 420 diff --git a/src/bounding_box.cpp b/src/bounding_box.cpp new file mode 100644 index 0000000..c1412af --- /dev/null +++ b/src/bounding_box.cpp @@ -0,0 +1,18 @@ +#include "bounding_box.h" + +int BoundingBox::width() const { + return right - left; +} + +int BoundingBox::height() const { + return bottom - top; +} + +int BoundingBox::area() const { + return width() * height(); +} + +bool BoundingBox::operator==(const BoundingBox& b) const { + return left == b.left and top == b.top and right == b.right and + bottom == b.bottom; +} diff --git a/src/bounding_box.h b/src/bounding_box.h index a982cd1..0b3104f 100644 --- a/src/bounding_box.h +++ b/src/bounding_box.h @@ -7,4 +7,10 @@ struct BoundingBox { uint32_t top; uint32_t right; uint32_t bottom; + + int width() const; + int height() const; + int area() const; + + bool operator==(const BoundingBox& b) const; }; diff --git a/src/pack.cpp b/src/pack.cpp index ed0fb0f..a046a5e 100644 --- a/src/pack.cpp +++ b/src/pack.cpp @@ -1,6 +1,43 @@ #include "pack.h" +#include +#include +#include + Frame pack(const Frame& in_frame, const std::vector& bboxes) { + // We sort the bounding boxes by maximum area + std::vector sorted_bboxes = bboxes; + std::sort(sorted_bboxes.begin(), sorted_bboxes.end(), + [](const auto& a, const auto& b) { return a.area() > b.area(); }); + + // We keep a mapping between the sorted bounding boxes and the original + // order + std::vector mapping(bboxes.size()); + for (int i = 0; i < sorted_bboxes.size(); ++i) { + const auto& s_box = sorted_bboxes[i]; + for (int j = 0; j < bboxes.size(); ++j) { + const auto& box = bboxes[j]; + if (box == s_box) { + mapping[i] = j; + continue; + } + } + } + + int max_area = 0; + for (const auto& box : sorted_bboxes) { + int area = box.area(); + std::cout << "bounding box area: " << area << "\n"; + max_area += area; + } + std::cout << "max area: " << max_area << "\n"; + int min_dim = int(ceil(std::sqrt(max_area))); + std::cout << "optimal image dimention: " << min_dim << " x " << min_dim + << "\n"; + const int in_min_dim = std::min(in_frame.width, in_frame.height); + std::cout << "maximum image dimention: " << in_min_dim << " x " + << in_min_dim << "\n"; + // TODO return Frame(in_frame); }