netatmo-algo/src/main_gui.cpp

209 lines
6 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;
struct Button {
2022-02-15 13:17:14 +01:00
Rectangle rect;
2022-02-13 22:07:26 +01:00
std::string text;
bool hover = false;
bool pressed() {
2022-02-15 13:17:14 +01:00
hover = CheckCollisionPointRec(GetMousePosition(), rect);
return hover and IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
2022-02-13 22:07:26 +01:00
};
void draw() const {
2022-02-15 13:17:14 +01:00
DrawRectangleRec(rect, LIGHTGRAY);
DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLUE);
DrawText(text.c_str(),
(rect.x + rect.width / 2 - MeasureText(text.c_str(), 10) / 2),
rect.y + 11, 10, DARKBLUE);
2022-02-13 22:07:26 +01:00
};
};
2022-02-15 13:17:14 +01:00
void draw(const freling::BoundingBox& box,
const Vector2& offset,
const Color& color) {
const Rectangle rect = {offset.x + box.left, offset.y + box.top,
static_cast<float>(box.right - box.left),
static_cast<float>(box.bottom - box.top)};
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"
<< " [x1 y1 x2 y2 ...]\n"
<< "x/y 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) {
const uint32_t x1 = atoi(argv[i]);
const uint32_t y1 = atoi(argv[i + 1]);
const uint32_t x2 = atoi(argv[i + 2]);
const uint32_t y2 = atoi(argv[i + 3]);
2022-02-15 13:17:14 +01:00
bboxes.push_back(freling::BoundingBox({x1, y1, x2, y2}));
2022-02-13 23:41:08 +01:00
i += 4;
}
2022-02-15 13:17:14 +01:00
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE};
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-13 22:07:26 +01:00
2022-02-15 13:17:14 +01:00
while (!WindowShouldClose()) {
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) {
const auto& color = bbox_colors[c % bboxes.size()];
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) {
const auto& color = bbox_colors[c % bboxes.size()];
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;
}