I have a 3d numpy array describing a polycube (imagine a 3d tetris piece). How can I calculate all 24 rotations?
Numpy\'s array manipulation routines includes a rot90 me
Update: Numpy 1.12.0 added an axes argument to the rot90 function
Here's how I made all 24 rotations:
from numpy import rot90, array
def rotations24(polycube):
"""List all 24 rotations of the given 3d array"""
def rotations4(polycube, axes):
"""List the four rotations of the given 3d array in the plane spanned by the given axes."""
for i in range(4):
yield rot90(polycube, i, axes)
# imagine shape is pointing in axis 0 (up)
# 4 rotations about axis 0
yield from rotations4(polycube, (1,2))
# rotate 180 about axis 1, now shape is pointing down in axis 0
# 4 rotations about axis 0
yield from rotations4(rot90(polycube, 2, axes=(0,2)), (1,2))
# rotate 90 or 270 about axis 1, now shape is pointing in axis 2
# 8 rotations about axis 2
yield from rotations4(rot90(polycube, axes=(0,2)), (0,1))
yield from rotations4(rot90(polycube, -1, axes=(0,2)), (0,1))
# rotate about axis 2, now shape is pointing in axis 1
# 8 rotations about axis 1
yield from rotations4(rot90(polycube, axes=(0,1)), (0,2))
yield from rotations4(rot90(polycube, -1, axes=(0,1)), (0,2))
Test that all 24 rotations are indeed distinct:
polycube = array([[[1, 1, 0],
[1, 1, 0],
[0, 0, 0]],
[[0, 0, 0],
[1, 0, 0],
[1, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]])
assert len(set(str(x) for x in rotations24(polycube))) == 24