chiark / gitweb /
distort-stl: debugging PS output, incomplete
[reprap-play.git] / distort-stl
1 #!/usr/bin/perl -w
2 #
3 # usage:
4 #   ./distort-stl <INPUT >OUTPUT DISTORTION [PARAMS...] ...
5 #
6 # DISTORTIONs:
7 #
8 #   project-cylinder RADIUS
9 #       projects the X-Z plane onto the cylinder of
10 #           radius RADIUS with axis [0, 0, t]
11 #       origin becomes [0, -RADIUS, 0]
12 #       other planes of the input are projected onto smaller
13 #           or larger cylinders accordingly
14 #       probably a bad idea if
15 #           object has any Y > RADIUS
16 #           object has any |X| > tau / RADIUS
17 #       technically, treats input as if it were
18 #       polar-rectangular coords:
19 #          Z' = Z; R' = Y + RADIUS; theta' = X / RADIUS
20 #       and then converts back into cartesian
21 #       honours fa but not fs or fn
22 #
23 #   set-fa $FA
24
25 use strict;
26 use autodie;
27
28 use List::Util;
29 use POSIX;
30 use File::Temp ();
31 use Data::Dumper;
32
33 sub TAU () { M_PI * 2; }
34
35 my $ps = %ENV{DISTORT_PS};
36 if ($ps) {
37     open PS, "> $ps" or die $!;
38     print PS "%!\n";
39 }
40
41 our $fa = 10;
42
43 our $triangles;
44 our $output;
45
46 sub shift_arg () {
47     die unless @ARGV;
48     scalar shift @ARGV;
49 }
50
51 #no warnings qw(recursion);
52
53 sub sprintf_triangle ($) {
54     my ($t) = @_;
55
56     if ($ps && $t->[3] =~ m/$ENV{DISTORT_PS_RE}/) {
57         printf PS <<'END',
58  %20.16g %20.16g %20.16g moveto
59  %20.16g %20.16g %20.16g lineto
60  %20.16g %20.16g %20.16g lineto
61  closepath stroke
62 END
63                 $t->[0][0], $t->[0][1], $t->[0][2],
64                 $t->[1][0], $t->[1][1], $t->[1][2],
65                 $t->[2][0], $t->[2][1], $t->[2][2],
66                 or die $!;
67         flush PS or die $!;
68     }
69
70     sprintf
71         "%11.6f,%11.6f,%11.6f / ".
72         "%11.6f,%11.6f,%11.6f / ".
73         "%11.6f,%11.6f,%11.6f  %-40s ",
74                 $t->[0][0], $t->[0][1], $t->[0][2],
75                 $t->[1][0], $t->[1][1], $t->[1][2],
76                 $t->[2][0], $t->[2][1], $t->[2][2],
77                 $t->[3];
78 }
79
80 sub maybe_subdivide_triangle ($$$$) {
81     my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
82
83     print STDERR sprintf_triangle $t;
84     
85     foreach my $ix (0..2) {
86         my $jx = ($ix+1) % 3;
87         my $kx = ($ix+2) % 3;
88         if ($edge_need_subdivide_fn->($t->[$ix], $t->[$jx])) {
89             printf STDERR
90                 " S i=%d j=%d k=%d ",
91                 $ix, $jx, $kx;
92             my @midp;
93             foreach my $ci (0..2) {
94                 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
95             }
96
97             printf STDERR
98                 " midp %11.6f,%11.6f,%11.6f\n",
99                 @midp;
100
101             # triangle i-j-k, splitting edge i-m
102             # gives    i-m-k, k-m-j
103             my $n = [ @$t ]; $n->[$ix] = \@midp;  $n->[3] = "$t->[3]a$ix$jx";
104             unshift @$changed, $n;
105
106             printf STDERR "%s\n", sprintf_triangle $n;
107
108             my $n = [ @$t ]; $n->[$jx] = \@midp;  $n->[3] = "$t->[3]b$ix$jx";
109             unshift @$changed, $n;
110
111             printf STDERR "%s\n", sprintf_triangle $n;
112
113             return;
114         }
115     }
116     push @$ok, $t;
117     printf STDERR "OK nok=%d nchanged=%d\n",
118         (scalar @$ok), (scalar @$changed);
119 }
120
121 sub maybe_subdivide ($) {
122     my ($edge_need_subdivide_fn) = @_;
123     
124     my @small_enough = ();
125     while (my $t = shift @$triangles) {
126         maybe_subdivide_triangle $t, \@small_enough, $triangles,
127             $edge_need_subdivide_fn;
128     }
129
130     $triangles = \@small_enough;
131 }
132
133 sub append_triangle ($) {
134     my ($t) = @_;
135     push @$output, $t;
136 }
137
138 #---------- set-fa ----------
139
140 sub op__set_fa () {
141     $fa = shift_arg;
142 }
143
144 #---------- project-cylinder ----------
145
146 our $project_cylinder_radius;
147 our $project_cylinder_max_d_theta;
148
149 sub project_cylinder_edge_need_subdivide ($$) {
150     my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
151     return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
152 }
153
154 sub project_cylinder_tri {
155     my ($t) = @_;
156
157     #print STDERR 'PROJECT', Dumper($t);
158
159     my $radius = $project_cylinder_radius;
160
161     my @ot;
162     foreach my $p (@$t) {
163         my ($x,$y,$z) = @$p;
164         my $r = $radius - $y;
165         my $theta = $x / $radius;
166         push @ot, [ $r * sin($theta),
167                     -$r * cos($theta),
168                     $z ];
169     }
170     append_triangle \@ot;
171 }
172
173 sub op__project_cylinder () {
174     $project_cylinder_radius = shift_arg;
175     $project_cylinder_max_d_theta = $fa * TAU/360;
176
177     maybe_subdivide \&project_cylinder_edge_need_subdivide;
178     
179     $output = [];
180     foreach my $t (@$triangles) {
181         project_cylinder_tri $t;
182     }
183     $triangles = $output;
184 }
185
186 #---------- main program ----------
187
188 our $raw;
189
190 while (@ARGV && $ARGV[0] =~ m/^-/) {
191     $_ = shift @ARGV;
192     last if m/^--$/;
193     if (s/^--raw$//) {
194         $raw = 1;
195     } else {
196         die "$_ ?";
197     }
198 }
199
200 my $itmp;
201 my $otmp;
202
203 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
204
205 if ($raw) {
206     open I, "<& STDIN";
207     $otmp = *STDOUT;
208 } else {
209     $itmp = new File::Temp;
210     $otmp = new File::Temp;
211
212     system "cat >$itmp";
213
214     open I, "admesh $admesh_stdout $itmp |";
215 }
216
217 my $triangle;
218
219 while (<I>) {
220     s/^\s*//;
221     if (m/^outer\s+loop/) {
222         die if $triangle;
223         $triangle = [];
224     } elsif (s/^vertex\s+//) {
225         my $lhs = $&;
226         s/\s+$//;
227         my @xyz = split /\s+/, $_;
228         die unless $triangle;
229         push @$triangle, \@xyz;
230     } elsif (m/^endloop/) {
231         die unless @$triangle == 3;
232         push @$triangle, $.;
233         push @$triangles, $triangle;
234         undef $triangle;
235     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
236     } else {
237         die "$_ ?";
238     }
239 }
240
241 close I;
242 <I> if 0; # suppresses Name "main::I" used only once
243
244 while (@ARGV) {
245     my $op = shift_arg;
246     $op =~ y/-/_/;
247     &{ ${*::}{"op__$op"} };
248 }
249
250 select $otmp;
251
252 print "solid distort-stl\n";
253
254 foreach my $t (@$triangles) {
255     print "  facet normal 0 0 0\n";
256     print "    outer loop\n";
257     die unless @$t==3;
258     foreach my $p (@$t) {
259         die unless @$p==3;
260         print "      vertex";
261         printf " %.18g", $_ foreach @$p;
262         print "\n";
263     }
264     print "    endloop\n";
265     print "  endfacet\n";
266 }
267
268 print "endsolid distort-stl\n";
269
270 flush $otmp;
271
272 if (!$raw) {
273     system "admesh --normal-values $admesh_stdout $otmp";
274 }