3D矢量的旋转?

时间:2011-07-23 18:57:34

标签: python vector rotation

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

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

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

对于轴向量指向的观察者,旋转应该是逆时针方向。这称为right hand rule

11 个答案:

答案 0 :(得分:97)

使用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]

答案 1 :(得分:42)

单行,具有numpy / scipy功能。

我们使用以下内容:

  

a 成为的单位向量,即 a = axis / norm(axis)
  和 A = I×a 是与 a 相关联的偏斜对称矩阵,即单位矩阵与 a      

然后 M = exp(θA)是旋转矩阵。

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 ,所以它的时间很昂贵,但可读性和安全性。 如果除了很多向量之外几乎没有旋转,这可能是一个好方法。

答案 2 :(得分:19)

我只是想提一下,如果需要速度,将unutbu的代码包装在scipy的weave.inline中,并将已经存在的矩阵作为参数传递,使运行时间减少20倍。

代码(在rotation_matrix_test.py中):

import numpy as np
import timeit

from math import cos, sin, sqrt
import numpy.random as nr

from scipy import weave

def rotation_matrix_weave(axis, theta, mat = None):
    if mat == None:
        mat = np.eye(3,3)

    support = "#include <math.h>"
    code = """
        double x = sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
        double a = cos(theta / 2.0);
        double b = -(axis[0] / x) * sin(theta / 2.0);
        double c = -(axis[1] / x) * sin(theta / 2.0);
        double d = -(axis[2] / x) * sin(theta / 2.0);

        mat[0] = a*a + b*b - c*c - d*d;
        mat[1] = 2 * (b*c - a*d);
        mat[2] = 2 * (b*d + a*c);

        mat[3*1 + 0] = 2*(b*c+a*d);
        mat[3*1 + 1] = a*a+c*c-b*b-d*d;
        mat[3*1 + 2] = 2*(c*d-a*b);

        mat[3*2 + 0] = 2*(b*d-a*c);
        mat[3*2 + 1] = 2*(c*d+a*b);
        mat[3*2 + 2] = a*a+d*d-b*b-c*c;
    """

    weave.inline(code, ['axis', 'theta', 'mat'], support_code = support, libraries = ['m'])

    return mat

def rotation_matrix_numpy(axis, theta):
    mat = np.eye(3,3)
    axis = axis/sqrt(np.dot(axis, axis))
    a = cos(theta/2.)
    b, c, d = -axis*sin(theta/2.)

    return np.array([[a*a+b*b-c*c-d*d, 2*(b*c-a*d), 2*(b*d+a*c)],
                  [2*(b*c+a*d), a*a+c*c-b*b-d*d, 2*(c*d-a*b)],
                  [2*(b*d-a*c), 2*(c*d+a*b), a*a+d*d-b*b-c*c]])

时间安排:

>>> import timeit
>>> 
>>> setup = """
... import numpy as np
... import numpy.random as nr
... 
... from rotation_matrix_test import rotation_matrix_weave
... from rotation_matrix_test import rotation_matrix_numpy
... 
... mat1 = np.eye(3,3)
... theta = nr.random()
... axis = nr.random(3)
... """
>>> 
>>> timeit.repeat("rotation_matrix_weave(axis, theta, mat1)", setup=setup, number=100000)
[0.36641597747802734, 0.34883809089660645, 0.3459300994873047]
>>> timeit.repeat("rotation_matrix_numpy(axis, theta)", setup=setup, number=100000)
[7.180983066558838, 7.172032117843628, 7.180462837219238]

答案 3 :(得分:12)

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

它提供了vector类,其方法为A.rotate(theta,B)。如果您不想在rotate(A,theta,B)上调用该方法,它还会提供帮助函数A

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

答案 4 :(得分:12)

这是一种使用极快速的四元数的优雅方法;我可以用适当的矢量化numpy数组计算每秒1000万次旋转。它依赖于四元数扩展到numpy found here

四元数理论: 四元数是一个具有一个实数和三个虚数维的数字,通常写为q = w + xi + yj + zk其中&#39; i&#39;,&#39; j&#39;,&#39; k&#39;是想象的维度。就像一个单位复数&#39; c&#39;可以代表c=exp(i * theta)的所有2d轮换,单位四元数&#39; q&#39;可以通过q=exp(p)代表所有三维旋转,其中&#39; p&#39;是由您的轴和角度设置的纯虚构四元数。

我们首先将您的轴和角度转换为四元数,其虚数由旋转轴给出,其大小以弧度的旋转角度的一半给出。 4个元素向量(w, x, y, z)的构造如下:

import numpy as np
import quaternion as quat

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

vector = np.array([0.] + v)
rot_axis = np.array([0.] + axis)
axis_angle = (theta*0.5) * rot_axis/np.linalg.norm(rot_axis)

首先,构造4个元素的numpy数组,其中实数分量w = 0,用于要旋转的矢量vector和旋转轴rot_axis。然后通过归一化然后乘以所需角度theta的一半来构造轴角度表示。请参阅here了解为什么需要一半的角度。

现在使用库创建四元数vqlog,并通过取指数获得单位轮换四元数q

vec = quat.quaternion(*v)
qlog = quat.quaternion(*axis_angle)
q = np.exp(qlog)

最后,通过以下操作计算向量的旋转。

v_prime = q * vec * np.conjugate(q)

print(v_prime) # quaternion(0.0, 2.7491163, 4.7718093, 1.9162971)

现在只丢弃真正的元素,你就拥有旋转的矢量!

v_prime_vec = v_prime.imag # [2.74911638 4.77180932 1.91629719] as a numpy array

请注意,如果必须通过许多顺序旋转旋转矢量,此方法特别有效,因为四元数乘积可以计算为q = q1 * q2 * q3 * q4 * ... * qn然后矢量仅按“q&#39;在最后使用v&#39; = q * v * conj(q)。

此方法为您提供轴角度<--->之间的无缝转换。简单地通过explog函数的3d旋转算子(是log(q)只返回轴角表示!)。有关四元数乘法等的工作原理的进一步说明,请参阅here

答案 5 :(得分:6)

我为Python {2,3}创建了一个相当完整的3D数学库。它仍然没有使用Cython,但在很大程度上依赖于numpy的效率。你可以在这里找到pip:

python[3] -m pip install math3d

或者看看我的gitweb http://git.automatics.dyndns.dk/?p=pymath3d.git,现在也看看github:https://github.com/mortlind/pymath3d

安装完成后,您可以在python中创建可以旋转矢量或成为变换对象一部分的方向对象。例如。以下代码片段组成一个方向,表示围绕轴[1,2,3]旋转1 rad,将其应用于向量[4,5,6],并打印结果:

import math3d as m3d
r = m3d.Orientation.new_axis_angle([1,2,3], 1)
v = m3d.Vector(4,5,6)
print(r * v)

输出为

<Vector: (2.53727, 6.15234, 5.71935)>

这比使用上面B. M.发布的scipy的oneliner更有效率,大约为4倍。但是,它需要安装我的math3d包。

答案 6 :(得分:2)

免责声明:我是此套餐的作者

虽然旋转的特殊类可以很方便,但在某些情况下,需要旋转矩阵(例如,用于处理scipy中的affine_transform函数等其他库)。为了避免每个人都实现自己的小矩阵生成函数,存在一个很小的纯python包,它只提供方便的旋转矩阵生成函数。该软件包位于github(mgen)上,可以通过pip安装:

pip install mgen

从自述文件复制的示例用法:

import numpy as np
np.set_printoptions(suppress=True)

from mgen import rotation_around_axis
from mgen import rotation_from_angles
from mgen import rotation_around_x

matrix = rotation_from_angles([np.pi/2, 0, 0], 'XYX')
matrix.dot([0, 1, 0])
# array([0., 0., 1.])

matrix = rotation_around_axis([1, 0, 0], np.pi/2)
matrix.dot([0, 1, 0])
# array([0., 0., 1.])

matrix = rotation_around_x(np.pi/2)
matrix.dot([0, 1, 0])
# array([0., 0., 1.])

请注意,矩阵只是常规的numpy数组,因此在使用此包时不会引入新的数据结构。

答案 7 :(得分:1)

使用pyquaternion非常简单;安装它,在您的控制台中运行:         进口点;         pip.main([ '安装', 'pyquaternion'])

安装完成后:

  from pyquaternion import Quaternion
  v = [3,5,0]
  axis = [4,4,1]
  theta = 1.2 #radian
  rotated_v = Quaternion(axis=axis,angle=theta).rotate(v)

答案 8 :(得分:1)

也可以使用四元数理论来解决:

def angle_axis_quat(theta, axis):
    """
    Given an angle and an axis, it returns a quaternion.
    """
    axis = np.array(axis) / np.linalg.norm(axis)
    return np.append([np.cos(theta/2)],np.sin(theta/2) * axis)

def mult_quat(q1, q2):
    """
    Quaternion multiplication.
    """
    q3 = np.copy(q1)
    q3[0] = q1[0]*q2[0] - q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3]
    q3[1] = q1[0]*q2[1] + q1[1]*q2[0] + q1[2]*q2[3] - q1[3]*q2[2]
    q3[2] = q1[0]*q2[2] - q1[1]*q2[3] + q1[2]*q2[0] + q1[3]*q2[1]
    q3[3] = q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1] + q1[3]*q2[0]
    return q3

def rotate_quat(quat, vect):
    """
    Rotate a vector with the rotation defined by a quaternion.
    """
    # Transfrom vect into an quaternion 
    vect = np.append([0],vect)
    # Normalize it
    norm_vect = np.linalg.norm(vect)
    vect = vect/norm_vect
    # Computes the conjugate of quat
    quat_ = np.append(quat[0],-quat[1:])
    # The result is given by: quat * vect * quat_
    res = mult_quat(quat, mult_quat(vect,quat_)) * norm_vect
    return res[1:]

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

print(rotate_quat(angle_axis_quat(theta, axis), v))
# [2.74911638 4.77180932 1.91629719]

答案 9 :(得分:0)

我需要围绕嵌入该模型的三个轴{x,y,z}之一旋转3D模型,这是在numpy中搜索如何执行此操作的最高结果。我使用了以下简单功能:

def rotate(X, theta, axis='x'):
  '''Rotate multidimensional array `X` `theta` degrees around axis `axis`'''
  c, s = np.cos(theta), np.sin(theta)
  if axis == 'x': return np.dot(X, np.array([
    [1.,  0,  0],
    [0 ,  c, -s],
    [0 ,  s,  c]
  ]))
  elif axis == 'y': return np.dot(X, np.array([
    [c,  0,  -s],
    [0,  1,   0],
    [s,  0,   c]
  ]))
  elif axis == 'z': return np.dot(X, np.array([
    [c, -s,  0 ],
    [s,  c,  0 ],
    [0,  0,  1.],
  ]))

答案 10 :(得分:0)

使用scipy的Rotation.from_rotvec()。参数是旋转矢量(单位矢量)乘以旋转角(以弧度为单位)。

from scipy.spatial.transform import Rotation
from numpy.linalg import norm


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

axis = axis / norm(axis)  # normalize the rotation vector first
rot = Rotation.from_rotvec(theta * axis)

new_v = rot.apply(v)  
print(new_v)    # results in [2.74911638 4.77180932 1.91629719]

根据关于轮换的数据,还有其他几种使用Rotation的方式:


离题注释:某些用户暗示,单行代码不一定是更好的代码。

相关问题