旋转(Rotation)矩阵转欧拉角(euler)

摘要

本文档讨论了从旋转矩阵中找所有可能欧拉角的简单技术。在计算机图形学、视觉,机器人和动力学中,有时候欧拉角的确定是必须的一步。然而,它的解可能不是那么的显而易见。
paper: Computing Euler angles from a rotation matrix
author:Gregory G. Slabaugh

Rotation matrices

我们从绕三个主轴旋转的标准定义开始。
绕x轴旋转ψ\psi弧度可定义为
Rx(ψ)=[1000cosψsinψ0sinψcosψ] R_x(\psi) = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos \psi & -sin \psi \\ 0 & \sin \psi & \cos \psi \end{bmatrix}
与此类似,绕y轴旋转θ\theta弧度可定义为
Ry(θ)=[cosθ0sinθ010sinθ0cosθ] R_y(\theta) = \begin{bmatrix} \cos \theta & 0 & sin \theta \\ 0 & 1 & 0 \\ -\sin \theta & 0 & \cos \theta \end{bmatrix}
最后,绕z轴旋转ϕ\phi弧度可定义为
Rz(ϕ)=[cosϕsinϕ0sinϕcosϕ0001] R_z(\phi) = \begin{bmatrix} \cos \phi & -\sin \phi & 0 \\ \sin \phi & \cos \phi & 0 \\ 0 & 0 & 1 \end{bmatrix}
ψ,θ,ϕ\psi, \theta, \phi是欧拉角。

Generalized rotation matrices

旋转矩阵一般可以写成
R=[R11R12R13R21R22R23R31R32R33] R = \begin{bmatrix} R_{11} & R_{12} & R_{13} \\ R_{21} & R_{22} & R_{23} \\ R_{31} & R_{32} & R_{33} \end{bmatrix}
可以将此矩阵考虑为三个旋转序列,每个旋转轴绕一个主轴旋转。由于矩阵乘法不满足交换律,因此绕轴旋转的顺序会影响结果。对于此分析,我们将首先绕x轴旋转,然后绕y轴旋转,最后绕z轴旋转。 这样的旋转序列可以表示为矩阵乘积,
R=Rz(ϕ)Ry(θ)Rx(ψ)=[cosθcosϕsinψsinθcosϕcosψsinϕcosψsinθcosϕ+sinψsinϕcosθsinϕsinψsinθsinϕ+cosψcosϕcosψsinθsinϕsinψcosϕsinθsinψcosθcosψcosθ] \begin{alignedat}{2} R = R_z(\phi)R_y(\theta)R_x(\psi) \\ &=\begin{bmatrix} \cos \theta \cos \phi & \sin \psi \sin \theta \cos \phi-\cos \psi \sin \phi & \cos \psi \sin \theta \cos \phi+\sin \psi \sin \phi \\ \cos \theta \sin \phi & \sin \psi \sin \theta \sin \phi+\cos \psi \cos \phi & \cos \psi \sin \theta \sin \phi-\sin \psi \cos \phi \\ -\sin \theta & \sin \psi \cos \theta & \cos \psi \cos \theta \end{bmatrix} \end{alignedat}
给定一个旋转矩阵RR,我们可以通过将RR每个元素与矩阵乘积Rz(ϕ)Ry(θ)Rx(ϕ)R_z(\phi)R_y(\theta)R_x(\phi)对应元素相等计算欧拉角ψ,θ,ϕ\psi, \theta, \phi。这将得到九个方程,可用于找到欧拉角。

Finding two possible angles for θ\theta

R31R_{31},我们得到
R31=sinθ R_{31} = - \sin \theta
对等式求反得到
θ=asin(R31)(1) \theta = - \mathrm{asin}(R_{31}) \tag{1}
然而,对等式(1)解释的时候应该注意到sin(πθ)=sinθ\sin (\pi - \theta)=\sin \theta,因此有两个不同的θ\theta都满足等式(1)(备注:除了θ=±π/2\theta= \pm \pi /2(即R31=±1R_{31}=\pm1))。因此θ\theta有两个有效的解。
θ1=asin(R31)θ2=πθ1=π+asin(R31) \begin{aligned} \theta_1 &= - \mathrm{asin}(R_{31}) \\ \theta_2 &= \pi - \theta_1 = \pi + \mathrm{asin}(R_{31}) \end{aligned}
接下来的报告中,我们将单独处理R31=±1R_{31}= \pm 1的情况。因此,通过利用R31R_{31}元素的值,我们可以确定θ\theta的两个不同的值。

Find the corresponding angles of ψ\psi

为了找到ψ\psi的值,我们观察到
R32R33=tan(ψ) \frac{R_{32}}{R_{33}} = \tan(\psi)
我们使用该等式解ψ\psi,有
ψ=atan2(R32,R33)(2) \psi = \mathrm{atan2}(R_{32}, R_{33}) \tag{2}
其中atan2(y,x)\mathrm{atan2}(y, x)表示变量x,yx, y的反正切,它类似于计算y / x的反正切,不同之处在于,两个自变量的符号都用于确定结果的象限,该象限在[-π,π]范围内。函数atan2\mathrm{atan2}在很多程序语言中都有实现。
在解释等式(2)时我们注意到,当cosθ>0\cos \theta \gt 0时, ψ=atan(R32,R33)\psi = \mathrm{atan(R_{32}, R_{33})},然而,当cosψ<0\cos \psi \lt 0时, ψ=atan2(R32,R33)\psi = \mathrm{atan2}(-R_{32}, -R_{33}),一个简单的解决方法是使用等式
ψ=atan2(R32cosθ,R33cosθ)(3) \psi = \mathrm{atan2}\left(\frac{R_{32}}{\cos \theta}, \frac{R_{33}}{\cos \theta} \right) \tag{3}
计算ψ\psi
等式3对于所有的情况都适用,除了当cosθ=0\cos \theta = 0时,随后将处理这种特殊情况。对于每一个θ\theta值,我们利用等式(3)计算一个对应的ψ\psi值,得到
ψ1=atan2(R32cosθ1,R33cosθ1)(4) \psi_1 = \mathrm{atan2} \left( \frac{R_{32}}{\cos \theta_1}, \frac{R_{33}}{\cos \theta_1} \right) \tag{4}
ψ2=atan2(R32cosθ2,R33cosθ2)(5) \psi_2 = \mathrm{atan2} \left( \frac{R_{32}}{\cos \theta_2}, \frac{R_{33}}{\cos \theta_2} \right) \tag{5}

Finding the corresponding angles of ϕ\phi

类似的分析也适用于寻找ϕ\phi,观察到
R21R11=tanϕ \frac{R_{21}}{R_{11}} = \tan \phi
ϕ\phi使用等式
ϕ=atan2(R21cosθ,R11cosθ)(6) \phi = \mathrm{atan2}\left( \frac{R_{21}}{\cos \theta}, \frac{R_{11}}{\cos \theta}\right) \tag{6}
同样,等式6对于所有的情况都适用,除了当cosθ=0\cos \theta = 0时,随后将处理这种特殊情况。对于每一个θ\theta值,我们利用等式(6)计算一个对应的ϕ\phi值,得到
ϕ1=atan2(R21cosθ1,R11cosθ1)(7) \phi_1 = \mathrm{atan2} \left( \frac{R_{21}}{\cos \theta_1}, \frac{R_{11}}{\cos \theta_1} \right) \tag{7}
ϕ2=atan2(R21cosθ2,R11cosθ2)(8) \phi_2 = \mathrm{atan2} \left( \frac{R_{21}}{\cos \theta_2}, \frac{R_{11}}{\cos \theta_2} \right) \tag{8}

Two solutions if cosθ0\cos \theta \ne 0

对于cosθ0\cos \theta \ne 0的情况,我们现在有两个三维(triplets)欧拉角来重现(reproduce)旋转矩阵,即
(ψ1,θ1,ϕ1)(ψ2,θ2,ϕ2) (\psi_1, \theta_1, \phi_1) \\ (\psi_2, \theta_2, \phi_2)
这两个解都是有效的。

What if cosθ=0\cos \theta=0?

如果旋转矩阵的R33R_{33}元素为1或-1(分别对应θ=π/2\theta = - \pi/2θ=π/2\theta = \pi/2cosθ=0\cos \theta = 0),则上述技术无效。当我们试图使用上面的技术解ψ,ϕ\psi, \phi时,将会出现问题,因为R11,R21,R32,R33R_{11}, R_{21}, R_{32}, R_{33}都等于0,等式(3)和(6)变成
ψ=atan2(0,0)ϕ=atan2(0,0) \psi = \mathrm{atan2} \left(0, 0 \right) \\ \phi = \mathrm{atan2} \left(0, 0 \right)
在这种情况下,R11,R21,R32,R33R_{11}, R_{21}, R_{32}, R_{33}不限制ψ\psiϕ\phi的值。 因此,我们必须使用旋转矩阵的不同元素来计算ψ\psiϕ\phi的值。

  • θ=π/2\theta = \pi/2情况:当θ=π/2\theta = \pi/2时有
    R12=sinψsinθcosϕcosψsinϕ=sinψcosϕcosψsinϕ=sin(ψϕ)R13=cosψsinθcosϕ+sinψsinϕ=cosψcosϕ+sinψsinϕ=cos(ψϕ)R22=sinψsinθsinϕ+cosψcosϕ=sinψsinϕ+cosψcosϕ=cos(ψϕ)=R13R23=cosψsinθsinϕsinψcosϕ=cosψsinϕsinψcosϕ=sin(ψϕ)=R12 R_{12} = \sin \psi \sin \theta \cos \phi-\cos \psi \sin \phi = \sin \psi \cos \phi-\cos \psi \sin \phi =\sin(\psi - \phi) \\ R_{13}=\cos \psi \sin \theta \cos \phi+\sin \psi \sin \phi = \cos \psi \cos \phi+\sin \psi \sin \phi=\cos (\psi - \phi) \\ R_{22} = \sin \psi \sin \theta \sin \phi+\cos \psi \cos \phi= \sin \psi \sin \phi+\cos \psi \cos \phi = \cos(\psi - \phi) = R_{13} \\ R_{23} = \cos \psi \sin \theta \sin \phi-\sin \psi \cos \phi = \cos \psi \sin \phi-\sin \psi \cos \phi = - \sin (\psi - \phi) = -R_{12}
    满足这些等式的任何ϕ,ψ\phi, \psi都是有效的解,利用等式R12,R13R_{12}, R_{13},我们发现
    (ψϕ)=atan2(R12,R13)ψ=ϕ+atan2(R12,R13) (\psi - \phi) = \mathrm{atan2}(R_{12}, R_{13}) \\ \psi = \phi + \mathrm{atan2}(R_{12}, R_{13})
  • θ=π/2\theta =- \pi/2情况:类似的, 有
    R12=sinψsinθcosϕcosψsinϕ=sinψcosϕcosψsinϕ=sin(ψ+ϕ)R13=cosψsinθcosϕ+sinψsinϕ=cosψcosϕ+sinψsinϕ=cos(ψ+ϕ)R22=sinψsinθsinϕ+cosψcosϕ=sinψsinϕ+cosψcosϕ=cos(ψ+ϕ)=R13R23=cosψsinθsinϕsinψcosϕ=cosψsinϕsinψcosϕ=sin(ψ+ϕ)=R12 R_{12} = \sin \psi \sin \theta \cos \phi-\cos \psi \sin \phi = -\sin \psi \cos \phi-\cos \psi \sin \phi =-\sin(\psi + \phi) \\ R_{13}=\cos \psi \sin \theta \cos \phi+\sin \psi \sin \phi = -\cos \psi \cos \phi+\sin \psi \sin \phi=-\cos (\psi +\phi) \\ R_{22} = \sin \psi \sin \theta \sin \phi+\cos \psi \cos \phi= -\sin \psi \sin \phi+\cos \psi \cos \phi = \cos(\psi + \phi) = -R_{13} \\ R_{23} = \cos \psi \sin \theta \sin \phi-\sin \psi \cos \phi = - \cos \psi \sin \phi-\sin \psi \cos \phi = - -\sin (\psi + \phi) = R_{12}
    同样,有
    (ψ+ϕ)=atan2(R12,R13)ψ=ϕ+atan2(R12,R13) (\psi + \phi) = \mathrm{atan2}(-R_{12}, -R_{13}) \\ \psi = -\phi + \mathrm{atan2}(-R_{12}, -R_{13})

旋转(Rotation)矩阵转欧拉角(euler)

  • 无论哪种情况: 在θ=π/2\theta = \pi/2θ=π/2\theta = -\pi/2的情况下,我们都发现ψ\psiϕ\phi是关联的。 这种现象称为万向节锁(Gimbal lock)。尽管在这种情况下,有无数个问题的解,但在实践中,人们常常有兴趣寻找一个解。 对于此任务,如上所述方便地设置ϕ=0\phi=0并计算ψ\psi

Pseudo-code

现在,我们通过图1中的伪代码实现来总结该方法。代码非常简单。

More than one solution

有趣的是,围绕三个主轴旋转的序列始终不止一种,他们都能得到物体相同的方向。 如本报告所示,在cosθ0\cos \theta \ne 0的非退化(non-degenerate)的情况下,有两个解。 对于cosθ=0\cos \theta = 0的退化情况,存在无限数量的解。

例如,考虑一本书放在您面前的桌子上。 将x轴定义为右侧,将y轴定义为远离您,将z轴定义为向上。 绕y轴旋转π\pi弧度将使书本旋转,使得后盖现在朝上。 实现相同方向的另一种方法是绕xx轴旋转书本π\pi弧度,然后绕z轴旋转π\pi弧度。 因此,存在不止一种方式来实现期望的旋转。

reference

[1]. Gregory G. Slabaugh, Computing Euler angles from a rotation matrix