Search code examples
pythonrotationquaternions

How to calculate the quaternions of all 24 rotations of a cube?


Similar to these two previous questions (How to calculate all 24 rotations of 3d array?, How to get all 24 rotations of a 3-dimensional array?), I want to find the all the 90 degree rotations of an object. However, I need the quaternions of those rotations.


Solution

  • import numpy as np
    import itertools
    from pyquaternion import Quaternion
    
    
    def rotations():
        for x, y, z in itertools.permutations([0, 1, 2]):
            for sx, sy, sz in itertools.product([-1, 1], repeat=3):
                rotation_matrix = np.zeros((3, 3))
                rotation_matrix[0, x] = sx
                rotation_matrix[1, y] = sy
                rotation_matrix[2, z] = sz
                if np.linalg.det(rotation_matrix) == 1:
                    quat = Quaternion(matrix=rotation_matrix)
                    yield quat.elements
    
    
    all_rotations = list(rotations())
    
    print(len(all_rotations))
    for x in all_rotations:
        print(x)
    

    Based on this answer by Igor Kołakowski of a similar question.