【问题标题】:How do you convert Euler angles to the Axis Angle representation in Python?如何将欧拉角转换为 Python 中的轴角表示?
【发布时间】:2020-03-18 17:39:07
【问题描述】:

我正在尝试将欧拉角转换为 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 顺序。

【问题讨论】:

  • 你能提供输入和预期的输出吗?请给我们一个例子
  • 我加了一个例子!

标签: python math rotation angle euler-angles


【解决方案1】:

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

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)

【讨论】:

  • 您以错误的顺序返回输出轴。但我想正确的结果可以通过在最后抛出变量来实现。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多