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

时间:2019-11-22 16:00:19

标签: python math rotation angle euler-angles

我正在尝试将Euler角度转换为Python中的Axis Angle表示形式。我已经在以下网站上复制了该函数:https://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToAngle/,并将其翻译为Python。但是,他们在此处使用的Euler顺序为yzx,而我的为zxy,因此导致转换错误。

是否有任何Python软件包都可以以Euler级的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阶。

1 个答案:

答案 0 :(得分:0)

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

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)