半闲居士视觉SLAM十四讲笔记(4)李群与李代数 - part 3 Sophus

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/youngpan1101/article/details/71088082

本系列文章由 youngpan1101 出品,转载请注明出处。
文章链接: http://blog.csdn.net/youngpan1101/article/details/71088082
作者:宋洋鹏(youngpan1101)
邮箱: [email protected]


-W350_2.gif-2.gif
该讲详细资料下载链接 【Baidu Yun】【Video】【Code

若您觉得本博文对您有帮助,请支持高博的新书《视觉SLAM十四讲》,【点击购买
若您觉得本博文对您有帮助,请支持高博的新书《视觉SLAM十四讲》,【点击购买
若您觉得本博文对您有帮助,请支持高博的新书《视觉SLAM十四讲》,【点击购买


李群与李代数

实践 Sophus

Sophus 简介

  • Eigen 提供几何模块,但不支持李代数。
  • Strasdat 维护的 Sophus 库 支持二维运动的 SO(2),SE(2) 和三维运动的 SO(3),SE(3),Sim(3) ,该库基于 Eigen,不需要其他的依赖库。[Sophus Github]
  • Sophus 早期版本只提供双精度的李群、李代数类,后续版本改写成了模板类,同时增加了使用难度,这里我们使用 的 Sophus 库,可以输入以下命令来获得非模板类的 Sophus :
git clone https://github.com/strasdat/Sophus.git 
cd Sophus 
git checkout a621ff 

编译 Sophus

  • 这里直接使用代码目录 slambook/3rdparty下提供的 Sophus 源码包
 tar -xzvf Sophus.tar.gz
 cd Sophus
 mkdir build
 cd build
 cmake ..
 make

工程代码和输出结果

  • CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( useSophus )

# 为使用 sophus,您需要使用find_package命令找到它
find_package( Sophus REQUIRED )
include_directories( ${Sophus_INCLUDE_DIRS} )

add_executable( useSophus useSophus.cpp )
target_link_libraries( useSophus ${Sophus_LIBRARIES} )
  • useSophus.cpp
#include <iostream>
#include <cmath>
using namespace std; 

#include <Eigen/Core>
#include <Eigen/Geometry>

#include "sophus/so3.h"
#include "sophus/se3.h"

int main( int argc, char** argv )
{
    // 沿Z轴转90度的旋转矩阵
    Eigen::Matrix3d R = Eigen::AngleAxisd(M_PI/2, Eigen::Vector3d(0,0,1)).toRotationMatrix();

    Sophus::SO3 SO3_R(R);               // Sophus::SO(3)可以直接从旋转矩阵构造
    Sophus::SO3 SO3_v( 0, 0, M_PI/2 );  // 亦可从旋转向量构造
    Eigen::Quaterniond q(R);            // 或者四元数
    Sophus::SO3 SO3_q( q );
    // 上述表达方式都是等价的
    // 输出SO(3)时,以so(3)形式输出
    cout << "SO(3) from matrix: " << SO3_R << endl;
    cout << "SO(3) from vector: " << SO3_v << endl;
    cout << "SO(3) from quaternion :" << SO3_q << endl;

    // 使用对数映射获得它的李代数
    Eigen::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; // transpose纯粹是为了输出美观一些

    // 增量扰动模型的更新
    Eigen::Vector3d update_so3(1e-4, 0, 0); //假设更新量为这么多
    Sophus::SO3 SO3_updated = Sophus::SO3::exp(update_so3)*SO3_R;
    cout << "SO3 updated = " << SO3_updated << endl;

    /********************萌萌的分割线*****************************/
    cout << "************我是分割线*************" << endl;
    // 对SE(3)操作大同小异
    Eigen::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= " << endl << SE3_Rt << endl;
    cout << "SE3 from q,t= " << endl << SE3_qt << 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 = " << endl << 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;
}
  • 输出结果
SO(3) from matrix:      0      0 1.5708

SO(3) from vector:      0      0 1.5708

SO(3) from quaternion :     0      0 1.5708

so3 =      0      0 1.5708
so3 hat=
      0 -1.5708       0
 1.5708       0      -0
     -0       0       0
so3 hat vee=      0      0 1.5708
SO3 updated =  7.85398e-05 -7.85398e-05       1.5708

************我是分割线*************
SE3 from R,t= 
     0      0 1.5708
1 0 0

SE3 from q,t= 
     0      0 1.5708
1 0 0

se3 =  0.785398 -0.785398         0         0         0    1.5708
se3 hat = 
        0   -1.5708         0  0.785398
   1.5708         0        -0 -0.785398
       -0         0         0         0
        0         0         0         0
se3 hat vee =  0.785398 -0.785398         0         0         0    1.5708
SE3 updated = 
2.22045e-16          -1           0      1.0001
          1 2.22045e-16           0           0
          0           0           1           0
          0           0           0           1

猜你喜欢

转载自blog.csdn.net/youngpan1101/article/details/71088082