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 our $debug = $ENV{DISTORT_DEBUG} // 0 ;
37 my $ps = $ENV{DISTORT_PS};
39 open PS, "> $ps" or die $!;
53 #no warnings qw(recursion);
55 sub sprintf_triangle ($) {
58 return '' unless $debug;
60 if ($ps && $t->[3] =~ m/$ENV{DISTORT_PS_RE}/) {
62 %20.16g %20.16g %20.16g moveto
63 %20.16g %20.16g %20.16g lineto
64 %20.16g %20.16g %20.16g lineto
67 $t->[0][0], $t->[0][1], $t->[0][2],
68 $t->[1][0], $t->[1][1], $t->[1][2],
69 $t->[2][0], $t->[2][1], $t->[2][2],
75 "%11.6f,%11.6f,%11.6f / ".
76 "%11.6f,%11.6f,%11.6f / ".
77 "%11.6f,%11.6f,%11.6f %-40s ",
78 $t->[0][0], $t->[0][1], $t->[0][2],
79 $t->[1][0], $t->[1][1], $t->[1][2],
80 $t->[2][0], $t->[2][1], $t->[2][2],
84 sub maybe_subdivide_triangle ($$$$) {
85 my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
87 print STDERR sprintf_triangle $t if $debug;
89 my (@longest) = qw(-1);
91 foreach my $ix (0..2) {
93 next unless $edge_need_subdivide_fn->($t->[$ix], $t->[$jx]);
95 foreach my $ci (0..2) {
96 my $d = $t->[$ix][$ci] - $t->[$jx][$ci];
99 next unless $l2 > $longest[0];
100 @longest = ($l2, $ix, $jx);
102 if ($longest[0] < 0) {
104 printf STDERR "OK nok=%d nchanged=%d\n",
105 (scalar @$ok), (scalar @$changed)
107 print STDERR Dumper(\@$ok) if $debug>=2;
110 my ($dummy,$ix,$jx) = @longest;
111 my $kx = ($ix+2) % 3;
114 " S i=%d j=%d k=%d ",
118 foreach my $ci (0..2) {
119 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
123 " midp %11.6f,%11.6f,%11.6f\n",
127 # triangle i-j-k, splitting edge i-m
130 my ($ixjx, $xwhat) = @_;
132 $n->[$ixjx] = \@midp;
133 $n->[3] = "$t->[3]$xwhat";
134 printf STDERR "%s\n", sprintf_triangle $n if $debug;
135 unshift @$changed, $n;
137 $gensplit->($ix, "a$ix$jx");
138 $gensplit->($jx, "b$ix$jx");
142 sub maybe_subdivide ($) {
143 my ($edge_need_subdivide_fn) = @_;
145 my @small_enough = ();
146 while (my $t = shift @$triangles) {
147 maybe_subdivide_triangle $t, \@small_enough, $triangles,
148 $edge_need_subdivide_fn;
151 $triangles = \@small_enough;
154 sub append_triangle ($) {
159 #---------- set-fa ----------
165 #---------- project-cylinder ----------
167 our $project_cylinder_radius;
168 our $project_cylinder_max_d_theta;
170 sub project_cylinder_edge_need_subdivide ($$) {
171 my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
172 return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
175 sub project_cylinder_tri {
178 #print STDERR 'PROJECT', Dumper($t);
180 my $radius = $project_cylinder_radius;
183 foreach my $p (@$t[0..2]) {
185 my $r = $radius - $y;
186 my $theta = $x / $radius;
187 push @ot, [ $r * sin($theta),
191 push @ot, $t->[3].'P';
192 append_triangle \@ot;
195 sub op__project_cylinder () {
196 $project_cylinder_radius = shift_arg;
197 $project_cylinder_max_d_theta = $fa * TAU/360;
199 maybe_subdivide \&project_cylinder_edge_need_subdivide;
202 foreach my $t (@$triangles) {
203 project_cylinder_tri $t;
205 $triangles = $output;
208 #---------- main program ----------
212 while (@ARGV && $ARGV[0] =~ m/^-/) {
225 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
231 $itmp = new File::Temp;
232 $otmp = new File::Temp;
236 open I, "admesh $admesh_stdout $itmp |";
243 if (m/^outer\s+loop/) {
246 } elsif (s/^vertex\s+//) {
249 my @xyz = split /\s+/, $_;
250 die unless $triangle;
251 push @$triangle, \@xyz;
252 } elsif (m/^endloop/) {
253 die unless @$triangle == 3;
255 push @$triangles, $triangle;
257 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
264 <I> if 0; # suppresses Name "main::I" used only once
269 &{ ${*::}{"op__$op"} };
274 print "solid distort-stl\n";
276 foreach my $t (@$triangles) {
277 print " facet normal 0 0 0\n";
278 print " outer loop\n";
280 foreach my $p (@$t[0..2]) {
283 printf " %.18g", $_ foreach @$p;
290 print "endsolid distort-stl\n";
295 system "admesh --normal-values $admesh_stdout $otmp";