netatmo-algo/src/main_gui.cpp

225 lines
6.5 KiB
C++
Raw Normal View History

2022-02-13 22:07:26 +01:00
#include <cstdlib>
2022-02-13 23:41:08 +01:00
#include <cstring>
2022-02-13 22:07:26 +01:00
#include <filesystem>
#include <iostream>
2022-02-13 23:41:08 +01:00
#include <optional>
2022-02-13 22:07:26 +01:00
#include <vector>
2022-02-12 15:34:35 +01:00
2022-02-13 22:07:26 +01:00
#include <raylib.h>
2022-02-15 13:17:14 +01:00
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-result"
#define RAYGUI_IMPLEMENTATION
#include <raygui.h>
#pragma clang diagnostic pop
2022-02-12 15:34:35 +01:00
2022-02-13 22:07:26 +01:00
#include "bounding_box.h"
2022-02-13 23:41:08 +01:00
#include "frame.h"
#include "pack.h"
#include "png.h"
2022-02-13 22:07:26 +01:00
namespace fs = std::filesystem;
2022-02-16 00:26:59 +01:00
Rectangle rect_from_bbox(const freling::BoundingBox& box,
const Vector2& offset) {
2022-02-15 21:18:50 +01:00
const Rectangle rect = {offset.x + box.x, offset.y + box.y,
static_cast<float>(box.width),
static_cast<float>(box.height)};
2022-02-16 00:26:59 +01:00
return rect;
}
void draw(const freling::BoundingBox& box,
const Vector2& offset,
const Color& color) {
const Rectangle rect = rect_from_bbox(box, offset);
2022-02-15 13:17:14 +01:00
DrawRectangleRec(rect, ColorAlpha(color, 0.3));
DrawRectangleLinesEx(rect, 3, color);
2022-02-13 23:41:08 +01:00
}
2022-02-15 13:17:14 +01:00
Image image_from_frame(const Frame& frame) {
Image image = {0};
2022-02-13 23:41:08 +01:00
2022-02-15 13:17:14 +01:00
const int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8;
2022-02-13 23:41:08 +01:00
const unsigned int size =
2022-02-15 13:17:14 +01:00
GetPixelDataSize(frame.width, frame.height, format);
2022-02-13 23:41:08 +01:00
image.data = RL_MALLOC(size);
memcpy(image.data, frame.data, size);
image.width = frame.width;
image.height = frame.height;
image.mipmaps = 1;
image.format = format;
return image;
}
2022-02-13 22:07:26 +01:00
int main(int argc, const char* argv[]) {
2022-02-13 23:41:08 +01:00
if (argc < 2 or (argc - 2) % 4 != 0) {
std::cerr
<< "Usage: " << argv[0] << " path/to/image"
2022-02-15 21:18:50 +01:00
<< " [x y width height ...]\n"
<< "x/y/w/h points must be grouped by 4 to define bounding boxes\n";
2022-02-13 22:07:26 +01:00
return 1;
}
const fs::path input(argv[1]);
if (!fs::exists(input)) {
std::cerr << "Input file " << input << " does not exist.\n";
return 1;
}
2022-02-13 23:41:08 +01:00
const std::optional<Frame> in_frame = load_png(input);
if (!in_frame) {
std::cerr << "Cannot load file " << input << "\n";
return 1;
}
2022-02-15 13:17:14 +01:00
std::vector<freling::BoundingBox> bboxes;
2022-02-13 23:41:08 +01:00
bboxes.reserve((argc - 2) / 4);
int i = 2;
while (i < argc) {
2022-02-15 21:18:50 +01:00
const int32_t x = atoi(argv[i]);
const int32_t y = atoi(argv[i + 1]);
const uint32_t w = atoi(argv[i + 2]);
const uint32_t h = atoi(argv[i + 3]);
bboxes.push_back(freling::BoundingBox({x, y, w, h}));
2022-02-13 23:41:08 +01:00
i += 4;
}
2022-02-16 00:26:59 +01:00
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE,
YELLOW, ORANGE, PURPLE};
2022-02-15 13:59:17 +01:00
std::vector<freling::BoundingBox> packed_bboxes;
2022-02-13 23:41:08 +01:00
2022-02-15 13:17:14 +01:00
Vector2 win_size = {800, 450};
2022-02-13 22:07:26 +01:00
const int padding = 5;
2022-02-15 13:17:14 +01:00
const Vector2 in_offset = {padding, 40};
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
InitWindow(win_size.x, win_size.y, "netatmo - rect packing");
SetTargetFPS(60);
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
Image in_img = image_from_frame(*in_frame);
Texture2D in_texture = LoadTextureFromImage(in_img);
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
Image pack_img;
Texture2D pack_texture;
2022-02-13 23:41:08 +01:00
bool packed = false;
win_size.x = std::max(win_size.x, in_offset.x + in_texture.width + padding);
win_size.y =
std::max(win_size.y, in_offset.y + in_texture.height + padding);
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
SetWindowSize(win_size.x, win_size.y);
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
const Vector2 b_size = {150, 30};
2022-02-16 00:26:59 +01:00
Vector2 mouse_pos = {0};
Vector2 mouse_delta = {0};
int b_moving_idx = -1;
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
while (!WindowShouldClose()) {
2022-02-16 00:26:59 +01:00
//
// Update
//
mouse_pos = GetMousePosition();
mouse_delta = GetMouseDelta();
for (int b = 0, size = bboxes.size(); b < size; ++b) {
const auto& box = bboxes[b];
const Rectangle rect = rect_from_bbox(box, in_offset);
if (CheckCollisionPointRec(mouse_pos, rect) and
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
b_moving_idx = b;
}
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
b_moving_idx = -1;
}
if (b_moving_idx != -1) {
auto& box = bboxes[b_moving_idx];
box.x += mouse_delta.x;
box.y += mouse_delta.y;
}
//
// Draw
//
2022-02-15 13:17:14 +01:00
BeginDrawing();
ClearBackground(RAYWHITE);
2022-02-13 23:41:08 +01:00
//
2022-02-15 13:59:17 +01:00
// Widgets: buttons, ...
2022-02-13 23:41:08 +01:00
//
2022-02-15 13:17:14 +01:00
if (GuiButton((Rectangle){padding, padding, b_size.x, b_size.y},
"Add Box")) {
bboxes.push_back(freling::BoundingBox({0, 0, 64, 64}));
2022-02-13 23:41:08 +01:00
}
2022-02-15 13:17:14 +01:00
if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x,
b_size.y},
"Pack Rects")) {
2022-02-15 13:59:17 +01:00
std::optional<Frame> f_packed =
pack(*in_frame, bboxes, packed_bboxes);
2022-02-13 23:41:08 +01:00
if (packed) {
2022-02-15 13:17:14 +01:00
UnloadTexture(pack_texture);
UnloadImage(pack_img);
2022-02-13 23:41:08 +01:00
}
2022-02-15 13:17:14 +01:00
pack_img = image_from_frame(*f_packed);
pack_texture = LoadTextureFromImage(pack_img);
2022-02-13 23:41:08 +01:00
packed = true;
win_size.x =
std::max(win_size.x, in_offset.x + in_texture.width +
padding * 2 + pack_texture.width);
win_size.y = std::max(
win_size.y,
in_offset.y + std::max(in_texture.height, pack_texture.height) +
padding);
2022-02-15 13:17:14 +01:00
SetWindowSize(win_size.x, win_size.y);
2022-02-12 15:34:35 +01:00
}
2022-02-13 23:41:08 +01:00
//
// Input image
2022-02-15 13:17:14 +01:00
//
DrawTexture(in_texture, in_offset.x, in_offset.y, WHITE);
DrawRectangleLines(in_offset.x, in_offset.y, in_texture.width,
in_texture.height, BLACK);
2022-02-13 23:41:08 +01:00
int c = 0;
for (const auto& b : bboxes) {
2022-02-16 00:26:59 +01:00
const auto& color = bbox_colors[c % bbox_colors.size()];
2022-02-13 23:41:08 +01:00
draw(b, in_offset, color);
++c;
};
2022-02-15 13:17:14 +01:00
//
2022-02-13 23:41:08 +01:00
// Packed image
2022-02-15 13:17:14 +01:00
//
2022-02-13 23:41:08 +01:00
if (packed) {
2022-02-15 13:59:17 +01:00
Vector2 pack_offset = {in_offset.x + in_texture.width + padding,
in_offset.y};
DrawTexture(pack_texture, pack_offset.x, pack_offset.y, WHITE);
DrawRectangleLines(pack_offset.x, pack_offset.y, pack_texture.width,
2022-02-15 13:17:14 +01:00
pack_texture.height, BLACK);
2022-02-15 13:59:17 +01:00
int c = 0;
for (const auto& b : packed_bboxes) {
2022-02-16 00:26:59 +01:00
const auto& color = bbox_colors[c % bbox_colors.size()];
2022-02-15 13:59:17 +01:00
draw(b, pack_offset, color);
++c;
};
2022-02-13 23:41:08 +01:00
}
2022-02-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
EndDrawing();
2022-02-13 22:07:26 +01:00
}
2022-02-12 15:34:35 +01:00
2022-02-13 22:07:26 +01:00
// Cleanup
2022-02-15 13:17:14 +01:00
UnloadTexture(in_texture);
UnloadImage(in_img);
2022-02-13 23:41:08 +01:00
if (packed) {
2022-02-15 13:17:14 +01:00
UnloadTexture(pack_texture);
UnloadImage(pack_img);
2022-02-13 23:41:08 +01:00
}
2022-02-15 13:17:14 +01:00
CloseWindow();
2022-02-12 15:34:35 +01:00
return 0;
}