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; }
35 my $ps = $ENV{DISTORT_PS};
37 open PS, "> $ps" or die $!;
51 #no warnings qw(recursion);
53 sub sprintf_triangle ($) {
56 if ($ps && $t->[3] =~ m/$ENV{DISTORT_PS_RE}/) {
58 %20.16g %20.16g %20.16g moveto
59 %20.16g %20.16g %20.16g lineto
60 %20.16g %20.16g %20.16g lineto
63 $t->[0][0], $t->[0][1], $t->[0][2],
64 $t->[1][0], $t->[1][1], $t->[1][2],
65 $t->[2][0], $t->[2][1], $t->[2][2],
71 "%11.6f,%11.6f,%11.6f / ".
72 "%11.6f,%11.6f,%11.6f / ".
73 "%11.6f,%11.6f,%11.6f %-40s ",
74 $t->[0][0], $t->[0][1], $t->[0][2],
75 $t->[1][0], $t->[1][1], $t->[1][2],
76 $t->[2][0], $t->[2][1], $t->[2][2],
80 sub maybe_subdivide_triangle ($$$$) {
81 my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
83 print STDERR sprintf_triangle $t;
85 foreach my $ix (0..2) {
88 if ($edge_need_subdivide_fn->($t->[$ix], $t->[$jx])) {
93 foreach my $ci (0..2) {
94 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
98 " midp %11.6f,%11.6f,%11.6f\n",
101 # triangle i-j-k, splitting edge i-m
103 my $n = [ @$t ]; $n->[$ix] = \@midp; $n->[3] = "$t->[3]a$ix$jx";
104 unshift @$changed, $n;
106 printf STDERR "%s\n", sprintf_triangle $n;
108 my $n = [ @$t ]; $n->[$jx] = \@midp; $n->[3] = "$t->[3]b$ix$jx";
109 unshift @$changed, $n;
111 printf STDERR "%s\n", sprintf_triangle $n;
117 printf STDERR "OK nok=%d nchanged=%d\n",
118 (scalar @$ok), (scalar @$changed);
121 sub maybe_subdivide ($) {
122 my ($edge_need_subdivide_fn) = @_;
124 my @small_enough = ();
125 while (my $t = shift @$triangles) {
126 maybe_subdivide_triangle $t, \@small_enough, $triangles,
127 $edge_need_subdivide_fn;
130 $triangles = \@small_enough;
133 sub append_triangle ($) {
138 #---------- set-fa ----------
144 #---------- project-cylinder ----------
146 our $project_cylinder_radius;
147 our $project_cylinder_max_d_theta;
149 sub project_cylinder_edge_need_subdivide ($$) {
150 my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
151 return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
154 sub project_cylinder_tri {
157 #print STDERR 'PROJECT', Dumper($t);
159 my $radius = $project_cylinder_radius;
162 foreach my $p (@$t) {
164 my $r = $radius - $y;
165 my $theta = $x / $radius;
166 push @ot, [ $r * sin($theta),
170 append_triangle \@ot;
173 sub op__project_cylinder () {
174 $project_cylinder_radius = shift_arg;
175 $project_cylinder_max_d_theta = $fa * TAU/360;
177 maybe_subdivide \&project_cylinder_edge_need_subdivide;
180 foreach my $t (@$triangles) {
181 project_cylinder_tri $t;
183 $triangles = $output;
186 #---------- main program ----------
190 while (@ARGV && $ARGV[0] =~ m/^-/) {
203 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
209 $itmp = new File::Temp;
210 $otmp = new File::Temp;
214 open I, "admesh $admesh_stdout $itmp |";
221 if (m/^outer\s+loop/) {
224 } elsif (s/^vertex\s+//) {
227 my @xyz = split /\s+/, $_;
228 die unless $triangle;
229 push @$triangle, \@xyz;
230 } elsif (m/^endloop/) {
231 die unless @$triangle == 3;
233 push @$triangles, $triangle;
235 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
242 <I> if 0; # suppresses Name "main::I" used only once
247 &{ ${*::}{"op__$op"} };
252 print "solid distort-stl\n";
254 foreach my $t (@$triangles) {
255 print " facet normal 0 0 0\n";
256 print " outer loop\n";
258 foreach my $p (@$t) {
261 printf " %.18g", $_ foreach @$p;
268 print "endsolid distort-stl\n";
273 system "admesh --normal-values $admesh_stdout $otmp";