diff options
Diffstat (limited to 'src/solve.c')
| -rw-r--r-- | src/solve.c | 359 |
1 files changed, 145 insertions, 214 deletions
diff --git a/src/solve.c b/src/solve.c index 2a498fc..73f1f87 100644 --- a/src/solve.c +++ b/src/solve.c @@ -133,6 +133,8 @@ struct solve_step { struct subassembly *assembly2; struct component *fix; + struct component *fix2; + struct component *fix3; }; static bool fix_first(struct constraint *constraints, size_t constraints_num, size_t *c) { @@ -401,6 +403,8 @@ 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++) { + if(assemblies[i].folded_into != NULL) continue; + struct component *fixpoint = NULL; // Find an articulation point that's fixed for(size_t j = 0; j < assemblies[i].articulation_num; j++) { @@ -446,6 +450,7 @@ static size_t build_triangles(struct constraints *constraints_in, struct compone continue; constrained_to_assembly: + assert(oppo != fixpoint); extra = &constraints[j]; extra_f = f; @@ -460,17 +465,17 @@ constrained_to_assembly: // 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; + assemblies[i].folded_into = assembly; + for(size_t j = 0; j < assemblies[i].articulation_num; j++) { + if(assemblies[i].articulation[j]->fixed) continue; - add_frontier(components[j]); - components[j]->ein = assembly; + add_frontier(assemblies[i].articulation[j]); + assemblies[i].articulation[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 @@ -483,170 +488,93 @@ constrained_to_assembly: steps_i++; } - // Look for two subassemblies that are each almost fixed (by two - // constraints) and are mutually constrained by two constraints - { - // Find the first subcomponent we can fix - for(size_t i = 0; i < component_num; i++) { - // If a component was already fixed, we don't need to do anything - if(components[i]->fixed) continue; - - // We are only looking for subcomponents - if(components[i]->ein == NULL) continue; - - struct constraint *not_angle_a = NULL; - bool f1_a; - struct constraint *possibly_angle_a = NULL; - bool f2_a; - - struct constraint *second_not_angle_a = NULL; - bool f3_a; - - struct subassembly *ein_a = components[i]->ein; - // @HACK Reset the points assembly information force it to not - // use the subassembly path - components[i]->ein = NULL; - if(try_fix_component(constraints_in, components[i], ¬_angle_a, &f1_a, &possibly_angle_a, &f2_a, &second_not_angle_a, &f3_a)) { - // This should never be set since we reset the subassembly - components[i]->ein = ein_a; - assert(second_not_angle_a == NULL); - - // Now find a second assembly - for(size_t j = 0; j < component_num; j++) { - // If a component was already fixed, we don't need to do anything - if(components[j]->fixed) continue; - - // We are only looking for subcomponents - if(components[j]->ein == NULL) continue; - if(components[j]->ein == ein_a) continue; - - struct constraint *not_angle_b = NULL; - bool f1_b; - struct constraint *possibly_angle_b = NULL; - bool f2_b; - - struct constraint *second_not_angle_b = NULL; - bool f3_b; - struct subassembly *ein_b = components[j]->ein; - // @HACK Reset the points assembly information force it to not - // use the subassembly path - components[j]->ein = NULL; - if(try_fix_component(constraints_in, components[j], ¬_angle_b, &f1_b, &possibly_angle_b, &f2_b, &second_not_angle_b, &f3_b)) { - components[j]->ein = ein_b; - // This should never be set since we reset the subassembly - assert(second_not_angle_b == NULL); - - - // We now have two subassmblies that are fixable by - // two constraints to the current one. Now we need - // to find two constraints that go between them - - struct constraint *not_angle_c = NULL; - bool f1_c; - struct constraint *possibly_angle_c = NULL; - bool f2_c; - - for(size_t k = 0; k < constraints_num; k++) { - if(constraints[k].used) continue; - - bool f; - struct component *oppo; - if(constraints[k].c1->ein == ein_a && constraints[k].c2->ein == ein_b) { - oppo = constraints[k].c2; - f = false; - } else if(constraints[k].c1->ein == ein_b && constraints[k].c2->ein == ein_a) { - oppo = constraints[k].c1; - f = true; - } else { - continue; - } - - assert(!oppo->fixed); - - not_angle_c = &constraints[k]; - f1_c = f; - - break; - } - - if(not_angle_c == NULL) continue; - - for(size_t k = 0; k < constraints_num; k++) { - if(constraints[k].used) continue; - - if(&constraints[k] == not_angle_c) continue; - - bool f; - struct component *oppo; - if(constraints[k].c1->ein == ein_a && constraints[k].c2->ein == ein_b) { - oppo = constraints[k].c2; - f = false; - } else if(constraints[k].c1->ein == ein_b && constraints[k].c2->ein == ein_a) { - oppo = constraints[k].c1; - f = true; - } else { - continue; - } - - assert(!oppo->fixed); - - possibly_angle_c = &constraints[k]; - f2_c = f; - - break; - } - - if(possibly_angle_c == NULL) continue; - - fprintf(stderr, "%ld %ld Assembly %p and %p, %d %d %s %s\n", i, j, ein_a, ein_b, f1_c, f2_c, constraint_type_name[not_angle_c->type], constraint_type_name[possibly_angle_c->type]); - - // We've fixed the subcomponents, so we have to fix - // the whole thing - for(size_t j = 0; j < component_num; j++) { - if(components[j]->ein != ein_a && components[j]->ein != ein_b) continue; - - add_frontier(components[j]); - components[j]->ein = assembly; - } - - // There's a total of 6 different constraints involved here - not_angle_a->used = assemblyid; - not_angle_a->order = order++; - not_angle_b->used = assemblyid; - not_angle_b->order = order++; - not_angle_c->used = assemblyid; - not_angle_c->order = order++; - - possibly_angle_a->used = assemblyid; - possibly_angle_a->order = order++; - possibly_angle_b->used = assemblyid; - possibly_angle_b->order = order++; - possibly_angle_c->used = assemblyid; - possibly_angle_c->order = order++; - - steps[steps_i].assembly = ein_a; - steps[steps_i].assembly2 = ein_b; - steps[steps_i].i = not_angle_a - constraints; - steps[steps_i].i_forward = f1_a; - steps[steps_i].j = possibly_angle_a - constraints; - steps[steps_i].j_forward = f2_a; - steps[steps_i].k = not_angle_b - constraints; - steps[steps_i].k_forward = f1_a; - steps[steps_i].x = possibly_angle_b - constraints; - steps[steps_i].x_forward = f2_b; - steps[steps_i].y = not_angle_c - constraints; - steps[steps_i].y_forward = f1_c; - steps[steps_i].z = possibly_angle_c - constraints; - steps[steps_i].z_forward = f2_c; - - steps_i++; - - goto candidate_found; - } - components[j]->ein = ein_b; + // Look for a triangle of assemblies + // First on one side + for(size_t i = 0; i < assemblyid-1; i++) { + if(assemblies[i].folded_into != NULL) continue; + + struct component *f1 = 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; + + f1 = assemblies[i].articulation[j]; + break; + } + + 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++) { + if(assemblies[j].folded_into != NULL) continue; + + struct component *f2 = NULL; + for(size_t k = 0; k < assemblies[j].articulation_num; k++) { + if(!assemblies[j].articulation[k]->fixed) continue; + + // @HACK The logic here also holds for lines, but I don't care + // to do the match right now + if(assemblies[j].articulation[k]->type != COM_POINT) continue; + + f2 = assemblies[j].articulation[k]; + break; + } + + 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; } + continue; +shared_articulation_found: + + // @HACK The logic here also holds for lines, but I don't care + // to do the math right now + // if(assemblies[j].articulation[k]->type != COM_POINT) continue; + + f3 = assemblies[j].articulation[k]; + break; + } + + 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; + for(size_t k = 0; k < assemblies[i].articulation_num; k++) { + if(assemblies[i].articulation[k]->fixed) continue; + + add_frontier(assemblies[i].articulation[k]); + assemblies[i].articulation[k]->ein = assembly; } - components[i]->ein = ein_a; + assemblies[j].folded_into = assembly; + for(size_t k = 0; k < assemblies[j].articulation_num; k++) { + if(assemblies[j].articulation[k]->fixed) continue; + + add_frontier(assemblies[j].articulation[k]); + assemblies[j].articulation[k]->ein = assembly; + } + + steps[steps_i].assembly = &assemblies[i]; + steps[steps_i].assembly2 = &assemblies[j]; + steps[steps_i].fix = f1; + steps[steps_i].fix2 = f2; + steps[steps_i].fix3 = f3; + + steps_i++; } } @@ -900,70 +828,72 @@ static void draw_for_subassembly(struct constraint *constraints, size_t fix, str static void draw_for_double_subassembly(struct constraint *constraints, size_t fix, struct solve_step *step, struct drawing *drawing, struct subassembly *assembly) { assert(step->assembly != NULL); assert(step->assembly2 != NULL); + assert(step->fix != NULL); + assert(step->fix2 != NULL); + assert(step->fix3 != NULL); - // Here we have two assemblies that relate to the current fixed set through - // two constraints each, but are also mutually constrained by two - // constraints. + // 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); - expand_constraint(a1, step->i); - expand_constraint(a2, step->j); + struct element *ax_oppo_locally = step->fix2->e; + struct element *bx_oppo_locally = step->fix->e; - expand_constraint(b1, step->k); - expand_constraint(b2, step->x); + struct element *fix_in_bx; + for(size_t i = 0; i < step->assembly->articulation_num; i++) { + if(step->assembly->articulation[i] != step->fix) continue; - expand_constraint(c1, step->y); - expand_constraint(c2, step->z); + fix_in_bx = step->assembly->articulation_position[i]; + break; + } - // Lets solve for a pretty static case first - assert(a1->type == CT_POINT_POINT_DISTANCE); - assert(a2->type == CT_POINT_LINE_DISTANCE); + struct element *fix2_in_ax; + for(size_t i = 0; i < step->assembly2->articulation_num; i++) { + if(step->assembly2->articulation[i] != step->fix2) continue; + + fix2_in_ax = step->assembly2->articulation_position[i]; + break; + } - assert(b1->type == CT_POINT_LINE_DISTANCE); - assert(b2->type == CT_LINE_LINE_ANGLE); + struct element *fix3_in_bx; + for(size_t i = 0; i < step->assembly->articulation_num; i++) { + if(step->assembly->articulation[i] != step->fix3) continue; - assert(c1->type == CT_POINT_LINE_DISTANCE); - assert(c2->type == CT_LINE_LINE_ANGLE); + fix3_in_bx = step->assembly->articulation_position[i]; + break; + } - // All the constraints form triangles, not necessarily required, but - // simpler - assert(a1_oppo == a2_oppo); - assert(a1_local != a2_local); - assert(b1_oppo == b2_oppo); - assert(b1_local != b2_local); - assert(c1_oppo == c2_oppo); - assert(c1_local != c2_local); + struct element *fix3_in_ax; + for(size_t i = 0; i < step->assembly2->articulation_num; i++) { + if(step->assembly2->articulation[i] != step->fix3) continue; - // The basic idea here is to create some new points for ax_oppo and bx_oppo - // which we can then use to place a circle or line - struct element *ax_oppo_locally = {}; - point_from_line_and_distance(drawing, constraints, step->j, step->i, a2_local->e, a1_local->e, constraints[step->j].c2 != a2_local, &ax_oppo_locally); + fix3_in_ax = step->assembly2->articulation_position[i]; + break; + } - struct element *bx_oppo_locally = {}; - build_angle_point_line2(drawing, constraints, step->x, step->k, b2_local->e, b1_local->e, true, &bx_oppo_locally); + assert(fix_in_bx != NULL); + assert(fix2_in_ax != NULL); + assert(fix3_in_ax != NULL); + assert(fix3_in_bx != NULL); //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. - // struct element *assembly1_distance = insert_cmd(drawing, (struct command){ .op = CMD_MEASURE_POINT_LINE_DISTANCE, .hidden = true, .result.type = ETYPE_VALUE, - .arg1 = a1_oppo->e, - .arg2 = c1_oppo->e, + .arg1 = fix2_in_ax, + .arg2 = fix3_in_ax, }); - // This line isn't really in our scope at this point. This is technically - // dubious, but it works due to the current implementation - struct element *cx_oppo_bx = {}; - build_angle_point_line2(drawing, constraints, step->z, step->y, c2_local->e, c1_local->e, true, &cx_oppo_bx); - struct element *assembly2_angle = insert_cmd(drawing, (struct command){ .op = CMD_MEASURE_LINE_LINE_ANGLE, .hidden = true, .result.type = ETYPE_VALUE, - .arg1 = b1_oppo->e, - .arg2 = cx_oppo_bx, + .arg1 = fix_in_bx, + .arg2 = fix3_in_bx, }); struct element* cx_oppo_direction_locally; @@ -986,14 +916,15 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f .arg2 = assembly1_distance, }); } + cx_oppo_direction_locally = cx_oppo_direction_locally; insert_cmd(drawing, (struct command){ .op = CMD_IMPORT_POINT_LINE, .arg1 = ax_oppo_locally, .arg2 = cx_oppo_direction_locally, - .d = step->assembly, - .attachp = a1_oppo->e, - .attachl = c1_oppo->e, + .d = step->assembly2, + .attachp = fix2_in_ax, + .attachl = fix3_in_ax, }); { @@ -1009,17 +940,17 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f .op = CMD_POINT_LINE_LINE, .hidden = true, .result.type = ETYPE_POINT, - .arg1 = b1_oppo->e, - .arg2 = cx_oppo_bx, + .arg1 = fix_in_bx, + .arg2 = fix3_in_bx, }); insert_cmd(drawing, (struct command){ .op = CMD_IMPORT_POINT_LINE, .arg1 = local_intersection, .arg2 = bx_oppo_locally, - .d = step->assembly2, + .d = step->assembly, .attachp = subassembly2_intersection, - .attachl = b1_oppo->e, + .attachl = fix_in_bx, }); } } |
