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
40 sub subdivide_triangle ($$) {
43 foreach my $ix (0..2) {
46 foreach my $ci (0..2) {
47 push @midp, 0.5 * ($it->[$ix][$ci] + $it->[$jx][$ci]);
51 foreach my $ix (0..2) {
53 $fn->([ $it->[$ix], $mids[$ix], $it->[$kx] ]);
58 #---------- project-cylinder ----------
60 our $project_cylinder_radius;
61 our $project_cylinder_max_d_theta;
63 sub project_cylinder_tri {
66 my $radius = project_cylinder_radius;
68 my @thetas = map { $_->[0] / $radius } @$it;
70 foreach my $ix (0..2) {
71 if (abs($thetas[$ix] - $thetas[($ix+1)%3])
72 > $project_cylinder_max_d_theta) {
73 subdivide_triangle $it, \&project_cylinder_tri;
82 my $theta = $x / $radius;
83 push @ot, [ $r * sin($theta),
87 push @triangles, \@ot;
90 sub op__project_cylinder () {
91 $project_cylinder_radius = shift_arg;
92 $project_cylinder_max_d_theta = $fa * TAU/360;
94 my @input = (@triangles);
97 project_cylinder_tri $_ foreach @input;
100 #---------- main program ----------
102 if (@ARGV && $ARGV[0] =~ m/^-/) {
103 die "no options supported\n";
106 my $itmp = new File::Temp;
107 my $otmp = new File::Temp;
111 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
113 open I, "admesh $admesh_stdout $itmp |";
118 if (m/^\s+outer\s+loop/) {
121 } elsif (s/^\s+vertex\s+//) {
124 my @xyz = split /\s+/, $_;
125 die unless $triangle;
126 push @$triangle, \@xya;
127 } elsif (m/^\s+endloop/) {
128 die unless @$triangle == 3;
129 push @triangles, $triangle;
130 } elsif (m/^\s+(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
137 <I> if 0; # suppresses Name "main::I" used only once
142 &{ ${*::}{"op__$op"} };
147 print "solid distort-stl\n";
149 foreach my $t (@triangles) {
150 print " facet normal 0 0 0\n";
151 print " outer loop\n";
153 printf " %.18g", $_ foreach @$t;
159 print "endsolid distort-stl\n";
163 system "admesh --normal-values $admesh_stdout $otmp";