我在一个坐标系中有一组点,我想将它们旋转到Python中的另一个坐标系。基于this answer,我编写了以下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。
这里是另一个关于实现此问题的答案:Change of basis in numpy
编辑:该版本适用于坐标系的特殊之处。它不是R,您需要反转。您正在计算R = A * B,因此通过将R取反,您将得到A ^ {-1} * B ^ {-1}。您需要先反转A,然后乘。