[Python] Inconsistent conversion from euler to quaternions and back

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to debug my code but im confused why converting from euler angles to quaternions and back is not giving me consistent results. Can someone help ?

    import numpy as np
    from scipy.spatial.transform import Rotation as R

    # Define test rotation in degrees
    testrotation = np.zeros([2, 2, 3])
    testrotation[0, 0] = [45, 30, 60]
    testrotation[0, 1] = [10, 20, 30]
    testrotation[1, 0] = [90, 0, 45]
    testrotation[1, 1] = [90, 45, 30]

    #################### Euler -> Quaternions using SciPy
    scipy_quaternions_temp = R.from_euler("ZYX", np.radians(testrotation.reshape(-1, 3)))
    scipy_quaternions = np.roll(scipy_quaternions_temp.as_quat(), shift=1, axis=-1).reshape(testrotation.shape[0], testrotation.shape[1], 4)

    print("Quaternions (wxyz format):\n", scipy_quaternions, "\n")

    #################### Quaternions -> Euler using SciPy
    scipy_euler_angles = np.rad2deg(scipy_quaternions_temp.as_euler('zyx', degrees=False)).reshape(testrotation.shape[0], testrotation.shape[1], 3)

    print("Euler angles after conversion and normalization:\n", scipy_euler_angles, "\n")

    #################### Euler -> Quaternions (back conversion) using SciPy
    scipy_quat_reconverted = R.from_euler('zyx', np.radians(scipy_euler_angles.reshape(-1, 3))).as_quat().reshape(testrotation.shape[0], testrotation.shape[1], 4)
    scipy_quat_reconverted = np.roll(scipy_quat_reconverted, shift=1, axis=-1)

    print("Reconverted quaternions (wxyz format):\n", scipy_quat_reconverted, "\n")
    sys.exit()


    You can see that the euler angles I get back are not the same.

    Quaternions (wxyz format):
    [[[ 0.82236317 0.36042341 0.39190384 0.20056212]
    [ 0.95154852 0.23929834 0.18930786 0.03813458]]

    [[ 0.65328148 0.27059805 0.27059805 0.65328148]
    [ 0.70105738 -0.09229596 0.43045933 0.56098553]]]

    Euler angles after conversion and normalization:
    [[[ 4.42303689e+00 5.21060674e+01 4.51703838e+01]
    [-1.11605468e+00 2.22421809e+01 2.84517753e+01]]

    [[ 9.00000000e+01 4.50000000e+01 1.27222187e-14]
    [ 9.00000000e+01 3.00000000e+01 -4.50000000e+01]]]

    Reconverted quaternions (wxyz format):
    [[[ 0.82236317 0.36042341 0.39190384 0.20056212]
    [ 0.95154852 0.23929834 0.18930786 0.03813458]]

    [[ 0.65328148 0.27059805 0.27059805 0.65328148]
    [ 0.70105738 -0.09229596 0.43045933 0.56098553]]]

    Continue reading...

Compartilhe esta Página