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