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