python - python 中的 Euler–Rodrigues 公式未返回预期的旋转值

标签 python python-2.7 math euler-angles

我有一个函数可以根据 Python 中的 Euler-Rodrigues 公式生成旋转矩阵。但是,当我将数组旋转半个 pi 时,它不会与原始向量成 90 度,我不确定为什么。如果我按 pi 或 2pi 旋转,那么我会得到预期的旋转。代码如下。

# This is my function for making the rotation matrix
def RotationMatrix(axis, theta):
    """
    This uses Euler-Rodrigues formula.
    """
    axis = np.asarray(axis)
    axis = axis / math.sqrt(np.dot(axis, axis))
    a = math.cos(theta / 2)
    b, c, d = -axis * math.sin(theta / 2)
    a2, b2, c2, d2 = a * a, b * b, c * c, d * d
    bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
    return np.array([
        [a2 + b2 - c2 - d2, 2 * (bc - ad), 2 * (bd + ac)],
        [2 * (bc + ad), a2 + c2 - b2 - d2, 2 * (cd - ab)],
        [2 * (bd - ac), 2 * (cd + ab), a2 + d2 - b2 - c2]
    ])

#Here I call the function to provide a rotation matrix 
#that should rotate by 90 degrees
x = RotationMatrix((0, 1, 0), (math.pi * .5))

print(x)
#Here I define my test vector to rotate
vector = np.array((3, 4, 0))

print(vector)
# Here I apply the rotation matrix (this will become a function 
# one day)
a1 = vector[0] * x[0,0] + vector[0] * x[0,1] + vector[0] * x[0,2]
b1 = vector[1] * x[1,0] + vector[1] * x[1,1] + vector[1] * x[1,2]
c1 = vector[2] * x[2,0] + vector[2] * x[2,1] + vector[2] * x[2,2]

appliedrotation = np.array((a1, b1, c1))

print(appliedrotation)

#below here I just get the dot product and magnitude so I 
#can calculate the rotation in degrees
dp = np.dot(vector, appliedrotation)
print(dp)
maga = math.sqrt(vector[0] ** 2 + vector[1] ** 2 + vector[2] ** 2)
magb =  math.sqrt(
    appliedrotation[0] ** 2 + appliedrotation[1] ** 2 + appliedrotation[2]
)
magc = maga * magb
hmm = dp / magc
hmm1 = ((math.acos(hmm)) * 180) / math.pi
print(hmm1)

有可能我只是忘记了我的 A-level 数学矢量知识,当在三个维度上旋转时,我不应该在半 pi 处发生 90 度的变化,但我正在努力解决这个问题。任何帮助将不胜感激。

最佳答案

您手动计算的点积在这里是错误的:

# Here I apply the rotation matrix (this will become a function one day)
a1 = vector[0] * x[0,0] + vector[0] * x[0,1] + vector[0] * x[0,2]
b1 = vector[1] * x[1,0] + vector[1] * x[1,1] + vector[1] * x[1,2]
c1 = vector[2] * x[2,0] + vector[2] * x[2,1] + vector[2] * x[2,2]

您希望结果的第一个组成部分是 x 的第一行,点缀有 vector,这意味着

a1 = vector[0] * x[0,0] + vector[1] * x[0,1] + vector[2] * x[0,2]

你实际上拥有的是某种类型的 x 行的加权和,使用 vector 的相应条目作为权重,这是不正确的。

您可以通过使用 np.dot 而不是手动矩阵向量乘法简单地计算它来看到这一点:

In [17]: x
Out[17]: 
array([[  2.22044605e-16,   0.00000000e+00,  -1.00000000e+00],
       [  0.00000000e+00,   1.00000000e+00,   0.00000000e+00],
       [  1.00000000e+00,   0.00000000e+00,   2.22044605e-16]])

In [18]: vector
Out[18]: array([3, 4, 0])

In [19]: np.dot(x, vector)
Out[19]: array([  6.66133815e-16,   4.00000000e+00,   3.00000000e+00])

这是 (0, 4, 3),正如预期的那样。

想象绘制矢量 (3, 4),在平面的正象限中指向上方和右侧(因为原始 z 坐标为 0)。如果您围绕 y 轴旋转 90 度,则矢量会从页面向您旋转,直到之前沿 x 轴的 3 分量现在沿 z 轴(而 y 分量保持不变) ),这意味着您现在位于向量 (0, 4, 3) 处。

关于python - python 中的 Euler–Rodrigues 公式未返回预期的旋转值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534947/

相关文章:

python - 从 Python 中的字符列表中形成字符串列表

java - 如何从Java中的其他给定点找到最远的点

javascript - 如何使用 JSXgraph 在曲线内部着色

python - 如何避免在 python 中覆盖文件?

python - 有什么方法可以正确打印 OrderedDict?

python - 卸载不同版本的python

JavaScript 无法检测两个 Div 之间的碰撞轴

python - 推送被拒绝,无法在heroku中编译Python应用程序(Python速成类(class))

python - Python 中 except 语句的排序

python - 如何仅设置 pandas 数据框最后一行的样式?