From e49c0ac340da53f3460a2aa02b514340ab0866e0 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Sun, 29 Mar 2026 12:07:19 +0200 Subject: Start testing as a cad tool --- examples/mount.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ examples/triangle_tip.c | 2 +- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 examples/mount.c (limited to 'examples') diff --git a/examples/mount.c b/examples/mount.c new file mode 100644 index 0000000..7ffdc34 --- /dev/null +++ b/examples/mount.c @@ -0,0 +1,119 @@ +#include "cad.h" + +void draw_from_constraints(struct constraints *c, struct topology *t, struct canvas *cv) { + struct drawing drawing = {}; + struct subassembly assemblies[16] = {}; + 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); + free(params); + + reconstruct_drawing(c, assemblies, &assemblies_num); + + begin_drawing(cv); + draw_topology(cv, t); + draw_constraints(cv, c); + end_drawing(cv); + + free_drawing(&drawing); +} + +int main(int argc, char *argv[]) { + struct constraints constraints = {}; + struct topology topo = {}; + + struct component mid_bot = {.type = COM_POINT}; + struct component pseudo = {.type = COM_POINT}; + struct component bot = {.type = COM_LINE}; + + struct component symmetry = {.type = COM_LINE}; + + struct component right_lower = {.type = COM_LINE}; + struct component left_lower = {.type = COM_LINE}; + + struct component right_under_hole_mid = {.type = COM_POINT}; + + struct component right_hole_bot_tan = {.type = COM_LINE}; + struct component left_hole_bot_tan = {.type = COM_LINE}; + struct component right_hole_top_tan = {.type = COM_LINE}; + struct component left_hole_top_tan = {.type = COM_LINE}; + + struct component right_hole_mid = {.type = COM_POINT}; + struct component left_hole_mid = {.type = COM_POINT}; + + struct component swoop_mid = {.type = COM_POINT}; + struct component swoop_top_tan = {.type = COM_POINT}; + + struct component left_bend = {.type = COM_POINT}; + struct component lb2 = {.type = COM_POINT}; + + struct component l1 = {.type = COM_LINE}; + + add_constraint(&constraints, (struct constraint[]){ + PP_DISTANCE(&mid_bot, &pseudo, 10), + POINT_ON_LINE(&mid_bot, &bot), + POINT_ON_LINE(&pseudo, &bot), + + LL_ANGLE(&bot, &symmetry, DEG(90)), + POINT_ON_LINE(&mid_bot, &symmetry), + + // @HACK This is going in the wrong direction? + PL_DISTANCE(&mid_bot, &right_lower, 35), + LL_ANGLE(&bot, &right_lower, DEG(-90)), + PL_DISTANCE(&left_lower, &mid_bot, 35), + LL_ANGLE(&bot, &left_lower, DEG(90)), + + PL_DISTANCE(&right_hole_mid, &bot, 80), + PL_DISTANCE(&left_hole_mid, &bot, 80), + PL_DISTANCE(&right_hole_mid, &symmetry, -60), + PL_DISTANCE(&left_hole_mid, &symmetry, 60), + + PL_DISTANCE(&right_under_hole_mid, &right_lower, 20), + PL_DISTANCE(&right_under_hole_mid, &right_hole_bot_tan, 20), + + LL_ANGLE(&right_hole_bot_tan, &symmetry, DEG(270)), + + PL_DISTANCE(&right_hole_mid, &right_hole_bot_tan, -28), + // PL_DISTANCE(&left_hole_mid, &left_hole_bot_tan, 56), + PL_DISTANCE(&right_hole_mid, &right_hole_top_tan, 28), + // PL_DISTANCE(&left_hole_mid, &left_hole_top_tan, 56), + + POINT_ON_LINE(&swoop_mid, &symmetry), + PP_DISTANCE(&swoop_mid, &right_hole_mid, 120 - 28), + // PL_DISTANCE(&swoop_mid, &swoop_top_tan, 120), + PL_DISTANCE(&swoop_mid, &right_hole_top_tan, 120), + + + // Make a triangle + CEND(), + }); + + add_fragment(&topo, (struct topology_elem[]){ + TOPO_MOVETO(&mid_bot), + TOPO_LINETO(&right_hole_mid), + + TOPO_MOVETO(&mid_bot), + // TOPO_LINETO(&p2), + // TOPO_LINETO(&p3), + TOPO_END(), + }); + + struct canvas canvas = { + .state = CANVAS_INIT, + .f = stdout, + }; + + draw_from_constraints(&constraints, &topo, &canvas); + + free_constraints(&constraints); + return 0; +} diff --git a/examples/triangle_tip.c b/examples/triangle_tip.c index b6fcf99..4da57da 100644 --- a/examples/triangle_tip.c +++ b/examples/triangle_tip.c @@ -52,7 +52,7 @@ int main(int argc, char *argv[]) { POINT_ON_LINE(&p2, &t1base), POINT_ON_LINE(&p1, &t1bas2), - LL_ANGLE(&t1base, &t1bas2, DEG(-10)), + LL_ANGLE(&t1base, &t1bas2, DEG(-5)), // With another triangle sharing a point PP_DISTANCE(&p4, &p5, 30), -- cgit v1.2.3