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 maybe_subdivide_triangle ($$$$) {
48 my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
50 foreach my $ix (0..2) {
53 if ($edge_need_subdivide_fn->($t[$ix], $t[$jx])) {
54 foreach my $ci (0..2) {
55 push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
57 # triangle i-j-k, splitting edge i-m
60 [ $t[$ix], \@midp, $t[kx] ],
61 [ $t[$kx], \@midp, $t[jx] ];
68 sub maybe_subdivide ($) {
69 my ($edge_need_subdivide_fn) = @_;
71 my @small_enough = ();
72 while (my $t = shift @$triangles) {
73 maybe_subdivide_triangle $t, \@small_enough, $triangles,
74 $edge_need_subdivide_fn;
77 $triangles = \@small_enough;
80 sub append_triangle ($) {
85 #---------- set-fa ----------
91 #---------- project-cylinder ----------
93 our $project_cylinder_radius;
94 our $project_cylinder_max_d_theta;
96 sub project_cylinder_edge_need_subdivide ($$) {
97 my @thetas = map { $_->[0] / $project_cylinder_radius } @_;
98 return abs($thetas[0] - $thetas[1]) > $project_cylinder_max_d_theta;
101 sub project_cylinder_tri {
104 #print STDERR 'PROJECT', Dumper($t);
106 my $radius = $project_cylinder_radius;
109 foreach my $p (@$t) {
111 my $r = $radius - $y;
112 my $theta = $x / $radius;
113 push @ot, [ $r * sin($theta),
117 append_triangle \@ot;
120 sub op__project_cylinder () {
121 $project_cylinder_radius = shift_arg;
122 $project_cylinder_max_d_theta = $fa * TAU/360;
124 maybe_subdivide \&project_cylinder_triangle_need_subdivide;
127 foreach my $t (@$triangles) {
128 project_cylinder_tri $t;
130 $triangles = $output;
133 #---------- main program ----------
137 while (@ARGV && $ARGV[0] =~ m/^-/) {
150 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
156 $itmp = new File::Temp;
157 $otmp = new File::Temp;
161 open I, "admesh $admesh_stdout $itmp |";
168 if (m/^outer\s+loop/) {
171 } elsif (s/^vertex\s+//) {
174 my @xyz = split /\s+/, $_;
175 die unless $triangle;
176 push @$triangle, \@xyz;
177 } elsif (m/^endloop/) {
178 die unless @$triangle == 3;
179 push @$triangles, $triangle;
181 } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
188 <I> if 0; # suppresses Name "main::I" used only once
193 &{ ${*::}{"op__$op"} };
198 print "solid distort-stl\n";
200 foreach my $t (@$triangles) {
201 print " facet normal 0 0 0\n";
202 print " outer loop\n";
204 foreach my $p (@$t) {
207 printf " %.18g", $_ foreach @$p;
214 print "endsolid distort-stl\n";
219 system "admesh --normal-values $admesh_stdout $otmp";