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 #---------- set-fa ----------
81 #---------- project-cylinder ----------
83 our $project_cylinder_radius;
84 our $project_cylinder_max_d_theta;
86 sub project_cylinder_triangle_need_subdivide ($) {
88 my @thetas = map { $_->[0] / $project_cylinder_radius } @$t;
90 foreach my $ix (0..2) {
91 if (abs($thetas[$ix] - $thetas[($ix+1)%3])
92 > $project_cylinder_max_d_theta) {
99 sub project_cylinder_tri {
102 #print STDERR 'PROJECT', Dumper($t);
104 my $radius = $project_cylinder_radius;
107 foreach my $p (@$t) {
109 my $r = $radius - $y;
110 my $theta = $x / $radius;
111 push @ot, [ $r * sin($theta),
115 append_triangle \@ot;
118 sub op__project_cylinder () {
119 $project_cylinder_radius = shift_arg;
120 $project_cylinder_max_d_theta = $fa * TAU/360;
122 my @small_enough = ();
123 while (my $t = shift @$triangles) {
124 if (!project_cylinder_triangle_need_subdivide $t) {
125 push @small_enough, $t;
127 local $output = $triangles;
128 subdivide_triangle $t, \&append_triangle;
132 $triangles = \@small_enough;
135 foreach my $t (@$triangles) {
136 project_cylinder_tri $t;
138 $triangles = $output;
141 #---------- main program ----------
145 while (@ARGV && $ARGV[0] =~ m/^-/) {
158 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
164 $itmp = new File::Temp;
165 $otmp = new File::Temp;
169 open I, "admesh $admesh_stdout $itmp |";
176 if (m/^outer\s+loop/) {
179 } elsif (s/^vertex\s+//) {
182 my @xyz = split /\s+/, $_;
183 die unless $triangle;
184 push @$triangle, \@xyz;
185 } elsif (m/^endloop/) {
186 die unless @$triangle == 3;
187 push @$triangles, $triangle;
189 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
196 <I> if 0; # suppresses Name "main::I" used only once
201 &{ ${*::}{"op__$op"} };
206 print "solid distort-stl\n";
208 foreach my $t (@$triangles) {
209 print " facet normal 0 0 0\n";
210 print " outer loop\n";
212 foreach my $p (@$t) {
215 printf " %.18g", $_ foreach @$p;
222 print "endsolid distort-stl\n";
227 system "admesh --normal-values $admesh_stdout $otmp";