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