fix naive packer
This commit is contained in:
parent
1d851158fc
commit
7d2b7b9ba7
3 changed files with 36 additions and 16 deletions
22
src/pack.cpp
22
src/pack.cpp
|
@ -57,18 +57,17 @@ std::optional<Frame> pack(const Frame& in_frame,
|
|||
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";
|
||||
std::cout << "[pack] max area: " << max_area << "\n";
|
||||
int optimal_size = int(ceil(std::sqrt(max_area)));
|
||||
std::cout << "optimal image dimention: " << optimal_size << " x "
|
||||
std::cout << "[pack] 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";
|
||||
std::cout << "[pack] maximum image dimention: " << max_size << " x "
|
||||
<< max_size << "\n";
|
||||
|
||||
// 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)
|
||||
|
@ -76,6 +75,8 @@ std::optional<Frame> pack(const Frame& in_frame,
|
|||
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) {
|
||||
std::cout << "[pack] trying to fit in " << size << " x " << size
|
||||
<< "\n";
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int next_row = 0;
|
||||
|
@ -84,16 +85,23 @@ std::optional<Frame> pack(const Frame& in_frame,
|
|||
for (int box_i = 0, box_max = bboxes.size();
|
||||
room_left and box_i < box_max; ++box_i) {
|
||||
auto& box = sorted_bboxes[box_i];
|
||||
std::cout << "[pack] box " << box.width << " x " << box.height
|
||||
<< "\n";
|
||||
|
||||
// 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) {
|
||||
if (x + box.width >= size and next_row + box.height >= size) {
|
||||
std::cout << "[pack] -> no room left (last row height = "
|
||||
<< size - y << ")\n";
|
||||
room_left = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we cannot fit the rect on the right, we fit it below.
|
||||
if (x + box.width >= size) {
|
||||
std::cout
|
||||
<< "[pack] -> cannot fit in current row (width left = "
|
||||
<< size - x << ")\n";
|
||||
x = 0;
|
||||
y = next_row;
|
||||
}
|
||||
|
@ -103,11 +111,13 @@ std::optional<Frame> pack(const Frame& in_frame,
|
|||
// know the next ones won't cross this line.
|
||||
if (x == 0) {
|
||||
next_row = box.height;
|
||||
std::cout << "[pack] -> create new row at line " << y << "\n";
|
||||
}
|
||||
|
||||
box.x = x;
|
||||
box.y = y;
|
||||
x += box.width;
|
||||
std::cout << "[pack] -> fit in (" << x << ", " << y << ")\n";
|
||||
}
|
||||
|
||||
if (room_left) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue