chiark / gitweb /
distort-stl: better, but still needs work
[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
39 sub shift_arg () {
40     die unless @ARGV;
41     scalar shift @ARGV;
42 }
43
44 #no warnings qw(recursion);
45
46 sub subdivide_triangle ($$) {
47     my ($t, $fn) = @_;
48
49     #print STDERR 'SUBDIV', Dumper($t, $fn);
50
51     my @mids;
52     foreach my $ix (0..2) {
53         my $jx = ($ix+1) % 3;
54         my @midp;
55         foreach my $ci (0..2) {
56             push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
57         }
58         push @mids, \@midp;
59     }
60     foreach my $ix (0..2) {
61         #print STDERR 'SUBDIV IX ', $ix, "\n";
62         my $kx = ($ix+2) % 3;
63         $fn->([ $t->[$ix], $mids[$ix], $mids[$kx] ]);
64     }
65     #print STDERR 'SUBDIV MID\n';
66     $fn->(\@mids);
67 }
68
69 #---------- project-cylinder ----------
70
71 our $project_cylinder_radius;
72 our $project_cylinder_max_d_theta;
73
74 sub project_cylinder_tri {
75     my ($t) = @_;
76
77     #print STDERR 'PROJECT', Dumper($t);
78
79     my $radius = $project_cylinder_radius;
80
81     my @thetas = map { $_->[0] / $radius } @$t;
82
83     foreach my $ix (0..2) {
84         if (abs($thetas[$ix] - $thetas[($ix+1)%3])
85             > $project_cylinder_max_d_theta) {
86             subdivide_triangle $t, \&project_cylinder_tri;
87             return;
88         }
89     }
90
91     my @ot;
92     foreach my $p (@$t) {
93         my ($x,$y,$z) = @$p;
94         my $r = $radius - $y;
95         my $theta = $x / $radius;
96         push @ot, [ $r * sin($theta),
97                     -$r * cos($theta),
98                     $z ];
99     }
100     push @triangles, \@ot;
101 }
102
103 sub op__project_cylinder () {
104     $project_cylinder_radius = shift_arg;
105     $project_cylinder_max_d_theta = $fa * TAU/360;
106
107     my @input = (@triangles);
108     @triangles = ();
109
110     project_cylinder_tri $_ foreach @input;
111 }
112
113 #---------- main program ----------
114
115 our $raw;
116
117 while (@ARGV && $ARGV[0] =~ m/^-/) {
118     $_ = shift @ARGV;
119     last if m/^--$/;
120     if (s/^--raw$//) {
121         $raw = 1;
122     } else {
123         die "$_ ?";
124     }
125 }
126
127 my $itmp;
128 my $otmp;
129
130 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
131
132 if ($raw) {
133     open I, "<& STDIN";
134     $otmp = *STDOUT;
135 } else {
136     $itmp = new File::Temp;
137     $otmp = new File::Temp;
138
139     system "cat >$itmp";
140
141     open I, "admesh $admesh_stdout $itmp |";
142 }
143
144 my $triangle;
145
146 while (<I>) {
147     s/^\s*//;
148     if (m/^outer\s+loop/) {
149         die if $triangle;
150         $triangle = [];
151     } elsif (s/^vertex\s+//) {
152         my $lhs = $&;
153         s/\s+$//;
154         my @xyz = split /\s+/, $_;
155         die unless $triangle;
156         push @$triangle, \@xyz;
157     } elsif (m/^endloop/) {
158         die unless @$triangle == 3;
159         push @triangles, $triangle;
160         undef $triangle;
161     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
162     } else {
163         die "$_ ?";
164     }
165 }
166
167 close I;
168 <I> if 0; # suppresses Name "main::I" used only once
169
170 while (@ARGV) {
171     my $op = shift_arg;
172     $op =~ y/-/_/;
173     &{ ${*::}{"op__$op"} };
174 }
175
176 select $otmp;
177
178 print "solid distort-stl\n";
179
180 foreach my $t (@triangles) {
181     print "  facet normal 0 0 0\n";
182     print "    outer loop\n";
183     die unless @$t==3;
184     foreach my $p (@$t) {
185         die unless @$p==3;
186         print "      vertex";
187         printf " %.18g", $_ foreach @$p;
188         print "\n";
189     }
190     print "    endloop\n";
191     print "  endfacet\n";
192 }
193
194 print "endsolid distort-stl\n";
195
196 flush $otmp;
197
198 if (!$raw) {
199     system "admesh --normal-values $admesh_stdout $otmp";
200 }