From ce28f0d350cc0ff7055eccccc6d8c6c29050a862 Mon Sep 17 00:00:00 2001 From: Froxwin <56168224+Froxwin@users.noreply.github.com> Date: Sun, 8 Mar 2026 12:08:24 +0530 Subject: [PATCH] feat: add bounding box calculation --- src/application/applicationstate.c | 11 +++++- src/core/primitives.c | 56 ++++++++++++++++++++++++++++++ src/core/primitives.h | 16 ++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/application/applicationstate.c b/src/application/applicationstate.c index 8cf451a..0fdf3e5 100644 --- a/src/application/applicationstate.c +++ b/src/application/applicationstate.c @@ -81,7 +81,8 @@ begin_click_dispatch: .stroke_width = 0.01}; VektorShape shape = (VektorShape){ .primitive = linePrimitive, .z_index = 0, .style = style}; - vektor_shapebuffer_add_shape(state->shapeBuffer, shape); + vektor_shapebuffer_add_shape(state->shapeBuffer, vektor_shape_new(linePrimitive, style, 0)); + state->selectedShape = &(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]); @@ -96,6 +97,13 @@ begin_click_dispatch: vektor_polyline_add_point(state->selectedShape->primitive.polyline, pos); + + vektor_shapes_update_bbox(state->shapeBuffer); + + + for (size_t i = 0; i < state->shapeBuffer->count; i++) { + g_print("<%f,%f>-<%f,%f>\n", state->shapeBuffer->shapes[i].bbox.min.x, state->shapeBuffer->shapes[i].bbox.min.y, state->shapeBuffer->shapes[i].bbox.max.x, state->shapeBuffer->shapes[i].bbox.max.y); + } } } @@ -111,6 +119,7 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) { stateOut->canvas = malloc(sizeof(VektorCanvas)); stateOut->widgetState = wstate; stateOut->currentColor = vektor_color_blank; + stateOut->selectedShape = NULL; vektor_canvas_init(wstate, stateOut->canvas, stateOut->shapeBuffer); // link all the buttons diff --git a/src/core/primitives.c b/src/core/primitives.c index e6c0d2d..f72453d 100644 --- a/src/core/primitives.c +++ b/src/core/primitives.c @@ -1,4 +1,7 @@ #include "primitives.h" +#include +#include +#include VektorPolyline* vektor_polyline_new(void) { VektorPolyline* pl = malloc(sizeof(VektorPolyline)); @@ -55,3 +58,56 @@ void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, } buffer->shapes[buffer->count++] = shape; } + +VektorBBox polyline_mk_bbox(VektorPrimitive prim) { + float min_x, max_x, min_y, max_y; + for (size_t i = 0; i < prim.polyline->count; i++) { + V2 p = prim.polyline->points[i]; + min_x = fminf(min_x, p.x); + min_y = fminf(min_y, p.y); + + max_x = fminf(max_x, p.x); + max_y = fminf(max_y, p.y); + } + return (VektorBBox){(V2){min_x, min_y}, (V2){max_x, max_y}}; +} + +VektorBBox polygon_mk_bbox(VektorPrimitive prim) { + float min_x, max_x, min_y, max_y; + for (size_t i = 0; i < prim.polygon->count; i++) { + V2 p = prim.polygon->points[i]; + min_x = fminf(min_x, p.x); + min_y = fminf(min_y, p.y); + + max_x = fminf(max_x, p.x); + max_y = fminf(max_y, p.y); + } + return (VektorBBox){(V2){min_x, min_y}, (V2){max_x, max_y}}; +} + +VektorBBox vektor_mk_bbox(VektorPrimitive prim) { + switch (prim.kind) { + case VEKTOR_POLYLINE: + return polyline_mk_bbox(prim); + break; + + case VEKTOR_POLYGON: + return polygon_mk_bbox(prim); + break; + + default: + // TODO: fill in all primitives + break; + } +} + +VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style, + int z_index) { + return (VektorShape){.primitive = prim, .style = style, .z_index = z_index, .bbox=vektor_mk_bbox(prim)}; +} + +void vektor_shapes_update_bbox(VektorShapeBuffer* buffer) { + for (size_t i = 0; i < buffer->count; i++) { + buffer->shapes[i].bbox = vektor_mk_bbox(buffer->shapes[i].primitive); + } +} \ No newline at end of file diff --git a/src/core/primitives.h b/src/core/primitives.h index a72b227..cf5734b 100644 --- a/src/core/primitives.h +++ b/src/core/primitives.h @@ -51,12 +51,26 @@ typedef struct { float stroke_width; } VektorStyle; +typedef struct { + V2 min; + V2 max; +} VektorBBox; + typedef struct { VektorStyle style; int z_index; + VektorBBox bbox; VektorPrimitive primitive; } VektorShape; +VektorBBox polyline_mk_bbox(VektorPrimitive prim); +VektorBBox polygon_mk_bbox(VektorPrimitive prim); + +VektorBBox vektor_mk_bbox(VektorPrimitive prim); + +VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style, + int z_index); + typedef struct { VektorShape* shapes; size_t count; @@ -64,5 +78,5 @@ typedef struct { } VektorShapeBuffer; void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, VektorShape shape); - +void vektor_shapes_update_bbox(VektorShapeBuffer* buffer); #endif // PRIMITIVES_H_