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.

nb2x2x12x2x23x2x13x2x23x3x13x3x23x3x3nb4x2x14x2x24x3x14x3x24x3x34x4x14x4x24x4x34x4x4nb5x2x15x2x25x3x15x3x25x3x35x4x15x4x25x4x35x4x45x5x15x5x25x5x35x5x45x5x5nb6x2x16x2x26x3x16x3x26x3x36x4x16x4x26x5x1nb7x2x17x2x27x3x18x2x18x3x19x2x19x3x110x2x111x2x112x2x113x2x1totalnb
31333313
4133444474
52215653555285
6314175966281565661656
71617298118721223926918711462575710227
815838699138132459219291277393810287965376181967358769218
92911666443895214295611128718143933007931049210572120833831715739225421889219792893497483109
101223881492210602212928177502266274855072936861012611255334303274813042466614942529212710152608550123004041822328624010529333324063934479810
1122450395461142846789337636525112161547064081423114678212138963286847284719612431263029970241336595206271138630954894887749435485682623661145547412319052953819243934411
12118548494812225112442814445251684218013113306989811126041103448958179389344411124222357763479921273753570295080508863112121710118446775381983710323544687131611221227442800119240615880069111680444112
139671519921369181051460072666119412416366444858036513554433117541789222065008510830729393289116640291704125397066946475275573994466134094996419231006212269219953777502511331347285346347368182574313238861111182026313
14390226020142020924012533971202830075722151815927919914366762546700-416819305538--3032014528248---14586695466510918-360352093987315312214118272154972814182237147225053601061321163029814
151032776641521885922964354335672472-34533720215172314599312-2439---42670----156280318718587770-45748-3668321535965747454220111193692059189549812740525463315
16252773651611323086133077319666678--166066917448-1008---47344----165078547--45289--1655432328331260973670760429135314936907912769316
173223799177184211105776013974047--171468606407-271---41330----17307126--34112--17666734117322883510649017212482709234313484419217
1811458021830717175366471---18308789020-55---27764----18142271--19282--18625422324147611124513457203192509718516744518
19772511910165239732119---1937318039-6---14083----194927--7852--1945768064673011211152492356723524775087519
2033413202662281562992---2014964070-1---5279----201335--2347--2026272682207759821551141679628692101620
211177221504----212749124----1470----21252--479--2111848114923830663354201292542321
2233562278----221248828----302----2242--77--224222683141101661725131089422
23769236----23465058----48----233--6--231174583803645148190323
24138241----24141878----6----241--1--242574173317814541224
252225----2534891----1----25----2541510573544125
26426----266873--------26---643626561011338026
27127----271020--------27---10042741203027
2828-4541--28120--------28---122281478428
2929-619--298--------29---92963629
3030-72--301--------30---1307430
3131-4--31--------31---31431
323274351--32--------3214771--322220732
3333901--33--------331783--33268433
343492--34--------34171--3426334
35356--35--------359--351535
36361--36-22912------361--362291436
3737--37-2468------37--37246837
3838--38-210------38--3821038
3939--39-10------39--391039
4040--40-1------40--40140
4141--4118770------41--411877041
4242--421821------42--42182142
4343--43142------43--4314243
444424473-449------44-48757447323944
45452184-451------45-432245650745
4646157-46--28928---46-300462938546
47477-47--2455---47-1247247447
48481-48--169---48-14817148
4949-49--7---49-49749
5050-50--1---5039742503974350
5151-51-----51312551312551
5252-52-----522015220152
5353-53-----53953953
5454-54-----54154154
5555-55-----555555
5656-56122145----565612214556
5757-578582----5757858257
5858-58466----585846658
5959-5916----59591659
606026668601----60602666960
6161174361----6161174361
62629862----62629862
6363463----6363463
6464164----6464164
total210621924110731574253total122342181106079091774738110513391832092246129414300200total3026432941501918231105852021992257861733342970512493211238048174952657475152284437094466total65298262466227608985715671922801325375566594426total158338686223691362119271875603760206449841191428764