chiark / gitweb /
quacks-ingredients: better counting reports
[reprap-play.git] / quacks-ingredients-counts
index 870970a9c718d296b1aca93b2a98ac8780ae34b1..e9f6acf1fde58562a48736930c091853e559579f 100755 (executable)
@@ -5,13 +5,30 @@ use strict;
 use Data::Dumper;
 use POSIX;
 
+sub xdata ($) {
+    my ($cb) = @_;
+    foreach my $count (qw(1 2 3)) {
+       foreach my $nspots (qw(1 2 3 4)) {
+           $_ = $cb->($count,$nspots)."\t".$_;
+       }
+    }
+}
+
 $_=<DATA>; chomp or die;
+xdata sub {
+    my ($xcount,$xnspots) = @_;
+    "${xcount}x". (qw(X One Two Three Four)[$xnspots]);
+};
 our @names = split /\t/, $_;
 
 our %count;
 
 foreach my $nspots (qw(1 2 3 4)) {
     $_=<DATA>; chomp or die;
+    xdata sub {
+       my ($xcount,$xnspots) = @_;
+       $xnspots == $nspots and $xcount;
+    };
     my @l = split /\t/, $_;
     foreach my $i (0..$#names) { $count{$names[$i]}{$nspots} = $l[$i] || 0; }
 }
@@ -20,6 +37,10 @@ $_ = Dumper(\%count);
 s{^}{// }mg;
 
 our $name;
+our $total_count;
+our $total_real_count;
+our $max_nrows=0;
+our $max_rowsz=0;
 
 sub wrtoplevel () {
     print "module $name(){ ////toplevel\n";
@@ -27,18 +48,23 @@ sub wrtoplevel () {
     my $total = 0; $total += $_ foreach values %$cs;
     my $rowsz = ceil(sqrt($total));
     my $nrows = ceil($total / $rowsz);
+    $total_count += $total;
+    $total_real_count += $total if $name =~ m/^[A-Z][a-z]+$/;
+    $max_nrows = $nrows if $nrows > $max_nrows;
+    $max_rowsz = $rowsz if $rowsz > $max_rowsz;
     my $ix = 0;
-    print "// total=$total, rowsz=$rowsz, nrows=$nrows\n";
-    foreach my $ndots (sort keys %$cs) {
-       my $c = $cs->{$ndots};
+    print "// $name: total=$total, rowsz=$rowsz, nrows=$nrows\n";
+    foreach my $nspots (sort keys %$cs) {
+       my $c = $cs->{$nspots};
        print <<END;
   union(){
-    \$ndots = $ndots;
+    Frame(\$phase, token_pitch * [ $rowsz + 1.00, $nrows + 0.50 ]);
+    \$nspots = $nspots;
 END
        while ($c--) {
            my $xy = sprintf "[ %5.1f, %5.1f ]",
-               $ix / $nrows - 0.5 * $rowsz,
-               $ix % $nrows - 0.5 * $nrows;
+               int($ix / $nrows) - 0.5 * ($rowsz-1),
+               $ix % $nrows - 0.5 * ($nrows-1);
            print "    translate(token_pitch * $xy) Token_L();\n";
            $ix++;
        }
@@ -53,6 +79,11 @@ foreach $name (sort keys %count) {
     wrtoplevel();
 }
 
+print <<END;
+// total_count=$total_count total_real_count=$total_real_count
+// max_rowsz=$max_rowsz max_nrows=$max_nrows
+END
+
 STDOUT->error and die $!;
 
 __DATA__