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 my @small_enough = ();
125 while (my $t = shift @$triangles) {
126 if (!project_cylinder_triangle_need_subdivide $t) {
127 push @small_enough, $t;
129 local $output = $triangles;
130 subdivide_triangle $t, \&append_triangle;
134 $triangles = \@small_enough;
137 foreach my $t (@$triangles) {
138 project_cylinder_tri $t;
140 $triangles = $output;
143 #---------- main program ----------
147 while (@ARGV && $ARGV[0] =~ m/^-/) {
160 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
166 $itmp = new File::Temp;
167 $otmp = new File::Temp;
171 open I, "admesh $admesh_stdout $itmp |";
178 if (m/^outer\s+loop/) {
181 } elsif (s/^vertex\s+//) {
184 my @xyz = split /\s+/, $_;
185 die unless $triangle;
186 push @$triangle, \@xyz;
187 } elsif (m/^endloop/) {
188 die unless @$triangle == 3;
189 push @$triangles, $triangle;
191 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
198 <I> if 0; # suppresses Name "main::I" used only once
203 &{ ${*::}{"op__$op"} };
208 print "solid distort-stl\n";
210 foreach my $t (@$triangles) {
211 print " facet normal 0 0 0\n";
212 print " outer loop\n";
214 foreach my $p (@$t) {
217 printf " %.18g", $_ foreach @$p;
224 print "endsolid distort-stl\n";
229 system "admesh --normal-values $admesh_stdout $otmp";