【问题标题】:Imprecision with rotation matrix to align a vector to an axis使用旋转矩阵将向量与轴对齐的不精确性
【发布时间】:2017-04-19 23:03:22
【问题描述】:

我已经用这个撞墙了几个小时,我似乎无法弄清楚我做错了什么。

我正在尝试生成一个旋转矩阵,它将向量与特定轴对齐(我最终将转换更多数据,因此拥有旋转矩阵很重要)。

我觉得我的方法是对的,如果我在各种向量上测试它,它效果很好,但是转换后的向量总是有点偏离 .

这是我用来测试该方法的完整代码示例:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
import matplotlib as mpl


def get_rotation_matrix(i_v, unit=None):
    # From http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q38
    if unit is None:
        unit = [1.0, 0.0, 0.0]
    # Normalize vector length
    i_v = np.divide(i_v, np.sqrt(np.dot(i_v, i_v)))
    # Get axis
    u, v, w = np.cross(i_v, unit)
    # Get angle
    phi = np.arccos(np.dot(i_v, unit))
    # Precompute trig values
    rcos = np.cos(phi)
    rsin = np.sin(phi)
    # Compute rotation matrix
    matrix = np.zeros((3, 3))
    matrix[0][0] = rcos + u * u * (1.0 - rcos)
    matrix[1][0] = w * rsin + v * u * (1.0 - rcos)
    matrix[2][0] = -v * rsin + w * u * (1.0 - rcos)
    matrix[0][1] = -w * rsin + u * v * (1.0 - rcos)
    matrix[1][1] = rcos + v * v * (1.0 - rcos)
    matrix[2][1] = u * rsin + w * v * (1.0 - rcos)
    matrix[0][2] = v * rsin + u * w * (1.0 - rcos)
    matrix[1][2] = -u * rsin + v * w * (1.0 - rcos)
    matrix[2][2] = rcos + w * w * (1.0 - rcos)
    return matrix

# Example Vector
origv = np.array([0.47404573,  0.78347482,  0.40180573])

# Compute the rotation matrix
R = get_rotation_matrix(origv)

# Apply the rotation matrix to the vector
newv = np.dot(origv.T, R.T)

# Get the 3D figure
fig = plt.figure()
ax = fig.gca(projection='3d')

# Plot the original and rotated vector
ax.plot(*np.transpose([[0, 0, 0], origv]), label="original vector", color="r")
ax.plot(*np.transpose([[0, 0, 0], newv]), label="rotated vector", color="b")

# Plot some axes for reference
ax.plot([0, 1], [0, 0], [0, 0], color='k')
ax.plot([0, 0], [0, 1], [0, 0], color='k')
ax.plot([0, 0], [0, 0], [0, 1], color='k')

# Show the plot and legend
ax.legend()
plt.show()

我已链接找到方法here。为什么它产生的变换总是稍微偏离一点???

【问题讨论】:

  • 你需要规范u, v, w
  • 哇...这么简单的解决方案。我不知道我是怎么错过的。非常感谢!

标签: python numpy rotation linear-algebra rotational-matrices


【解决方案1】:

您需要规范uvw 才能正常工作。所以替换

u, v, w = np.cross(i_v, unit)

uvw = np.cross(i_v, unit)
uvw /= np.linalg.norm(uvw)

这与您已经拥有的i_v = np.divide(i_v, np.sqrt(np.dot(i_v, i_v))) 行基本相同。

你可以做得更好,完全避免触发:

def get_rotation_matrix(i_v, unit=None):
    # From http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q38
    if unit is None:
        unit = [1.0, 0.0, 0.0]
    # Normalize vector length
    i_v /= np.linalg.norm(i_v)

    # Get axis
    uvw = np.cross(i_v, unit)

    # compute trig values - no need to go through arccos and back
    rcos = np.dot(i_v, unit)
    rsin = np.linalg.norm(uvw)

    #normalize and unpack axis
    if not np.isclose(rsin, 0):
        uvw /= rsin
    u, v, w = uvw

    # Compute rotation matrix - re-expressed to show structure
    return (
        rcos * np.eye(3) +
        rsin * np.array([
            [ 0, -w,  v],
            [ w,  0, -u],
            [-v,  u,  0]
        ]) +
        (1.0 - rcos) * uvw[:,None] * uvw[None,:]
    )

最后一个表达式是维基百科页面中的这个等式:

【讨论】:

  • 这与我最初在算法中的结果非常接近,但我猜我的 uvw 仍然不正确 - 所以我在调试时得到了另一个解决方案。我肯定会使用更快的解决方案。再次感谢!
  • 请注意,像这样组合矩阵可能并不比逐元素更快——我只是为了清楚起见。不过避免arccos 是值得的,因为那样和cos 一起会失去精度。
  • 另外,rsin == 0 时应该跳过规范化步骤
  • 看来uvw的计算不正确。它应该是 uvw=np.cross(unit,i_v) 而不是相反。现在有什么产生相反的角度。我一直在用 3D 体积幻象把头撞在墙上。随着我概述的更改,它对我有用。
  • @kabammi:实际上,我的错误在于[u]_x 矩阵的转录,但它们本质上是等价的。好收获!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2015-12-05
相关资源
最近更新 更多