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