python - 将一个 3D 坐标系旋转到另一个
问题描述
我在一个坐标系中有一组点,我想将它们旋转到 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。
这是另一个关于实现这一点的 SO 答案:Change of basis in numpy
编辑:奇怪的是这个版本适用于坐标系。您需要反转的不是 R。你正在计算 R = A * B,所以通过反转 R,你得到 A^{-1} * B^{-1}。您需要先反转 A,然后再相乘。
推荐阅读
- python - 添加一个包含未来 12 个月总回报的列
- java - 将 Long 转换为 LocalDateTime
- python - 从文本中删除一组特定字符(在python中)
- c - BME680 空气质量值
- jmeter - 在单个请求中两次传递 DBID 值
- python - pipenv jupyter notebook matplotlib moviepy TypeError
- networkx - Networkx OSMNX 和 Folium 绘制不同颜色的边缘
- javascript - Multer不将文件保存在文件夹中
- python - 为什么在区分时间序列时会出现长度不匹配错误?
- winapi - WinAPI 子窗口的 Z 顺序