How to calculate all 24 rotations of 3d array?

后端 未结 9 748
清酒与你
清酒与你 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 11:24

    There are 24 rotation matrices at the bottom of this page: http://www.euclideanspace.com/maths/algebra/matrix/transforms/examples/index.htm .

    If a tetris piece were represented by an array of coordinate triples, to apply a rotation to the piece you would just matrix multiply the matrix by each of the triples in the array. You would do that 24 times to get the 24 different pieces.

    For example, if your array of boxes is (1,2,5), (1,2,4), (1,3,4)

    And the rotation you are considering at the moment is for example

        1 0  0
    M = 0 0 -1
        0 1  0
    

    Then the rotated figure has representation

    (1,2,5).M, (1,2,4).M, (1,3,4).M
    

    where the . represents matrix multiplication. Do that for each M in the list and you've got all 24 rotations. (You can either pre- or post- multiply by the matrices, it doesn't matter if you want the whole set of rotations in no particular order.)

    So that is very straightforward.

    Now, to get the data out of your data structure and into the array structure I have used above, you would have to do something like (sorry I don't know Python)

    for i = 0 to 2
      for j = 0 to 2 
        for k = 0 to 2
          if polycube(i,j,k)==1 then push(i-1,j-1,k-1) onto end of newArray
    

    or something like that. Finally, to go from the newArray representation to the polycube you would do something like

      polycube = all zeros
        foreach point (i,j,k) in newArray
          polycube(i+1,j+1,k+1) = 1
    

    For a 2x2x2 polycube, you would do

    for i = 0 to 1
      for j = 0 to 1 
        for k = 0 to 1
          if polycube(i,j,k)==1 then push(i-0.5,j-0.5,k-0.5) onto end of newArray
    

    and the corresponding inverse function.

提交回复
热议问题