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 foreach my $ix (0..2) {
92 if ($edge_need_subdivide_fn->($t->[$ix], $t->[$jx])) {
98 foreach my $ci (0..2) {
99 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
103 " midp %11.6f,%11.6f,%11.6f\n",
107 # triangle i-j-k, splitting edge i-m
110 my ($ixjx, $xwhat) = @_;
112 $n->[$ixjx] = \@midp;
113 $n->[3] = "$t->[3]$xwhat";
114 printf STDERR "%s\n", sprintf_triangle $n if $debug;
115 unshift @$changed, $n;
117 $gensplit->($ix, "a$ix$jx");
118 $gensplit->($jx, "b$ix$jx");
123 printf STDERR "OK nok=%d nchanged=%d\n",
124 (scalar @$ok), (scalar @$changed)
128 sub maybe_subdivide ($) {
129 my ($edge_need_subdivide_fn) = @_;
131 my @small_enough = ();
132 while (my $t = shift @$triangles) {
133 maybe_subdivide_triangle $t, \@small_enough, $triangles,
134 $edge_need_subdivide_fn;
137 $triangles = \@small_enough;
140 sub append_triangle ($) {
145 #---------- set-fa ----------
151 #---------- project-cylinder ----------
153 our $project_cylinder_radius;
154 our $project_cylinder_max_d_theta;
156 sub project_cylinder_edge_need_subdivide ($$) {
157 my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
158 return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
161 sub project_cylinder_tri {
164 #print STDERR 'PROJECT', Dumper($t);
166 my $radius = $project_cylinder_radius;
169 foreach my $p (@$t) {
171 my $r = $radius - $y;
172 my $theta = $x / $radius;
173 push @ot, [ $r * sin($theta),
177 append_triangle \@ot;
180 sub op__project_cylinder () {
181 $project_cylinder_radius = shift_arg;
182 $project_cylinder_max_d_theta = $fa * TAU/360;
184 maybe_subdivide \&project_cylinder_edge_need_subdivide;
187 foreach my $t (@$triangles) {
188 project_cylinder_tri $t;
190 $triangles = $output;
193 #---------- main program ----------
197 while (@ARGV && $ARGV[0] =~ m/^-/) {
210 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
216 $itmp = new File::Temp;
217 $otmp = new File::Temp;
221 open I, "admesh $admesh_stdout $itmp |";
228 if (m/^outer\s+loop/) {
231 } elsif (s/^vertex\s+//) {
234 my @xyz = split /\s+/, $_;
235 die unless $triangle;
236 push @$triangle, \@xyz;
237 } elsif (m/^endloop/) {
238 die unless @$triangle == 3;
240 push @$triangles, $triangle;
242 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
249 <I> if 0; # suppresses Name "main::I" used only once
254 &{ ${*::}{"op__$op"} };
259 print "solid distort-stl\n";
261 foreach my $t (@$triangles) {
262 print " facet normal 0 0 0\n";
263 print " outer loop\n";
265 foreach my $p (@$t) {
268 printf " %.18g", $_ foreach @$p;
275 print "endsolid distort-stl\n";
280 system "admesh --normal-values $admesh_stdout $otmp";