chiark / gitweb /
quacks-ingredients: ad hoc print jobs
[reprap-play.git] / quacks-ingredients-counts
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Data::Dumper;
6 use POSIX;
7
8 sub xdata ($) {
9     my ($cb) = @_;
10     foreach my $count (qw(1 2 3)) {
11         foreach my $nspots (qw(1 2 3 4)) {
12             $_ = $cb->($count,$nspots)."\t".$_;
13         }
14     }
15 }
16
17 $_=<DATA>; chomp or die;
18 xdata sub {
19     my ($xcount,$xnspots) = @_;
20     "${xcount}x". (qw(X One Two Three Four)[$xnspots]);
21 };
22 our @names = split /\t/, $_;
23
24 our %count;
25
26 foreach my $nspots (qw(1 2 3 4)) {
27     $_=<DATA>; chomp or die;
28     xdata sub {
29         my ($xcount,$xnspots) = @_;
30         $xnspots == $nspots and $xcount;
31     };
32     my @l = split /\t/, $_;
33     foreach my $i (0..$#names) { $count{$names[$i]}{$nspots} = $l[$i] || 0; }
34 }
35
36 $_ = Dumper(\%count);
37 s{^}{// }mg;
38
39 our $name;
40 our $total_count;
41 our $max_nrows=0;
42 our $max_rowsz=0;
43
44 sub wrtoplevel () {
45     print "module $name(){ ////toplevel\n";
46     my $cs = $count{$name};
47     my $total = 0; $total += $_ foreach values %$cs;
48     my $rowsz = ceil(sqrt($total));
49     my $nrows = ceil($total / $rowsz);
50     $total_count += $total;
51     $max_nrows = $nrows if $nrows > $max_nrows;
52     $max_rowsz = $rowsz if $rowsz > $max_rowsz;
53     my $ix = 0;
54     print "// total=$total, rowsz=$rowsz, nrows=$nrows\n";
55     foreach my $nspots (sort keys %$cs) {
56         my $c = $cs->{$nspots};
57         print <<END;
58   union(){
59     Frame(\$phase, token_pitch * [ $rowsz + 1.00, $nrows + 0.50 ]);
60     \$nspots = $nspots;
61 END
62         while ($c--) {
63             my $xy = sprintf "[ %5.1f, %5.1f ]",
64                 int($ix / $nrows) - 0.5 * ($rowsz-1),
65                 $ix % $nrows - 0.5 * ($nrows-1);
66             print "    translate(token_pitch * $xy) Token_L();\n";
67             $ix++;
68         }
69         print <<END;
70   };
71 END
72     }
73     print "}\n";
74 }
75
76 foreach $name (sort keys %count) {
77     wrtoplevel();
78 }
79
80 print <<END;
81 // total_count=$total_count
82 // max_rowsz=$max_rowsz max_nrows=$max_nrows
83 END
84
85 STDOUT->error and die $!;
86
87 __DATA__
88 White   Green   Blue    Red     Yellow  Purple  Black   Orange
89 20      15      14      12      13      15      18      20
90 8       10      10      8       6
91 4
92         13      10      10      10