From 754a18240de72cec691389825ffa3487f7052ca7 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Tue, 8 Sep 2026 23:35:45 +0200 Subject: I think we actually have the examples working At the same time, for the first time --- src/construction.c | 34 +++++++-- src/debug.c | 4 +- src/main.c | 1 + src/solve.c | 211 ++++++++++++++++++++++++++++------------------------- 4 files changed, 139 insertions(+), 111 deletions(-) (limited to 'src') diff --git a/src/construction.c b/src/construction.c index 996b2d2..6b55c23 100644 --- a/src/construction.c +++ b/src/construction.c @@ -2,6 +2,28 @@ #include #include +void tangents(vec2 c2, double r1, double r2, struct line *l) { + double r = r2 - r1; + double z = sqrt(c2[0]) + sqrt(c2[1]); + double d = z - sqrt(r); + if (d < -DBL_EPSILON) return; + d = sqrt(fabs(d)); + l->norm[0] = (c2[0] * r + c2[1] * d) / z; + l->norm[1] = (c2[1] * r - c2[0] * d) / z; + l->C = r1; +} + +void line_tangent_circles(struct circle c1, struct circle c2, uint8_t root, struct line *l) { + vec2 o; + glm_vec2_sub(c2.center, c2.center, o); + + double i = ((root >> 0) & 0x1) * 2.0f - 1; + double j = ((root >> 1) & 0x1) * 2.0f - 1; + + tangents(o, c1.radius * i, c2.radius * j, l); + l->C -= l->norm[0] * c1.center[0] + l->norm[1] * l->norm[1]; +} + bool circle_line_intersect(struct circle circle, struct line line, uint8_t root, struct point *point) { double a = line.norm[0]; double b = line.norm[1]; @@ -108,10 +130,8 @@ static void transform_part(struct command *cmd, const struct command *last_cmd, vec2 p; glm_vec2_zero(p); - if(line->norm[0] > line->norm[1]) - glm_vec2_copy((vec2){-line->C / line->norm[0], 0}, p); - else - glm_vec2_copy((vec2){0, -line->C / line->norm[1]}, p); + // Point closest to the origin + glm_vec2_scale(line->norm, -line->C / glm_vec2_norm2(line->norm), p); // Rotate the line to the new orientation affine_transform_vec2(rotate, line->norm, line->norm); @@ -211,7 +231,7 @@ void place_points(struct drawing *drawing, double inputs[]) { uint32_t i = 0; for(struct command *current = drawing->root; current != NULL; current = current->next) { - // if(i++ > 50) break; + // if(i++ > 87) break; current->placed = true; switch(current->op) { @@ -293,9 +313,7 @@ void place_points(struct drawing *drawing, double inputs[]) { // centers abort(); } else { - // We should probably implement tangents for circles that - // don't happen to be 0 radius - abort(); + line_tangent_circles(current->arg1->circle, current->arg2->circle, current->root, ¤t->result.line); } }break; case CMD_POINT_CIRCLE_LINE: { diff --git a/src/debug.c b/src/debug.c index 0a9767a..a538e13 100644 --- a/src/debug.c +++ b/src/debug.c @@ -11,8 +11,8 @@ void print_subassemblies(struct constraints *c, struct subassembly *assemblies, fprintf(stderr, "=== Subassemblies: %zu ===\n", assemblies_num); for(size_t i = 0; i < assemblies_num; i++) { struct subassembly *a = &assemblies[i]; - fprintf(stderr, "Assembly %zu: %zu steps, fix=%zu, fixed=%s\n", - i, a->steps_num, a->fix, a->fixed ? "yes" : "no"); + 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); if(a->articulation_num > 0) { fprintf(stderr, " Articulations (%zu):\n", a->articulation_num); diff --git a/src/main.c b/src/main.c index 60ca295..b174794 100644 --- a/src/main.c +++ b/src/main.c @@ -129,6 +129,7 @@ void draw_from_constraints(struct constraints *c, struct topology *t, struct can } place_points(&drawing, params); + dump_program(&drawing, params); free(params); print_subassemblies(c, assemblies, assembly_num); diff --git a/src/solve.c b/src/solve.c index 73f1f87..f62604c 100644 --- a/src/solve.c +++ b/src/solve.c @@ -443,7 +443,7 @@ static size_t build_triangles(struct constraints *constraints_in, struct compone } for(size_t k = 0; k < assemblies[i].articulation_num; k++) { - if(assemblies[i].articulation[k] == oppo) continue; + if(assemblies[i].articulation[k] != oppo) continue; goto constrained_to_assembly; } @@ -486,6 +486,7 @@ constrained_to_assembly: steps[steps_i].fix = fixpoint; steps_i++; + goto candidate_found; } // Look for a triangle of assemblies @@ -507,7 +508,6 @@ constrained_to_assembly: } if(f1 == NULL) continue; - fprintf(stderr, "First leg %ld\n", i); // Then the side that shares an articulation with the first side, and also has one fixed for(size_t j = i+1; j < assemblyid-1; j++) { @@ -526,12 +526,10 @@ constrained_to_assembly: } if(f2 == NULL) continue; - fprintf(stderr, "Potential second leg %ld\n", j); struct component *f3 = NULL; for(size_t k = 0; k < assemblies[j].articulation_num; k++) { for(size_t x = 0; x < assemblies[i].articulation_num; x++) { - fprintf(stderr, "Potential check %p %p\n", assemblies[j].articulation[k], assemblies[i].articulation[x]); if(assemblies[j].articulation[k] != assemblies[i].articulation[x]) continue; goto shared_articulation_found; @@ -549,8 +547,6 @@ shared_articulation_found: if(f3 == NULL) continue; - fprintf(stderr, " Found match %ld %ld\n", i, j); - // No new constraints were used for this, we are just situating // 2 subassemblies by two relative to us. assemblies[i].folded_into = assembly; @@ -575,6 +571,7 @@ shared_articulation_found: steps[steps_i].fix3 = f3; steps_i++; + goto candidate_found; } } @@ -707,62 +704,19 @@ static void draw_for_subassembly(struct constraint *constraints, size_t fix, str oppo_j = constraints[step->j].c1; } + struct element *attachl = NULL; + for(size_t i = 0; i < step->assembly->articulation_num; i++) { + if(step->assembly->articulation[i] != oppo_j) continue; + + attachl = step->assembly->articulation_position[i]; + break; + } + assert(attachl != NULL); + struct element *attachp = NULL; struct element *p = NULL; if(step->fix == NULL) { - struct component *local_i; - struct component *oppo_i; - if(step->i_forward) { - local_i = constraints[step->i].c1; - oppo_i = constraints[step->i].c2; - } else { - local_i = constraints[step->i].c2; - oppo_i = constraints[step->i].c1; - } - attachp = oppo_i->e; - - struct component *local_k; - struct component *oppo_k; - if(step->k_forward) { - local_k = constraints[step->k].c1; - oppo_k = constraints[step->k].c2; - } else { - local_k = constraints[step->k].c2; - oppo_k = constraints[step->k].c1; - } - - // The local_ side is the "outer" (more leaf) component - // By the construction we know that i and k are never angle constraints. - - // @HACK for now just solve the simple case where oppo_i and oppo_k are the - // same component. Technically I think we should be able to solve other - // cases as well, I just don't want to do the math. - assert(oppo_i == oppo_k); - - if(constraints[step->i].type == CT_POINT_POINT_DISTANCE - && local_i->type == COM_POINT - && constraints[step->k].type == CT_POINT_POINT_DISTANCE - && local_k->type == COM_POINT) { - - draw_point_from_2_distance(drawing, constraints, step->i, step->k, local_i, local_k, &p, false); - assert(p != NULL); - } else if(constraints[step->i].type == CT_POINT_LINE_DISTANCE - && local_i->type == COM_LINE - && constraints[step->k].type == CT_POINT_POINT_DISTANCE - && local_k->type == COM_POINT) { - assert(oppo_i->type == COM_POINT); - - point_from_line_and_distance(drawing, constraints, step->i, step->k, local_i->e, local_k->e, constraints[step->k].c2 == local_k, &p); - } else if(constraints[step->i].type == CT_POINT_POINT_DISTANCE - && local_i->type == COM_POINT - && constraints[step->k].type == CT_POINT_LINE_DISTANCE - && local_k->type == COM_LINE) { - assert(oppo_i->type == COM_POINT); - - point_from_line_and_distance(drawing, constraints, step->k, step->i, local_k->e, local_i->e, constraints[step->i].c2 == local_i, &p); - } else { - abort(); - } + abort(); } else { p = step->fix->e; @@ -805,7 +759,7 @@ static void draw_for_subassembly(struct constraint *constraints, size_t fix, str .arg2 = l, .d = step->assembly, .attachp = attachp, - .attachl = oppo_j->e, + .attachl = attachl, }); } @@ -832,11 +786,6 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f assert(step->fix2 != NULL); assert(step->fix3 != NULL); - // Lets solve for a pretty static case first - assert(step->fix->type == COM_LINE); - assert(step->fix2->type == COM_POINT); - assert(step->fix3->type == COM_LINE); - struct element *ax_oppo_locally = step->fix2->e; struct element *bx_oppo_locally = step->fix->e; @@ -877,6 +826,10 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f assert(fix3_in_ax != NULL); assert(fix3_in_bx != NULL); + // Lets solve for a pretty static case first + assert(step->fix2->type == COM_POINT); + assert(step->fix3->type == COM_LINE); + //cx's naming is strange, the two local points are in bx and the opposing //point is in ax. We know that since ax was built before bx and had the two //local elements been in ax, we would have included the point as well. @@ -888,46 +841,46 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f .arg2 = fix3_in_ax, }); - struct element *assembly2_angle = insert_cmd(drawing, (struct command){ - .op = CMD_MEASURE_LINE_LINE_ANGLE, - .hidden = true, - .result.type = ETYPE_VALUE, - .arg1 = fix_in_bx, - .arg2 = fix3_in_bx, - }); - - struct element* cx_oppo_direction_locally; - { - struct element* l = insert_cmd(drawing, (struct command){ - .op = CMD_LINE_POINT_LINE_ANGLE, - .hidden = false, - .result.type = ETYPE_LINE, - .arg1 = ax_oppo_locally, - .arg2 = bx_oppo_locally, - .arg3 = assembly2_angle, + if(step->fix->type == COM_LINE) { + struct element *assembly2_angle = insert_cmd(drawing, (struct command){ + .op = CMD_MEASURE_LINE_LINE_ANGLE, + .hidden = true, + .result.type = ETYPE_VALUE, + .arg1 = fix_in_bx, + .arg2 = fix3_in_bx, }); - cx_oppo_direction_locally = insert_cmd(drawing, (struct command){ - .op = CMD_LINE_LINE_DISTANCE_PARALLEL, - .hidden = false, - .result.type = ETYPE_LINE, - .root = 1, - .arg1 = l, - .arg2 = assembly1_distance, - }); - } - cx_oppo_direction_locally = cx_oppo_direction_locally; + struct element* cx_oppo_direction_locally; + { + struct element* l = insert_cmd(drawing, (struct command){ + .op = CMD_LINE_POINT_LINE_ANGLE, + .hidden = false, + .result.type = ETYPE_LINE, + .arg1 = ax_oppo_locally, + .arg2 = bx_oppo_locally, + .arg3 = assembly2_angle, + }); - insert_cmd(drawing, (struct command){ - .op = CMD_IMPORT_POINT_LINE, - .arg1 = ax_oppo_locally, - .arg2 = cx_oppo_direction_locally, - .d = step->assembly2, - .attachp = fix2_in_ax, - .attachl = fix3_in_ax, - }); + cx_oppo_direction_locally = insert_cmd(drawing, (struct command){ + .op = CMD_LINE_LINE_DISTANCE_PARALLEL, + .hidden = false, + .result.type = ETYPE_LINE, + .root = 1, + .arg1 = l, + .arg2 = assembly1_distance, + }); + } + + assert(fix3_in_ax != NULL); + insert_cmd(drawing, (struct command){ + .op = CMD_IMPORT_POINT_LINE, + .arg1 = ax_oppo_locally, + .arg2 = cx_oppo_direction_locally, + .d = step->assembly2, + .attachp = fix2_in_ax, + .attachl = fix3_in_ax, + }); - { struct element *local_intersection = insert_cmd(drawing, (struct command){ .op = CMD_POINT_LINE_LINE, .hidden = true, @@ -943,6 +896,7 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f .arg1 = fix_in_bx, .arg2 = fix3_in_bx, }); + assert(fix_in_bx != NULL); insert_cmd(drawing, (struct command){ .op = CMD_IMPORT_POINT_LINE, @@ -952,6 +906,61 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f .attachp = subassembly2_intersection, .attachl = fix_in_bx, }); + } else { + assert(step->fix->type == COM_POINT); + + struct element *assembly2_distance = insert_cmd(drawing, (struct command){ + .op = CMD_MEASURE_POINT_LINE_DISTANCE, + .hidden = true, + .result.type = ETYPE_VALUE, + .arg1 = fix_in_bx, + .arg2 = fix3_in_bx, + }); + + struct element *c1 = insert_cmd(drawing, (struct command){ + .op = CMD_CIRCLE_CENTER_RADIUS, + .hidden = true, + .result.type = ETYPE_CIRCLE, + .arg1 = ax_oppo_locally, + .arg2 = assembly1_distance, + }); + + struct element *c2 = insert_cmd(drawing, (struct command){ + .op = CMD_CIRCLE_CENTER_RADIUS, + .hidden = true, + .result.type = ETYPE_CIRCLE, + .arg1 = bx_oppo_locally, + .arg2 = assembly2_distance, + }); + + struct element* cx_oppo_direction_locally = insert_cmd(drawing, (struct command){ + .op = CMD_LINE_CIRCLE_CIRCLE_TANGENT, + .hidden = false, + .result.type = ETYPE_LINE, + .arg1 = c1, + .arg2 = c2, + }); + + assert(fix3_in_ax != NULL); + assert(fix3_in_bx != NULL); + + insert_cmd(drawing, (struct command){ + .op = CMD_IMPORT_POINT_LINE, + .arg1 = ax_oppo_locally, + .arg2 = cx_oppo_direction_locally, + .d = step->assembly2, + .attachp = fix2_in_ax, + .attachl = fix3_in_ax, + }); + + insert_cmd(drawing, (struct command){ + .op = CMD_IMPORT_POINT_LINE, + .arg1 = bx_oppo_locally, + .arg2 = cx_oppo_direction_locally, + .d = step->assembly, + .attachp = fix_in_bx, + .attachl = fix3_in_bx, + }); } } -- cgit v1.2.3