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_need_subdivide () {
81 foreach my $t (@$triangles) {
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) {
94 sub project_cylinder_tri {
97 #print STDERR 'PROJECT', Dumper($t);
99 my $radius = $project_cylinder_radius;
102 foreach my $p (@$t) {
104 my $r = $radius - $y;
105 my $theta = $x / $radius;
106 push @ot, [ $r * sin($theta),
110 append_triangle \@ot;
113 sub op__project_cylinder () {
114 $project_cylinder_radius = shift_arg;
115 $project_cylinder_max_d_theta = $fa * TAU/360;
117 while (project_cylinder_need_subdivide()) {
119 foreach my $t (@$triangles) {
120 subdivide_triangle $t, \&append_triangle;
122 $triangles = $output;
126 foreach my $t (@$triangles) {
127 project_cylinder_tri $t;
129 $triangles = $output;
132 #---------- main program ----------
136 while (@ARGV && $ARGV[0] =~ m/^-/) {
149 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
155 $itmp = new File::Temp;
156 $otmp = new File::Temp;
160 open I, "admesh $admesh_stdout $itmp |";
167 if (m/^outer\s+loop/) {
170 } elsif (s/^vertex\s+//) {
173 my @xyz = split /\s+/, $_;
174 die unless $triangle;
175 push @$triangle, \@xyz;
176 } elsif (m/^endloop/) {
177 die unless @$triangle == 3;
178 push @$triangles, $triangle;
180 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
187 <I> if 0; # suppresses Name "main::I" used only once
192 &{ ${*::}{"op__$op"} };
197 print "solid distort-stl\n";
199 foreach my $t (@$triangles) {
200 print " facet normal 0 0 0\n";
201 print " outer loop\n";
203 foreach my $p (@$t) {
206 printf " %.18g", $_ foreach @$p;
213 print "endsolid distort-stl\n";
218 system "admesh --normal-values $admesh_stdout $otmp";