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