summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2026-09-27 23:39:11 +0200
committerJesper Jensen <jesper@jnsn.dev>2026-09-27 23:39:11 +0200
commitbfbecd4a3639fb4b8c85f0637c612bb65ef7ec99 (patch)
tree15d382a58012caacd84c4da9ebfb3f81dabce9c7
parentf16fc18221cd8d1a8cb1bf9e40790f3b482fb199 (diff)
downloadconstraint-bfbecd4a3639fb4b8c85f0637c612bb65ef7ec99.tar.gz
constraint-bfbecd4a3639fb4b8c85f0637c612bb65ef7ec99.zip
Closer to drawing the box and linesborrow-component
We're stilling missing the lines somehow
-rw-r--r--examples/box_and_lines.c9
-rw-r--r--inc/cad/construction.h2
-rw-r--r--src/construction.c57
-rw-r--r--src/solve.c187
4 files changed, 196 insertions, 59 deletions
diff --git a/examples/box_and_lines.c b/examples/box_and_lines.c
index d90f208..7d63851 100644
--- a/examples/box_and_lines.c
+++ b/examples/box_and_lines.c
@@ -1,4 +1,5 @@
#include "cad.h"
+#include "cad/debug.h"
struct mid {
struct component p1;
@@ -135,7 +136,7 @@ void a_box(struct box *box, struct topology *topo, struct constraints *constr) {
void draw_from_constraints(struct constraints *c, struct topology *t, struct canvas *cv) {
struct drawing drawing = {};
- struct subassembly assemblies[16] = {};
+ struct subassembly assemblies[64] = {};
size_t assemblies_num;
bool rc = solve_constraints(c, &drawing, assemblies, &assemblies_num);
assert(rc);
@@ -148,11 +149,15 @@ 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, assemblies_num);
+
begin_drawing(cv);
draw_topology(cv, t);
draw_constraints(cv, c);
+ draw_elements(cv, &drawing);
end_drawing(cv);
free_drawing(&drawing);
@@ -173,7 +178,7 @@ int main(int argc, char *argv[]) {
add_constraint(&constraints, (struct constraint[]){
POINT_ON_LINE(&box2.bottom_left, &box1.bottom),
- PP_DISTANCE(&box1.bottom_right, &box2.bottom_left, 3),
+ PP_DISTANCE(&box1.bottom_right, &box2.bottom_left, 5),
LL_ANGLE(&box2.bottom, &box1.bottom, 0),
POINT_ON_LINE(&box3.bottom_left, &box1.bottom),
diff --git a/inc/cad/construction.h b/inc/cad/construction.h
index cb6b2bc..0f8437a 100644
--- a/inc/cad/construction.h
+++ b/inc/cad/construction.h
@@ -21,9 +21,11 @@ enum operation {
// Handle assemblies
CMD_IMPORT_POINT_LINE,
+ CMD_IMPORT_POINT_POINT,
CMD_IMPORT_LINE_LINE,
// Measure elements
+ CMD_MEASURE_POINT_POINT_DISTANCE,
CMD_MEASURE_POINT_LINE_DISTANCE,
CMD_MEASURE_LINE_LINE_ANGLE,
};
diff --git a/src/construction.c b/src/construction.c
index 2177fba..0da289a 100644
--- a/src/construction.c
+++ b/src/construction.c
@@ -110,6 +110,7 @@ static void transform_part(struct command *cmd, const struct command *last_cmd,
case CMD_VALUE_INPUT:
case CMD_OFFSET_INPUT:
case CMD_MEASURE_POINT_LINE_DISTANCE:
+ case CMD_MEASURE_POINT_POINT_DISTANCE:
case CMD_MEASURE_LINE_LINE_ANGLE:
break;
case CMD_LINE_X:
@@ -141,6 +142,7 @@ static void transform_part(struct command *cmd, const struct command *last_cmd,
case CMD_IMPORT_POINT_LINE:
case CMD_IMPORT_LINE_LINE:
+ case CMD_IMPORT_POINT_POINT:
transform_part(cmd->d->first_command, cmd->d->last_command, transform);
break;
}
@@ -196,7 +198,10 @@ void dump_program(struct drawing *drawing, double inputs[]) {
fprintf(stderr, "%p -> CCP(%p, %p) = ((x-%f)^2 + (y-%f)^2 = %f^2)\n", &current->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", &current->result, current->arg1, current->arg2, current->attachp, current->attachl);
+ fprintf(stderr, "%p -> IPL(%p, %p, %p, %p) = <N/A>\n", &current->result, current->arg1, current->arg2, current->attachp, current->attachl);
+ }break;
+ case CMD_IMPORT_POINT_POINT: {
+ fprintf(stderr, "%p -> IPP(%p, %p, %p, %p) = <N/A>\n", &current->result, current->arg1, current->arg2, current->attachp, current->attachl);
}break;
case CMD_IMPORT_LINE_LINE: {
// Deprecated
@@ -204,6 +209,9 @@ void dump_program(struct drawing *drawing, double inputs[]) {
case CMD_MEASURE_POINT_LINE_DISTANCE: {
fprintf(stderr, "%p -> MPL(%p, %p) = %f\n", &current->result, current->arg1, current->arg2, current->result.value);
}break;
+ case CMD_MEASURE_POINT_POINT_DISTANCE: {
+ fprintf(stderr, "%p -> MPP(%p, %p) = %f\n", &current->result, current->arg1, current->arg2, current->result.value);
+ }break;
case CMD_MEASURE_LINE_LINE_ANGLE: {
fprintf(stderr, "%p -> MLL(%p, %p) = %f\n", &current->result, current->arg1, current->arg2, current->result.value);
}break;
@@ -371,6 +379,45 @@ void place_points(struct drawing *drawing, double inputs[]) {
fprintf(stderr, "Assembly %p %p %p %f %f %f\n", current->d, current->d->first_command, current->d->last_command, transform[2][0], transform[2][1], theta);
transform_part(current->d->first_command, current->d->last_command, transform);
}break;
+ case CMD_IMPORT_POINT_POINT: {
+ assert(current->arg1->type == ETYPE_POINT);
+ assert(current->arg2->type == ETYPE_POINT);
+
+ assert(current->d != NULL);
+ assert(current->attachp != NULL);
+ assert(current->attachl != NULL);
+ assert(current->attachp->type == ETYPE_POINT);
+ assert(current->attachl->type == ETYPE_POINT);
+
+ struct line l1;
+ line_through_points(current->arg1->point, current->arg2->point, &l1);
+ fprintf(stderr, "theta %f %f\n", current->arg1->point.pos[0], current->arg1->point.pos[1]);
+ fprintf(stderr, "theta %f %f\n", current->arg2->point.pos[0], current->arg2->point.pos[1]);
+ struct line l2;
+ line_through_points(current->attachp->point, current->attachl->point, &l2);
+ fprintf(stderr, "theta %f %f\n", current->attachp->point.pos[0], current->attachp->point.pos[1]);
+ fprintf(stderr, "theta %f %f\n", current->attachl->point.pos[0], current->attachl->point.pos[1]);
+
+ double theta;
+ // Align the two lines
+ theta = atan2(l1.norm[1], l1.norm[0]) - atan2(l2.norm[1], l2.norm[0]);
+
+ mat3 transform;
+ glm_mat3_identity(transform);
+
+ glm_translate2d(transform, current->arg1->point.pos);
+
+ glm_rotate2d(transform, theta);
+
+ {
+ vec2 negative_translate;
+ glm_vec2_negate_to(current->attachp->point.pos, negative_translate);
+ glm_translate2d(transform, negative_translate);
+ }
+
+ fprintf(stderr, "PP Assembly %p %p %p %f %f %f\n", current->d, current->d->first_command, current->d->last_command, transform[2][0], transform[2][1], theta);
+ transform_part(current->d->first_command, current->d->last_command, transform);
+ }break;
case CMD_IMPORT_LINE_LINE: {
abort();
}break;
@@ -382,6 +429,13 @@ void place_points(struct drawing *drawing, double inputs[]) {
double dot = glm_vec2_dot(current->arg2->line.norm, current->arg1->point.pos);
current->result.value = (dot + current->arg2->line.C) / glm_vec2_norm(current->arg2->line.norm);
}break;
+ case CMD_MEASURE_POINT_POINT_DISTANCE: {
+ assert(current->arg1->type == ETYPE_POINT);
+ assert(current->arg2->type == ETYPE_POINT);
+ assert(current->result.type == ETYPE_VALUE);
+
+ current->result.value = glm_vec2_distance(current->arg1->point.pos, current->arg2->point.pos);
+ }break;
case CMD_MEASURE_LINE_LINE_ANGLE: {
assert(current->arg1->type == ETYPE_LINE);
assert(current->arg2->type == ETYPE_LINE);
@@ -391,7 +445,6 @@ void place_points(struct drawing *drawing, double inputs[]) {
current->result.value = theta;
}break;
}
- // printf("%fx + %fy + %f = 0\n", cmd[2].line.norm[0], cmd[2].line.norm[1], cmd[2].line.C);
}
diff --git a/src/solve.c b/src/solve.c
index b70ef54..409e088 100644
--- a/src/solve.c
+++ b/src/solve.c
@@ -517,6 +517,8 @@ constrained_to_assembly:
for(size_t k = 0; k < assemblies[j].articulation_num; k++) {
if(!assemblies[j].articulation[k]->fixed) continue;
+ if(assemblies[j].articulation[k] == f1) 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;
@@ -786,6 +788,9 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f
assert(step->fix2 != NULL);
assert(step->fix3 != NULL);
+ assert(step->fix != step->fix2);
+ assert(step->fix2 != step->fix3);
+
struct element *ax_oppo_locally = step->fix2->e;
struct element *bx_oppo_locally = step->fix->e;
@@ -826,22 +831,36 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f
assert(fix3_in_ax != NULL);
assert(fix3_in_bx != NULL);
+ assert(fix3_in_bx != fix_in_bx);
+ assert(fix3_in_ax != fix2_in_ax);
+
// 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.
- struct element *assembly1_distance = insert_cmd(drawing, (struct command){
- .op = CMD_MEASURE_POINT_LINE_DISTANCE,
- .hidden = true,
- .result.type = ETYPE_VALUE,
- .arg1 = fix2_in_ax,
- .arg2 = fix3_in_ax,
- });
+ struct element *assembly1_distance;
+ if(step->fix3->type == COM_LINE) {
+ assembly1_distance = insert_cmd(drawing, (struct command){
+ .op = CMD_MEASURE_POINT_LINE_DISTANCE,
+ .hidden = true,
+ .result.type = ETYPE_VALUE,
+ .arg1 = fix2_in_ax,
+ .arg2 = fix3_in_ax,
+ });
+ } else {
+ assembly1_distance = insert_cmd(drawing, (struct command){
+ .op = CMD_MEASURE_POINT_POINT_DISTANCE,
+ .hidden = true,
+ .result.type = ETYPE_VALUE,
+ .arg1 = fix2_in_ax,
+ .arg2 = fix3_in_ax,
+ });
+ }
if(step->fix->type == COM_LINE) {
+ assert(step->fix3->type == COM_LINE);
struct element *assembly2_angle = insert_cmd(drawing, (struct command){
.op = CMD_MEASURE_LINE_LINE_ANGLE,
.hidden = true,
@@ -909,58 +928,116 @@ static void draw_for_double_subassembly(struct constraint *constraints, size_t f
} 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,
- });
+ if(step->fix3->type == COM_LINE) {
- 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 *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 *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 *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* 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,
- });
+ 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,
+ });
- assert(fix3_in_ax != NULL);
- assert(fix3_in_bx != NULL);
+ 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,
+ });
- 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,
- });
+ assert(fix3_in_ax != NULL);
+ assert(fix3_in_bx != NULL);
- 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,
- });
+ 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,
+ });
+
+ } else {
+
+ struct element *assembly2_distance = insert_cmd(drawing, (struct command){
+ .op = CMD_MEASURE_POINT_POINT_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_locally = insert_cmd(drawing, (struct command){
+ .op = CMD_POINT_CIRCLE_CIRCLE,
+ .hidden = false,
+ .result.type = ETYPE_POINT,
+ .arg1 = c1,
+ .arg2 = c2,
+ });
+
+ assert(fix3_in_ax != NULL);
+ assert(fix3_in_bx != NULL);
+
+ insert_cmd(drawing, (struct command){
+ .op = CMD_IMPORT_POINT_POINT,
+ .arg1 = ax_oppo_locally,
+ .arg2 = cx_oppo_locally,
+ .d = step->assembly2,
+ .attachp = fix2_in_ax,
+ .attachl = fix3_in_ax,
+ });
+
+ insert_cmd(drawing, (struct command){
+ .op = CMD_IMPORT_POINT_POINT,
+ .arg1 = bx_oppo_locally,
+ .arg2 = cx_oppo_locally,
+ .d = step->assembly,
+ .attachp = fix_in_bx,
+ .attachl = fix3_in_bx,
+ });
+ }
}
}
@@ -1284,7 +1361,7 @@ bool solve_constraints(struct constraints *constraints, struct drawing *drawing,
assemblies[*assemblies_num].articulation[i]->in = NULL;
}
(*assemblies_num)++;
- assert(*assemblies_num <= 32);
+ assert(*assemblies_num <= 64);
}
// Fill out the aliased points out with the values from their targets