How to calculate all 24 rotations of 3d array?

后端 未结 9 746
清酒与你
清酒与你 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:18

    I encountered a similar problem when trying to randomly rotate a 3D one hot encoded numpy array, just before feeding into my neural network. I will demonstrate here for a 3D array, but this also works with a 4th dimension (when using OHE).

    >>> m = np.reshape(np.arange(8),(2,2,2))
    >>> m
    array([[[0, 1],
            [2, 3]],
    
           [[4, 5],
            [6, 7]]])
    

    Next we rotate the array 3 times, each time in a different direction. Repeat 24,000 times to see distribution (expecting 1000 counts for each unique rotation):

    >>> rot_list = []
    >>> for _ in range(24000):
            a = np.rot90(m,np.random.randint(0,4),axes=(0,np.random.randint(1,3)))
            b = np.rot90(a,np.random.randint(0,4),axes=(np.random.randint(1,3),0))
            c = np.rot90(b,np.random.randint(0,4),axes=(1,2)) # or axes=(2,1)
            rot_list.append(c)
    >>> unique_rotation_matrices, counts = np.unique(np.asarray(rot_list),axis=0, return_counts=True)
    >>> len(unique_rotation_matrices)
    24
    

    So we see we get all the 24 possible rotations. Let's look at their distribution:

    >>> counts
    [1062  946  917  982 1096  978 1153  936  939  907 1183  932  958  932 1122
      926 1115  954  933  932 1135  924 1138  900]
    

    Distribution looks quite even, but rerunning this a number of times reveals it is slightly biased.

提交回复
热议问题