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

后端 未结 4 1330
既然无缘
既然无缘 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:16

    A die (half a pair of dice) is handy for observing the 24 different orientations, and can suggest operation sequences to generate them. You will see that any of six faces can be uppermost, and the sides below can be rotated into four different cardinal directions. Let us denote two operations: “turn” and “roll”, where turn rotates the die about the z axis from one cardinal to the next, and roll rotates the die 90° away from you, so the away-face becomes the bottom face and the near face the top. These operations can be expressed using rotation matrices as mentioned in the answer of Felipe Lopes, or can be expressed as simple functions that when given (x,y,z) return (-y,x,z) or (x,z,-y), respectively.

    Anyhow, if you place the die with 1 on the near face, 2 at right, and 3 on top, you will find that the following sequence of steps generates the twelve different orientations with 1, 2, or 3 spots on top: RTTTRTTTRTTT. Then the sequence RTR exposes 6, 4, 5 where 1, 2, 3 originally were, and a repeat of the sequence RTTTRTTTRTTT generates the twelve orientations with 4, 5, or 6 spots on top. The mentioned sequence is embedded in the following python code.

    def roll(v): return (v[0],v[2],-v[1])
    def turn(v): return (-v[1],v[0],v[2])
    def sequence (v):
        for cycle in range(2):
            for step in range(3):  # Yield RTTT 3 times
                v = roll(v)
                yield(v)           #    Yield R
                for i in range(3): #    Yield TTT
                    v = turn(v)
                    yield(v)
            v = roll(turn(roll(v)))  # Do RTR
    
    p = sequence(( 1, 1, 1))
    q = sequence((-1,-1, 1))
    for i in sorted(zip(p,q)):
        print i
    

    The rationale for printing out a sorted list of transformed pairs of points is twofold: (i) any face orientation can be specified by the locations of two of its corners; (ii) it then is easy to check for uniqueness of each pair, eg by piping output to uniq.

    Here is how the sorted output begins:

    ((-1, -1, -1), (-1, 1, 1))
    ((-1, -1, -1), (1, -1, 1))
    ((-1, -1, -1), (1, 1, -1))
    ((-1, -1, 1), (-1, 1, -1))
    ((-1, -1, 1), (1, -1, -1))
    ((-1, -1, 1), (1, 1, 1))
    ((-1, 1, -1), (-1, -1, 1))
    ((-1, 1, -1), (1, -1, -1))
    ((-1, 1, -1), (1, 1, 1))
    

提交回复
热议问题