+sub project_cylinder_need_subdivide () {
+ foreach my $t (@$triangles) {
+ my @thetas = map { $_->[0] / $project_cylinder_radius } @$t;
+
+ foreach my $ix (0..2) {
+ if (abs($thetas[$ix] - $thetas[($ix+1)%3])
+ > $project_cylinder_max_d_theta) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+sub project_cylinder_tri {
+ my ($t) = @_;
+
+ #print STDERR 'PROJECT', Dumper($t);
+
+ my $radius = $project_cylinder_radius;
+
+ my @ot;
+ foreach my $p (@$t) {
+ my ($x,$y,$z) = @$p;
+ my $r = $radius - $y;
+ my $theta = $x / $radius;
+ push @ot, [ $r * sin($theta),
+ -$r * cos($theta),
+ $z ];
+ }
+ append_triangle \@ot;
+}
+
+sub op__project_cylinder () {
+ $project_cylinder_radius = shift_arg;
+ $project_cylinder_max_d_theta = $fa * TAU/360;
+
+ while (project_cylinder_need_subdivide()) {
+ $output = [];
+ foreach my $t (@$triangles) {
+ subdivide_triangle $t, \&append_triangle;
+ }
+ $triangles = $output;
+ }
+
+ $output = [];
+ foreach my $t (@$triangles) {
+ project_cylinder_tri $t;
+ }
+ $triangles = $output;
+}
+
+#---------- main program ----------
+
+our $raw;
+
+while (@ARGV && $ARGV[0] =~ m/^-/) {
+ $_ = shift @ARGV;
+ last if m/^--$/;
+ if (s/^--raw$//) {
+ $raw = 1;
+ } else {
+ die "$_ ?";
+ }
+}
+
+my $itmp;
+my $otmp;
+
+my $admesh_stdout = '--write-ascii-stl /dev/fd/3 3>&1 >/dev/null';
+
+if ($raw) {
+ open I, "<& STDIN";
+ $otmp = *STDOUT;
+} else {
+ $itmp = new File::Temp;
+ $otmp = new File::Temp;
+
+ system "cat >$itmp";
+
+ open I, "admesh $admesh_stdout $itmp |";
+}
+
+my $triangle;