netatmo-algo/src/pack.cpp

134 lines
4.4 KiB
C++
Raw Normal View History

2022-02-12 00:28:54 +01:00
#include "pack.h"
2022-02-14 00:13:09 +01:00
#include <algorithm>
2022-02-15 13:17:14 +01:00
#include <cassert>
2022-02-14 00:13:09 +01:00
#include <cmath>
2022-02-15 13:17:14 +01:00
#include <cstring>
2022-02-14 00:13:09 +01:00
#include <iostream>
2022-02-15 13:17:14 +01:00
namespace freling {
void blit(const Frame& in_frame,
const BoundingBox& in_box,
Frame& out_frame,
const BoundingBox& out_box) {
2022-02-15 21:18:50 +01:00
assert(in_box.width == out_box.width);
assert(in_box.height == out_box.height);
2022-02-15 13:17:14 +01:00
2022-02-15 21:18:50 +01:00
const int data_width = in_box.width * sizeof(Pixel);
2022-02-15 13:59:17 +01:00
const int in_row_size = in_frame.width;
const int out_row_size = out_frame.width;
2022-02-15 13:17:14 +01:00
2022-02-15 21:18:50 +01:00
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;
2022-02-15 13:59:17 +01:00
2022-02-15 13:17:14 +01:00
memcpy(out_frame.data + out_offset, in_frame.data + in_offset,
data_width);
}
}
std::optional<Frame> pack(const Frame& in_frame,
2022-02-15 13:59:17 +01:00
const std::vector<BoundingBox>& bboxes,
std::vector<BoundingBox>& packed_bboxes) {
2022-02-15 13:17:14 +01:00
if (bboxes.size() == 0) {
std::cerr << "No bounding box, cannot pack.\n";
return {};
}
2022-02-15 21:18:50 +01:00
// We sort the bounding boxes by height
2022-02-14 00:13:09 +01:00
std::vector<BoundingBox> sorted_bboxes = bboxes;
std::sort(sorted_bboxes.begin(), sorted_bboxes.end(),
2022-02-15 21:18:50 +01:00
[](const auto& a, const auto& b) { return a.height > b.height; });
2022-02-14 00:13:09 +01:00
// We keep a mapping between the sorted bounding boxes and the original
// order
std::vector<int> 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";
2022-02-15 21:18:50 +01:00
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
2022-02-14 00:13:09 +01:00
<< "\n";
2022-02-15 21:18:50 +01:00
// 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]);
}
return packed_frame;
}
}
std::cerr << "Cannot pack rectangles.\n";
return {};
2022-02-12 00:28:54 +01:00
}
2022-02-15 13:17:14 +01:00
} // namespace freling