-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_scad.c
81 lines (73 loc) · 2.63 KB
/
main_scad.c
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
#include <graphviz/cgraph.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include "color.h"
#include "layout.h"
static void usage(char *command_name) {
fprintf(stderr, "usage: %s\n", command_name);
fprintf(stderr, "reads a dot grid on stdin and prints openscad on standard out\n");
fprintf(stderr, "expects input to have _0, _1, _2 attributes as created by 'grid'\n");
}
static void openscad_grid(Agraph_t *maze, FILE *stream) {
fputs("module edge(x, y, z) {\n", stdout);
fputs(" length = norm([x, y, z]);\n", stdout);
fputs(" inclination = acos(z/length);\n", stdout);
fputs(" azimuth = atan2(y,x);\n", stdout);
fputs(" rotate([0, inclination, azimuth])\n", stdout);
fputs(" cylinder(h=length, r=1);\n", stdout);
fputs("}\n", stdout);
fputs("\n", stdout);
double scale = 4.0;
fputs("union() {\n", stdout);
for (Agnode_t *node = agfstnode(maze); NULL != node; node = agnxtnode(maze, node)) {
struct maze_pt3 nodep;
if (0 == maze_read_location(maze, node, &nodep)) {
double source_x = scale * nodep.x;
double source_y = scale * nodep.y;
double source_z = scale * nodep.z;
struct maze_rgb color;
bool has_color = (0 == maze_read_rgb(node, &color));
if (has_color) {
fprintf(stdout, "color([%f, %f, %f]) ", color.r, color.g, color.b);
}
fprintf(stdout, "translate([%f, %f, %f]) sphere(r=1); // %s\n",
source_x, source_y, source_z, agnameof(node));
for (Agedge_t *e = agfstedge(maze, node); NULL != e; e = agnxtedge(maze, e, node)) {
Agnode_t *other = agtail(e);
struct maze_pt3 otherp;
if (other != node && 0 == maze_read_location(maze, other, &otherp)) {
double dest_x = scale * otherp.x;
double dest_y = scale * otherp.y;
double dest_z = scale * otherp.z;
double delta_x = dest_x - source_x;
double delta_y = dest_y - source_y;
double delta_z = dest_z - source_z;
char *supported = agget(e, "gravity_support");
if (NULL != supported && (0 == strcmp("true", supported))) {
fprintf(stdout, "color([1.0,1.0,1.0]) ");
} else if (has_color) {
fprintf(stdout, "color([%f, %f, %f]) ", color.r, color.g, color.b);
}
fprintf(stdout, "translate([%f, %f, %f]) // %s -> %s\n",
source_x, source_y, source_z,
agnameof(node), agnameof(other));
fprintf(stdout, " edge(%f, %f, %f);\n",
delta_x, delta_y, delta_z);
}
}
}
}
fputs("}\n", stdout);
}
int main(int argc, char** argv) {
char *command_name = argv[0];
Agraph_t *maze = agread(stdin, NULL);
if (NULL == maze) {
fprintf(stderr, "could not read maze from stdin\n");
usage(command_name);
return 1;
}
openscad_grid(maze, stdout);
agclose(maze);
}