fix naive packer
This commit is contained in:
parent
1d851158fc
commit
7d2b7b9ba7
|
@ -8,8 +8,7 @@ The subject is available here: [Test Algo](./test_algo.pdf)
|
||||||
- [ ] skyline
|
- [ ] skyline
|
||||||
- [X] add raygui
|
- [X] add raygui
|
||||||
- [X] resize box
|
- [X] resize box
|
||||||
- [ ] add box in gui by pressing down
|
- [X] delete box in gui by clicking
|
||||||
- [ ] delete box in gui by clicking
|
|
||||||
- [ ] wrap stb_rect_pack?
|
- [ ] wrap stb_rect_pack?
|
||||||
- [X] change bbox api to origin + size
|
- [X] change bbox api to origin + size
|
||||||
- [ ] dummy A2
|
- [ ] dummy A2
|
||||||
|
|
|
@ -119,8 +119,10 @@ int main(int argc, const char* argv[]) {
|
||||||
const Vector2 b_size = {150, 30};
|
const Vector2 b_size = {150, 30};
|
||||||
Vector2 mouse_pos = {0};
|
Vector2 mouse_pos = {0};
|
||||||
Vector2 mouse_delta = {0};
|
Vector2 mouse_delta = {0};
|
||||||
int b_moving_idx = -1;
|
constexpr int idx_unset = -1;
|
||||||
int b_resize_idx = -1;
|
int b_moving_idx = idx_unset;
|
||||||
|
int b_resize_idx = idx_unset;
|
||||||
|
int b_delete_idx = idx_unset;
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
//
|
//
|
||||||
|
@ -131,8 +133,10 @@ int main(int argc, const char* argv[]) {
|
||||||
for (int b = 0, size = bboxes.size(); b < size; ++b) {
|
for (int b = 0, size = bboxes.size(); b < size; ++b) {
|
||||||
const auto& box = bboxes[b];
|
const auto& box = bboxes[b];
|
||||||
const Rectangle rect = rect_from_bbox(box, in_offset);
|
const Rectangle rect = rect_from_bbox(box, in_offset);
|
||||||
if (CheckCollisionPointRec(mouse_pos, rect) and
|
if (not CheckCollisionPointRec(mouse_pos, rect)) {
|
||||||
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
continue;
|
||||||
|
}
|
||||||
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||||
const Rectangle handle_area = {
|
const Rectangle handle_area = {
|
||||||
rect.x + rect.width - resize_handle,
|
rect.x + rect.width - resize_handle,
|
||||||
rect.y + rect.height - resize_handle, resize_handle,
|
rect.y + rect.height - resize_handle, resize_handle,
|
||||||
|
@ -143,23 +147,30 @@ int main(int argc, const char* argv[]) {
|
||||||
b_moving_idx = b;
|
b_moving_idx = b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
|
||||||
|
b_delete_idx = b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
|
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
|
||||||
b_moving_idx = -1;
|
b_moving_idx = idx_unset;
|
||||||
b_resize_idx = -1;
|
b_resize_idx = idx_unset;
|
||||||
}
|
}
|
||||||
if (b_moving_idx != -1) {
|
if (b_moving_idx != idx_unset) {
|
||||||
auto& box = bboxes[b_moving_idx];
|
auto& box = bboxes[b_moving_idx];
|
||||||
box.x += mouse_delta.x;
|
box.x += mouse_delta.x;
|
||||||
box.y += mouse_delta.y;
|
box.y += mouse_delta.y;
|
||||||
}
|
}
|
||||||
if (b_resize_idx != -1) {
|
if (b_resize_idx != idx_unset) {
|
||||||
auto& box = bboxes[b_resize_idx];
|
auto& box = bboxes[b_resize_idx];
|
||||||
box.width = std::max(static_cast<int>(box.width + mouse_delta.x),
|
box.width = std::max(static_cast<int>(box.width + mouse_delta.x),
|
||||||
resize_handle);
|
resize_handle);
|
||||||
box.height = std::max(static_cast<int>(box.height + mouse_delta.y),
|
box.height = std::max(static_cast<int>(box.height + mouse_delta.y),
|
||||||
resize_handle);
|
resize_handle);
|
||||||
}
|
}
|
||||||
|
if (b_delete_idx != idx_unset) {
|
||||||
|
bboxes.erase(bboxes.begin() + b_delete_idx);
|
||||||
|
b_delete_idx = idx_unset;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Draw
|
// Draw
|
||||||
|
|
22
src/pack.cpp
22
src/pack.cpp
|
@ -57,18 +57,17 @@ std::optional<Frame> pack(const Frame& in_frame,
|
||||||
int max_area = 0;
|
int max_area = 0;
|
||||||
for (const auto& box : sorted_bboxes) {
|
for (const auto& box : sorted_bboxes) {
|
||||||
int area = box.area();
|
int area = box.area();
|
||||||
std::cout << "bounding box area: " << area << "\n";
|
|
||||||
max_area += area;
|
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)));
|
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";
|
<< optimal_size << "\n";
|
||||||
|
|
||||||
// cf. subject: D < min(M, N )
|
// cf. subject: D < min(M, N )
|
||||||
const int max_size = std::min(in_frame.width, in_frame.height) - 1;
|
const int max_size = std::min(in_frame.width, in_frame.height) - 1;
|
||||||
std::cout << "maximum image dimention: " << max_size << " x " << max_size
|
std::cout << "[pack] maximum image dimention: " << max_size << " x "
|
||||||
<< "\n";
|
<< max_size << "\n";
|
||||||
|
|
||||||
// We will try to fit all the rectangles in a given square of size S.
|
// 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)
|
// 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 nb_candidates = 5;
|
||||||
const int size_increment = (max_size - optimal_size) / nb_candidates;
|
const int size_increment = (max_size - optimal_size) / nb_candidates;
|
||||||
for (int size = optimal_size; size <= max_size; size += size_increment) {
|
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 x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
int next_row = 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();
|
for (int box_i = 0, box_max = bboxes.size();
|
||||||
room_left and box_i < box_max; ++box_i) {
|
room_left and box_i < box_max; ++box_i) {
|
||||||
auto& box = sorted_bboxes[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
|
// If we don't have room in either dimension, we won't be able to
|
||||||
// pack within this size candidate.
|
// 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;
|
room_left = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we cannot fit the rect on the right, we fit it below.
|
// If we cannot fit the rect on the right, we fit it below.
|
||||||
if (x + box.width >= size) {
|
if (x + box.width >= size) {
|
||||||
|
std::cout
|
||||||
|
<< "[pack] -> cannot fit in current row (width left = "
|
||||||
|
<< size - x << ")\n";
|
||||||
x = 0;
|
x = 0;
|
||||||
y = next_row;
|
y = next_row;
|
||||||
}
|
}
|
||||||
|
@ -103,11 +111,13 @@ std::optional<Frame> pack(const Frame& in_frame,
|
||||||
// know the next ones won't cross this line.
|
// know the next ones won't cross this line.
|
||||||
if (x == 0) {
|
if (x == 0) {
|
||||||
next_row = box.height;
|
next_row = box.height;
|
||||||
|
std::cout << "[pack] -> create new row at line " << y << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
box.x = x;
|
box.x = x;
|
||||||
box.y = y;
|
box.y = y;
|
||||||
x += box.width;
|
x += box.width;
|
||||||
|
std::cout << "[pack] -> fit in (" << x << ", " << y << ")\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (room_left) {
|
if (room_left) {
|
||||||
|
|
Loading…
Reference in a new issue