diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2026-08-30 01:09:31 +0200 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2026-08-30 01:09:31 +0200 |
| commit | d15fe2f08c1be5e2c972617a4d2f992667563caa (patch) | |
| tree | e8d67b03418285a738c0d53b65779b6e9935443e /src | |
| parent | 8036447ee793fbff03147683f4a2afe9684641ce (diff) | |
| download | constraint-d15fe2f08c1be5e2c972617a4d2f992667563caa.tar.gz constraint-d15fe2f08c1be5e2c972617a4d2f992667563caa.zip | |
I think that's single component borrow working
Diffstat (limited to 'src')
| -rw-r--r-- | src/construction.c | 6 | ||||
| -rw-r--r-- | src/solve.c | 219 |
2 files changed, 164 insertions, 61 deletions
diff --git a/src/construction.c b/src/construction.c index f2a473a..2a2487b 100644 --- a/src/construction.c +++ b/src/construction.c @@ -188,7 +188,7 @@ void dump_program(struct drawing *drawing, double inputs[]) { fprintf(stderr, "%p -> PLL(%p, %p) = <%f, %f>\n", ¤t->result, current->arg1, current->arg2, current->result.point.pos[0], current->result.point.pos[1]); }break; case CMD_CIRCLE_CENTER_POINT: { - fprintf(stderr, "%p -> CCR(%p, %p) = ((x-%f)^2 + (y-%f)^2 = %f^2)\n", ¤t->result, current->arg1, current->arg2, current->result.circle.center[0], current->result.circle.center[1], current->result.circle.radius); + fprintf(stderr, "%p -> CCP(%p, %p) = ((x-%f)^2 + (y-%f)^2 = %f^2)\n", ¤t->result, current->arg1, current->arg2, current->result.circle.center[0], current->result.circle.center[1], current->result.circle.radius); }break; case CMD_IMPORT_POINT_LINE: { fprintf(stderr, "%p -> IMP(%p, %p, %p, %p) = <N/A>\n", ¤t->result, current->arg1, current->arg2, current->attachp, current->attachl); @@ -408,12 +408,12 @@ void place_points(struct drawing *drawing, double inputs[]) { mat3 transform; glm_mat3_identity(transform); - glm_rotate2d(transform, theta); + // glm_rotate2d(transform, theta); { vec2 negative_translate; glm_vec2_negate_to(origin->result.point.pos, negative_translate); - glm_translate2d(transform, negative_translate); + // glm_translate2d(transform, negative_translate); } fprintf(stderr, "W %f %f %f\n", origin->result.point.pos[0], origin->result.point.pos[1], theta); diff --git a/src/solve.c b/src/solve.c index 7f3df82..2a498fc 100644 --- a/src/solve.c +++ b/src/solve.c @@ -131,6 +131,8 @@ struct solve_step { struct subassembly *assembly; struct subassembly *assembly2; + + struct component *fix; }; static bool fix_first(struct constraint *constraints, size_t constraints_num, size_t *c) { @@ -274,9 +276,10 @@ static bool try_fix_component(struct constraints *constraints_in, struct compone return true; } -static size_t build_triangles(struct constraints *constraints_in, struct component **components, size_t component_num, size_t origin, uint8_t assemblyid, struct subassembly *assembly) { +static size_t build_triangles(struct constraints *constraints_in, struct component **components, size_t component_num, size_t origin, uint8_t assemblyid, struct subassembly *assemblies) { struct constraint *constraints = constraints_in->elements; size_t constraints_num = constraints_in->length; + struct subassembly *assembly = &assemblies[assemblyid-1]; struct solve_step *steps = assembly->steps; uint64_t order = 1; @@ -395,6 +398,91 @@ static size_t build_triangles(struct constraints *constraints_in, struct compone } } + // Look for a subassembly that has a fixed articulation point and + // a constraint to something fixed + for(size_t i = 0; i < assemblyid-1; i++) { + struct component *fixpoint = NULL; + // Find an articulation point that's fixed + for(size_t j = 0; j < assemblies[i].articulation_num; j++) { + if(!assemblies[i].articulation[j]->fixed) continue; + + // @HACK The logic here also holds for lines, but I don't care + // to do the match right now + if(assemblies[i].articulation[j]->type != COM_POINT) continue; + + fixpoint = assemblies[i].articulation[j]; + break; + } + + if(fixpoint == NULL) continue; + + struct constraint *extra = NULL; + bool extra_f; + // Find a constraint that complements the fixpoint + for(size_t j = 0; j < constraints_num; j++) { + if(constraints[j].used) continue; + + // @HACK The logic here also holds for other constraints, but + // I don't care to do the match right now + if(constraints[j].type != CT_LINE_LINE_ANGLE) continue; + + bool f; + struct component *oppo; + if(constraints[j].c1->fixed) { + oppo = constraints[j].c2; + f = true; + } else if(constraints[j].c2->fixed) { + oppo = constraints[j].c1; + f = false; + } else { + continue; + } + + for(size_t k = 0; k < assemblies[i].articulation_num; k++) { + if(assemblies[i].articulation[k] == oppo) continue; + + goto constrained_to_assembly; + } + continue; +constrained_to_assembly: + + assert(oppo != fixpoint); + extra = &constraints[j]; + extra_f = f; + break; + } + + if(extra == NULL) continue; + + // @HACK The math of this step only works for this combination + assert(fixpoint->type == COM_POINT); + assert(extra->type == CT_LINE_LINE_ANGLE); + + // We've fixed the whole of assembly i, so mark everything in there + // fixed + for(size_t j = 0; j < component_num; j++) { + if(components[j]->ein != &assemblies[i]) continue; + + add_frontier(components[j]); + components[j]->ein = assembly; + } + + extra->used = assemblyid; + extra->used = order++; + + fprintf(stderr, "WOOO %ld\n", i); + steps[steps_i].assembly = &assemblies[i]; + // j is the participant that's allowed to be an angle in the + // non-fixpoint version of subassembly fixing. We keep to that here + // to make the drawing simpler. + steps[steps_i].j = extra - constraints; + steps[steps_i].j_forward = extra_f; + + steps[steps_i].fix = fixpoint; + + steps_i++; + } + // Look for two subassemblies that are each almost fixed (by two // constraints) and are mutually constrained by two constraints { @@ -681,16 +769,6 @@ static void draw_point_from_2_distance(struct drawing *drawing, struct constrain } static void draw_for_subassembly(struct constraint *constraints, size_t fix, struct solve_step *step, struct drawing *drawing, struct subassembly *assembly) { - 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; - } - struct component *local_j; struct component *oppo_j; if(step->j_forward) { @@ -701,60 +779,85 @@ static void draw_for_subassembly(struct constraint *constraints, size_t fix, str oppo_j = constraints[step->j].c1; } - 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; - } + 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; - // The local_ side is the "outer" (more leaf) component - // By the construction we know that i and k are never angle constraints. + 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; + } - // @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); + // The local_ side is the "outer" (more leaf) component + // By the construction we know that i and k are never angle constraints. - // @HACK Let's only solve for an explicit angle for now - assert(constraints[step->j].type == CT_LINE_LINE_ANGLE); + // @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); - struct element *p = NULL; - 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); + 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(); + } } else { - abort(); + p = step->fix->e; + + for(size_t i = 0; i < step->assembly->articulation_num; i++) { + if(step->assembly->articulation[i] != step->fix) continue; + + attachp = step->assembly->articulation_position[i]; + break; + } + + assert(attachp != NULL); } + // @HACK Let's only solve for an explicit angle for now + assert(constraints[step->j].type == CT_LINE_LINE_ANGLE); + // The point we share with the subassembly // The line that matches the angle struct element *theta = insert_cmd(drawing, (struct command){ .op = CMD_VALUE_INPUT, .index = step->j, - .dir = constraints[step->j].forward, + .dir = constraints[step->j].forward ^ step->j_forward, .result.type = ETYPE_VALUE, }); struct element *l = insert_cmd(drawing, (struct command){ @@ -773,7 +876,7 @@ static void draw_for_subassembly(struct constraint *constraints, size_t fix, str .arg1 = p, .arg2 = l, .d = step->assembly, - .attachp = oppo_i->e, + .attachp = attachp, .attachl = oppo_j->e, }); } @@ -1220,13 +1323,13 @@ bool solve_constraints(struct constraints *constraints, struct drawing *drawing, assemblies[*assemblies_num].steps = calloc(1, sizeof(struct solve_step) * constraints->length); assemblies[*assemblies_num].articulation = malloc(sizeof(struct component*) * constraints->length); assemblies[*assemblies_num].articulation_position = malloc(sizeof(struct element*) * constraints->length); - assemblies[*assemblies_num].steps_num = build_triangles(constraints, components, dest_num, fix, *assemblies_num+1, &assemblies[*assemblies_num]); + assemblies[*assemblies_num].steps_num = build_triangles(constraints, components, dest_num, fix, (*assemblies_num)+1, assemblies); assemblies[*assemblies_num].fix = fix; - // printf("Assembly %ld %ld\n", *assemblies_num, fix); - // for(size_t i = 0; i < assemblies[*assemblies_num].articulation_num; i++) { - // printf(" Articulation %p\n", assemblies[*assemblies_num].articulation[i]); - // } + fprintf(stderr, "Assembly %ld %ld\n", *assemblies_num, fix); + for(size_t i = 0; i < assemblies[*assemblies_num].articulation_num; i++) { + fprintf(stderr, " Articulation %p\n", assemblies[*assemblies_num].articulation[i]); + } // printf("Solved in %ld steps\n", assemblies[*assemblies_num].steps_num); |
