简体   繁体   English

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

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

I am trying to convert Euler angles to the Axis Angle representation in Python.我正在尝试将欧拉角转换为 Python 中的轴角表示。 I have already copied the function on this website: https://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToAngle/ and translated it to Python.我已经在这个网站上复制了 function: https://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToAngle/并翻译成 ZA7F5F7F35426B62821B52821B However, the Euler order they use here is yzx, while mine is zxy, so this leads to incorrect conversion.但是,他们这里使用的欧拉顺序是 yzx,而我的是 zxy,所以这会导致错误的转换。

Are there any Python packages that can do this conversion with an Euler order of zxy or can someone supply me with the pseudocode for this transformation?是否有任何 Python 包可以使用 zxy 的欧拉顺序进行此转换,或者有人可以为我提供此转换的伪代码吗?

My code currently looks like this (so I want to make a function called "euler_zxy_to_axis_angle" instead of the one I have now).我的代码目前看起来像这样(所以我想制作一个名为“euler_zxy_to_axis_angle”的 function 而不是我现在拥有的那个)。

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

So an example of what I want to do would be convert Euler angles with the order zxy, where z=1.2, x=1.5, y=1.0 to the correct angle-axis representation, which in this case would be axis = [ 0.3150331, 0.6684339, 0.6737583], angle = 2.4361774 .因此,我想要做的一个例子是将欧拉角转换为 zxy 阶,其中z=1.2, x=1.5, y=1.0为正确的角轴表示,在这种情况下axis = [ 0.3150331, 0.6684339, 0.6737583], angle = 2.4361774 (According to https://www.andre-gaschler.com/rotationconverter/ ). (根据https://www.andre-gaschler.com/rotationconverter/ )。

Currently my function is returning axis=[ 0.7371612, 0.6684339, 0.098942 ] angle = 2.4361774 , since it is interpreting the Euler angles as having an yzx order.目前我的 function 正在返回axis=[ 0.7371612, 0.6684339, 0.098942 ] angle = 2.4361774 ,因为它将欧拉角解释为具有 yzx 顺序。

So after messing around with the numbers I reassigned values in the equation and got因此,在弄乱了数字之后,我重新分配了方程式中的值并得到了

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))

the output of which is其中的 output 是

(0.31503310585743804, 0.668433885385261, 0.6737583269114973, 2.4361774412758335) (0.31503310585743804, 0.668433885385261, 0.6737583269114973, 2.4361774412758335)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM