python - 如何计算 3d 数组的所有 24 次旋转?

标签 python numpy

我有一个描述 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/

相关文章:

python - 如何在 Tkinter 中找出 PhotoImage 的大小?

python - 拆分 Pandas DF 将其转换为列表

python - 如何根据两列或多列条件将数据框分配给变量

python - 对 laspy 创建的 numpy 数组进行排序

python - 一个numpy数组的 View 是一个 View 的副本?

python - numpy:用不同的值填充偏移对角线

python - 获取错误 : jinja2. exception.TemplateAssertionError:没有名为 'basename' 的过滤器

python - 如何使用Python请求设置媒体类型?

python - 什么是 numpy.fft.rfft 和 numpy.fft.irfft 及其在 MATLAB 中的等效代码

numpy - Numpy `where` 子句的奇怪行为