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