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