我有一个描述 polycube 的 3d numpy 数组(想象一个 3D 俄罗斯方 block )。如何计算所有 24 次旋转?
Numpy 的数组操作例程包括 rot90方法,它给出了 24 个中的 4 个,但我不知道如何计算其余的。我唯一的想法是将 3d 数组转换为 2d 坐标矩阵,乘以旋转矩阵,然后再转换回来。但我宁愿直接使用 3d 数组。
2x2x2 数组示例:
>>> from numpy import array
>>> polycube
array([[[1, 0],
[1, 0]],
[[1, 1],
[0, 0]]])
3x3x3 数组示例:
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]]])
编辑:我只想要 24 个保持方向的等距,而不是全部 48 个旋转和反射(尽管知道如何制作它们也会很有趣)。如果它有助于测试,我相信 3x3x3 示例没有旋转对称性并且是手性的(因此 48 是不同的)。
动机:我正在为 Soma cube 编写求解器-风格的谜题。
最佳答案
更新:在 Numpy 1.12.0 添加轴参数到 rot90 function 后进行了简化
这是我完成所有 24 次旋转的方法:
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))
测试所有 24 次旋转确实是不同的:
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
关于python - 如何计算 3d 数组的所有 24 次旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33190042/