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};
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)
109 my ($dummy,$ix,$jx) = @longest;
110 my $kx = ($ix+2) % 3;
113 " S i=%d j=%d k=%d ",
117 foreach my $ci (0..2) {
118 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
122 " midp %11.6f,%11.6f,%11.6f\n",
126 # triangle i-j-k, splitting edge i-m
129 my ($ixjx, $xwhat) = @_;
131 $n->[$ixjx] = \@midp;
132 $n->[3] = "$t->[3]$xwhat";
133 printf STDERR "%s\n", sprintf_triangle $n if $debug;
134 unshift @$changed, $n;
136 $gensplit->($ix, "a$ix$jx");
137 $gensplit->($jx, "b$ix$jx");
141 sub maybe_subdivide ($) {
142 my ($edge_need_subdivide_fn) = @_;
144 my @small_enough = ();
145 while (my $t = shift @$triangles) {
146 maybe_subdivide_triangle $t, \@small_enough, $triangles,
147 $edge_need_subdivide_fn;
150 $triangles = \@small_enough;
153 sub append_triangle ($) {
158 #---------- set-fa ----------
164 #---------- project-cylinder ----------
166 our $project_cylinder_radius;
167 our $project_cylinder_max_d_theta;
169 sub project_cylinder_edge_need_subdivide ($$) {
170 my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
171 return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
174 sub project_cylinder_tri {
177 #print STDERR 'PROJECT', Dumper($t);
179 my $radius = $project_cylinder_radius;
182 foreach my $p (@$t) {
184 my $r = $radius - $y;
185 my $theta = $x / $radius;
186 push @ot, [ $r * sin($theta),
190 append_triangle \@ot;
193 sub op__project_cylinder () {
194 $project_cylinder_radius = shift_arg;
195 $project_cylinder_max_d_theta = $fa * TAU/360;
197 maybe_subdivide \&project_cylinder_edge_need_subdivide;
200 foreach my $t (@$triangles) {
201 project_cylinder_tri $t;
203 $triangles = $output;
206 #---------- main program ----------
210 while (@ARGV && $ARGV[0] =~ m/^-/) {
223 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
229 $itmp = new File::Temp;
230 $otmp = new File::Temp;
234 open I, "admesh $admesh_stdout $itmp |";
241 if (m/^outer\s+loop/) {
244 } elsif (s/^vertex\s+//) {
247 my @xyz = split /\s+/, $_;
248 die unless $triangle;
249 push @$triangle, \@xyz;
250 } elsif (m/^endloop/) {
251 die unless @$triangle == 3;
253 push @$triangles, $triangle;
255 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
262 <I> if 0; # suppresses Name "main::I" used only once
267 &{ ${*::}{"op__$op"} };
272 print "solid distort-stl\n";
274 foreach my $t (@$triangles) {
275 print " facet normal 0 0 0\n";
276 print " outer loop\n";
278 foreach my $p (@$t) {
281 printf " %.18g", $_ foreach @$p;
288 print "endsolid distort-stl\n";
293 system "admesh --normal-values $admesh_stdout $otmp";