三维矢量的旋转?

2024-09-27 22:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个向量作为Python列表和一个角度。E、 g.:

v = [3,5,0]
axis = [4,4,1]
theta = 1.2 #radian

在绕轴旋转v向量时,获得结果向量的最佳/最简单方法是什么?

对于轴向量指向的观察者来说,旋转应该是逆时针的。这叫做right hand rule


Tags: 方法right列表rule向量观察者指向角度
3条回答

使用Euler-Rodrigues formula

import numpy as np
import math

def rotation_matrix(axis, theta):
    """
    Return the rotation matrix associated with counterclockwise rotation about
    the given axis by theta radians.
    """
    axis = np.asarray(axis)
    axis = axis / math.sqrt(np.dot(axis, axis))
    a = math.cos(theta / 2.0)
    b, c, d = -axis * math.sin(theta / 2.0)
    aa, bb, cc, dd = a * a, b * b, c * c, d * d
    bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
    return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
                     [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
                     [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])

v = [3, 5, 0]
axis = [4, 4, 1]
theta = 1.2 

print(np.dot(rotation_matrix(axis, theta), v)) 
# [ 2.74911638  4.77180932  1.91629719]

一个单行程序,具有numpy/scipy功能。

我们使用以下方法:

let a be the unit vector along axis, i.e. a = axis/norm(axis)
and A = I × a be the skew-symmetric matrix associated to a, i.e. the cross product of the identity matrix with a

then M = exp(θ A) is the rotation matrix.

from numpy import cross, eye, dot
from scipy.linalg import expm, norm

def M(axis, theta):
    return expm(cross(eye(3), axis/norm(axis)*theta))

v, axis, theta = [3,5,0], [4,4,1], 1.2
M0 = M(axis, theta)

print(dot(M0,v))
# [ 2.74911638  4.77180932  1.91629719]

expm(code here)计算指数的泰勒级数:
\sum_{k=0}^{20} \frac{1}{k!} (θ A)^k ,所以它的时间昂贵,但可读性和安全性。 这是一个很好的方法,如果你有很少的旋转,但有很多矢量。

看看http://vpython.org/contents/docs/visual/VisualIntro.html

它提供了一个具有方法A.rotate(theta,B)vector类。如果您不想在A上调用方法,它还提供一个助手函数rotate(A,theta,B)

http://vpython.org/contents/docs/visual/vector.html

相关问题 更多 >

    热门问题