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