1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#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 p1 = {.type = COM_POINT};
struct component p2 = {.type = COM_POINT};
struct component p3 = {.type = COM_POINT};
struct component p4 = {.type = COM_POINT};
struct component p5 = {.type = COM_POINT};
struct component t1base = {.type = COM_LINE};
struct component t2base = {.type = COM_LINE};
struct component t2side = {.type = COM_LINE};
add_constraint(&constraints, (struct constraint[]){
// Make a triangle
PP_DISTANCE(&p1, &p2, 30),
PP_DISTANCE(&p3, &p1, 30),
PP_DISTANCE(&p2, &p3, 30),
POINT_ON_LINE(&p1, &t1base),
POINT_ON_LINE(&p2, &t1base),
// With another triangle sharing a point
PP_DISTANCE(&p4, &p5, 30),
PP_DISTANCE(&p3, &p4, 30),
PP_DISTANCE(&p5, &p3, 30),
// LL_ANGLE(&t2side, &t2base, DEG(120)),
POINT_ON_LINE(&p3, &t2side),
POINT_ON_LINE(&p4, &t2side),
POINT_ON_LINE(&p4, &t2base),
POINT_ON_LINE(&p5, &t2base),
// The edge they don't share is constrained
LL_ANGLE(&t1base, &t2base, DEG(70)),
CEND(),
});
add_fragment(&topo, (struct topology_elem[]){
TOPO_MOVETO(&p1),
TOPO_LINETO(&p2),
TOPO_LINETO(&p3),
TOPO_LINETO(&p1),
TOPO_MOVETO(&p3),
TOPO_LINETO(&p4),
TOPO_LINETO(&p5),
TOPO_LINETO(&p3),
TOPO_END(),
});
struct canvas canvas = {
.state = CANVAS_INIT,
.f = stdout,
};
draw_from_constraints(&constraints, &topo, &canvas);
free_constraints(&constraints);
return 0;
}
|