高翔视觉slam(第二版)第四讲:带模板的Sophus库安装

一、安装fmt库

由于Sophus模板库是依赖于fmt库的,因此要先安装fmt库;

git clone  https://github.com/fmtlib/fmt.git
cd fmt
mkdir build
cd build
cmake ..
make
sudo make install

安装好fmt库后再安装Sophus模板库
(不要用git checkout a621ff指令,这个指令会切换到非模板类sophus库)

cd Sophus
mkdir build 
cd build
cmake ..
make

二、安装Sophus库

git clone https://github.com/strasdat/Sophus.git
cd Sophus
mkdir build 
cd build
cmake ..
make
sudo make install#这一步是把编译好的库安装到系统中,不然在运行时会报错

编译Sophus的过程中,提示如下错误:

/Sophus/sophus/so2.cpp:32:26: error: lvalue required as left operand of assignment
unit_complex_.real() = 1.;

/Sophus/sophus/so2.cpp:33:26: error: lvalue required as left operand of assignment
unit_complex_.imag() = 0.;


解决方法:将上文修改为

SO2::SO2()
{
//unit_complex_.real() = 1.;
//unit_complex_.imag() = 0.;
unit_complex_.real(1.);
unit_complex_.imag(0.);
}

三、useSophus源代码替换如下:

#include <iostream>
#include <cmath>
using namespace std; 
 
#include <Eigen/Core>
#include <Eigen/Geometry>
 
#include "sophus/so3.hpp"
#include "sophus/se3.hpp"

int main( int argc, char** argv )
{
    // 沿Z轴转90度的旋转矩阵
    Eigen::Matrix3d R = Eigen::AngleAxisd(M_PI/2, Eigen::Vector3d(0,0,1)).toRotationMatrix();
    
    Sophus::SO3<double> SO3_R(R);               // Sophus::SO(3)可以直接从旋转矩阵构造
//  Sophus::SO3 SO3_v( 0, 0, M_PI/2 );  // 亦可从旋转向量构造 
    Eigen::Quaterniond q(R);            // 或者四元数
    Sophus::SO3<double> SO3_q( q );
    // 上述表达方式都是等价的
    // 输出SO(3)时,以so(3)形式输出
    cout<<"SO(3) from matrix: "<<SO3_R.log()<<endl;
//  cout<<"SO(3) from vector: "<<SO3_v<<endl;
    cout<<"SO(3) from quaternion :"<<SO3_q.log()<<endl;
    
    // 使用对数映射获得它的李代数
    Eigen::Vector3d so3 = SO3_R.log();
    cout<<"so3 = "<<so3.transpose()<<endl;
    // hat 为向量到反对称矩阵
    cout<<"so3 hat=\n"<<Sophus::SO3<double>::hat(so3)<<endl;
    // 相对的,vee为反对称到向量
    cout<<"so3 hat vee= "<<Sophus::SO3<double>::vee( Sophus::SO3<double>::hat(so3) ).transpose()<<endl; // transpose纯粹是为了输出美观一些
    
    // 增量扰动模型的更新
    Eigen::Vector3d update_so3(1e-4, 0, 0); //假设更新量为这么多
    Sophus::SO3<double> SO3_updated = Sophus::SO3<double>::exp(update_so3)*SO3_R;
    cout<<"SO3 updated = "<<SO3_updated.log()<<endl;
    
    /********************分割线*****************************/
    cout<<"************我是分割线*************"<<endl;
    // 对SE(3)操作大同小异
    Eigen::Vector3d t(1,0,0);           // 沿X轴平移1
    Sophus::SE3<double> SE3_Rt(R, t);           // 从R,t构造SE(3)
    Sophus::SE3<double> SE3_qt(q,t);            // 从q,t构造SE(3)
    cout<<"SE3 from R,t= "<<endl<<SE3_Rt.log()<<endl;
    cout<<"SE3 from q,t= "<<endl<<SE3_qt.log()<<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<double>::hat(se3)<<endl;
    cout<<"se3 hat vee = "<<Sophus::SE3<double>::vee( Sophus::SE3<double>::hat(se3) ).transpose()<<endl;
    
    // 最后,演示一下更新
    Vector6d update_se3; //更新量
    update_se3.setZero();
    update_se3(0,0) = 1e-4d;
    Sophus::SE3<double> SE3_updated = Sophus::SE3<double>::exp(update_se3)*SE3_Rt;
    cout<<"SE3 updated = "<<endl<<SE3_updated.matrix()<<endl;
    
    return 0;
}

四、trajectoryError运行报错
1.运行ch4的example报错:

/usr/bin/ld: CMakeFiles/trajectoryError.dir/trajectoryError.cpp.o: in function `std::make_unsigned::type fmt::v8::detail::to_unsigned(int)’: 等等····

原因:应遵循调用层次,从依赖到被依赖。所以在CMakeLists.txt中添加:

target_link_libraries(trajectoryError ${Sophus_LIBRARIES} fmt)

trajectoryError依赖于Sophus,Sophus依赖于fmt。

2.路径问题

代码中修改为:

string groundtruth_file = "../../example/groundtruth.txt";
string estimated_file = "../../example/estimated.txt";

在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值