How to get all 24 rotations of a 3-dimensional array?

后端 未结 4 1317
既然无缘
既然无缘 2021-01-30 18:34

I have a 3-dimensional array. Think of it as a brick. There are 24 possible rotations of this brick (that keep its edges parallel to coordinate axes). How do I generate all corr

4条回答
  •  一生所求
    2021-01-30 19:36

    You can use rotation matrices. Rotating a 3D array around the x-axis means that the element at position (i,j,k) will be mapped to position (i,-k,j). Of course, if your array is 0-indexed, you probably have to replace -k with size-1-k or something like that.

    Similarly, rotating around the y-axis maps (i,j,k) to (k,j,-i). These two rotations can be represented as matrices. For the x-axis rotation:

    |i'|   |1  0  0| |i|
    |j'| = |0  0 -1|*|j|
    |k'|   |0  1  0| |k|
    

    And for the y-axis rotation:

    |i'|   |0  0  1| |i|
    |j'| = |0  1  0|*|j| 
    |k'|   |-1 0  0| |k|
    

    Any general rotation can be described as a sequence of those two rotations. Applying two rotations consecutively is just multiplying the 3x3 matrices. So, if you find all possible products of them, you'd get 24 matrices (including the identity), each one corresponds to a valid rotation of your array. It's a little tricky to find all possible multiplications, because they don't commute.

    I think you can just brute-force all products of the form (A^p)*(B^q)*(A^r)*(B^s), where A and B are the two matrices before and p,q,r,s are their powers, and range from 0 to 3 (exponentiating A or B to 4 will take them back to the identity matrix).

    Doing it this way, you can generate all 24 valid rotation matrices, and rotate the 3D array using each one of them, taking the care to shift the negative indexes so that you don't access out of bounds.

提交回复
热议问题