使用python在3D中围绕任意轴旋转一个点

时间:2013-07-20 15:16:12

标签: python

如果我在3D (x,y,z)中有一个点,我需要围绕一个任意轴旋转这个点,该轴以逆时针方向角度θ经过两个点(x1,y1,z1)(x2,y2,z2),怎么能我是用python做的吗?

我读了很多关于3D旋转的内容,但我没能使用python,所以请任何人帮忙吗?

2 个答案:

答案 0 :(得分:5)

我会看看Chris Gohlke的简单Python库:transformations。他包含了源代码中嵌入的许多示例。祝你好运。

答案 1 :(得分:4)

因此,您可以使用3D旋转矩阵旋转单位向量(u x ,u y ,u z ),< strong>通过原点:

enter image description here

简化单位向量,然后进行矩阵乘法。

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]

这应该有效。

相关问题