summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2026-09-20 11:25:42 +0200
committerJesper Jensen <jesper@jnsn.dev>2026-09-20 11:25:42 +0200
commitf16fc18221cd8d1a8cb1bf9e40790f3b482fb199 (patch)
tree55e96733668c2637437849638f79072a87ec8149
parent3bc85ce682ca92e2c6a17753f46c23f3902b60c7 (diff)
downloadconstraint-f16fc18221cd8d1a8cb1bf9e40790f3b482fb199.tar.gz
constraint-f16fc18221cd8d1a8cb1bf9e40790f3b482fb199.zip
Add another example
-rw-r--r--examples/inset.c153
-rw-r--r--src/debug.c2
-rw-r--r--src/solve.c2
3 files changed, 155 insertions, 2 deletions
diff --git a/examples/inset.c b/examples/inset.c
new file mode 100644
index 0000000..4c02a5e
--- /dev/null
+++ b/examples/inset.c
@@ -0,0 +1,153 @@
+#include "cad.h"
+#include "cad/svg.h"
+#include "cad/debug.h"
+
+void draw_from_constraints(struct constraints *c, struct topology *t, struct canvas *cv) {
+ struct drawing drawing = {};
+ struct subassembly assemblies[32] = {};
+ size_t assemblies_num;
+ bool rc = solve_constraints(c, &drawing, assemblies, &assemblies_num);
+ assert(rc);
+
+ // Copy over all the parameter values to a new array
+ // @PERF: Maybe we should just store them in a separate array to start with
+ double *params = malloc(sizeof(double) * (c->length));
+ for(size_t i = 0; i < c->length; i++) {
+ params[i] = c->elements[i].v;
+ }
+
+ place_points(&drawing, params);
+ dump_program(&drawing, params);
+ free(params);
+
+ reconstruct_drawing(c, assemblies, &assemblies_num);
+ print_subassemblies(c, assemblies, assemblies_num);
+
+ begin_drawing(cv);
+ draw_topology(cv, t);
+ draw_constraints(cv, c);
+ draw_elements(cv, &drawing);
+ end_drawing(cv);
+
+ free_drawing(&drawing);
+}
+
+int main(int argc, char *argv[]) {
+ struct constraints constraints = {};
+ struct topology topo = {};
+
+ struct component a1 = {.type = COM_POINT};
+ struct component d1 = {.type = COM_POINT};
+ struct component p1 = {.type = COM_POINT};
+ struct component c1 = {.type = COM_POINT};
+ struct component a2 = {.type = COM_POINT};
+ struct component d2 = {.type = COM_POINT};
+ struct component p2 = {.type = COM_POINT};
+ struct component c2 = {.type = COM_POINT};
+ struct component a3 = {.type = COM_POINT};
+ struct component d3 = {.type = COM_POINT};
+ struct component p3 = {.type = COM_POINT};
+ struct component c3 = {.type = COM_POINT};
+ struct component a4 = {.type = COM_POINT};
+ struct component d4 = {.type = COM_POINT};
+ struct component p4 = {.type = COM_POINT};
+ struct component c4 = {.type = COM_POINT};
+
+ struct component l1 = {.type = COM_LINE};
+ struct component l1i = {.type = COM_LINE};
+ struct component l2 = {.type = COM_LINE};
+ struct component l2i = {.type = COM_LINE};
+ struct component l3 = {.type = COM_LINE};
+ struct component l3i = {.type = COM_LINE};
+ struct component l4 = {.type = COM_LINE};
+ struct component l4i = {.type = COM_LINE};
+
+ add_constraint(&constraints, (struct constraint[]){
+ PP_DISTANCE(&p1, &p2, 10),
+ PP_DISTANCE(&a1, &d2, 20),
+
+ POINT_ON_LINE(&a1, &l1),
+ POINT_ON_LINE(&a1, &l2i),
+
+ POINT_ON_LINE(&d1, &l1i),
+ POINT_ON_LINE(&d1, &l2),
+
+ POINT_ON_LINE(&a2, &l2),
+ POINT_ON_LINE(&a2, &l3i),
+
+ POINT_ON_LINE(&d2, &l2i),
+ POINT_ON_LINE(&d2, &l3),
+
+ POINT_ON_LINE(&a3, &l3),
+ POINT_ON_LINE(&a3, &l4i),
+
+ POINT_ON_LINE(&d3, &l3i),
+ POINT_ON_LINE(&d3, &l4),
+
+ POINT_ON_LINE(&a4, &l4),
+ POINT_ON_LINE(&a4, &l1i),
+
+ POINT_ON_LINE(&d4, &l4i),
+ POINT_ON_LINE(&d4, &l1),
+
+ POINT_ON_LINE(&p1, &l1),
+ POINT_ON_LINE(&p2, &l1),
+
+ POINT_ON_LINE(&p2, &l2),
+ POINT_ON_LINE(&p3, &l2),
+
+ POINT_ON_LINE(&p3, &l3),
+ POINT_ON_LINE(&p4, &l3),
+
+ POINT_ON_LINE(&p4, &l4),
+ POINT_ON_LINE(&p1, &l4),
+
+ LL_ANGLE(&l1, &l1i, DEG(0)),
+ LL_ANGLE(&l2, &l2i, DEG(0)),
+ LL_ANGLE(&l3, &l3i, DEG(0)),
+ LL_ANGLE(&l4, &l4i, DEG(0)),
+
+ PL_DISTANCE(&p1, &l1i, 2),
+ PL_DISTANCE(&p2, &l2i, 2),
+ PL_DISTANCE(&p3, &l3i, 2),
+ PL_DISTANCE(&p4, &l4i, 2),
+
+ POINT_ON_LINE(&c1, &l1i),
+ POINT_ON_LINE(&c1, &l2i),
+
+ POINT_ON_LINE(&c2, &l2i),
+ POINT_ON_LINE(&c2, &l3i),
+
+ POINT_ON_LINE(&c3, &l3i),
+ POINT_ON_LINE(&c3, &l4i),
+
+ POINT_ON_LINE(&c4, &l4i),
+ POINT_ON_LINE(&c4, &l1i),
+
+ LL_ANGLE(&l1, &l2, DEG(90)),
+ LL_ANGLE(&l2, &l3, DEG(90)),
+ LL_ANGLE(&l3, &l4, DEG(90)),
+
+ CEND(),
+ });
+
+ add_fragment(&topo, (struct topology_elem[]){
+ TOPO_MOVETO(&p1),
+ TOPO_LINETO(&p2),
+ TOPO_LINETO(&p3),
+ TOPO_LINETO(&p4),
+ TOPO_LINETO(&p1),
+
+ TOPO_END(),
+ });
+
+ struct canvas canvas = {
+ .state = CANVAS_INIT,
+ .f = stdout,
+ };
+
+ draw_from_constraints(&constraints, &topo, &canvas);
+
+ free_constraints(&constraints);
+ return 0;
+}
diff --git a/src/debug.c b/src/debug.c
index a538e13..94eab37 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -12,7 +12,7 @@ void print_subassemblies(struct constraints *c, struct subassembly *assemblies,
for(size_t i = 0; i < assemblies_num; i++) {
struct subassembly *a = &assemblies[i];
fprintf(stderr, "Assembly %zu: %zu steps, fix=%zu, fixed=%s, parent=%zu\n",
- i, a->steps_num, a->fix, a->fixed ? "yes" : "no", a->folded_into-assemblies);
+ i, a->steps_num, a->fix, a->fixed ? "yes" : "no", a->folded_into == NULL ? 0 : a->folded_into-assemblies);
if(a->articulation_num > 0) {
fprintf(stderr, " Articulations (%zu):\n", a->articulation_num);
diff --git a/src/solve.c b/src/solve.c
index f62604c..b70ef54 100644
--- a/src/solve.c
+++ b/src/solve.c
@@ -1284,7 +1284,7 @@ bool solve_constraints(struct constraints *constraints, struct drawing *drawing,
assemblies[*assemblies_num].articulation[i]->in = NULL;
}
(*assemblies_num)++;
- assert(*assemblies_num <= 16);
+ assert(*assemblies_num <= 32);
}
// Fill out the aliased points out with the values from their targets