How to calculate all 24 rotations of 3d array?

后端 未结 9 739
清酒与你
清酒与你 2021-02-12 10:45

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

9条回答
  •  北海茫月
    2021-02-12 10:59

    Look at the code for rot90. I see 3 variations on flip and swapaxes, depending on k the axis parameter.

    fliplr(m).swapaxes(0, 1)
    fliplr(flipud(m))
    fliplr(m.swapaxes(0, 1))
    

    fliplr(m) is just m[:, ::-1], and not surprisingly, flipud is m[::-1, ...].

    You could flip the 3rd axis with m[:,:,::-1], or m[...,::-1].

    np.transpose is another tool for permuting axes, that may, or may not, be easier to use than swapaxes.

    If rot90 gives you 4 of the rotations, you should be able apply the same routines to produce the others. You just have to understand the logic underlying rot90.

    e.g.

    def flipbf(m):
        return m[:,:,::-1]
    
    flipbf(m).swapaxes(0, 2)
    flipbf(m).swapaxes(1, 2)
    etc
    

提交回复
热议问题