chiark / gitweb /
Merge branch 'master' of 2play
[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 $_=<DATA>; chomp or die;
9 our @names = split /\t/, $_;
10
11 our %count;
12
13 foreach my $nspots (qw(1 2 3 4)) {
14     $_=<DATA>; chomp or die;
15     my @l = split /\t/, $_;
16     foreach my $i (0..$#names) { $count{$names[$i]}{$nspots} = $l[$i] || 0; }
17 }
18
19 $_ = Dumper(\%count);
20 s{^}{// }mg;
21
22 our $name;
23 our $total_count;
24 our $max_nrows=0;
25 our $max_rowsz=0;
26
27 sub wrtoplevel () {
28     print "module $name(){ ////toplevel\n";
29     my $cs = $count{$name};
30     my $total = 0; $total += $_ foreach values %$cs;
31     my $rowsz = ceil(sqrt($total));
32     my $nrows = ceil($total / $rowsz);
33     $total_count += $total;
34     $max_nrows = $nrows if $nrows > $max_nrows;
35     $max_rowsz = $rowsz if $rowsz > $max_rowsz;
36     my $ix = 0;
37     print "// total=$total, rowsz=$rowsz, nrows=$nrows\n";
38     foreach my $nspots (sort keys %$cs) {
39         my $c = $cs->{$nspots};
40         print <<END;
41   union(){
42     Frame(\$phase, token_pitch * [ $rowsz + 0.75, $nrows ]);
43     \$nspots = $nspots;
44 END
45         while ($c--) {
46             my $xy = sprintf "[ %5.1f, %5.1f ]",
47                 int($ix / $nrows) - 0.5 * ($rowsz-1),
48                 $ix % $nrows - 0.5 * ($nrows-1);
49             print "    translate(token_pitch * $xy) Token_L();\n";
50             $ix++;
51         }
52         print <<END;
53   };
54 END
55     }
56     print "}\n";
57 }
58
59 foreach $name (sort keys %count) {
60     wrtoplevel();
61 }
62
63 print <<END;
64 // total_count=$total_count
65 // max_rowsz=$max_rowsz max_nrows=$max_nrows
66 END
67
68 STDOUT->error and die $!;
69
70 __DATA__
71 White   Green   Blue    Red     Yellow  Purple  Black   Orange
72 20      15      14      12      13      15      18      20
73 8       10      10      8       6
74 4
75         13      10      10      10