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
33 sub TAU () { M_PI * 2; }
45 #no warnings qw(recursion);
47 sub subdivide_triangle ($$) {
50 #print STDERR 'SUBDIV', Dumper($t, $fn);
53 foreach my $ix (0..2) {
56 foreach my $ci (0..2) {
57 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
61 foreach my $ix (0..2) {
62 #print STDERR 'SUBDIV IX ', $ix, "\n";
64 $fn->([ $t->[$ix], $mids[$ix], $mids[$kx] ]);
66 #print STDERR 'SUBDIV MID\n';
70 sub append_triangle ($) {
75 #---------- project-cylinder ----------
77 our $project_cylinder_radius;
78 our $project_cylinder_max_d_theta;
80 sub project_cylinder_triangle_need_subdivide ($) {
82 my @thetas = map { $_->[0] / $project_cylinder_radius } @$t;
84 foreach my $ix (0..2) {
85 if (abs($thetas[$ix] - $thetas[($ix+1)%3])
86 > $project_cylinder_max_d_theta) {
93 sub project_cylinder_need_subdivide () {
94 foreach my $t (@$triangles) {
95 next unless project_cylinder_triangle_need_subdivide $t;
101 sub project_cylinder_tri {
104 #print STDERR 'PROJECT', Dumper($t);
106 my $radius = $project_cylinder_radius;
109 foreach my $p (@$t) {
111 my $r = $radius - $y;
112 my $theta = $x / $radius;
113 push @ot, [ $r * sin($theta),
117 append_triangle \@ot;
120 sub op__project_cylinder () {
121 $project_cylinder_radius = shift_arg;
122 $project_cylinder_max_d_theta = $fa * TAU/360;
124 while (project_cylinder_need_subdivide()) {
126 foreach my $t (@$triangles) {
127 subdivide_triangle $t, \&append_triangle;
129 $triangles = $output;
133 foreach my $t (@$triangles) {
134 project_cylinder_tri $t;
136 $triangles = $output;
139 #---------- main program ----------
143 while (@ARGV && $ARGV[0] =~ m/^-/) {
156 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
162 $itmp = new File::Temp;
163 $otmp = new File::Temp;
167 open I, "admesh $admesh_stdout $itmp |";
174 if (m/^outer\s+loop/) {
177 } elsif (s/^vertex\s+//) {
180 my @xyz = split /\s+/, $_;
181 die unless $triangle;
182 push @$triangle, \@xyz;
183 } elsif (m/^endloop/) {
184 die unless @$triangle == 3;
185 push @$triangles, $triangle;
187 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
194 <I> if 0; # suppresses Name "main::I" used only once
199 &{ ${*::}{"op__$op"} };
204 print "solid distort-stl\n";
206 foreach my $t (@$triangles) {
207 print " facet normal 0 0 0\n";
208 print " outer loop\n";
210 foreach my $p (@$t) {
213 printf " %.18g", $_ foreach @$p;
220 print "endsolid distort-stl\n";
225 system "admesh --normal-values $admesh_stdout $otmp";