Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一个三维坐标系旋转到另一个三维坐标系_Python_Python 3.x_3d_Linear Algebra_Coordinate Transformation - Fatal编程技术网

Python 将一个三维坐标系旋转到另一个三维坐标系

Python 将一个三维坐标系旋转到另一个三维坐标系,python,python-3.x,3d,linear-algebra,coordinate-transformation,Python,Python 3.x,3d,Linear Algebra,Coordinate Transformation,我在一个坐标系中有一组点,我想用Python将它们旋转到另一个坐标系。基于此,我编写了以下Python函数: def change_of_basis(points, initial, final): ''' rotate points/vectors in a 3D coordinate system to a new coordinate system input: m x 3 array of points or vectors that have to b

我在一个坐标系中有一组点,我想用Python将它们旋转到另一个坐标系。基于此,我编写了以下Python函数:

def change_of_basis(points, initial, final):
    '''
    rotate points/vectors in a 3D coordinate system to a new coordinate system

        input: m x 3 array of points or vectors that have to be transformed from the initial to the final csys
        initial: sequence of sequences of floats representing the normalized axis of the csys that has to be transformed
        final: sequence of sequences of floats representing the normalized axis of the csys to which has to be transformed

        return: the points/vectors in the new coordinate system
    '''
    x1, y1, z1 = initial
    x2, y2, z2 = final

    M11, M12, M13 = np.dot(x1, x2), np.dot(x1, y2), np.dot(x1, z2)
    M21, M22, M23 = np.dot(y1, x2), np.dot(y1, y2), np.dot(y1, z2)
    M31, M32, M33 = np.dot(z1, x2), np.dot(z1, y2), np.dot(z1, z2)

    # set up rotation matrix
    R = np.array([[M11, M12, M13],
                  [M21, M22, M23],
                  [M31, M32, M33]])

    return np.linalg.inv(R).dot(points)
运行示例:

  initial =  [[ 0.98078528  0.         -0.19509032]
             [-0.19509032  0.         -0.98078528]
             [ 0.          1.          0.        ]]

  final =  [[ 0.83335824 -0.08626633 -0.54595986]
            [-0.55273325 -0.13005679 -0.82314712]
            [ 0.          0.98774564 -0.15607226]]


   new_cys = change_of_basis(initial, initial, final )
绘制此图将得到如下所示的结果。其目的是将红色/橙色坐标系转换为黄色坐标系,但结果是蓝色坐标系。有人能看出我犯了什么错误以及如何纠正吗

编辑:
它可以转换坐标系。我将上面的函数改为现在的函数。它允许我将红色坐标系转换为黄色坐标系。现在我需要的是将第一个(红色)坐标系中的一组点转换为第二个(黄色)坐标系中的一组点。我原以为这个函数会起作用,但事实并非如此,一组点的变换不同吗?

我不是线性代数专家,但我认为你的错误在于没有反转初始坐标系

如果A和B是你的基矩阵,你在计算A*B,但是你需要计算的是A^{-1}*B

这很有意义-你乘以A^{-1}从A转换成标准基,然后乘以B从标准基转换成B

下面是另一个关于实现这一点的答案:


编辑:奇怪的是,此版本适用于坐标系。你需要反转的不是R。计算的是R=A*B,所以通过反转R得到A^{-1}*B^{-1}。您需要先反转A,然后进行乘法。

或许可以尝试使用矩阵的转置来旋转点云。请参见,这表明用于旋转坐标系的矩阵和用于旋转对象的矩阵是彼此的转置。该链接还提供了有关在两个坐标系之间定义通用变换矩阵的有用信息: 和旋转矩阵(即转置): 其中(x′,x)表示x′和x轴之间的角度,(x′,y)表示x′和y轴之间的角度,等等


讨论了当两个坐标系都不是由正交向量定义的坐标系时,使用恒等式变换作为两个坐标系之间的参考:(1,0,0);(0,1,0); (0,0,1)

我使用您提供的链接成功地完成了转换。当我尝试将旋转应用于我的点云时,它不起作用(请参见编辑)。我遗漏了什么吗?请多解释一下,并告诉我你建议功能性的原因。