eigen——旋转向量、旋转矩阵、四元数、欧拉角的初始化及相互转化

//
// Created by iqr on 2019/11/6.
//

#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Dense>

using namespace std;
#define sqr_2 sqrt(2)

int main(int argc, char **argv)
{
    double theta = M_PI / 4;
    Eigen::AngleAxisd rotation_vec(theta, Eigen::Vector3d(0, 0, 1));     // 旋转向量
    Eigen::Matrix3d rotation_mat;
    rotation_mat<< sqr_2/2, sqr_2/(-2), 0,
                   sqr_2/2, sqr_2/2,    0,
                   0,       0,          1;    // 旋转矩阵
    Eigen::Quaterniond rotation_quat(cos(theta/2), 0, 0, sin(theta/2));  // 四元数
    Eigen::Vector3d rotation_eulerAngle(theta, 0, 0);    // 欧拉角

    Eigen::AngleAxisd vec;  // 旋转向量
    Eigen::Matrix3d mat;    // 旋转矩阵
    Eigen::Quaterniond quat;// 四元数
    Eigen::Vector3d euler;  // 欧拉角
    // 旋转向量转其他
    printf(">>> 旋转向量转其他形式:\n");
    mat = rotation_vec.matrix();
    cout << "旋转矩阵=\n" << mat << endl;
    quat = rotation_vec;
    cout << "四元数: (x, y, z, w)=(" << quat.x() << "," << quat.y() << "," << quat.z() << "," << quat.w() << ")" << endl;
    euler = rotation_vec.matrix().eulerAngles(2,1,0);
    cout << "欧拉角: (roll, pitch, yaw)=" << euler << endl << endl;

    // 旋转矩阵转其他
    printf(">>> 旋转矩阵转其他形式:\n");
    vec = rotation_mat;
    cout << "旋转向量: axis=" << vec.axis() << "\nangle=" << vec.angle() << endl;
    quat = rotation_mat;
    cout << "四元数: (x, y, z, w)=(" << quat.x() << "," << quat.y() << "," << quat.z() << "," << quat.w() << ")" << endl;
    euler = rotation_mat.eulerAngles(2,1,0);
    cout << "欧拉角: (roll, pitch, yaw)=" << euler << endl << endl;

    // 四元数转其他
    printf(">>> 四元数转其他形式:\n");
    vec = rotation_quat;
    cout << "旋转向量: axis=" << vec.axis() << "\nangle=" << vec.angle() << endl;
    mat = rotation_quat.toRotationMatrix();
    cout << "旋转矩阵=\n" << mat << endl;
    euler = mat.eulerAngles(2,1,0);
    cout << "欧拉角: (yaw, pitch, roll)=" << euler << endl << endl;

    // 欧拉角转其他
    printf(">>> 欧拉角转其他形式:\n");
    Eigen::AngleAxisd rollAngle(Eigen::AngleAxisd(rotation_eulerAngle(2),Eigen::Vector3d::UnitX()));
    Eigen::AngleAxisd pitchAngle(Eigen::AngleAxisd(rotation_eulerAngle(1),Eigen::Vector3d::UnitY()));
    Eigen::AngleAxisd yawAngle(Eigen::AngleAxisd(rotation_eulerAngle(0),Eigen::Vector3d::UnitZ()));
    vec = yawAngle * pitchAngle * rollAngle;
    cout << "旋转向量: axis=" << vec.axis() << "\nangle=" << vec.angle() << endl;
    mat = yawAngle * pitchAngle * rollAngle;
    cout << "旋转矩阵=\n" << mat << endl;
    quat = yawAngle * pitchAngle * rollAngle;
    cout << "四元数: (x, y, z, w)=(" << quat.x() << "," << quat.y() << "," << quat.z() << "," << quat.w() << ")" << endl << endl;

    // 欧式变换矩阵
    printf(">>> 旋转向量+平移向量=欧式变换矩阵");
    Eigen::Vector3d translation(1, 2, 3);
    Eigen::Isometry3d T = Eigen::Isometry3d::Identity();    // 虽然称为3d,实质上是4*4的矩阵 齐次坐标
    T.rotate (rotation_mat);                                // 按照rotation_vec进行旋转
    T.pretranslate (translation);     // 把平移向量设成(1,2,3)
    cout << "欧式变换矩阵=\n" << T.matrix() << endl << endl;

    // 欧式变换矩阵分解
    printf(">>> 欧式变换矩阵分解为:旋转向量+平移向量");
    mat = T.rotation();
    translation = T.translation();
    cout << "旋转矩阵=\n" << mat << endl;
    cout << "平移矩阵=\n" << translation << endl << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值