implement naive packer
This commit is contained in:
parent
8698cf8160
commit
e31683d2fe
8 changed files with 114 additions and 78 deletions
93
src/pack.cpp
93
src/pack.cpp
|
@ -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;
|
||||
|
||||
Frame packed_frame(largest.width(), largest.height());
|
||||
blit(in_frame, largest, packed_frame, out_largest);
|
||||
return packed_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];
|
||||
|
||||
// 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]);
|
||||
}
|
||||
|
||||
return packed_frame;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Cannot pack rectangles.\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace freling
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue