chiark / gitweb /
distort-stl: new approach, wip fixes
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 7 Jan 2018 22:24:37 +0000 (22:24 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 7 Jan 2018 22:24:37 +0000 (22:24 +0000)
distort-stl

index eb3a7a15deb92a6c393b45fd3db21f63426cab00..1b512f853a3a2cc6047878f6783573c7c725dbe4 100755 (executable)
 use strict;
 use autodie;
 
-use File::Temp;
 use List::Util;
+use POSIX;
+use File::Temp ();
+
+sub TAU () { M_PI * 2; }
 
 our $fa = 10;
 
@@ -38,19 +41,19 @@ sub shift_arg () {
 }
 
 sub subdivide_triangle ($$) {
-    my ($it, $fn) = @_;
+    my ($t, $fn) = @_;
     my @mids;
     foreach my $ix (0..2) {
        my $jx = ($ix+1) % 3;
        my @midp;
        foreach my $ci (0..2) {
-           push @midp, 0.5 * ($it->[$ix][$ci] + $it->[$jx][$ci]);
+           push @midp, 0.5 * ($t->[$ix][$ci] + $t->[$jx][$ci]);
        }
        push @mids, @midp;
     }
     foreach my $ix (0..2) {
        my $kx = ($ix+2) % 3;
-       $fn->([ $it->[$ix], $mids[$ix], $it->[$kx] ]);
+       $fn->([ $t->[$ix], $mids[$ix], $t->[$kx] ]);
     }
     $fn->(\@mids);
 }
@@ -61,22 +64,22 @@ our $project_cylinder_radius;
 our $project_cylinder_max_d_theta;
 
 sub project_cylinder_tri {
-    my ($it) = @_;
+    my ($t) = @_;
 
-    my $radius = project_cylinder_radius;
+    my $radius = $project_cylinder_radius;
 
-    my @thetas = map { $_->[0] / $radius } @$it;
+    my @thetas = map { $_->[0] / $radius } @$t;
 
     foreach my $ix (0..2) {
        if (abs($thetas[$ix] - $thetas[($ix+1)%3])
            > $project_cylinder_max_d_theta) {
-           subdivide_triangle $it, \&project_cylinder_tri;
+           subdivide_triangle $t, \&project_cylinder_tri;
            return;
        }
     }
 
     my @ot;
-    foreach my $p (@it) {
+    foreach my $p ($t) {
        my ($x,$y,$z) = @$p;
        my $r = $radius - $y;
        my $theta = $x / $radius;
@@ -115,19 +118,21 @@ open I, "admesh $admesh_stdout $itmp |";
 my $triangle;
 
 while (<I>) {
-    if (m/^\s+outer\s+loop/) {
+    s/^\s*//;
+    if (m/^outer\s+loop/) {
        die if $triangle;
        $triangle = [];
-    } elsif (s/^\s+vertex\s+//) {
+    } elsif (s/^vertex\s+//) {
        my $lhs = $&;
        s/\s+$//;
        my @xyz = split /\s+/, $_;
        die unless $triangle;
-       push @$triangle, \@xya;
-    } elsif (m/^\s+endloop/) {
+       push @$triangle, \@xyz;
+    } elsif (m/^endloop/) {
        die unless @$triangle == 3;
        push @triangles, $triangle;
-    } elsif (m/^\s+(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
+       undef $triangle;
+    } elsif (m/^(?:solid|facet\s+normal|endfacet|endsolid)\s/) {
     } else {
        die "$_ ?";
     }