implement naive packer

This commit is contained in:
Fabien Freling 2022-02-15 21:18:50 +01:00
parent 8698cf8160
commit e31683d2fe
8 changed files with 114 additions and 78 deletions

3
.gitignore vendored
View file

@ -2,3 +2,6 @@
packing_*
build.sh
*.a
callgrind.out.*
massif.out.*

View file

@ -4,12 +4,13 @@ The subject is available here: [Test Algo](./test_algo.pdf)
## TODO
- [ ] document
- [ ] dumb packing
- [X] dumb packing
- [ ] skyline
- [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
- [X] change bbox api to origin + size
## Installation

View file

@ -3,7 +3,7 @@ exe_cli := "./packing_cli"
exe_gui := "./packing_gui"
build_sh := "build.sh"
params := "lenna.png 0 0 64 64 100 100 200 164 80 200 150 420"
params := "lenna.png 0 0 64 64 100 100 100 64 80 200 70 220"
build-raylib:
@ -33,5 +33,15 @@ generate-build:
debug: build-cli
lldb {{exe_cli}} {{params}}
memcheck: build-cli
valgrind --leak-check=yes {{exe_cli}} {{params}}
massif: build-cli
valgrind --tool=massif --massif-out-file=pipeline.massif {{exe_cli}} {{params}}
ms_print pipeline.massif
callgrind: build-cli
valgrind --tool=callgrind {{exe_cli}} {{params}}
archive: generate-build
git archive --add-file={{build_sh}} --output={{name}}.zip --prefix={{name}}/ HEAD

View file

@ -2,21 +2,12 @@
namespace freling {
int BoundingBox::width() const {
return right - left;
}
int BoundingBox::height() const {
return bottom - top;
}
int BoundingBox::area() const {
return width() * height();
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;
return x == b.x and y == b.y and width == b.width and height == b.height;
}
} // namespace freling

View file

@ -5,13 +5,11 @@
namespace freling {
struct BoundingBox {
uint32_t left;
uint32_t top;
uint32_t right;
uint32_t bottom;
int32_t x;
int32_t y;
uint32_t width;
uint32_t height;
int width() const;
int height() const;
int area() const;
bool operator==(const BoundingBox& b) const;

View file

@ -14,8 +14,8 @@ int main(int argc, const char* argv[]) {
if (argc < 6 or (argc - 2) % 4 != 0) {
std::cerr
<< "Usage: " << argv[0] << " path/to/image"
<< " x1 y1 x2 y2 [...]\n"
<< "x/y points must be grouped by 4 to define bounding boxes\n";
<< " [x y width height ...]\n"
<< "x/y/w/h points must be grouped by 4 to define bounding boxes\n";
return 1;
}
@ -35,15 +35,16 @@ int main(int argc, const char* argv[]) {
std::vector<freling::BoundingBox> bboxes;
bboxes.reserve((argc - 2) / 4);
while (i < argc) {
const uint32_t x1 = atoi(argv[i]);
const uint32_t y1 = atoi(argv[i + 1]);
const uint32_t x2 = atoi(argv[i + 2]);
const uint32_t y2 = atoi(argv[i + 3]);
bboxes.push_back(freling::BoundingBox({x1, y1, x2, y2}));
const int32_t x = atoi(argv[i]);
const int32_t y = atoi(argv[i + 1]);
const uint32_t w = atoi(argv[i + 2]);
const uint32_t h = atoi(argv[i + 3]);
bboxes.push_back(freling::BoundingBox({x, y, w, h}));
i += 4;
}
std::optional<Frame> regions = pack(*in_frame, bboxes);
std::vector<freling::BoundingBox> packed_bboxes;
std::optional<Frame> regions = pack(*in_frame, bboxes, packed_bboxes);
return 0;
}

View file

@ -19,31 +19,12 @@
namespace fs = std::filesystem;
struct Button {
Rectangle rect;
std::string text;
bool hover = false;
bool pressed() {
hover = CheckCollisionPointRec(GetMousePosition(), rect);
return hover and IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
};
void draw() const {
DrawRectangleRec(rect, LIGHTGRAY);
DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLUE);
DrawText(text.c_str(),
(rect.x + rect.width / 2 - MeasureText(text.c_str(), 10) / 2),
rect.y + 11, 10, DARKBLUE);
};
};
void draw(const freling::BoundingBox& box,
const Vector2& offset,
const Color& color) {
const Rectangle rect = {offset.x + box.left, offset.y + box.top,
static_cast<float>(box.right - box.left),
static_cast<float>(box.bottom - box.top)};
const Rectangle rect = {offset.x + box.x, offset.y + box.y,
static_cast<float>(box.width),
static_cast<float>(box.height)};
DrawRectangleRec(rect, ColorAlpha(color, 0.3));
DrawRectangleLinesEx(rect, 3, color);
}
@ -69,8 +50,8 @@ int main(int argc, const char* argv[]) {
if (argc < 2 or (argc - 2) % 4 != 0) {
std::cerr
<< "Usage: " << argv[0] << " path/to/image"
<< " [x1 y1 x2 y2 ...]\n"
<< "x/y points must be grouped by 4 to define bounding boxes\n";
<< " [x y width height ...]\n"
<< "x/y/w/h points must be grouped by 4 to define bounding boxes\n";
return 1;
}
@ -90,11 +71,11 @@ int main(int argc, const char* argv[]) {
bboxes.reserve((argc - 2) / 4);
int i = 2;
while (i < argc) {
const uint32_t x1 = atoi(argv[i]);
const uint32_t y1 = atoi(argv[i + 1]);
const uint32_t x2 = atoi(argv[i + 2]);
const uint32_t y2 = atoi(argv[i + 3]);
bboxes.push_back(freling::BoundingBox({x1, y1, x2, y2}));
const int32_t x = atoi(argv[i]);
const int32_t y = atoi(argv[i + 1]);
const uint32_t w = atoi(argv[i + 2]);
const uint32_t h = atoi(argv[i + 3]);
bboxes.push_back(freling::BoundingBox({x, y, w, h}));
i += 4;
}
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE};

View file

@ -12,16 +12,16 @@ 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());
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 data_width = in_box.width * sizeof(Pixel);
const int in_row_size = in_frame.width;
const int out_row_size = out_frame.width;
for (unsigned int i = 0; i < in_box.height(); ++i) {
const int in_offset = in_box.left + (i + in_box.top) * in_row_size;
const int out_offset = out_box.left + (i + out_box.top) * out_row_size;
for (unsigned int i = 0; i < in_box.height; ++i) {
const int in_offset = in_box.x + (i + in_box.y) * in_row_size;
const int out_offset = out_box.x + (i + out_box.y) * out_row_size;
memcpy(out_frame.data + out_offset, in_frame.data + in_offset,
data_width);
@ -35,10 +35,10 @@ std::optional<Frame> pack(const Frame& in_frame,
std::cerr << "No bounding box, cannot pack.\n";
return {};
}
// We sort the bounding boxes by maximum area
// We sort the bounding boxes by height
std::vector<BoundingBox> sorted_bboxes = bboxes;
std::sort(sorted_bboxes.begin(), sorted_bboxes.end(),
[](const auto& a, const auto& b) { return a.area() > b.area(); });
[](const auto& a, const auto& b) { return a.height > b.height; });
// We keep a mapping between the sorted bounding boxes and the original
// order
@ -61,22 +61,73 @@ std::optional<Frame> pack(const Frame& in_frame,
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
int optimal_size = int(ceil(std::sqrt(max_area)));
std::cout << "optimal image dimention: " << optimal_size << " x "
<< optimal_size << "\n";
// cf. subject: D < min(M, N )
const int max_size = std::min(in_frame.width, in_frame.height) - 1;
std::cout << "maximum image dimention: " << max_size << " x " << max_size
<< "\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";
const auto& largest = sorted_bboxes[0];
BoundingBox out_largest = {0, 0, static_cast<uint32_t>(largest.width()),
static_cast<uint32_t>(largest.height())};
packed_bboxes.clear();
packed_bboxes.push_back(out_largest);
// We will try to fit all the rectangles in a given square of size S.
// optimal_size <= S <= max_size (smallest dimension of input frame)
// To find S, we will generate N candidates and try to fit everything.
const int nb_candidates = 5;
const int size_increment = (max_size - optimal_size) / nb_candidates;
for (int size = optimal_size; size <= max_size; size += size_increment) {
int x = 0;
int y = 0;
int next_row = 0;
bool room_left = true;
for (int box_i = 0, box_max = bboxes.size();
room_left and box_i < box_max; ++box_i) {
auto& box = sorted_bboxes[box_i];
// If we don't have room in either dimension, we won't be able to
// pack within this size candidate.
if (x + box.width >= size or y + box.height >= size) {
room_left = false;
continue;
}
// If we cannot fit the rect on the right, we fit it below.
if (x + box.width >= size) {
x = 0;
y = next_row;
}
// If we add a box in a new row, we bump the next row index.
// Because we previously sorted the rectangles by height, we
// know the next ones won't cross this line.
if (x == 0) {
next_row = box.height;
}
box.x = x;
box.y = y;
x += box.width;
}
if (room_left) {
Frame packed_frame(size, size);
packed_frame.fill(0);
packed_bboxes.resize(bboxes.size());
for (int i = 0; i < mapping.size(); ++i) {
int box_index = mapping[i];
packed_bboxes[box_index] = sorted_bboxes[i];
blit(in_frame, bboxes[box_index], packed_frame,
sorted_bboxes[i]);
}
Frame packed_frame(largest.width(), largest.height());
blit(in_frame, largest, packed_frame, out_largest);
return packed_frame;
}
}
std::cerr << "Cannot pack rectangles.\n";
return {};
}
} // namespace freling