map regions in origin
This commit is contained in:
parent
38e9a97a85
commit
4447cd67d1
|
@ -11,7 +11,7 @@ The subject is available here: [Test Algo](./test_algo.pdf)
|
|||
- [X] delete box in gui by clicking
|
||||
- [ ] wrap stb_rect_pack?
|
||||
- [X] change bbox api to origin + size
|
||||
- [ ] dummy A2
|
||||
- [X] dummy A2
|
||||
|
||||
## Installation
|
||||
|
||||
|
|
|
@ -7,10 +7,10 @@ std::vector<BoundingBox> A2(const std::vector<BoundingBox>& regions) {
|
|||
for (int i = 0, size = regions.size(); i < size; ++i) {
|
||||
auto& box = updated_boxes[i];
|
||||
const auto& region = regions[i];
|
||||
box.x = region.x + 2;
|
||||
box.y = region.y + 2;
|
||||
box.width = region.width - 4;
|
||||
box.height = region.height - 4;
|
||||
box.x = region.x + 10;
|
||||
box.y = region.y + 10;
|
||||
box.width = region.width - 20;
|
||||
box.height = region.height - 20;
|
||||
}
|
||||
return updated_boxes;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,10 @@ namespace freling {
|
|||
int BoundingBox::area() const {
|
||||
return width * height;
|
||||
}
|
||||
bool BoundingBox::contains(int x, int y) const {
|
||||
return this->x <= x and x <= this->x + this->width and this->y <= y and
|
||||
y <= this->y + this->height;
|
||||
}
|
||||
|
||||
bool BoundingBox::operator==(const BoundingBox& b) const {
|
||||
return x == b.x and y == b.y and width == b.width and height == b.height;
|
||||
|
|
|
@ -11,6 +11,7 @@ struct BoundingBox {
|
|||
uint32_t height;
|
||||
|
||||
int area() const;
|
||||
bool contains(int x, int y) const;
|
||||
|
||||
bool operator==(const BoundingBox& b) const;
|
||||
};
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include "a2.h"
|
||||
#include "bounding_box.h"
|
||||
#include "frame.h"
|
||||
#include "mapping.h"
|
||||
#include "pack.h"
|
||||
#include "png.h"
|
||||
|
||||
|
@ -31,12 +32,12 @@ Rectangle rect_from_bbox(const freling::BoundingBox& box,
|
|||
return rect;
|
||||
}
|
||||
|
||||
void draw(const freling::BoundingBox& box,
|
||||
const Vector2& offset,
|
||||
const Color& color) {
|
||||
void draw_resizable(const freling::BoundingBox& box,
|
||||
const Vector2& offset,
|
||||
const Color& color) {
|
||||
const Rectangle rect = rect_from_bbox(box, offset);
|
||||
DrawRectangleRec(rect, ColorAlpha(color, 0.3));
|
||||
DrawRectangleLinesEx(rect, 3, color);
|
||||
DrawRectangleLinesEx(rect, 2, color);
|
||||
DrawTriangle(
|
||||
(Vector2){rect.x + rect.width - resize_handle, rect.y + rect.height},
|
||||
(Vector2){rect.x + rect.width, rect.y + rect.height},
|
||||
|
@ -44,6 +45,13 @@ void draw(const freling::BoundingBox& box,
|
|||
color);
|
||||
}
|
||||
|
||||
void draw_region(const freling::BoundingBox& box,
|
||||
const Vector2& offset,
|
||||
const Color& color) {
|
||||
const Rectangle rect = rect_from_bbox(box, offset);
|
||||
DrawRectangleRec(rect, ColorAlpha(color, 0.8));
|
||||
}
|
||||
|
||||
Image image_from_frame(const Frame& frame) {
|
||||
Image image = {0};
|
||||
|
||||
|
@ -97,6 +105,7 @@ int main(int argc, const char* argv[]) {
|
|||
YELLOW, ORANGE, PURPLE};
|
||||
std::vector<freling::BoundingBox> packed_bboxes;
|
||||
std::vector<freling::BoundingBox> a2_bboxes;
|
||||
std::vector<freling::BoundingBox> a2_bboxes_origin;
|
||||
|
||||
Vector2 win_size = {800, 450};
|
||||
const int padding = 5;
|
||||
|
@ -162,6 +171,7 @@ int main(int argc, const char* argv[]) {
|
|||
auto& box = bboxes[b_moving_idx];
|
||||
box.x += mouse_delta.x;
|
||||
box.y += mouse_delta.y;
|
||||
packed = false;
|
||||
}
|
||||
if (b_resize_idx != idx_unset) {
|
||||
auto& box = bboxes[b_resize_idx];
|
||||
|
@ -169,11 +179,13 @@ int main(int argc, const char* argv[]) {
|
|||
resize_handle);
|
||||
box.height = std::max(static_cast<int>(box.height + mouse_delta.y),
|
||||
resize_handle);
|
||||
packed = false;
|
||||
}
|
||||
if (b_delete_idx != idx_unset) {
|
||||
bboxes.erase(bboxes.begin() + b_delete_idx);
|
||||
b_delete_idx = idx_unset;
|
||||
}
|
||||
a2_processed = a2_processed and packed;
|
||||
|
||||
//
|
||||
// Draw
|
||||
|
@ -192,6 +204,7 @@ int main(int argc, const char* argv[]) {
|
|||
if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x,
|
||||
b_size.y},
|
||||
"Pack Rects")) {
|
||||
a2_processed = false;
|
||||
std::optional<Frame> f_packed =
|
||||
freling::pack(*in_frame, bboxes, packed_bboxes);
|
||||
|
||||
|
@ -219,6 +232,8 @@ int main(int argc, const char* argv[]) {
|
|||
b_size.x, b_size.y},
|
||||
"A2")) {
|
||||
a2_bboxes = freling::A2(packed_bboxes);
|
||||
a2_bboxes_origin =
|
||||
freling::map_to_origin(bboxes, packed_bboxes, a2_bboxes);
|
||||
a2_processed = true;
|
||||
}
|
||||
|
||||
|
@ -232,10 +247,19 @@ int main(int argc, const char* argv[]) {
|
|||
int c = 0;
|
||||
for (const auto& b : bboxes) {
|
||||
const auto& color = bbox_colors[c % bbox_colors.size()];
|
||||
draw(b, in_offset, color);
|
||||
draw_resizable(b, in_offset, color);
|
||||
++c;
|
||||
};
|
||||
|
||||
c = 0;
|
||||
if (a2_processed) {
|
||||
for (const auto& b : a2_bboxes_origin) {
|
||||
const auto& color = bbox_colors[c % bbox_colors.size()];
|
||||
draw_region(b, in_offset, color);
|
||||
++c;
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
// Packed image
|
||||
//
|
||||
|
@ -249,7 +273,8 @@ int main(int argc, const char* argv[]) {
|
|||
int c = 0;
|
||||
for (const auto& b : packed_bboxes) {
|
||||
const auto& color = bbox_colors[c % bbox_colors.size()];
|
||||
draw(b, pack_offset, color);
|
||||
const Rectangle rect = rect_from_bbox(b, pack_offset);
|
||||
DrawRectangleLinesEx(rect, 2, color);
|
||||
++c;
|
||||
};
|
||||
|
||||
|
@ -257,7 +282,7 @@ int main(int argc, const char* argv[]) {
|
|||
int c = 0;
|
||||
for (const auto& b : a2_bboxes) {
|
||||
const auto& color = bbox_colors[c % bbox_colors.size()];
|
||||
draw(b, pack_offset, color);
|
||||
draw_region(b, pack_offset, color);
|
||||
++c;
|
||||
};
|
||||
}
|
||||
|
|
28
src/mapping.cpp
Normal file
28
src/mapping.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "mapping.h"
|
||||
|
||||
namespace freling {
|
||||
|
||||
std::vector<BoundingBox> map_to_origin(
|
||||
const std::vector<BoundingBox>& origin_bboxes,
|
||||
const std::vector<BoundingBox>& packed_bboxes,
|
||||
const std::vector<BoundingBox>& a2_bboxes) {
|
||||
std::vector<BoundingBox> mapped_bboxes(a2_bboxes.size());
|
||||
for (int i = 0, i_max = a2_bboxes.size(); i < i_max; ++i) {
|
||||
const auto& a2_box = a2_bboxes[i];
|
||||
for (int j = 0, j_max = packed_bboxes.size(); j < j_max; ++j) {
|
||||
const auto& packed_box = packed_bboxes[j];
|
||||
if (packed_box.contains(a2_box.x, a2_box.y)) {
|
||||
const auto& origin_box = origin_bboxes[j];
|
||||
auto& mapped_box = mapped_bboxes[i];
|
||||
mapped_box.width = a2_box.width;
|
||||
mapped_box.height = a2_box.height;
|
||||
mapped_box.x = a2_box.x - packed_box.x + origin_box.x;
|
||||
mapped_box.y = a2_box.y - packed_box.y + origin_box.y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return mapped_bboxes;
|
||||
}
|
||||
|
||||
} // namespace freling
|
12
src/mapping.h
Normal file
12
src/mapping.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "bounding_box.h"
|
||||
|
||||
namespace freling {
|
||||
std::vector<BoundingBox> map_to_origin(
|
||||
const std::vector<BoundingBox>& origin_bboxes,
|
||||
const std::vector<BoundingBox>& packed_bboxes,
|
||||
const std::vector<BoundingBox>& a2_bboxes);
|
||||
}
|
Loading…
Reference in a new issue