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