SLAM库系列-Eigen库:旋转矩阵、旋转向量、四元数的构造与相互转换

该代码示例展示了如何在C++中使用Eigen库进行旋转向量、旋转矩阵和四元数之间的转换。首先定义了一个沿Z轴旋转45度的旋转向量,然后生成对应的旋转矩阵和四元数。接着,代码演示了四种不同的转换方式:旋转矩阵到旋转向量、旋转矩阵到四元数、旋转向量到旋转矩阵以及旋转向量到四元数,最后是四元数到旋转向量和旋转矩阵的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码段内详细注释:

/*********************
* 目标:已知旋转向量定义为沿着Z轴旋转45°。实现旋转向量、旋转矩阵、四元数之间互相转换
*********************/
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Geometry>

using namespace std;

int main(int argc, char** argv) {
    //沿Z轴旋转45°的旋转向量
    Eigen::AngleAxisd rotation_vector(M_PI/4, Eigen::Vector3d(0, 0, 1));
    cout << "旋转向量的旋转轴 = \n" << rotation_vector.axis() << "\n 旋转向量角度 = " 
         << rotation_vector.angle() << endl;

    //沿Z轴旋转45°的旋转矩阵
    Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();//创建单位矩阵
    rotation_matrix << 0.707, -0.707, 0,
                       0.707, 0.707,  0,
                       0,      0,     1;
    cout << "旋转矩阵 = \n" << rotation_matrix << endl;

    //沿Z轴旋转45°的四元数
    Eigen::Quaterniond quat = Eigen::Quaterniond(0, 0, 0.383, 0.924);
    cout << "四元数 = \n" << quat.coeffs() << endl;
    cout << "其中: \n x =" << quat.x() << "\n y = " << quat.y() << "\n z = " << quat.z()
         << "\n w = " << quat.w() << endl;

    //以下是互相转换
    //1. 旋转矩阵转换旋转向量
    rotation_vector.fromRotationMatrix(rotation_matrix);//方法一
    cout << "(旋转矩阵转换得到)旋转向量的旋转轴(方法一) = \n" << rotation_vector.axis() << "\n 旋转向量的角度 = "
         << rotation_vector.angle() << endl;
    rotation_vector = rotation_matrix;//方法二
    cout << "(旋转矩阵转换得到)旋转向量的旋转轴(方法二) = \n" << rotation_vector.axis() << "\n 旋转向量的角度 = "
         << rotation_vector.angle() << endl;
    rotation_vector = Eigen:AngleAxisd(rotation_matrix);//方法三
    cout << "(旋转矩阵转换得到)旋转向量的旋转轴(方法三) = \n" << rotation_vector.axis() << "\n 旋转向量的角度 = "
         << rotation_vector.angle() << endl;
    
    //2. 旋转矩阵转换四元数
    quat = Eigen:Quaterniond(rotation_matrix);//方法一
    cout << "(旋转矩阵转换得到)四元数(方法一) = \n" << quat.coeffs() << endl;
    quat = rotation_matrix;//方法二
    cout << "(旋转矩阵转换得到)四元数(方法二) = \n" << quat.coeffs() << endl;

    //3. 旋转向量转换旋转矩阵
    cout << "(旋转向量转换得到)旋转矩阵(方法一) = \n" << rotation_vector.matrix() << endl;//方法一
    cout << "(旋转向量转换得到)旋转矩阵(方法二) = \n" << rotation_vector.toRotationMatrix() << endl;//方法二

    //4. 旋转向量转换四元数
    quat = Eigen::Quaterniond(rotation_vector);
    cout << "(旋转向量转换得到)四元数 = \n" << quat.coeffs() << endl;

    //5. 四元数转换为旋转向量
    rotation_vector = quat;
    cout << "(四元数转换得到)旋转向量的旋转轴 = \n" << rotation_vector.axis() << "旋转向量的角度 = "
         << rotation_vector.angle() << endl;

    //6. 四元数转换为旋转矩阵
    rotation_matrix = quat.matrix();//方法一
    cout << "(四元数转换得到)旋转矩阵(方法一) = \n" << rotation_matrix << endl;
    rotation_matrix = quat.toRotationMatrix();//方法二
    cout << "(四元数转换得到)旋转矩阵(方法二) = \n" << rotation_matrix << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值