chiark / gitweb /
poster-tube-lid: catch: wip rework entirely
[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} // 0 ;
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     my (@longest) = qw(-1);
90
91     foreach my $ix (0..2) {
92         my $jx = ($ix+1) % 3;
93         next unless $edge_need_subdivide_fn->($t->[$ix], $t->[$jx]);
94         my $l2 = 0;
95         foreach my $ci (0..2) {
96             my $d = $t->[$ix][$ci] - $t->[$jx][$ci];
97             $l2 += $d*$d;
98         }
99         next unless $l2 > $longest[0];
100         @longest = ($l2, $ix, $jx);
101     }
102     if ($longest[0] < 0) {
103         push @$ok, $t;
104         printf STDERR "OK nok=%d nchanged=%d\n",
105             (scalar @$ok), (scalar @$changed)
106             if $debug;
107         print STDERR Dumper(\@$ok) if $debug>=2;
108         return;
109     }
110     my ($dummy,$ix,$jx) = @longest;
111     my $kx = ($ix+2) % 3;
112
113     printf STDERR
114         " S i=%d j=%d k=%d ",
115         $ix, $jx, $kx
116         if $debug;
117     my @midp;
118     foreach my $ci (0..2) {
119         push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
120     }
121
122     printf STDERR
123         " midp %11.6f,%11.6f,%11.6f\n",
124         @midp
125         if $debug;
126
127     # triangle i-j-k, splitting edge i-m
128     # gives    i-m-k, k-m-j
129     my $gensplit = sub {
130         my ($ixjx, $xwhat) = @_;
131         my $n = [ @$t ];
132         $n->[$ixjx] = \@midp;
133         $n->[3] = "$t->[3]$xwhat";
134         printf STDERR "%s\n", sprintf_triangle $n if $debug;
135         unshift @$changed, $n;
136     };
137     $gensplit->($ix, "a$ix$jx");
138     $gensplit->($jx, "b$ix$jx");
139     return;
140 }
141
142 sub maybe_subdivide ($) {
143     my ($edge_need_subdivide_fn) = @_;
144     
145     my @small_enough = ();
146     while (my $t = shift @$triangles) {
147         maybe_subdivide_triangle $t, \@small_enough, $triangles,
148             $edge_need_subdivide_fn;
149     }
150
151     $triangles = \@small_enough;
152 }
153
154 sub append_triangle ($) {
155     my ($t) = @_;
156     push @$output, $t;
157 }
158
159 #---------- set-fa ----------
160
161 sub op__set_fa () {
162     $fa = shift_arg;
163 }
164
165 #---------- project-cylinder ----------
166
167 our $project_cylinder_radius;
168 our $project_cylinder_max_d_theta;
169
170 sub project_cylinder_edge_need_subdivide ($$) {
171     my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
172     return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
173 }
174
175 sub project_cylinder_tri {
176     my ($t) = @_;
177
178     #print STDERR 'PROJECT', Dumper($t);
179
180     my $radius = $project_cylinder_radius;
181
182     my @ot;
183     foreach my $p (@$t[0..2]) {
184         my ($x,$y,$z) = @$p;
185         my $r = $radius - $y;
186         my $theta = $x / $radius;
187         push @ot, [ $r * sin($theta),
188                     -$r * cos($theta),
189                     $z ];
190     }
191     push @ot, $t->[3].'P';
192     append_triangle \@ot;
193 }
194
195 sub op__project_cylinder () {
196     $project_cylinder_radius = shift_arg;
197     $project_cylinder_max_d_theta = $fa * TAU/360;
198
199     maybe_subdivide \&project_cylinder_edge_need_subdivide;
200     
201     $output = [];
202     foreach my $t (@$triangles) {
203         project_cylinder_tri $t;
204     }
205     $triangles = $output;
206 }
207
208 #---------- main program ----------
209
210 our $raw;
211
212 while (@ARGV && $ARGV[0] =~ m/^-/) {
213     $_ = shift @ARGV;
214     last if m/^--$/;
215     if (s/^--raw$//) {
216         $raw = 1;
217     } else {
218         die "$_ ?";
219     }
220 }
221
222 my $itmp;
223 my $otmp;
224
225 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
226
227 if ($raw) {
228     open I, "<& STDIN";
229     $otmp = *STDOUT;
230 } else {
231     $itmp = new File::Temp;
232     $otmp = new File::Temp;
233
234     system "cat >$itmp";
235
236     open I, "admesh $admesh_stdout $itmp |";
237 }
238
239 my $triangle;
240
241 while (<I>) {
242     s/^\s*//;
243     if (m/^outer\s+loop/) {
244         die if $triangle;
245         $triangle = [];
246     } elsif (s/^vertex\s+//) {
247         my $lhs = $&;
248         s/\s+$//;
249         my @xyz = split /\s+/, $_;
250         die unless $triangle;
251         push @$triangle, \@xyz;
252     } elsif (m/^endloop/) {
253         die unless @$triangle == 3;
254         push @$triangle, $.;
255         push @$triangles, $triangle;
256         undef $triangle;
257     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
258     } else {
259         die "$_ ?";
260     }
261 }
262
263 close I;
264 <I> if 0; # suppresses Name "main::I" used only once
265
266 while (@ARGV) {
267     my $op = shift_arg;
268     $op =~ y/-/_/;
269     &{ ${*::}{"op__$op"} };
270 }
271
272 select $otmp;
273
274 print "solid distort-stl\n";
275
276 foreach my $t (@$triangles) {
277     print "  facet normal 0 0 0\n";
278     print "    outer loop\n";
279     die unless @$t==4;
280     foreach my $p (@$t[0..2]) {
281         die unless @$p==3;
282         print "      vertex";
283         printf " %.18g", $_ foreach @$p;
284         print "\n";
285     }
286     print "    endloop\n";
287     print "  endfacet\n";
288 }
289
290 print "endsolid distort-stl\n";
291
292 flush $otmp;
293
294 if (!$raw) {
295     system "admesh --normal-values $admesh_stdout $otmp";
296 }