chiark / gitweb /
distort-stl: new approach
[reprap-play.git] / distort-stl
1 #!/usr/bin/perl -w
2 #
3 # usage:
4 #   ./distort-stl <INPUT >OUTPUT DISTORTION [PARAMS...] ...
5 #
6 # DISTORTIONs:
7 #
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
22 #
23 #   set-fa $FA
24
25 use strict;
26 use autodie;
27
28 use File::Temp;
29 use List::Util;
30
31 our $fa = 10;
32
33 our @triangles;
34
35 sub shift_arg () {
36     die unless @ARGV;
37     scalar shift @ARGV;
38 }
39
40 sub subdivide_triangle ($$) {
41     my ($it, $fn) = @_;
42     my @mids;
43     foreach my $ix (0..2) {
44         my $jx = ($ix+1) % 3;
45         my @midp;
46         foreach my $ci (0..2) {
47             push @midp, 0.5 * ($it->[$ix][$ci] + $it->[$jx][$ci]);
48         }
49         push @mids, @midp;
50     }
51     foreach my $ix (0..2) {
52         my $kx = ($ix+2) % 3;
53         $fn->([ $it->[$ix], $mids[$ix], $it->[$kx] ]);
54     }
55     $fn->(\@mids);
56 }
57
58 #---------- project-cylinder ----------
59
60 our $project_cylinder_radius;
61 our $project_cylinder_max_d_theta;
62
63 sub project_cylinder_tri {
64     my ($it) = @_;
65
66     my $radius = project_cylinder_radius;
67
68     my @thetas = map { $_->[0] / $radius } @$it;
69
70     foreach my $ix (0..2) {
71         if (abs($thetas[$ix] - $thetas[($ix+1)%3])
72             > $project_cylinder_max_d_theta) {
73             subdivide_triangle $it, \&project_cylinder_tri;
74             return;
75         }
76     }
77
78     my @ot;
79     foreach my $p (@it) {
80         my ($x,$y,$z) = @$p;
81         my $r = $radius - $y;
82         my $theta = $x / $radius;
83         push @ot, [ $r * sin($theta),
84                     -$r * cos($theta),
85                     $z ];
86     }
87     push @triangles, \@ot;
88 }
89
90 sub op__project_cylinder () {
91     $project_cylinder_radius = shift_arg;
92     $project_cylinder_max_d_theta = $fa * TAU/360;
93
94     my @input = (@triangles);
95     @triangles = ();
96
97     project_cylinder_tri $_ foreach @input;
98 }
99
100 #---------- main program ----------
101
102 if (@ARGV && $ARGV[0] =~ m/^-/) {
103     die "no options supported\n";
104 }
105
106 my $itmp = new File::Temp;
107 my $otmp = new File::Temp;
108
109 system "cat >$itmp";
110
111 my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
112
113 open I, "admesh $admesh_stdout $itmp |";
114
115 my $triangle;
116
117 while (<I>) {
118     if (m/^\s+outer\s+loop/) {
119         die if $triangle;
120         $triangle = [];
121     } elsif (s/^\s+vertex\s+//) {
122         my $lhs = $&;
123         s/\s+$//;
124         my @xyz = split /\s+/, $_;
125         die unless $triangle;
126         push @$triangle, \@xya;
127     } elsif (m/^\s+endloop/) {
128         die unless @$triangle == 3;
129         push @triangles, $triangle;
130     } elsif (m/^\s+(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
131     } else {
132         die "$_ ?";
133     }
134 }
135
136 close I;
137 <I> if 0; # suppresses Name "main::I" used only once
138
139 while (@ARGV) {
140     my $op = shift_arg;
141     $op =~ y/-/_/;
142     &{ ${*::}{"op__$op"} };
143 }
144
145 select $otmp;
146
147 print "solid distort-stl\n";
148
149 foreach my $t (@triangles) {
150     print "  facet normal 0 0 0\n";
151     print "    outer loop\n";
152     print "      vertex";
153     printf " %.18g", $_ foreach @$t;
154     print "\n";
155     print "    endloop\n";
156     print "  endfacet\n";
157 }
158
159 print "endsolid distort-stl\n";
160
161 flush $otmp;
162
163 system "admesh --normal-values $admesh_stdout $otmp";