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