chiark / gitweb /
distort-stl: introduce $gensplit (nfc)
[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 our $debug = $ENV{DISTORT_DEBUG};
36
37 my $ps = $ENV{DISTORT_PS};
38 if ($ps) {
39     open PS, "> $ps" or die $!;
40     print PS "%!\n";
41 }
42
43 our $fa = 10;
44
45 our $triangles;
46 our $output;
47
48 sub shift_arg () {
49     die unless @ARGV;
50     scalar shift @ARGV;
51 }
52
53 #no warnings qw(recursion);
54
55 sub sprintf_triangle ($) {
56     my ($t) = @_;
57
58     return '' unless $debug;
59
60     if ($ps && $t->[3] =~ m/$ENV{DISTORT_PS_RE}/) {
61         printf PS <<'END',
62  %20.16g %20.16g %20.16g moveto
63  %20.16g %20.16g %20.16g lineto
64  %20.16g %20.16g %20.16g lineto
65  closepath stroke
66 END
67                 $t->[0][0], $t->[0][1], $t->[0][2],
68                 $t->[1][0], $t->[1][1], $t->[1][2],
69                 $t->[2][0], $t->[2][1], $t->[2][2],
70                 or die $!;
71         flush PS or die $!;
72     }
73
74     sprintf
75         "%11.6f,%11.6f,%11.6f / ".
76         "%11.6f,%11.6f,%11.6f / ".
77         "%11.6f,%11.6f,%11.6f  %-40s ",
78                 $t->[0][0], $t->[0][1], $t->[0][2],
79                 $t->[1][0], $t->[1][1], $t->[1][2],
80                 $t->[2][0], $t->[2][1], $t->[2][2],
81                 $t->[3];
82 }
83
84 sub maybe_subdivide_triangle ($$$$) {
85     my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
86
87     print STDERR sprintf_triangle $t if $debug;
88     
89     foreach my $ix (0..2) {
90         my $jx = ($ix+1) % 3;
91         my $kx = ($ix+2) % 3;
92         if ($edge_need_subdivide_fn->($t->[$ix], $t->[$jx])) {
93             printf STDERR
94                 " S i=%d j=%d k=%d ",
95                 $ix, $jx, $kx
96                 if $debug;
97             my @midp;
98             foreach my $ci (0..2) {
99                 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
100             }
101
102             printf STDERR
103                 " midp %11.6f,%11.6f,%11.6f\n",
104                 @midp
105                 if $debug;
106
107             # triangle i-j-k, splitting edge i-m
108             # gives    i-m-k, k-m-j
109             my $gensplit = sub {
110                 my ($ixjx, $xwhat) = @_;
111                 my $n = [ @$t ];
112                 $n->[$ixjx] = \@midp;
113                 $n->[3] = "$t->[3]$xwhat";
114                 printf STDERR "%s\n", sprintf_triangle $n if $debug;
115                 unshift @$changed, $n;
116             };
117             $gensplit->($ix, "a$ix$jx");
118             $gensplit->($jx, "b$ix$jx");
119             return;
120         }
121     }
122     push @$ok, $t;
123     printf STDERR "OK nok=%d nchanged=%d\n",
124         (scalar @$ok), (scalar @$changed)
125         if $debug;
126 }
127
128 sub maybe_subdivide ($) {
129     my ($edge_need_subdivide_fn) = @_;
130     
131     my @small_enough = ();
132     while (my $t = shift @$triangles) {
133         maybe_subdivide_triangle $t, \@small_enough, $triangles,
134             $edge_need_subdivide_fn;
135     }
136
137     $triangles = \@small_enough;
138 }
139
140 sub append_triangle ($) {
141     my ($t) = @_;
142     push @$output, $t;
143 }
144
145 #---------- set-fa ----------
146
147 sub op__set_fa () {
148     $fa = shift_arg;
149 }
150
151 #---------- project-cylinder ----------
152
153 our $project_cylinder_radius;
154 our $project_cylinder_max_d_theta;
155
156 sub project_cylinder_edge_need_subdivide ($$) {
157     my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
158     return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
159 }
160
161 sub project_cylinder_tri {
162     my ($t) = @_;
163
164     #print STDERR 'PROJECT', Dumper($t);
165
166     my $radius = $project_cylinder_radius;
167
168     my @ot;
169     foreach my $p (@$t) {
170         my ($x,$y,$z) = @$p;
171         my $r = $radius - $y;
172         my $theta = $x / $radius;
173         push @ot, [ $r * sin($theta),
174                     -$r * cos($theta),
175                     $z ];
176     }
177     append_triangle \@ot;
178 }
179
180 sub op__project_cylinder () {
181     $project_cylinder_radius = shift_arg;
182     $project_cylinder_max_d_theta = $fa * TAU/360;
183
184     maybe_subdivide \&project_cylinder_edge_need_subdivide;
185     
186     $output = [];
187     foreach my $t (@$triangles) {
188         project_cylinder_tri $t;
189     }
190     $triangles = $output;
191 }
192
193 #---------- main program ----------
194
195 our $raw;
196
197 while (@ARGV && $ARGV[0] =~ m/^-/) {
198     $_ = shift @ARGV;
199     last if m/^--$/;
200     if (s/^--raw$//) {
201         $raw = 1;
202     } else {
203         die "$_ ?";
204     }
205 }
206
207 my $itmp;
208 my $otmp;
209
210 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
211
212 if ($raw) {
213     open I, "<& STDIN";
214     $otmp = *STDOUT;
215 } else {
216     $itmp = new File::Temp;
217     $otmp = new File::Temp;
218
219     system "cat >$itmp";
220
221     open I, "admesh $admesh_stdout $itmp |";
222 }
223
224 my $triangle;
225
226 while (<I>) {
227     s/^\s*//;
228     if (m/^outer\s+loop/) {
229         die if $triangle;
230         $triangle = [];
231     } elsif (s/^vertex\s+//) {
232         my $lhs = $&;
233         s/\s+$//;
234         my @xyz = split /\s+/, $_;
235         die unless $triangle;
236         push @$triangle, \@xyz;
237     } elsif (m/^endloop/) {
238         die unless @$triangle == 3;
239         push @$triangle, $.;
240         push @$triangles, $triangle;
241         undef $triangle;
242     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
243     } else {
244         die "$_ ?";
245     }
246 }
247
248 close I;
249 <I> if 0; # suppresses Name "main::I" used only once
250
251 while (@ARGV) {
252     my $op = shift_arg;
253     $op =~ y/-/_/;
254     &{ ${*::}{"op__$op"} };
255 }
256
257 select $otmp;
258
259 print "solid distort-stl\n";
260
261 foreach my $t (@$triangles) {
262     print "  facet normal 0 0 0\n";
263     print "    outer loop\n";
264     die unless @$t==3;
265     foreach my $p (@$t) {
266         die unless @$p==3;
267         print "      vertex";
268         printf " %.18g", $_ foreach @$p;
269         print "\n";
270     }
271     print "    endloop\n";
272     print "  endfacet\n";
273 }
274
275 print "endsolid distort-stl\n";
276
277 flush $otmp;
278
279 if (!$raw) {
280     system "admesh --normal-values $admesh_stdout $otmp";
281 }