4 # ./distort-stl <INPUT >OUTPUT DISTORTION [PARAMS...] ...
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
32 sub TAU () { M_PI * 2; }
43 sub subdivide_triangle ($$) {
46 foreach my $ix (0..2) {
49 foreach my $ci (0..2) {
50 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
54 foreach my $ix (0..2) {
56 $fn->([ $t->[$ix], $mids[$ix], $t->[$kx] ]);
61 #---------- project-cylinder ----------
63 our $project_cylinder_radius;
64 our $project_cylinder_max_d_theta;
66 sub project_cylinder_tri {
69 my $radius = $project_cylinder_radius;
71 my @thetas = map { $_->[0] / $radius } @$t;
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;
85 my $theta = $x / $radius;
86 push @ot, [ $r * sin($theta),
90 push @triangles, \@ot;
93 sub op__project_cylinder () {
94 $project_cylinder_radius = shift_arg;
95 $project_cylinder_max_d_theta = $fa * TAU/360;
97 my @input = (@triangles);
100 project_cylinder_tri $_ foreach @input;
103 #---------- main program ----------
107 while (@ARGV && $ARGV[0] =~ m/^-/) {
120 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
126 $itmp = new File::Temp;
127 $otmp = new File::Temp;
131 open I, "admesh $admesh_stdout $itmp |";
138 if (m/^outer\s+loop/) {
141 } elsif (s/^vertex\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;
151 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
158 <I> if 0; # suppresses Name "main::I" used only once
163 &{ ${*::}{"op__$op"} };
168 print "solid distort-stl\n";
170 foreach my $t (@triangles) {
171 print " facet normal 0 0 0\n";
172 print " outer loop\n";
174 printf " %.18g", $_ foreach @$t;
180 print "endsolid distort-stl\n";
185 system "admesh --normal-values $admesh_stdout $otmp";