如果我在 3D 中有一个点,(x,y,z)
并且我需要围绕一个任意轴旋转该点,该轴通过两个点(x1,y1,z1)
并且(x2,y2,z2)
逆时针角度为 theta,我该如何使用 python 来做到这一点?
我读了很多关于 3D 旋转的文章,但我没能用 python 做到这一点,所以请有人帮忙吗?
如果我在 3D 中有一个点,(x,y,z)
并且我需要围绕一个任意轴旋转该点,该轴通过两个点(x1,y1,z1)
并且(x2,y2,z2)
逆时针角度为 theta,我该如何使用 python 来做到这一点?
我读了很多关于 3D 旋转的文章,但我没能用 python 做到这一点,所以请有人帮忙吗?
所以你有你的 3D 旋转矩阵,用于围绕单位向量(u x,u y,u z)进行旋转,通过原点:
获得单位向量很容易,然后只需进行矩阵乘法即可。
from math import pi ,sin, cos
def R(theta, u):
return [[cos(theta) + u[0]**2 * (1-cos(theta)),
u[0] * u[1] * (1-cos(theta)) - u[2] * sin(theta),
u[0] * u[2] * (1 - cos(theta)) + u[1] * sin(theta)],
[u[0] * u[1] * (1-cos(theta)) + u[2] * sin(theta),
cos(theta) + u[1]**2 * (1-cos(theta)),
u[1] * u[2] * (1 - cos(theta)) - u[0] * sin(theta)],
[u[0] * u[2] * (1-cos(theta)) - u[1] * sin(theta),
u[1] * u[2] * (1-cos(theta)) + u[0] * sin(theta),
cos(theta) + u[2]**2 * (1-cos(theta))]]
def Rotate(pointToRotate, point1, point2, theta):
u= []
squaredSum = 0
for i,f in zip(point1, point2):
u.append(f-i)
squaredSum += (f-i) **2
u = [i/squaredSum for i in u]
r = R(theta, u)
rotated = []
for i in range(3):
rotated.append(round(sum([r[j][i] * pointToRotate[j] for j in range(3)])))
return rotated
point = [1,0,0]
p1 = [0,0,0]
p2 = [0,1,0]
print Rotate(point, p1, p2, pi) # [-1.0, 0.0, 0.0]
那应该行得通。
我会看一下 Chris Gohlke 的简单 Python 库:transformations。他在源代码中包含了许多示例。祝你好运。