chiark / gitweb /
poster-tube-lid: catch: Colourise CatchDemo
[reprap-play.git] / distort-stl
index f8989923b6809d763ffbeeb4fb7172dcdb1210ba..0eb71fd6179121f346c1d60e4c52e89ba82d1528 100755 (executable)
@@ -34,15 +34,21 @@ sub TAU () { M_PI * 2; }
 
 our $fa = 10;
 
-our @triangles;
+our $triangles;
+our $output;
 
 sub shift_arg () {
     die unless @ARGV;
     scalar shift @ARGV;
 }
 
+#no warnings qw(recursion);
+
 sub subdivide_triangle ($$) {
     my ($t, $fn) = @_;
+
+    #print STDERR 'SUBDIV', Dumper($t, $fn);
+
     my @mids;
     foreach my $ix (0..2) {
        my $jx = ($ix+1) % 3;
@@ -50,37 +56,55 @@ sub subdivide_triangle ($$) {
        foreach my $ci (0..2) {
            push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
        }
-       push @mids, @midp;
+       push @mids, \@midp;
     }
     foreach my $ix (0..2) {
+       #print STDERR 'SUBDIV IX ', $ix, "\n";
        my $kx = ($ix+2) % 3;
-       $fn->([ $t->[$ix], $mids[$ix], $t->[$kx] ]);
+       $fn->([ $t->[$ix], $mids[$ix], $mids[$kx] ]);
     }
+    #print STDERR 'SUBDIV MID\n';
     $fn->(\@mids);
 }
 
+sub append_triangle ($) {
+    my ($t) = @_;
+    push @$output, $t;
+}
+
+#---------- set-fa ----------
+
+sub op__set_fa () {
+    $fa = shift_arg;
+}
+
 #---------- project-cylinder ----------
 
 our $project_cylinder_radius;
 our $project_cylinder_max_d_theta;
 
-sub project_cylinder_tri {
+sub project_cylinder_triangle_need_subdivide ($) {
     my ($t) = @_;
-
-    my $radius = $project_cylinder_radius;
-
-    my @thetas = map { $_->[0] / $radius } @$t;
+    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) {
-           subdivide_triangle $t, \&project_cylinder_tri;
-           return;
+           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) {
+    foreach my $p (@$t) {
        my ($x,$y,$z) = @$p;
        my $r = $radius - $y;
        my $theta = $x / $radius;
@@ -88,17 +112,30 @@ sub project_cylinder_tri {
                    -$r * cos($theta),
                    $z ];
     }
-    push @triangles, \@ot;
+    append_triangle \@ot;
 }
 
 sub op__project_cylinder () {
     $project_cylinder_radius = shift_arg;
     $project_cylinder_max_d_theta = $fa * TAU/360;
 
-    my @input = (@triangles);
-    @triangles = ();
+    my @small_enough = ();
+    while (my $t = shift @$triangles) {
+       if (!project_cylinder_triangle_need_subdivide $t) {
+           push @small_enough, $t;
+       } else {
+           local $output = $triangles;
+           subdivide_triangle $t, \&append_triangle;
+       }
+    }
+
+    $triangles = \@small_enough;
 
-    project_cylinder_tri $_ foreach @input;
+    $output = [];
+    foreach my $t (@$triangles) {
+       project_cylinder_tri $t;
+    }
+    $triangles = $output;
 }
 
 #---------- main program ----------
@@ -147,7 +184,7 @@ while (<I>) {
        push @$triangle, \@xyz;
     } elsif (m/^endloop/) {
        die unless @$triangle == 3;
-       push @triangles, $triangle;
+       push @$triangles, $triangle;
        undef $triangle;
     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
     } else {
@@ -168,10 +205,12 @@ select $otmp;
 
 print "solid distort-stl\n";
 
-foreach my $t (@triangles) {
+foreach my $t (@$triangles) {
     print "  facet normal 0 0 0\n";
     print "    outer loop\n";
+    die unless @$t==3;
     foreach my $p (@$t) {
+       die unless @$p==3;
        print "      vertex";
        printf " %.18g", $_ foreach @$p;
        print "\n";