【问题标题】:How to find the rotation matrix of 3 orthogonal vectors in space. My current method rotates the vectors to the wrong orientation如何在空间中找到 3 个正交向量的旋转矩阵。我当前的方法将矢量旋转到错误的方向
【发布时间】:2020-10-17 20:41:46
【问题描述】:

我正在寻找使三个(几乎)正交向量处于世界坐标系相同方向的旋转矩阵。

我的三个(几乎)正交向量在 python 中可以这样表示:

vectors = np.array([[ 0.43187079,  0.90161148,  0.02417362],
   [-0.46076794,  0.19750816,  0.86526495],
   [ 0.77535832, -0.38482109,  0.50073167]])

我目前使用的代码可以使向量与世界坐标平行,但方向不正确。运行这段代码,

xrotation = np.arctan2(vectors[2, 1], vectors[2, 2])
xRot = np.array([[1, 0, 0],
                 [0, np.cos(xrotation), -np.sin(xrotation)],
                 [0, np.sin(xrotation), np.cos(xrotation)]])
vectors_x = np.zeros((3, 3))
for i in range(3):
    vectors_x[i, :] = np.linalg.inv(xRot.transpose()) @ vectors[i, :]
yrotation = np.arctan2(vectors_x[1, 2], vectors_x[1, 0])
yRot = np.array([[np.cos(yrotation), 0, np.sin(yrotation)],
                 [0, 1, 0],
                 [-np.sin(yrotation), 0, np.cos(yrotation)]])
vectors_y = np.zeros((3, 3))
for i in range(3):
    vectors_y[i, :] = np.linalg.pinv(yRot.transpose()) @ vectors_x[i, :]

zrotation = np.arctan2(vectors_y[0, 0], vectors_y[0, 1])
zRot = np.array([[np.cos(zrotation), -np.sin(zrotation), 0],
                 [np.sin(zrotation), np.cos(zrotation), 0],
                 [0, 0, 1]])
vectors_z = np.zeros((3, 3))
for i in range(3):
    vectors_z[i, :] = np.linalg.pinv(zRot.transpose()) @ vectors_y[i, :]

给出三个旋转的正交向量:

>vectors_z
>array([[-1.11022302e-16,  1.00000000e+00,  3.19660393e-09],
       [ 1.00000000e+00, -3.70417658e-09, -2.77555756e-16],
       [ 2.12261116e-09, -1.98949113e-09, -1.00000000e+00]])

我需要在代码中进行哪些更改才能使其处于正确的方向,如下所示:

array([[ 1, 0, 0],
       [ 0, 1, 0],
       [ 0, 0, 1]])

我知道可以通过以正确的顺序将矢量旋转 90/180 度来实现这一点,但必须通过在上面的代码中执行其他操作来获得更有效的方法。

感谢您的宝贵时间!!!

【问题讨论】:

    标签: python numpy linear-algebra coordinate-systems euler-angles


    【解决方案1】:

    想通了。切换到ZYZ旋转模式,重做欧拉角计算方法。希望有一天这对某人有所帮助。

    import numpy as np
    
    def z_rotation(zrotation):
        z1Rot = np.array([[np.cos(zrotation), -np.sin(zrotation), 0],
                          [np.sin(zrotation), np.cos(zrotation), 0],
                          [0, 0, 1]])
        return z1Rot
    
    def y_rotation(yrotation):
        yRot = np.array([[np.cos(yrotation), 0, np.sin(yrotation)],
                         [0, 1, 0],
                         [-np.sin(yrotation), 0, np.cos(yrotation)]])
        return yRot
    
    def forward_rotation(Rot,vectors_in):
        vectors = np.zeros((3, 3))
        for i in range(3):
            vectors[i, :] = vectors_in[i, :] @ Rot
        return vectors
    
    
    def reverse_rotation(Rot, vectors_in):
        vectors = np.zeros((3, 3))
        for i in range(3):
            vectors[i, :] = np.linalg.pinv(Rot.transpose()) @ vectors_in[i, :]
        return vectors
    
    org_vectors = np.array([[1,0,0],[0,1,0],[0,0,1]])
    
    z1_angle = (-.5 + np.random.random()) * 1800
    y_angle = (-.5 + np.random.random()) * 1800
    z2_angle = (-.5 + np.random.random()) * 1800
    
    z1 = z1_angle*np.pi/180
    y = y_angle*np.pi/180
    z2 = z2_angle*np.pi/180
    
    z1Rot = z_rotation(z1)
    z1vectors = forward_rotation(z1Rot, org_vectors)
    
    
    yRot = y_rotation(y)
    yvectors = forward_rotation(yRot, z1vectors)
    
    z2Rot = z_rotation(z2)
    z2vectors = forward_rotation(z2Rot, yvectors)
    
    z2angle_calc = np.arctan2(z2vectors[2,1],z2vectors[2,0])
    z2rot_2 = z_rotation(z2angle_calc)
    
    new_y = forward_rotation(z2rot_2, z2vectors)
    
    yangle_2 = np.arctan2(new_y[2,0],new_y[2,2])
    yrot_2 = y_rotation(yangle_2)
    
    new_z1 = forward_rotation(yrot_2, new_y)
    
    
    z1angle_2 = yangle_2 = np.arctan2(new_z1[0,1],new_z1[0, 0])
    z1rot_2 = z_rotation(z1angle_2)
    
    new_org_vectors = forward_rotation(z1rot_2, new_z1)
    print(new_org_vectors)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多