手写VIO-使用Eigen实现旋转矩阵更新

使用Eigen实现旋转矩阵更新

所有的文件和代码我放在了社区里,水平有限,大家感兴趣可以访问看看
https://github.com/gisnewbird/VIO.git
第一周的习题,比较R和q对旋转矩阵的更新效果,因为是win环境,比较复杂,就只是用Eigen实现了简单的比较,使用Sophus库的实现要更见简单也好理解些,我看到了使用Sophus库实现旋转矩阵更新
这篇整理的不错,就不重复写了。大家可以移步去看看。

====================
2019.10.19
我把Sophus库加上了,重新调整了一下

====================

开始正文

在这里插入图片描述

//
// Created by dukeguo on 2019/10/17.
//
#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Eigen>
#include <Eigen/Dense>
#include <ctime>
#include <sophus/so3.hpp>
#include <sophus/se3.hpp>
因为win中找不到M_PI,就自己定义了
//#define M_PI   3.14159265358979323846

int main(int argv, char ** argc) {
    ///define a minimized vecter
    Eigen::Vector3d w(0.01, 0.02, 0.03);//[0.01, 0.02, 0.03]^T
    Eigen::AngleAxisd rotation_vetor = Eigen::AngleAxisd(M_PI/2, Eigen::Vector3d(0,0,1));
    Eigen::Matrix3d R = rotation_vetor.toRotationMatrix();
    //std::cout << R * R.transpose() << std::endl;
    Sophus::SO3d SO3_R(R);

    // Quaternion update
    // 四元数更新,使用Eigen库
    clock_t time_quaternion = clock();
    std::cout << "=================================" << std::endl;
    Eigen::Quaterniond q = Eigen::Quaterniond(R);//build the R_Quaternion
    //std::cout << "quaternion from rotation vector : " << q.coeffs().transpose() << std::endl;
    Eigen::Quaterniond w_quaternion(1, w[0] / 2, w[1] / 2, w[2] / 2);//w 2 quaternion
    Eigen::Quaterniond q_update = (q * w_quaternion).normalized();//update the quaternion & normalized
    std::cout << "Eigen Quaternion updated : " << std::endl << q_update.toRotationMatrix() << std::endl;
    std::cout << "使用Eigen库进行Quaternion更新 use time " << 1000 * (clock() - time_quaternion) / (double) CLOCKS_PER_SEC << "ms" << std::endl;

    // 李代数更新
    // 使用Sophus库
    clock_t time_Sophus_so3 = clock();
    std::cout << "=================================" << std::endl;
    Eigen::Vector3d update_so3(0.01, 0.02, 0.03); //更新量
    Sophus::SO3d SO3_updated = SO3_R * Sophus::SO3d::exp(update_so3);
    std::cout<<"Sophus SO3 updated = "<< std::endl << SO3_updated.matrix() <<std::endl;
    std::cout << "使用Sophus库进行SO3更新 use time " << 1000 * (clock() - time_Sophus_so3) / (double) CLOCKS_PER_SEC << "ms" << std::endl;

    // 使用Eigen库
    clock_t time_so3 = clock();
    std::cout << "=================================" << std::endl;
    double Theta = sqrt(w[0] * w[0] + w[1] * w[1] + w[2] * w[2]);     	 // Theta = sqrt(x ^ 2 + y ^ 2 + z ^ 2)
    Eigen::Vector3d a = w.array() / Theta;                               // vecter_a = w / Theta
    Eigen::Matrix3d a_hat,I;                                             //             0    -a3      a2
    a_hat <<        0,       -w[2] / Theta,    w[1] / Theta,             // a_hat <<   a3      0     -a1
            w[2] / Theta,        0,        -w[0] / Theta,               //           -a2     a1       0
            -w[1] / Theta,   w[0] / Theta,         0;
    I << 1, 0, 0,
            0, 1, 0,
            0, 0, 1;
    //Rodrigues's formula: cos(Theta) * I + (1 - cos(Theta)) *  vecter_a * vecter_a ^ T + sin(Theta) * a_hat
    Eigen::Matrix3d exp_w_hat = cos(Theta) * I.array() + (1 - cos(Theta)) * (a * a.transpose()).array() + a_hat.array() * sin(Theta);
    //update:
    Eigen::Matrix3d R_update = R * exp_w_hat;
    std::cout << "Eigen SO3 updated : " << std::endl << R_update << std::endl;
    std::cout << "使用Eigen库进行SO3更新 use time " << 1000 * (clock() - time_so3) / (double) CLOCKS_PER_SEC << "ms" << std::endl;

    return 0;
}

运行的结果如下:
在这里插入图片描述
可以看到,用Eigen的话,使用四元数更新的效率更高一些,Sophus库效率的比较就没有那么明显了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

beeean

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值