chiark / gitweb /
poster-tube-lid-parametric: wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 20 Oct 2017 17:01:07 +0000 (18:01 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 20 Oct 2017 17:01:07 +0000 (18:01 +0100)
poster-tube-lid-parametric.pl [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index bc77bff..2c14993
@@ -3,6 +3,8 @@ use strict;
 
 use Math::Vector::Real;
 
+sub TAU () { PI*2; }
+
 my $thick = 4;
 
 my $small_dia = 20;
@@ -108,9 +110,72 @@ sub point ($$) {
     } else {
        die "$jp ?";
     }
-
     return
        $i_offset +
        $i_outward * $j_x +
        [0,0,1] * $j_y;
 }
+
+sub get_sections_ptvals {
+    my $last_name;
+    my @out;
+    while (defined my $name = shift @_) {
+       if ($name eq '|') {
+           push @out, [ $last_name, 1.0 ];
+       } else {
+           my $count = shift @_;
+           foreach (my $ix = 0; $ix < $count; $ix++) {
+               push @out, [ $name, $ix/$count ];
+           }
+           $last_name = $name;
+       }
+    }
+}
+
+our @points;
+our %point_indices;
+our @triangles;
+
+sub triangle {
+    my @pixs;
+    foreach my $pval (@_) {
+       my $pix = $point_indices{$pval}
+           //= ((push @points, $pval), $#points);
+       return if grep { $pix eq $_ } @pixs
+       push @pixs, $pix;
+    }
+    push @triangles, \@pixs;
+}
+
+sub make_sheet () {
+    my @ipts = get_sections_ptvals(@i_sections);
+    my @jpts = get_sections_ptvals(@j_sections);
+    my @sheet;
+    foreach my $ipt (@ipts) {
+       my @row = ();
+       foreach my $jpt (@jpts) {
+           push @row, &point @$ipt, @$jpt;
+       }
+       push @sheet, \@row;
+    }
+    foreach (my $qi=0; $qi<$#ipts; $qi++) { # i direction does not wrap
+       my $qi2 = $qi+1;
+       foreach (my $qj=0; $qj<@jpts; $qj++) { # j direction does wrap
+           my $qj2 = ($qj+1) % @jpts;
+           my $p0 = $sheet[$qi][$qj];
+           triangle($p0, $sheet[$qi2][$qj], $sheet[$qi2][$qj2]);
+           triangle($p0, $sheet[$qi2][$qj2], $sheet[$qi2][$qj2]);
+       }
+    }
+}
+
+sub write_out ($) {
+    print "polyhedron(points=[\n" or die $!;
+    print $_,",\n" or die $! foreach @points;
+    print "],triangles=[\n" or die $!;
+    print $_,",\n" or die $! foreach @triangles;
+    print "],convexity=10)\n" or die $!;
+}
+
+make_sheet();
+write_out();