Eigen实现欧拉角、四元数和旋转矩阵之间的变换

include相应的头文件

#include <Eigen/Geometry>

旋转矩阵和旋转向量的表示和声明及旋转

// 3D 旋转矩阵直接使用 Matrix3d 或 Matrix3f
Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
// 旋转向量使用 AngleAxis, 它底层不直接是 Matrix ,但运算可以当作矩阵(因为重载了运算符)
Eigen::AngleAxisd rotation_vector ( M_PI/4, Eigen::Vector3d ( 0,0,1 ) ); // 沿 Z 轴旋转 45 度
cout .precision(3);
cout<<"rotation matrix =\n"<<rotation_vector.matrix() <<endl; //用
matrix() 转换成矩阵
// 用 AngleAxis 可以进行坐标变换
Eigen::Vector3d v ( 1,0,0 );
Eigen::Vector3d v_rotated = rotation_vector * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
// 或者用旋转矩阵
v_rotated = rotation_matrix * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;

欧拉角:可以将旋转矩阵直接转换成欧拉角

Eigen::Vector3d euler_angles = rotation_matrix.eulerAngles ( 2,1,0 ); // ZYX 顺序,即 yaw pitch roll
顺序
cout<<"yaw pitch roll = "<<euler_angles.transpose()<<endl;

欧氏变换矩阵使用 Eigen::Isometry

Eigen::Isometry3d T=Eigen::Isometry3d::Identity(); //虽然称为 3d ,实质上是 4*4 的矩阵
T.rotate ( rotation_vector ); //按照 rotation_vector 进行旋转
T.pretranslate ( Eigen::Vector3d ( 1,3,4 ) ); //把平移向量设成 (1,3,4)
cout << "Transform matrix = \n" << T.matrix() <<endl;

用变换矩阵进行坐标变换

Eigen::Vector3d v_transformed = T*v; //
相当于 R*v+t
cout<<"v tranformed = "<<v_transformed.transpose()<<endl;

对于仿射和射影变换,使用 Eigen::Affine3d 和 Eigen::Projective3d 即可
四元数
q=[cos(θ/2),nxsin(θ/2),nysin(θ/2),nzsin(θ/2)]

    // 可以直接把 AngleAxis 赋值给四元数,反之亦然
    Eigen::Quaterniond q = Eigen::Quaterniond ( rotation_vector );
    cout<<"quaternion = \n"<<q.coeffs() <<endl; // 请注意 coeffs 的顺序是 (x,y,z,w), w 为实部,前三者为虚
    部
    // 使用四元数旋转一个向量,使用重载的乘法即可
v_rotated = q*v; // 注意数学上是 qvq^{-1}
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;

想进一步了解 Eigen 的几何模块可以参考(http://eigen.
tuxfamily.org/dox/group__TutorialGeometry.html)

猜你喜欢

转载自blog.csdn.net/qq_30339595/article/details/84780046