当前位置 博文首页 > lovely_yoshino的博客:SLAM本质剖析-Sophus

    lovely_yoshino的博客:SLAM本质剖析-Sophus

    作者:[db:作者] 时间:2021-09-16 22:30

    0. 前言

    在了解SLAM的原理、流程后,个人经常实时困惑该如何去从零开始去设计编写一套能够符合我们需求的SLAM框架。作者认为Ceres、Eigen、Sophus、G2O这几个函数库无法避免,而作者在此之前已经对Ceres、Eigen、G2O做了详细的介绍,目前仍剩下Sophus还未进行详写,所以这篇文章作为这个系列的最后一篇文章,主要对Sophus函数库进行详细的阐述,来方便各位后续的开发。

    1. Sophus 示例

    Eigen库是一个开源的C++线性代数库,它提供了快速的有关矩阵的线性代数运算,还包括解方程等功能。但是Eigen库提供了集合模块,但没有提供李代数的支持。一个较好的李群和李代数的库是Sophus库,它很好的支持了SO(3),so(3),SE(3)和se(3)。Sophus库是基于Eigen基础上开发的,继承了Eigen库中的定义的各个类。因此在使用Eigen库中的类时,既可以使用Eigen命名空间,也可以使用Sophus命名空间。比如

    Eigen::Matrix3d 和 Sophus::Matrix3d
    Eigen::Vector3d 和 Sophus::Vector3d
    

    此外,为了方便说明SE(4)和se(4),Sophus库还typedef了Vector4d、Matrix4d、Vector6d和Matrix6d等,即:

    Sophus::Vector4d
    Sophus::Matrix4d
    Sophus::Vector6d
    Sophus::Matrix6d
    
    #include <iostream>
    #include <cmath>
    #include <Eigen/Core>
    #include <Eigen/Geometry>
    #include "sophus/se3.h" //原版本为 sophus/se3.hpp
    
    using namespace std;
    using namespace Eigen;
    
    /// 本程序演示sophus的基本用法
    
    int main(int argc, char **argv) {
    
        // 沿Z轴转90度的旋转矩阵
        Matrix3d R = AngleAxisd(M_PI / 2, Vector3d(0, 0, 1)).toRotationMatrix();
        // 或者四元数
        Quaterniond q(R);
        Sophus::SO3 SO3_R(R);              // Sophus::SO3可以直接从旋转矩阵构造
        Sophus::SO3 SO3_q(q);              // 也可以通过四元数构造
        // 二者是等价的
        cout << "SO(3) from matrix:\n" << SO3_R.matrix() << endl;
        cout << "SO(3) from quaternion:\n" << SO3_q.matrix() << endl;
        cout << "they are equal" << endl;
    
        // 使用对数映射获得它的李代数
        Vector3d so3 = SO3_R.log();
        cout << "so3 = " << so3.transpose() << endl;
        // hat 为向量到反对称矩阵
        cout << "so3 hat=\n" << Sophus::SO3::hat(so3) << endl;
        // 相对的,vee为反对称到向量
        cout << "so3 hat vee= " << Sophus::SO3::vee(Sophus::SO3::hat(so3)).transpose() << endl;
    
        // 增量扰动模型的更新
        Vector3d update_so3(1e-4, 0, 0); //假设更新量为这么多
        Sophus::SO3 SO3_updated = Sophus::SO3::exp(update_so3) * SO3_R;
        cout << "SO3 updated = \n" << SO3_updated.matrix() << endl;
    
        cout << "*******************************" << endl;
        // 对SE(3)操作大同小异
        Vector3d t(1, 0, 0);           // 沿X轴平移1
        Sophus::SE3 SE3_Rt(R, t);           // 从R,t构造SE(3)
        Sophus::SE3 SE3_qt(q, t);            // 从q,t构造SE(3)
        cout << "SE3 from R,t= \n" << SE3_Rt.matrix() << endl;
        cout << "SE3 from q,t= \n" << SE3_qt.matrix() << endl;
        // 李代数se(3) 是一个六维向量,方便起见先typedef一下
        typedef Eigen::Matrix<double, 6, 1> Vector6d;
        Vector6d se3 = SE3_Rt.log();
        cout << "se3 = " << se3.transpose() << endl;
        // 观察输出,会发现在Sophus中,se(3)的平移在前,旋转在后.
        // 同样的,有hat和vee两个算符
        cout << "se3 hat = \n" << Sophus::SE3::hat(se3) << endl;
        cout << "se3 hat vee = " << Sophus::SE3::vee(Sophus::SE3::hat(se3)).transpose() << endl;
    
        // 最后,演示一下更新
        Vector6d update_se3; //更新量
        update_se3.setZero();
        update_se3(0, 0) = 1e-4d;
        Sophus::SE3 SE3_updated = Sophus::SE3::exp(update_se3) * SE3_Rt;
        cout << "SE3 updated = " << endl << SE3_updated.matrix() << endl;
    
        return 0;
    }
    

    Sophus常见函数总结

    Sophus库的各种形式的表示如下:

    …详情请参照古月居

    cs