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