Geometry : Polycubes (and Polyominoes)

Created on August 16, 2023.

TL;DR

Introduction

Everything started with this YouTube video from Computerphile : Mike's Cube Code where Mike Pound introduces the concept and shares his attempt at enumerating the polycubes with their number of cubes n with some basic Python code. I suggest you watch the video but if you don't, polycubes are shapes formed by the connection of cubes face to face. All cubes have the same size (let's say their side length is 1). Any cube may have up to 6 neighbors in the six possible directions: up, down, left, right, front, back. In any polycube, also called a lattice animal, there is no cube or set of cubes that is separated from the rest.

Since some mathematicians seem to like counting, they have tried counting the number of different polycubes that can be made from n cubes. For precise enumeration, one would need to not overcount or undercount and so define whether two shapes are the same or not. The rules are that two polycubes that differ by reflection are considered different and that two polycubes that differ by rotation are considered identical (there are 24 possible rotations for a given polycube). The result from this computation is available on the Online Encyclopedia of Integer Sequences (OIES) as sequence A000162 up till n=14 and on Kevin Gong's website up till n=16. As you can see, the growth is exponential with a growing factor of around 7. The main algorithm for this computation is to generate polycubes of level n by adding a cube at every possible position on every polycube of level n-1 and discard the identical ones. This growing procedure guarantees that the shapes are always connected and that none is by-passed but the computing time and memory consumption are huge.

The last computation (n=16) is from 2004. Thanks to the Computerphile video, a collaborative open-access Github repository was created: Opencubes where people have started improving the original code, implementing it with other languages and discussing computation techniques. So if you feel like you could make some progress on the matter, I invite you to participate !

I was not satisfy with the rendering tool from the repository. It would generate a view over all the polycubes dispatched over a big plane. So, I decided to make my own viewer. The requirements were to load big datasets, with potentially billions of polycubes, big or small, browse through the models rapidly and rotate around each model. For big model, the limit is the GPU instancing of your computer but ten or hundred thousand cubes on a single polycube is okay. For datasets, I have managed to compress the data and load it into memory in an optimized way. Though, my server drive being only a few gigabytes large, I won't upload larger datasets. You can still view your own datasets if they are in the correct format.

Polycubes that fits exactly into a 3x3x3 cuboid

Actually, my first interest was related to modular origami. I had started building polycubes and planning for others using online voxel builders and suddendly became interested on the polycubes that could fit exactly into a 3x3x3 cube by spanning across this cube. What I mean is there should be at least a cube on each face of the 3x3x3 cube. This way, the shape would not fit into a smaller cuboid. With this restriction, it means that the smallest polycube is of length n=7 and the largest polycube is the 3x3x3 cube itself with n=27. Moreover, all polycubes with n>18 necessarily satisfy the requirement.

For the first step, I took the Python code from the Opencubes repo, modified and optimized it to generate all polycubes that would be contained in a 3x3x3 grid. The modifications are very light. For the first layer (n=1), every position in the 3x3x3 cube is considered as a starting position. The working volume is fixed, it is that of the 3x3x3 grid, which simplifies a lot the rotations (which can be precomputed as a set of permutations) as well as the comparison process because the keys that represent each shape are the same length (27). In the end, the speedup is by a factor of 4 or so for a total of around 20 minutes. All the shapes (1,597,143) are stored in a Numpy array.

For the second step, I just remove the polycubes that do not span across the 3x3x3 grid (it took a minute to run on my computer and removed 22,890 invalid polycubes). In the end, I have counted 1,574,253 polycubes over the 2^(3x3x3)=134,217,728 combinations in a 3x3x3 grid so a little bit more than 1%. The results are summed up in this table:


n count n count n count
7 118 14 226020 21 11772
8 913 15 277664 22 3356
9 4438 16 277365 23 769
10 14922 17 223799 24 138
11 39546 18 145802 25 22
12 84948 19 77251 26 4
13 151992 20 33413 27 1

Polycubes in cuboids

Edit from 09/09/2023:

Before explaining why I have considered the computation of cuboids, I may explain how a polycube is encoded in the original code. In python, it is represented as a uint8 array with the minimum dimensions to hold it. Values in the array can be either 1 to indicate the presence of a cube or 0 for its absence. At some point, this array needs to be hashed so a hashable key is needed. This key is made of the grid dimensions which are coded onto a byte each then concatenated together with the flattened polycube array. The flattened array is packed in chunks of eight elements per byte and zero padded. This makes for a very simple packing scheme: x y z bits. This is also the encoding I use for the datasets files that I load into the viewer. Such a file can be compressed efficiently with gzip too.

When thinking about the computation of the polycubes of length n. I realized that they can be split into groups based on the minimum cuboid size that they can fit in. So instead of computing the big dataset, we could compute many smaller datasets, one for each cuboid large enough to contain a polycube of a certain length. Really that's all, since that from a cuboid shape we can compute easily the range of polycube lengths it can contain, we know which cuboids to compute.

Thanks to some very fast C code (from here ), I was able to compute the number of polycubes that fits into many cuboids. Please go see the table in the Annex if you want the numbers. I have yet to compute all the cuboids needed for n>10 though. I may update this table in the future if I spend some of my computer time on it.

Looking at the evolution of the number of polycubes with each count in a given cuboid, in a log scale, it certainly follows a polynomial equation of degree 2. I have no idea why though. I mean, I expect it to go up and down but not with this specific shape. Is there a formula that relates the shape of the cuboid to the coefficient of the polynomial ? I don't know.

PIC PIC PIC PIC PIC

After thoughts

Polycubes make for interesting puzzle games, the most well known is the Soma cube where a 3x3x3 cube has to be assembled from a set of polycubes, many variations exist for the polycubes that are part of the set. This could be a question on its own: let's say there are a certain number of polycubes in a set, what are all the numbers and shapes these polycubes could have and be assembled into the 3x3x3 cube.

I have seen other puzzles where a given shape had to be made with pentacubes only. I mean at this point, the realm of ideas becomes limitless.

As a final touch to this article, let me show you some of the polycubes I have made with modular origami and a lot of patience. From left to right: a T-Rex, a tree and an abstract four-legged shape, the central column is not into contact with the table beneath.

PIC PIC PIC

Annex

The table with my results is below. It is also available for download as a text file.

nb2x2x12x2x23x2x13x2x23x3x13x3x23x3x3nb4x2x14x2x24x3x14x3x24x3x34x4x14x4x24x4x34x4x4nb5x2x15x2x25x3x15x3x25x3x35x4x15x4x25x4x35x4x45x5x15x5x25x5x35x5x45x5x5nb6x2x16x2x26x3x16x3x26x3x36x4x16x4x26x5x1nb7x2x17x2x27x3x17x3x27x4x18x2x18x2x28x3x19x2x19x2x29x3x110x2x111x2x112x2x113x2x1totalnbtotal+Nx1x1A000162
3133331322
413344447488
522156535552852929
6314175966281565661656166166
7161729811872122392691871146257571,02271,0231,023
8158386991381324592,1929127739381028796537618196735876,92186,9226,922
92911,6664,4389521429,56111,2871814,3933,007931,0492105,7212,0833831,71573922542188921979289349748,310948,31148,311
10122,38814,922106022129,28177,50226627,48550,7293,6861012,61125533,43032,7481,30424,66614,9425292,12710152,60855012,3004,0418223,28624010529333321,46715540122639346,54210346,543346,543
1122,45039,54611428467,893376,365251121,615470,64081,423114,678212138,963286,8472,847196,124312,63029,9702,41336,59520,6271138,63095489,48877,4943,54856,8262,36611455,4741,23123,3221,551901,458529531568192,465,831112,465,8322,522,522
1211,85484,948122251124,4281,444,525168421,8013,113,306989,811126,041103448,9581,793,8934,4411,124,2223,577,634799,2127,375357,029508,05088,63112121,7101,184467,753819,83710,323544,68713,161122122,7442,800203,0618,23911910,2462,4061582,171800691117,028,1581217,028,15918,598,427
13967151,9921369181,0514,600,726661,194,12416,366,4448,580,365135,544331,175,4178,922,2065,0085,108,30729,393,28911,664,02917,0412,539,7066,946,4752,755,73994,4661340,9499641,923,1006,212,26921,9953,777,50251,13313472,8534,6341,271,66429,4427350,9306,81825717,6214,3132388611113,189,92013113,189,921138,462,649
14390226,0201420209,24012,533,971202,830,07572,215,18159,279,199143,66762,546,70037,182,6504,16819,305,538--30,32014,528,248---1458,6695466,510,918-36,03520,939,873153,122141182,7215,4976,313,21679,15528198,47414,182237102,25714,72250536010613255,506,05014255,506,0511,039,496,297
15103277,664152188,59229,643,54335,672,472-345,337,202151,72314,599,312134,054,1112,43962,280,083--42,67070,102,536---1562,80318718,587,770-45,748-366,83215359,6574,74526,033,702167,3544617,70122,011119467,11836,920591895498127698,977,23815698,977,2397,859,514,470
1625277,365161132,30861,330,77319,666,678--166066,917,448426,100,6731,008174,249,373--47,344----1650,78547--45,289-720,24416554,3232,83391,547,012285,37511,553,11726,097361,732,47470,7604291,3531,493690775,315,96116775,315,96259,795,121,480
173223,7991771,842111,057,76013,974,047--171468,606,407-271---41,330----1730,7126--34,112-1,168,73417666,7341,173278,968,808395,3383,148,39722,88355,281,824106,4901721,2482,7092,343423,807,29317423,807,294
181145,8021830,717175,366,47117,035,290--18308,789,020-55---27,764----1814,2271--19,282-1,574,30718625,422324444,2765,145,03614,761113,285,053124,513457203,1925,097222,651,40718222,651,408
1977,2511910,165239,732,11917,348,172--1937,318,039-6---14,083----194,927--7,852-1,752,47019457,68064399,6186,753,5836,73027,576,667112,11152492,3567,235301,581,38519301,581,386
2033,413202,662281,562,99214,664,976--2014,964,070-1---5,279----201,335--2,347-1,599,76920262,7268283,0237,118,4272,20747,173,63175,9821551,1416,796357,760,84220357,760,843
2111,77221504282,313,76010,261,190--212,749,124----1,470----21252--479-1,178,61821118,4811154,4086,020,28649266,374,23738,30663354,201369,227,92221369,227,923
223,3562278241,008,3525,961,764--221,248,828----302----2242--77-689,7162242,22664,1494,099,2418376,711,62314,1101661,725329,845,73922329,845,740
23769236175,295,0262,883,576--23465,058----48----233--6-315,0742311,74519,9922,251,467872,803,4433,8036451254,050,48123254,050,482
24138241109,001,9741,165,728--24141,878----6----241--1-111,790242,5744,7051,002,112156,809,616733178168,241,33724168,241,338
25222558,119,482391,916--2534,891----1----25---30,49925415800360,60036,527,278105795,466,0162595,466,017
2642626,620,639109,373--266,873--------26---6,4362656105105,10819,406,65510146,255,2602646,255,261
2712710,455,81724,723--271,020--------27---1,004274824,3628,533,634119,040,5742719,040,575
28283,509,5844,541--28120--------28---12228114,5213,107,8216,626,711286,626,712
2929997,606619--298--------29---929619934,1941,933,055291,933,056
3030238,06972--301--------30---13072230,761468,97630468,977
313146,6384--31--------31---31446,11192,7573192,758
32327,4351--32--------3214,771--3217,40229,6103229,611
3333901--33--------331,783--339003,584333,585
343492--34--------34171--349035334354
35356--35--------359--355203521
36361--36-22,912------361--36122,9153622,916
3737--37-2,468------37--372,468372,469
3838--38-210------38--3821038211
3939--39-10------39--39103911
4040--40-1------40--401402
4141--4118,770------41--4118,7704118,771
4242--421,821------42--421,821421,822
4343--43142------43--4314243143
444424,473-449------44-48,7574473,2394473,240
45452,184-451------45-4,322456,507456,508
4646157-46--28,928---46-3004629,3854629,386
47477-47--2,455---47-12472,474472,475
48481-48--169---48-14817148172
4949-49--7---49-497498
5050-50--1---5039,7425039,7435039,744
5151-51-----513,125513,125513,126
5252-52-----522015220152202
5353-53-----5395395310
5454-54-----541541542
5555-55-----5555551
5656-56122,145----5656122,14556122,146
5757-578,582----57578,582578,583
5858-58466----585846658467
5959-5916----5959165917
606026,668601----606026,6696026,670
61611,74361----61611,743611,744
62629862----6262986299
6363463----63634635
6464164----64641642
total21062192411,0731,574,253total122,3421811,060,7901,825,354,3281,051103,765,02892,246,129414,300,200total3026,43294150,191,823608,395,95421,992262,315,62933,429,70512,493,211238,04887,597,8017,475,1522,844,37094,466total65298,2624,66227,608,9857,156,719228,01325,375,5669,735,647total1583,386,86223,69136238,465,884119,271875437,132,743603,7602,0644,98411,91428,764