如何将欧拉角转换为 Python 中的轴角表示法?

How do you convert Euler angles to the Axis Angle representation in Python?

我正在尝试将欧拉角转换为 Python 中的轴角表示。我已经复制了这个网站上的功能:https://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToAngle/并翻译成Python。但是他们这里使用的欧拉阶是yzx,而我的是zxy,所以会导致转换不正确

是否有任何 Python 包可以使用 zxy 的欧拉阶进行这种转换,或者有人可以为我提供这种转换的伪代码吗?

我的代码目前看起来像这样(所以我想创建一个名为 "euler_zxy_to_axis_angle" 的函数,而不是我现在的函数)。

def euler_yzx_to_axis_angle(y_euler, z_euler, x_euler, normalize=True):
    # Assuming the angles are in radians.
    c1 = math.cos(y_euler/2)
    s1 = math.sin(y_euler/2)
    c2 = math.cos(z_euler/2)
    s2 = math.sin(z_euler/2)
    c3 = math.cos(x_euler/2)
    s3 = math.sin(x_euler/2)
    c1c2 = c1*c2
    s1s2 = s1*s2
    w = c1c2*c3 - s1s2*s3
    x = c1c2*s3 + s1s2*c3
    y = s1*c2*c3 + c1*s2*s3
    z = c1*s2*c3 - s1*c2*s3
    angle = 2 * math.acos(w)
    if normalize:
        norm = x*x+y*y+z*z
        if norm < 0.001:
            # when all euler angles are zero angle =0 so
            # we can set axis to anything to avoid divide by zero
            x = 1
            y = 0
            z = 0
        else:
            norm = math.sqrt(norm)
            x /= norm
            y /= norm
            z /= norm
    return x, y, z, angle

所以我想做的一个例子是将欧拉角转换为 zxy 阶,其中 z=1.2, x=1.5, y=1.0 为正确的角度轴表示,在本例中为 axis = [ 0.3150331, 0.6684339, 0.6737583], angle = 2.4361774。 (根据 https://www.andre-gaschler.com/rotationconverter/)。

目前我的函数正在返回 axis=[ 0.7371612, 0.6684339, 0.098942 ] angle = 2.4361774,因为它将欧拉角解释为具有 yzx 顺序。

所以在弄乱了数字之后,我重新分配了等式中的值并得到了

import math

def euler_yzx_to_axis_angle(z_e, x_e, y_e, normalize=True):
    # Assuming the angles are in radians.
    c1 = math.cos(z_e/2)
    s1 = math.sin(z_e/2)
    c2 = math.cos(x_e/2)
    s2 = math.sin(x_e/2)
    c3 = math.cos(y_e/2)
    s3 = math.sin(y_e/2)
    c1c2 = c1*c2
    s1s2 = s1*s2
    w = c1c2*c3 - s1s2*s3
    x = c1c2*s3 + s1s2*c3
    y = s1*c2*c3 + c1*s2*s3
    z = c1*s2*c3 - s1*c2*s3
    angle = 2 * math.acos(w)
    if normalize:
        norm = x*x+y*y+z*z
        if norm < 0.001:
            # when all euler angles are zero angle =0 so
            # we can set axis to anything to avoid divide by zero
            x = 1
            y = 0
            z = 0
        else:
            norm = math.sqrt(norm)
            x /= norm
            y /= norm
            z /= norm
    return z, x, y, angle
print(euler_yzx_to_axis_angle(1.2, 1.5, 1.0))

其输出为

(0.31503310585743804, 0.668433885385261, 0.6737583269114973, 2.4361774412758335)