From acfa01613f6e7fbeace88a15df61252156d39cf1 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Thu, 20 Nov 2025 22:31:53 +0100 Subject: Separate everything out --- inc/cad/solve.h | 18 +++++++++++++++++- inc/cad/svg.h | 19 +++++++++++++++++++ inc/cad/topology.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ inc/cad/util.h | 19 +++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 inc/cad/svg.h create mode 100644 inc/cad/topology.h create mode 100644 inc/cad/util.h (limited to 'inc') diff --git a/inc/cad/solve.h b/inc/cad/solve.h index 4eae272..85b902b 100644 --- a/inc/cad/solve.h +++ b/inc/cad/solve.h @@ -22,6 +22,20 @@ struct component { bool drawn; }; +struct alias { + struct component *alias; + struct component *target; +}; + +struct constraints { + struct constraint *elements; + size_t length; + size_t capacity; + + struct alias aliases[16]; + size_t aliases_num; +}; + extern char *constraint_type_name[]; enum constraint_type { CT_POINT_POINT_DISTANCE, @@ -49,4 +63,6 @@ struct constraint { bool used; }; -bool solve_constraints(struct constraint *constraints, size_t constraints_num, struct drawing *drawing); +void alias_point(struct constraints *c, struct component *alias, struct component *target); +void add_constraint(struct constraints *c, struct constraint *new); +bool solve_constraints(struct constraints *constraints, struct drawing *drawing); diff --git a/inc/cad/svg.h b/inc/cad/svg.h new file mode 100644 index 0000000..68634b8 --- /dev/null +++ b/inc/cad/svg.h @@ -0,0 +1,19 @@ +#pragma once + +#include "cad/solve.h" +#include "cad/topology.h" + +enum LineStyle { + LSTYLE_NORMAL, + LSTYLE_CONSTRUCTION, + LSTYLE_INDICATOR, +}; +#define TEXT_OFFSET 0.4 + +void plot_line_between(struct point p1, struct point p2); +void plot_arc_between_style(struct point c, struct point p1, struct point p2, enum LineStyle style); +void plot_arc_between(struct point c, struct point p1, struct point p2); +void plot_text(struct point p, double angle, char* str); + +void draw_constraints(struct constraints *constraints); +void draw_topology(struct topology *topo); diff --git a/inc/cad/topology.h b/inc/cad/topology.h new file mode 100644 index 0000000..b3879b2 --- /dev/null +++ b/inc/cad/topology.h @@ -0,0 +1,50 @@ +#pragma once + +#include + +#define TOPO_MOVETO(c) \ + { .cmd = {TOPO_MOVETO} }, \ + { .arg = {c} } \ + +#define TOPO_LINETO(c) \ + { .cmd = {TOPO_LINETO} }, \ + { .arg = {c} } \ + +#define TOPO_ARCTO(center, end) \ + { .cmd = {TOPO_ARCTO} }, \ + { .arg = {center} }, \ + { .arg = {end} } \ + +#define TOPO_END() \ + { .cmd = {TOPO_END} } \ + +enum topology_op { + TOPO_MOVETO, + TOPO_LINETO, + TOPO_ARCTO, + + TOPO_END, +}; + +struct topology_cmd { + enum topology_op op; +}; + +struct topology_arg { + struct component *c; +}; + +struct topology_elem { + union { + struct topology_cmd cmd; + struct topology_arg arg; + }; +}; + +struct topology { + struct topology_elem *elements; + size_t length; + size_t capacity; +}; + +void add_fragment(struct topology *topo, struct topology_elem *new); diff --git a/inc/cad/util.h b/inc/cad/util.h new file mode 100644 index 0000000..440d29b --- /dev/null +++ b/inc/cad/util.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include +#include +#include + +static void resize_buffer(void **buffer, size_t *capacity, size_t elems, size_t elemSize) { + assert(elems > 0); + uint64_t newpower = (sizeof(elems) * CHAR_BIT) - __builtin_clzl(elems-1); + elems = 1 << newpower; + + if(elems != *capacity) { + *capacity = elems; + *buffer = realloc(*buffer, *capacity * elemSize); + } +} + -- cgit v1.2.3