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 sprintf_triangle ($) {
51 "%11.6f,%11.6f,%11.6f / ".
52 "%11.6f,%11.6f,%11.6f / ".
53 "%11.6f,%11.6f,%11.6f %-40s ",
54 $t->[0][0], $t->[0][1], $t->[0][2],
55 $t->[1][0], $t->[1][1], $t->[1][2],
56 $t->[2][0], $t->[2][1], $t->[2][2],
60 sub maybe_subdivide_triangle ($$$$) {
61 my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
63 print STDERR sprintf_triangle $t;
65 foreach my $ix (0..2) {
68 if ($edge_need_subdivide_fn->($t->[$ix], $t->[$jx])) {
73 foreach my $ci (0..2) {
74 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
76 # triangle i-j-k, splitting edge i-m
78 my $n = [ @$t ]; $n->[$ix] = \@midp; $n->[3] = "$t->[3]a$ix$jx";
79 unshift @$changed, $n;
81 my $n = [ @$t ]; $n->[$jx] = \@midp; $n->[3] = "$t->[3]b$ix$jx";
82 unshift @$changed, $n;
84 " midp %11.6f,%11.6f,%11.6f\n",
90 printf STDERR "OK nok=%d nchanged=%d\n",
91 (scalar @$ok), (scalar @$changed);
94 sub maybe_subdivide ($) {
95 my ($edge_need_subdivide_fn) = @_;
97 my @small_enough = ();
98 while (my $t = shift @$triangles) {
99 maybe_subdivide_triangle $t, \@small_enough, $triangles,
100 $edge_need_subdivide_fn;
103 $triangles = \@small_enough;
106 sub append_triangle ($) {
111 #---------- set-fa ----------
117 #---------- project-cylinder ----------
119 our $project_cylinder_radius;
120 our $project_cylinder_max_d_theta;
122 sub project_cylinder_edge_need_subdivide ($$) {
123 my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
124 return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
127 sub project_cylinder_tri {
130 #print STDERR 'PROJECT', Dumper($t);
132 my $radius = $project_cylinder_radius;
135 foreach my $p (@$t) {
137 my $r = $radius - $y;
138 my $theta = $x / $radius;
139 push @ot, [ $r * sin($theta),
143 append_triangle \@ot;
146 sub op__project_cylinder () {
147 $project_cylinder_radius = shift_arg;
148 $project_cylinder_max_d_theta = $fa * TAU/360;
150 maybe_subdivide \&project_cylinder_edge_need_subdivide;
153 foreach my $t (@$triangles) {
154 project_cylinder_tri $t;
156 $triangles = $output;
159 #---------- main program ----------
163 while (@ARGV && $ARGV[0] =~ m/^-/) {
176 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
182 $itmp = new File::Temp;
183 $otmp = new File::Temp;
187 open I, "admesh $admesh_stdout $itmp |";
194 if (m/^outer\s+loop/) {
197 } elsif (s/^vertex\s+//) {
200 my @xyz = split /\s+/, $_;
201 die unless $triangle;
202 push @$triangle, \@xyz;
203 } elsif (m/^endloop/) {
204 die unless @$triangle == 3;
206 push @$triangles, $triangle;
208 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
215 <I> if 0; # suppresses Name "main::I" used only once
220 &{ ${*::}{"op__$op"} };
225 print "solid distort-stl\n";
227 foreach my $t (@$triangles) {
228 print " facet normal 0 0 0\n";
229 print " outer loop\n";
231 foreach my $p (@$t) {
234 printf " %.18g", $_ foreach @$p;
241 print "endsolid distort-stl\n";
246 system "admesh --normal-values $admesh_stdout $otmp";