四元数转换欧拉角(12种)附加代码实现
四元数转换欧拉角(12种)附加代码实现
四元数矩阵
关于四元数的相关性质以及特点详见上一篇文章。
假设四元数为:
q 4 + q 1 ∗ i + q 2 ∗ j + q 3 ∗ k 标 准 四 元 数 格 式 : w + x ∗ i + y ∗ j + z ∗ k q_4+q_1*i+q_2*j+q_3*k \\ 标准四元数格式:w+x*i+y*j+z*k q4+q1∗i+q2∗j+q3∗k标准四元数格式:w+x∗i+y∗j+z∗k
各项参数一一对应。
其所对应的四元数矩阵为:
[ q 1 2 + q 4 2 − q 2 2 − q 3 2 2 q 1 q 2 + 2 q 3 q 4 2 q 1 q 3 − 2 q 2 q 4 2 q 1 q 2 − 2 q 3 q 4 q 2 2 + q 4 2 − q 1 2 − q 3 2 2 q 2 q 3 + 2 q 1 q 4 2 q 1 q 3 + 2 q 2 q 4 2 q 2 q 3 − 2 q 1 q 4 q 3 2 + q 4 2 − q 1 2 − q 2 2 ] (1) \left[ \begin{matrix} q_1^2+q_4^2-q_2^2-q_3^2 & 2q_1q_2+2q_3q_4 & 2q_1q_3-2q_2q_4 \\ 2q_1q_2-2q_3q_4 & q_2^2+q_4^2-q_1^2-q_3^2 & 2q_2q_3+2q_1q_4 \\ 2q_1q_3+2q_2q_4 & 2q_2q_3-2q_1q_4 & q_3^2+q_4^2-q_1^2-q_2^2 \end{matrix} \right] \tag{1} ⎣⎡q12+q42−q22−q322q1q2−2q3q42q1q3+2q2q42q1q2+2q3q4q22+q42−q12−q322q2q3−2q1q42q1q3−2q2q42q2q3+2q1q4q32+q42−q12−q22⎦⎤(1)
其所对应的个个方向上的向量矩阵(12种)为:
[comment]: < “c表示cos(),s表示sin();1表示第一方向上的夹角,2表示第二方向上的夹角,3表示第三方向上的夹角”
- 在X-Z-Y方向上:
[ c 2 c 3 s 1 s 3 + c 1 c 3 s 2 c 3 s 1 s 2 − c 1 s 3 − s 2 c 1 c 2 c 2 s 1 c 2 s 3 c 1 s 2 s 3 − c 3 s 1 c 1 c 3 + s 1 s 2 s 3 ] (2) \left[ \begin{matrix} c_2c_3 & s_1s_3+c_1c_3s_2 & c_3s_1s_2-c_1s_3 \\ -s_2 & c_1c_2 & c_2s_1 \\ c_2s_3 & c_1s_2s_3-c_3s_1 & c_1c_3+s_1s_2s_3 \end{matrix} \right] \tag{2} ⎣⎡c2c3−s2c2s3s1s3+c1c3s2c1c2c1s2s3−c3s1c3s1s2−c1s3c2s1c1c3+s1s2s3⎦⎤(2)
- 在X-Y-Z方向上:
[ c 2 c 3 c 1 s 3 + c 3 s 1 s 2 s 1 s 3 − c 1 c 3 s 2 − c 2 s 3 c 1 c 3 c 3 s 1 + c 1 s 2 s 3 s 2 − c 2 s 1 c 1 c 2 ] (3) \left[ \begin{matrix} c_2c_3 & c_1s_3+c_3s_1s_2 & s_1s_3-c_1c_3s_2 \\ -c_2s_3 & c_1c_3 & c_3s_1+c_1s_2s_3 \\ s_2 & -c_2s_1 & c_1c_2 \end{matrix} \right] \tag{3} ⎣⎡c2c3−c2s3s2c1s3+c3s1s2c1c3−c2s1s1s3−c1c3s2c3s1+c1s2s3c1c2⎦⎤(3)
- 在Y-X-Z方向上:
[ c 1 c 3 + s 1 s 2 s 3 c 2 c 3 c 1 s 2 s 3 − c 3 s 1 c 2 s 1 s 2 − c 1 s 3 c 2 c 3 c 1 c 3 s 2 + s 1 s 3 c 2 s 1 − s 2 c 1 c 2 ] (4) \left[ \begin{matrix} c_1c_3+s_1s_2s_3 & c_2c_3 & c_1s_2s_3-c_3s_1 \\ c_2s_1s_2-c_1s_3 & c_2c_3 & c_1c_3s_2+s_1s_3 \\ c_2s_1 & -s_2 & c_1c_2 \end{matrix} \right] \tag{4} ⎣⎡c1c3+s1s2s3c2s1s2−c1s3c2s1c2c3c2c3−s2c1s2s3−c3s1c1c3s2+s1s3c1c2⎦⎤(4)
- 在Y-Z-X方向上:
[ c 1 c 2 s 2 − c 2 s 1 s 1 s 3 − c 1 c 3 s 2 c 2 c 3 c 1 s 3 + c 3 s 1 s 2 c 3 s 1 + c 1 s 2 s 3 − c 2 s 3 c 1 c 3 − s 1 s 2 s 3 ] (5) \left[ \begin{matrix} c_1c_2 & s_2 & -c_2s_1 \\ s_1s_3-c_1c_3s_2 & c_2c_3 & c_1s_3+c_3s_1s_2 \\ c_3s_1+c_1s_2s_3 & -c_2s_3 & c_1c_3-s_1s_2s_3 \end{matrix} \right] \tag{5} ⎣⎡c1c2s1s3−c1c3s2c3s1+c1s2s3s2c2c3−c2s3−c2s1c1s3+c3s1s2c1c3−s1s2s3⎦⎤(5)
- 在Z-Y-X方向上:
[ c 1 c 2 c 2 s 1 − s 2 c 1 s 2 s 3 − c 3 s 1 c 1 c 3 + s 1 s 2 s 3 c 2 s 3 s 1 s 3 + c 1 c 3 s 2 c 3 s 1 s 2 − c 1 s 3 c 2 c 3 ] (6) \left[ \begin{matrix} c_1c_2 & c_2s_1 & -s_2 \\ c_1s_2s_3-c_3s_1 & c_1c_3+s_1s_2s_3 & c_2s_3 \\ s_1s_3+c_1c_3s_2 & c_3s_1s_2-c_1s_3 & c_2c_3 \end{matrix} \right] \tag{6} ⎣⎡c1c2c1s2s3−c3s1s1s3+c1c3s2c2s1c1c3+s1s2s3c3s1s2−c1s3−s2c2s3c2c3⎦⎤(6)
- 在Z-X-Y方向上:
[ c 1 c 3 − s 2 1 s 2 s 3 c 3 s 1 + c 1 s 2 s 3 − c 2 s 3 − c 2 s 1 c 1 c 2 s 2 c 1 s 3 + c 3 s 1 s 2 s 1 s 3 − c 1 c 3 s 2 c 2 c 3 ] (7) \left[ \begin{matrix} c_1c_3-s2_1s_2s_3 & c_3s_1+c_1s_2s_3 & -c_2s_3 \\ -c_2s_1 & c_1c_2 & s_2 \\ c_1s_3+c_3s_1s_2 & s_1s_3-c_1c_3s_2 & c_2c_3 \end{matrix} \right] \tag{7} ⎣⎡c1c3−s21s2s3−c2s1c1s3+c3s1s2c3s1+c1s2s3c1c2s1s3−c1c3s2−c2s3s2c2c3⎦⎤(7)
(经典欧拉角)
- 在X1-Z2-X3方向上:
[ c 2 c 1 s 2 s 1 s 2 − c 3 s 2 c 1 c 2 s 3 − s 1 s 3 c 1 s 3 + c 2 c 3 s 1 s 2 s 3 − c 3 s 1 − c 1 s 2 s 3 c 1 c 3 − c 2 s 1 s 3 ] (8) \left[ \begin{matrix} c_2 & c_1s_2 & s_1s_2 \\ -c_3s_2 & c_1c_2s_3-s_1s_3 & c_1s_3+c_2c_3s_1 \\ s_2s_3 & -c_3s_1-c_1s_2s_3 & c_1c_3-c_2s_1s_3 \end{matrix} \right] \tag{8} ⎣⎡c2−c3s2s2s3c1s2c1c2s3−s1s3−c3s1−c1s2s3s1s2c1s3+c2c3s1c1c3−c2s1s3⎦⎤(8)
- 在X1-Y2-X3方向上:
[ c 2 s 1 s 2 − c 1 s 2 s 2 s 3 c 1 c 3 − c 2 s 1 s 3 c 3 s 1 + c 1 c 2 s 3 s 2 s 3 − c 1 s 3 − c 2 s 3 s 1 c 1 c 2 c 3 − s 1 s 3 ] (9) \left[ \begin{matrix} c_2 & s_1s_2 & -c_1s_2 \\ s_2s_3 & c_1c_3-c_2s_1s_3 & c_3s_1+c_1c_2s_3 \\ s_2s_3 & -c_1s_3-c_2s_3s_1 & c_1c_2c_3-s_1s_3 \end{matrix} \right] \tag{9} ⎣⎡c2s2s3s2s3s1s2c1c3−c2s1s3−c1s3−c2s3s1−c1s2c3s1+c1c2s3c1c2c3−s1s3⎦⎤(9)
- 在Y1-X2-Y3方向上:
[ c 1 c 3 − c 2 s 1 s 3 s 2 s 3 − c 3 s 1 − c 1 c 2 s 3 s 1 s 2 c 2 c 1 s 2 c 1 s 3 + c 2 c 3 s 1 − c 3 s 2 c 1 c 2 c 3 − s 1 s 3 ] (10) \left[ \begin{matrix} c_1c_3-c_2s_1s_3 & s_2s_3 & -c_3s_1-c_1c_2s_3 \\ s_1s_2 & c_2 & c_1s_2 \\ c_1s_3+c_2c_3s_1 & -c_3s_2 & c_1c_2c_3-s_1s_3 \end{matrix} \right] \tag{10} ⎣⎡c1c3−c2s1s3s1s2c1s3+c2c3s1s2s3c2−c3s2−c3s1−c1c2s3c1s2c1c2c3−s1s3⎦⎤(10)
- 在Y1-Z2-Z3方向上:
[ c 1 c 2 c 3 − s 1 s 3 s 2 c 3 − c 1 s 3 − c 2 c 3 s 1 − c 1 s 2 c 2 s 1 s 2 c 3 s 1 + c 1 c 2 s 3 s 2 s 3 c 1 c 3 − c 2 s 1 s 3 ] (11) \left[ \begin{matrix} c_1c_2c_3-s_1s_3 & s_2c_3 & -c_1s_3-c_2c_3s_1 \\ -c_1s_2 & c_2 & s_1s_2 \\ c_3s_1+c_1c_2s_3 & s_2s_3 & c_1c_3-c_2s_1s_3 \end{matrix} \right] \tag{11} ⎣⎡c1c2c3−s1s3−c1s2c3s1+c1c2s3s2c3c2s2s3−c1s3−c2c3s1s1s2c1c3−c2s1s3⎦⎤(11)
- 在Z1-Y2-Z3方向上:
[ c 1 c 2 c 3 − s 1 s 3 c 1 s 3 + c 2 c 3 s 1 − c 2 c 3 − c 3 s 1 c 1 c 3 − c 2 s 1 s 3 s 2 s 3 c 1 s 2 s 1 s 2 c 2 ] (12) \left[ \begin{matrix} c_1c_2c_3-s_1s_3 & c_1s_3+c_2c_3s_1 & -c_2c_3 \\ -c_3s_1 & c_1c_3-c_2s_1s_3 & s_2s_3 \\ c_1s_2 & s_1s_2 & c_2 \end{matrix} \right] \tag{12} ⎣⎡c1c2c3−s1s3−c3s1c1s2c1s3+c2c3s1c1c3−c2s1s3s1s2−c2c3s2s3c2⎦⎤(12)
- 在Z1-X2-Z3方向上:
[ c 1 c 3 − c 2 s 1 s 3 c 3 s 1 + c 1 c 2 c 3 s 2 s 3 − c 1 s 3 − c 2 c 3 s 1 c 1 c 2 c 3 − s 1 s 3 c 3 s 2 s 1 s 2 − c 1 s 2 c 2 ] (13) \left[ \begin{matrix} c_1c_3-c_2s_1s_3 & c_3s_1+c_1c_2c_3 & s_2s_3 \\ -c_1s_3-c_2c_3s_1 & c_1c_2c_3-s_1s_3 & c_3s_2 \\ s_1s_2 & -c_1s_2 & c_2 \end{matrix} \right] \tag{13} ⎣⎡c1c3−c2s1s3−c1s3−c2c3s1s1s2c3s1+c1c2c3c1c2c3−s1s3−c1s2s2s3c3s2c2⎦⎤(13)
将他们对应四元数一一对应就能够得到他们对应的计算公式:(计算过程在草稿纸上,过程略)
- 在X-Z-Y方向上:
{ X = a r c t a n 2 ( 2 ∗ ( y z + x w ) , 1 − 2 ( x 2 + z 2 ) ) Y = a r c t a n 2 ( z ∗ ( x z + y w ) , 1 − 2 ( y 2 + z 2 ) ) Z = a r c s i n ( 2 ∗ ( z w − x y ) ) (14) \begin{cases} X=arctan2(2*(yz+xw),1-2(x^2+z^2)) \\ Y=arctan2(z*(xz+yw),1-2(y^2+z^2)) \\ Z=arcsin(2*(zw-xy)) \end{cases}\tag{14} ⎩⎪⎨⎪⎧X=arctan2(2∗(yz+xw),1−2(x2+z2))Y=arctan2(z∗(xz+yw),1−2(y2+z2))Z=arcsin(2∗(zw−xy))(14)
- 在X-Y-Z方向上:
{ X = a r c t a n 2 ( 2 ∗ ( x w − y z ) , 1 − 2 ( x 2 + y 2 ) ) Y = a r c s i n ( 2 ∗ ( z x + y w ) ) Z = a r c t a n 2 ( 2 ∗ ( w z − x y ) , 1 − 2 ( y 2 + z 2 ) ) (15) \begin{cases} X=arctan2(2*(xw-yz),1-2(x^2+y^2)) \\ Y=arcsin(2*(zx+yw)) \\ Z=arctan2(2*(wz-xy),1-2(y^2+z^2)) \end{cases}\tag{15} ⎩⎪⎨⎪⎧X=arctan2(2∗(xw−yz),1−2(x2+y2))Y=arcsin(2∗(zx+yw))Z=arctan2(2∗(wz−xy),1−2(y2+z2))(15)
- 在Y-X-Z方向上:
{ X = a r c s i n ( 2 ∗ ( x w + y z ) ) Y = a r c t a n 2 ( 2 ∗ ( x z − y w ) , 1 − 2 ( x 2 + y 2 ) ) Z = a r c t a n 2 ( 2 ∗ ( w z + x y ) , 1 − 2 ( x 2 + z 2 ) ) (16) \begin{cases} X=arcsin(2*(xw+yz)) \\ Y=arctan2(2*(xz-yw),1-2(x^2+y^2)) \\ Z=arctan2(2*(wz+xy),1-2(x^2+z^2)) \end{cases}\tag{16} ⎩⎪⎨⎪⎧X=arcsin(2∗(xw+yz))Y=arctan2(2∗(xz−yw),1−2(x2+y2))Z=arctan2(2∗(wz+xy),1−2(x2+z2))(16)
- 在Y-Z-X方向上:
{ X = a r c t a n 2 ( 2 ∗ ( w x + z y ) , 1 − 2 ( x 2 + z 2 ) ) Y = a r c t a n 2 ( 2 ∗ ( y w − x z ) , 1 − 2 ( y 2 + z 2 ) ) Z = a r c s i n ( 2 ∗ ( x y + z w ) ) (17) \begin{cases} X=arctan2(2*(wx+zy),1-2(x^2+z^2)) \\ Y=arctan2(2*(yw-xz),1-2(y^2+z^2)) \\ Z=arcsin(2*(xy+zw)) \end{cases}\tag{17} ⎩⎪⎨⎪⎧X=arctan2(2∗(wx+zy),1−2(x2+z2))Y=arctan2(2∗(yw−xz),1−2(y2+z2))Z=arcsin(2∗(xy+zw))(17)
- 在Z-Y-X方向上:
{ X = a r c t a n 2 ( 2 ∗ ( w x + z y ) , 1 − 2 ( x 2 + y 2 ) ) Y = a r c s i n ( 2 ∗ ( y w − x z ) ) Z = a r c t a n 2 ( 2 ∗ ( x y + z w ) , 1 − 2 ( y 2 + z 2 ) ) (18) \begin{cases} X=arctan2(2*(wx+zy),1-2(x^2+y^2)) \\ Y=arcsin(2*(yw-xz)) \\ Z=arctan2(2*(xy+zw),1-2(y^2+z^2)) \end{cases}\tag{18} ⎩⎪⎨⎪⎧X=arctan2(2∗(wx+zy),1−2(x2+y2))Y=arcsin(2∗(yw−xz))Z=arctan2(2∗(xy+zw),1−2(y2+z2))(18)
- 在Z-X-Y方向上:
{ X = a r c s i n ( 2 ∗ ( y z + x w ) ) Y = a r c t a n 2 ( 2 ∗ ( y w − x z ) , 1 − 2 ( x 2 + y 2 ) ) Z = a r c t a n 2 ( 2 ∗ ( z w − x y ) , 1 − 2 ( x 2 + z 2 ) ) (19) \begin{cases} X=arcsin(2*(yz+xw)) \\ Y=arctan2(2*(yw-xz),1-2(x^2+y^2)) \\ Z=arctan2(2*(zw-xy),1-2(x^2+z^2)) \\ \end{cases}\tag{19} ⎩⎪⎨⎪⎧X=arcsin(2∗(yz+xw))Y=arctan2(2∗(yw−xz),1−2(x2+y2))Z=arctan2(2∗(zw−xy),1−2(x2+z2))(19)
(经典欧拉角)
- 在X1-Z2-X3方向上:
{ X 1 = a r c t a n 2 ( 2 ∗ ( x z − y w ) , 2 ∗ ( x y + z w ) ) Z 2 = a r c c o s ( 1 − 2 ( y 2 + z 2 ) ) X 3 = a r c t a n 2 ( 2 ∗ ( − x z − y w ) , 2 ( x y + z w ) ) (20) \begin{cases} X1=arctan2(2*(xz-yw),2*(xy+zw)) \\ Z2=arccos(1-2(y^2+z^2)) \\ X3=arctan2(2*(-xz-yw),2(xy+zw)) \\ \end{cases}\tag{20} ⎩⎪⎨⎪⎧X1=arctan2(2∗(xz−yw),2∗(xy+zw))Z2=arccos(1−2(y2+z2))X3=arctan2(2∗(−xz−yw),2(xy+zw))(20)
- 在X1-Y2-X3方向上:
{ X 1 = a r c t a n 2 ( 2 ∗ ( − x y − z w ) , 2 ∗ ( x z − y w ) ) Y 2 = a r c c o s ( 1 − 2 ( y 2 + z 2 ) ) X 3 = a r c t a n 2 ( 2 ∗ ( x y − z w ) , 2 ( x z + y w ) ) (21) \begin{cases} X1=arctan2(2*(-xy-zw),2*(xz-yw)) \\ Y2=arccos(1-2(y^2+z^2)) \\ X3=arctan2(2*(xy-zw),2(xz+yw)) \\ \end{cases}\tag{21} ⎩⎪⎨⎪⎧X1=arctan2(2∗(−xy−zw),2∗(xz−yw))Y2=arccos(1−2(y2+z2))X3=arctan2(2∗(xy−zw),2(xz+yw))(21)
- 在Y1-X2-Y3方向上:
{ Y 1 = a r c t a n 2 ( 2 ∗ ( x y − z w ) , 2 ∗ ( y z + x w ) ) X 2 = a r c c o s ( 1 − 2 ( x 2 + z 2 ) ) Y 3 = a r c t a n 2 ( 2 ∗ ( − x y − z w ) , 2 ∗ ( y z − x w ) ) (22) \begin{cases} Y1=arctan2(2*(xy-zw),2*(yz+xw)) \\ X2=arccos(1-2(x^2+z^2)) \\ Y3=arctan2(2*(-xy-zw),2*(yz-xw)) \\ \end{cases}\tag{22} ⎩⎪⎨⎪⎧Y1=arctan2(2∗(xy−zw),2∗(yz+xw))X2=arccos(1−2(x2+z2))Y3=arctan2(2∗(−xy−zw),2∗(yz−xw))(22)
- 在Y1-Z2-Z3方向上:
{ Y 1 = a r c t a n 2 ( 2 ∗ ( − y z − x w ) , 2 ∗ ( x y + z w ) ) Z 2 = a r c c o s ( 1 − 2 ( x 2 + z 2 ) ) Y 3 = a r c t a n 2 ( 2 ∗ ( y z − x w ) , 2 ∗ ( x y − z w ) ) (23) \begin{cases} Y1=arctan2(2*(-yz-xw),2*(xy+zw)) \\ Z2=arccos(1-2(x^2+z^2)) \\ Y3=arctan2(2*(yz-xw),2*(xy-zw)) \\ \end{cases}\tag{23} ⎩⎪⎨⎪⎧Y1=arctan2(2∗(−yz−xw),2∗(xy+zw))Z2=arccos(1−2(x2+z2))Y3=arctan2(2∗(yz−xw),2∗(xy−zw))(23)
- 在Z1-Y2-Z3方向上:
{ Z 1 = a r c t a n 2 ( 2 ∗ ( y z − x w ) , 2 ∗ ( x y + z w ) ) Y 2 = a r c c o s ( 1 − 2 ( x 2 + y 2 ) ) Z 3 = a r c t a n 2 ( 2 ∗ ( y z − x w ) , 2 ∗ ( x z − y w ) ) (24) \begin{cases} Z1=arctan2(2*(yz-xw),2*(xy+zw)) \\ Y2=arccos(1-2(x^2+y^2)) \\ Z3=arctan2(2*(yz-xw),2*(xz-yw)) \\ \end{cases}\tag{24} ⎩⎪⎨⎪⎧Z1=arctan2(2∗(yz−xw),2∗(xy+zw))Y2=arccos(1−2(x2+y2))Z3=arctan2(2∗(yz−xw),2∗(xz−yw))(24)
- 在Z1-X2-Z3方向上:
{ Z 1 = a r c t a n 2 ( 2 ∗ ( − x z − y w ) , 2 ∗ ( y z − x w ) ) X 2 = a r c c o s ( 1 − 2 ( x 2 + y 2 ) ) Z 3 = a r c t a n 2 ( 2 ∗ ( x z − y w ) , 2 ∗ ( y z − x w ) ) (25) \begin{cases} Z1=arctan2(2*(-xz-yw),2*(yz-xw)) \\ X2=arccos(1-2(x^2+y^2)) \\ Z3=arctan2(2*(xz-yw),2*(yz-xw)) \\ \end{cases}\tag{25} ⎩⎪⎨⎪⎧Z1=arctan2(2∗(−xz−yw),2∗(yz−xw))X2=arccos(1−2(x2+y2))Z3=arctan2(2∗(xz−yw),2∗(yz−xw))(25)
以上就是12中四元数转换为欧拉角的方法。
代码实现如下:
import math#以Z-Y-X为例
def Quaternion2Eulerransform_zyx(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truer = math.atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y))p = math.asin(2 * (w * y - z * x))y = math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度r = math.degrees(r)p = math.degrees(p)y = math.degrees(y)return [r,p,y]#以Z-X-Y为例
def Quaternion2Eulerransform_zxy(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truer = math.asin(2 * (w * x + y * z))p = math.atan2(2 * (w * y - z * x), 1 - 2 * (x * x + y * y))y = math.atan2(2 * (w * z - x * y), 1 - 2 * (z * z + x * x))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度r = math.degrees(r)p = math.degrees(p)y = math.degrees(y)return [r,p,y]#以X-Y-Z为例
def Quaternion2Eulerransform_xyz(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truer = math.atan2(2 * (w * x-y*z), 1-2*y*y-2*x*x)p = math.asin(2 * (w * y + z * x))y = math.atan2(2 * (w * z - y * x), 1-2*(y*y+z*z))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度r = math.degrees(r)p = math.degrees(p)y = math.degrees(y)return [r,p,y]#以X-Z-Y为例
def Quaternion2Eulerransform_xzy(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truer = math.atan2(2 * (y*z-w * z), 1-2*z*z-2*x*x)p = math.atan2(2 * (x*z-w * y), 1-2*z*z-2*y*y)y = math.asin(2 * (w * z + x * y))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度r = math.degrees(r)p = math.degrees(p)y = math.degrees(y)return [r,p,y]#以Y-X-Z为例
def Quaternion2Eulerransform_yxz(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truer = math.asin(2 * (w * x - z * y))p = math.atan2(2 * (x*z+w * y), 1-2*x*x-2*y*y)y = math.atan2(2 * (x*y+w * z), 1-2*x*x-2*z*z)#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度r = math.degrees(r)p = math.degrees(p)y = math.degrees(y)return [r,p,y]#以Y-Z-X为例
def Quaternion2Eulerransform_yzx(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truer = math.atan2(2 * (w*x-y*z), 1-2*z*z-2*x*x)p = math.atan2(2 * (w*y-x*z), 1-2*z*z-2*y*y)y = math.asin(2 * (y * x - z * w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度r = math.degrees(r)p = math.degrees(p)y = math.degrees(y)return [r,p,y]#以x1-z2-x3为例
def Quaternion2Eulerransform_xzx(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truex1 = math.atan2(2 * (z*x-y*w), 2*(x*y+z*w))z2 = math.acos(1-2*(y*y+z*z))x3 = math.atan2(2 * (-z*x-y*w), 2*(x*y-z*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度x1 = math.degrees(x1)z2 = math.degrees(z2)x3 = math.degrees(x3)return [x1,z2,x3]#以x1-y2-x3为例
def Quaternion2Eulerransform_xyx(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truex1 = math.atan2(2 * (-z*w-y*x), 2*(x*z+y*w))y2 = math.acos(1-2*(y*y+z*z))x3 = math.atan2(2 * (y*x-z*w), 2*(x*z+y*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度x1 = math.degrees(x1)y2 = math.degrees(y2)x3 = math.degrees(x3)return [x1,y2,x3]#以y1-x2-y3为例
def Quaternion2Eulerransform_yxy(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truey1 = math.atan2(2 * (-z*w+y*x), 2*(y*z+x*w))x2 = math.acos(1-2*(x*x+z*z))y3 = math.atan2(2 * (-y*x-z*w), 2*(y*z+x*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度y1 = math.degrees(y1)x2 = math.degrees(x2)y3 = math.degrees(y3)return [y1,x2,y3]#以y1-x2-y3为例
def Quaternion2Eulerransform_yxy(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truey1 = math.atan2(2 * (-z*w+y*x), 2*(y*z+x*w))x2 = math.acos(1-2*(x*x+z*z))y3 = math.atan2(2 * (-y*x-z*w), 2*(y*z+x*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度y1 = math.degrees(y1)x2 = math.degrees(x2)y3 = math.degrees(y3)return [y1,x2,y3]#以y1-z2-y3为例
def Quaternion2Eulerransform_yzy(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truey1 = math.atan2(2 * (-z*y-w*x), 2*(y*x-z*w))z2 = math.acos(1-2*(x*x+z*z))y3 = math.atan2(2 * (y*z-x*w), 2*(y*x-z*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度y1 = math.degrees(y1)z2 = math.degrees(z2)y3 = math.degrees(y3)return [y1,z2,y3]#以z1-y2-z3为例
def Quaternion2Eulerransform_zyz(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truez1 = math.atan2(2 * (z*y-w*x), 2*(z*x+y*w))y2 = math.acos(1-2*(x*x+y*y))z3 = math.atan2(2 * (-y*z-x*w), 2*(z*x-y*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度z1 = math.degrees(z1)y2 = math.degrees(y2)z3 = math.degrees(z3)return [z1,y2,z3]#以z1-x2-z3为例
def Quaternion2Eulerransform_zxz(w,x,y,z):"""四元素欧->欧拉角"""angle_is_not_rad = Truez1 = math.atan2(2 * (-z*x-w*y), 2*(z*y-z*w))x2 = math.acos(1-2*(x*x+y*y))z3 = math.atan2(2 * (x*z-y*w), 2*(z*y+x*w))#将结果转换为角度if angle_is_not_rad : # pi -> 180 #将数值转换为角度z1 = math.degrees(z1)x2 = math.degrees(x2)z3 = math.degrees(z3)return [z1,x2,z3]if __name__ =="__main__":input_data=[0.5,0.5,0.5,0.5] #测试数据list_1=Quaternion2Eulerransform_zyx(input_data[0],input_data[1],input_data[2],input_data[3])list_2=Quaternion2Eulerransform_zxy(input_data[0],input_data[1],input_data[2],input_data[3])list_3=Quaternion2Eulerransform_xyz(input_data[0],input_data[1],input_data[2],input_data[3])list_4=Quaternion2Eulerransform_xzy(input_data[0],input_data[1],input_data[2],input_data[3])list_5=Quaternion2Eulerransform_yxz(input_data[0],input_data[1],input_data[2],input_data[3])list_6=Quaternion2Eulerransform_yzx(input_data[0],input_data[1],input_data[2],input_data[3])list_7=Quaternion2Eulerransform_xzx(input_data[0],input_data[1],input_data[2],input_data[3])list_8=Quaternion2Eulerransform_xyx(input_data[0],input_data[1],input_data[2],input_data[3])list_9=Quaternion2Eulerransform_yxy(input_data[0],input_data[1],input_data[2],input_data[3])list_10=Quaternion2Eulerransform_yzy(input_data[0],input_data[1],input_data[2],input_data[3])list_11=Quaternion2Eulerransform_zyz(input_data[0],input_data[1],input_data[2],input_data[3])list_12=Quaternion2Eulerransform_zxz(input_data[0],input_data[1],input_data[2],input_data[3])#输出print("在Z-Y-X的情况下: "+"x_ola="+str(list_1[0])+'; '+"y_ola="+str(list_1[1])+'; '+"z_ola="+str(list_1[2])) print("在Z-X-Y的情况下: "+"x_ola="+str(list_2[0])+'; '+"y_ola="+str(list_2[1])+'; '+"z_ola="+str(list_2[2]))print("在X-Y-Z的情况下: "+"x_ola="+str(list_3[0])+'; '+"y_ola="+str(list_3[1])+'; '+"z_ola="+str(list_3[2]))print("在X-Z-Y的情况下: "+"x_ola="+str(list_4[0])+'; '+"y_ola="+str(list_4[1])+'; '+"z_ola="+str(list_4[2]))print("在Y-X-Z的情况下: "+"x_ola="+str(list_5[0])+'; '+"y_ola="+str(list_5[1])+'; '+"z_ola="+str(list_5[2]))print("在Y-Z-X的情况下: "+"x_ola="+str(list_6[0])+'; '+"y_ola="+str(list_6[1])+'; '+"z_ola="+str(list_6[2]))print("在x1-z2-x3的情况下: "+"x1_ola="+str(list_7[0])+'; '+"z2_ola="+str(list_7[1])+'; '+"x3_ola="+str(list_7[2]))print("在x1-y2-x3的情况下: "+"x1_ola="+str(list_8[0])+'; '+"y2_ola="+str(list_8[1])+'; '+"x3_ola="+str(list_8[2]))print("在y1-x2-y3的情况下: "+"y1_ola="+str(list_9[0])+'; '+"x2_ola="+str(list_9[1])+'; '+"y3_ola="+str(list_9[2]))print("在y1-z2-y3的情况下: "+"y1_ola="+str(list_10[0])+'; '+"z2_ola="+str(list_10[1])+'; '+"y3_ola="+str(list_10[2]))print("在z1-y2-z3的情况下: "+"z1_ola="+str(list_11[0])+'; '+"y2_ola="+str(list_11[1])+'; '+"z3_ola="+str(list_11[2]))print("在z1-x2-z3的情况下: "+"z1_ola="+str(list_12[0])+'; '+"x2_ola="+str(list_12[1])+'; '+"z3_ola="+str(list_12[2]))
参考文献:
https://encyclopedia.thefreedictionary.com/Euler+angles[^1]
https://zhuanlan.zhihu.com/p/103622849[^2]
https://zhuanlan.zhihu.com/p/104288667[^3]
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
