作者:乐民修德 | 来源:互联网 | 2023-09-24 13:51
编写程序时有时会遇到在四元数、旋转矩阵、欧拉角之间进行转换的操作,使用eulerAngles函数从旋转矩阵中获得欧拉角,了解其使用方法才能保证转换时不出错。
头文件 使用这个函数只需#include
即可,源代码位置为/usr/include/eigen3/Eigen/src/Geometry/EulerAngles.h
,源代码已上传,地址为https://download.csdn.net/download/qingtian11112/12292259,方便查看。
CMakeLists.txt find_package(Eigen3 REQUIRED) include_directories(${EIGEN3_INCLUDE_DIRS})
函数原型 template < typename Derived> inline Matrix< typename MatrixBase< Derived> :: Scalar, 3 , 1 > MatrixBase< Derived> :: eulerAngles ( Index a0, Index a1, Index a2) const
使用方法 从函数原型上&#xff0c;可以看出其有三个参数&#xff0c;a0、a1、a2的取值为{0,1,2},分别表示X轴&#xff0c;Y轴和Z轴。 例如
Vector3f ea &#61; mat. eulerAngles ( 2 , 0 , 2 ) ;
等价于
mat &#61;&#61; AngleAxisf ( ea[ 0 ] , Vector3f:: UnitZ ( ) ) * AngleAxisf ( ea[ 1 ] , Vector3f:: UnitX ( ) ) * AngleAxisf ( ea[ 2 ] , Vector3f:: UnitZ ( ) ) ;
即mat
为按照参数中相应的顺序右乘得到。 如果已知 A&#61;Rx[ϕ]Ry[θ]Rz[ψ]A&#61;R_x[\phi]R_y[\theta]R_z[\psi] A &#61; R x [ ϕ ] R y [ θ ] R z [ ψ ] 可以使用
Vector3f ea &#61; R. eulerAngles ( 0 , 1 , 2 ) ;
则 {ea[0]&#61;ϕea[1]&#61;θea[2]&#61;ψ\begin{cases} ea[0]&#61;\phi \\ ea[1]&#61;\theta \\ ea[2]&#61;\psi \end{cases} ⎩ ⎪ ⎨ ⎪ ⎧ e a [ 0 ] &#61; ϕ e a [ 1 ] &#61; θ e a [ 2 ] &#61; ψ
存在的问题 这个函数转换出来的欧拉角可能不稳定&#xff0c;如下图所示&#xff1a; 形成这个现象的原因在四元数和欧拉角互相转换这篇知乎回答中有解释&#xff0c;其中也给出了解决办法&#xff0c;但要注意这个解法是针对左手旋转 的&#xff0c;要使用的话需要根据情况进行修改。也可参考这篇英文维基Conversion between quaternions and Euler angles