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