feat: add bounding box calculation
This commit is contained in:
@@ -81,7 +81,8 @@ begin_click_dispatch:
|
|||||||
.stroke_width = 0.01};
|
.stroke_width = 0.01};
|
||||||
VektorShape shape = (VektorShape){
|
VektorShape shape = (VektorShape){
|
||||||
.primitive = linePrimitive, .z_index = 0, .style = style};
|
.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->selectedShape =
|
||||||
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
||||||
@@ -96,6 +97,13 @@ begin_click_dispatch:
|
|||||||
|
|
||||||
vektor_polyline_add_point(state->selectedShape->primitive.polyline,
|
vektor_polyline_add_point(state->selectedShape->primitive.polyline,
|
||||||
pos);
|
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->canvas = malloc(sizeof(VektorCanvas));
|
||||||
stateOut->widgetState = wstate;
|
stateOut->widgetState = wstate;
|
||||||
stateOut->currentColor = vektor_color_blank;
|
stateOut->currentColor = vektor_color_blank;
|
||||||
|
stateOut->selectedShape = NULL;
|
||||||
vektor_canvas_init(wstate, stateOut->canvas, stateOut->shapeBuffer);
|
vektor_canvas_init(wstate, stateOut->canvas, stateOut->shapeBuffer);
|
||||||
|
|
||||||
// link all the buttons
|
// link all the buttons
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "primitives.h"
|
#include "primitives.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
VektorPolyline* vektor_polyline_new(void) {
|
VektorPolyline* vektor_polyline_new(void) {
|
||||||
VektorPolyline* pl = malloc(sizeof(VektorPolyline));
|
VektorPolyline* pl = malloc(sizeof(VektorPolyline));
|
||||||
@@ -55,3 +58,56 @@ void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer,
|
|||||||
}
|
}
|
||||||
buffer->shapes[buffer->count++] = shape;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,12 +51,26 @@ typedef struct {
|
|||||||
float stroke_width;
|
float stroke_width;
|
||||||
} VektorStyle;
|
} VektorStyle;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
V2 min;
|
||||||
|
V2 max;
|
||||||
|
} VektorBBox;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
VektorStyle style;
|
VektorStyle style;
|
||||||
int z_index;
|
int z_index;
|
||||||
|
VektorBBox bbox;
|
||||||
VektorPrimitive primitive;
|
VektorPrimitive primitive;
|
||||||
} VektorShape;
|
} 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 {
|
typedef struct {
|
||||||
VektorShape* shapes;
|
VektorShape* shapes;
|
||||||
size_t count;
|
size_t count;
|
||||||
@@ -64,5 +78,5 @@ typedef struct {
|
|||||||
} VektorShapeBuffer;
|
} VektorShapeBuffer;
|
||||||
|
|
||||||
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, VektorShape shape);
|
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, VektorShape shape);
|
||||||
|
void vektor_shapes_update_bbox(VektorShapeBuffer* buffer);
|
||||||
#endif // PRIMITIVES_H_
|
#endif // PRIMITIVES_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user