chiark / gitweb /
poster-tube-lid: catch: adjust stalk height from v1
[reprap-play.git] / distort-stl
index dd430c9e452663b9aafcabdd7dc0abc3dfaf5ae7..68290c410a6ae4738d680d6409abd8f3464c008e 100755 (executable)
@@ -32,6 +32,14 @@ use Data::Dumper;
 
 sub TAU () { M_PI * 2; }
 
 
 sub TAU () { M_PI * 2; }
 
+our $debug = $ENV{DISTORT_DEBUG} // 0 ;
+
+my $ps = $ENV{DISTORT_PS};
+if ($ps) {
+    open PS, "> $ps" or die $!;
+    print PS "%!\n";
+}
+
 our $fa = 10;
 
 our $triangles;
 our $fa = 10;
 
 our $triangles;
@@ -44,25 +52,91 @@ sub shift_arg () {
 
 #no warnings qw(recursion);
 
 
 #no warnings qw(recursion);
 
+sub sprintf_triangle ($) {
+    my ($t) = @_;
+
+    return '' unless $debug;
+
+    if ($ps && $t->[3] =~ m/$ENV{DISTORT_PS_RE}/) {
+       printf PS <<'END',
+ %20.16g %20.16g %20.16g moveto
+ %20.16g %20.16g %20.16g lineto
+ %20.16g %20.16g %20.16g lineto
+ closepath stroke
+END
+               $t->[0][0], $t->[0][1], $t->[0][2],
+               $t->[1][0], $t->[1][1], $t->[1][2],
+               $t->[2][0], $t->[2][1], $t->[2][2],
+               or die $!;
+       flush PS or die $!;
+    }
+
+    sprintf
+       "%11.6f,%11.6f,%11.6f / ".
+       "%11.6f,%11.6f,%11.6f / ".
+       "%11.6f,%11.6f,%11.6f  %-40s ",
+               $t->[0][0], $t->[0][1], $t->[0][2],
+               $t->[1][0], $t->[1][1], $t->[1][2],
+               $t->[2][0], $t->[2][1], $t->[2][2],
+               $t->[3];
+}
+
 sub maybe_subdivide_triangle ($$$$) {
     my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
 sub maybe_subdivide_triangle ($$$$) {
     my ($t, $ok, $changed, $edge_need_subdivide_fn) = @_;
-    
+
+    print STDERR sprintf_triangle $t if $debug;
+
+    my (@longest) = qw(-1);
+
     foreach my $ix (0..2) {
        my $jx = ($ix+1) % 3;
     foreach my $ix (0..2) {
        my $jx = ($ix+1) % 3;
-       my $kx = ($ix+2) % 3;
-       if ($edge_need_subdivide_fn->($t[$ix], $t[$jx])) {
-           foreach my $ci (0..2) {
-               push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
-           }
-           # triangle i-j-k, splitting edge i-m
-           # gives    i-m-k, k-m-j
-           push @$changed,
-               [ $t[$ix], \@midp, $t[kx] ],
-               [ $t[$kx], \@midp, $t[jx] ];
-           return;
+       next unless $edge_need_subdivide_fn->($t->[$ix], $t->[$jx]);
+       my $l2 = 0;
+       foreach my $ci (0..2) {
+           my $d = $t->[$ix][$ci] - $t->[$jx][$ci];
+           $l2 += $d*$d;
        }
        }
+       next unless $l2 > $longest[0];
+       @longest = ($l2, $ix, $jx);
+    }
+    if ($longest[0] < 0) {
+       push @$ok, $t;
+       printf STDERR "OK nok=%d nchanged=%d\n",
+           (scalar @$ok), (scalar @$changed)
+           if $debug;
+       print STDERR Dumper(\@$ok) if $debug>=2;
+       return;
     }
     }
-    push @$ok, $t;
+    my ($dummy,$ix,$jx) = @longest;
+    my $kx = ($ix+2) % 3;
+
+    printf STDERR
+       " S i=%d j=%d k=%d ",
+       $ix, $jx, $kx
+       if $debug;
+    my @midp;
+    foreach my $ci (0..2) {
+       push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
+    }
+
+    printf STDERR
+       " midp %11.6f,%11.6f,%11.6f\n",
+       @midp
+       if $debug;
+
+    # triangle i-j-k, splitting edge i-m
+    # gives    i-m-k, k-m-j
+    my $gensplit = sub {
+       my ($ixjx, $xwhat) = @_;
+       my $n = [ @$t ];
+       $n->[$ixjx] = \@midp;
+       $n->[3] = "$t->[3]$xwhat";
+       printf STDERR "%s\n", sprintf_triangle $n if $debug;
+       unshift @$changed, $n;
+    };
+    $gensplit->($ix, "a$ix$jx");
+    $gensplit->($jx, "b$ix$jx");
+    return;
 }
 
 sub maybe_subdivide ($) {
 }
 
 sub maybe_subdivide ($) {
@@ -106,7 +180,7 @@ sub project_cylinder_tri {
     my $radius = $project_cylinder_radius;
 
     my @ot;
     my $radius = $project_cylinder_radius;
 
     my @ot;
-    foreach my $p (@$t) {
+    foreach my $p (@$t[0..2]) {
        my ($x,$y,$z) = @$p;
        my $r = $radius - $y;
        my $theta = $x / $radius;
        my ($x,$y,$z) = @$p;
        my $r = $radius - $y;
        my $theta = $x / $radius;
@@ -114,6 +188,7 @@ sub project_cylinder_tri {
                    -$r * cos($theta),
                    $z ];
     }
                    -$r * cos($theta),
                    $z ];
     }
+    push @ot, $t->[3].'P';
     append_triangle \@ot;
 }
 
     append_triangle \@ot;
 }
 
@@ -121,7 +196,7 @@ sub op__project_cylinder () {
     $project_cylinder_radius = shift_arg;
     $project_cylinder_max_d_theta = $fa * TAU/360;
 
     $project_cylinder_radius = shift_arg;
     $project_cylinder_max_d_theta = $fa * TAU/360;
 
-    maybe_subdivide \&project_cylinder_triangle_need_subdivide;
+    maybe_subdivide \&project_cylinder_edge_need_subdivide;
     
     $output = [];
     foreach my $t (@$triangles) {
     
     $output = [];
     foreach my $t (@$triangles) {
@@ -176,6 +251,7 @@ while (<I>) {
        push @$triangle, \@xyz;
     } elsif (m/^endloop/) {
        die unless @$triangle == 3;
        push @$triangle, \@xyz;
     } elsif (m/^endloop/) {
        die unless @$triangle == 3;
+       push @$triangle, $.;
        push @$triangles, $triangle;
        undef $triangle;
     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
        push @$triangles, $triangle;
        undef $triangle;
     } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
@@ -200,8 +276,8 @@ print "solid distort-stl\n";
 foreach my $t (@$triangles) {
     print "  facet normal 0 0 0\n";
     print "    outer loop\n";
 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 @$t==4;
+    foreach my $p (@$t[0..2]) {
        die unless @$p==3;
        print "      vertex";
        printf " %.18g", $_ foreach @$p;
        die unless @$p==3;
        print "      vertex";
        printf " %.18g", $_ foreach @$p;