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; }
44 #no warnings qw(recursion);
46 sub subdivide_triangle ($$) {
49 #print STDERR 'SUBDIV', Dumper($t, $fn);
52 foreach my $ix (0..2) {
55 foreach my $ci (0..2) {
56 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
60 foreach my $ix (0..2) {
61 #print STDERR 'SUBDIV IX ', $ix, "\n";
63 $fn->([ $t->[$ix], $mids[$ix], $mids[$kx] ]);
65 #print STDERR 'SUBDIV MID\n';
69 #---------- project-cylinder ----------
71 our $project_cylinder_radius;
72 our $project_cylinder_max_d_theta;
74 sub project_cylinder_tri {
77 #print STDERR 'PROJECT', Dumper($t);
79 my $radius = $project_cylinder_radius;
81 my @thetas = map { $_->[0] / $radius } @$t;
83 foreach my $ix (0..2) {
84 if (abs($thetas[$ix] - $thetas[($ix+1)%3])
85 > $project_cylinder_max_d_theta) {
86 subdivide_triangle $t, \&project_cylinder_tri;
95 my $theta = $x / $radius;
96 push @ot, [ $r * sin($theta),
100 push @triangles, \@ot;
103 sub op__project_cylinder () {
104 $project_cylinder_radius = shift_arg;
105 $project_cylinder_max_d_theta = $fa * TAU/360;
107 my @input = (@triangles);
110 project_cylinder_tri $_ foreach @input;
113 #---------- main program ----------
117 while (@ARGV && $ARGV[0] =~ m/^-/) {
130 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
136 $itmp = new File::Temp;
137 $otmp = new File::Temp;
141 open I, "admesh $admesh_stdout $itmp |";
148 if (m/^outer\s+loop/) {
151 } elsif (s/^vertex\s+//) {
154 my @xyz = split /\s+/, $_;
155 die unless $triangle;
156 push @$triangle, \@xyz;
157 } elsif (m/^endloop/) {
158 die unless @$triangle == 3;
159 push @triangles, $triangle;
161 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
168 <I> if 0; # suppresses Name "main::I" used only once
173 &{ ${*::}{"op__$op"} };
178 print "solid distort-stl\n";
180 foreach my $t (@triangles) {
181 print " facet normal 0 0 0\n";
182 print " outer loop\n";
184 foreach my $p (@$t) {
187 printf " %.18g", $_ foreach @$p;
194 print "endsolid distort-stl\n";
199 system "admesh --normal-values $admesh_stdout $otmp";