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