SLAM十四讲ch4代码调整
代码新环境下的调整——2021.6.9
1.由于使用了新的模板类的Sophus,不能通过3dparty中的压缩包直接安装,具体安装参考https://blog.csdn.net/weixin_44684139/article/details/104803225
2.同样因为时模板类的Sophus,源码中的所有Sophus::SO3都需要声明为Sophus::SO3d 或者 Sophus::SO3.
Sophus::SO3<double>
3.Sophus::SO3 SO3_v( 0, 0, M_PI/2 ) 声明后仍然报错,未找到解决方案,但因为只用于第一段比较,所以删除不影响。
//Sophus::SO3<double> SO3_v( 0, 0, M_PI/2 )
4.按照文中apt编译的Eigen会在/usr/include/eigen3中,如果源码中#include <Eigen/Core>显示未找到,可以将/usr/include/eigen3/Eigen复制到include路径中:
sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen
5.解决了上述问题后,如果编译仍然出错,且是一堆超长的乱码,出现
undefined reference to `fmt::v7::detail::assert_fail(char const*, int, char const*)'
说明未连接fmt库,安装fmt后,在CMakelists.txt中将lib的连接代码改为如下:
target_link_libraries( useSophus ${Sophus_LIBRARIES} fmt)
自己安装库路径会在/usr/local/include下,相关库链接如下:
- Eigen3.3.7版本:https://gitlab.com/libeigen/eigen/-/releases
- Sophus:https://github.com/strasdat/Sophus
- fmt(这个很重要,没有就会一直报错):https://github.com/fmtlib/fmt
源码如下
#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include "sophus/so3.hpp"
#include "sophus/se3.hpp"
using namespace std;
using namespace Eigen;
int main( int argc, char** argv )
{
// 沿Z轴转90度的旋转矩阵
Matrix3d R = AngleAxisd(M_PI/2, Vector3d(0,0,1)).toRotationMatrix();
Quaterniond q(R); // 或者四元数
Sophus::SO3d SO3_R(R); // Sophus::SO(3)可以直接从旋转矩阵构造
Sophus::SO3<double> SO3_q( q );
// 上述表达方式都是等价的
// 输出SO(3)时,以so(3)形式输出
cout<<"SO(3) from matrix: "<<SO3_R.log()<<endl;
cout<<"SO(3) from quaternion :"<<SO3_q.log()<<endl;
// 使用对数映射获得它的李代数
Vector3d so3 = SO3_R.log();
cout<<"so3 = "<<so3.transpose()<<endl;
// hat 为向量到反对称矩阵
cout<<"so3 hat=\n"<<Sophus::SO3d::hat(so3)<<endl;
// 相对的,vee为反对称到向量
cout<<"so3 hat vee= "<<Sophus::SO3d::vee( Sophus::SO3d::hat(so3) ).transpose()<<endl; // transpose纯粹是为了输出美观一些
// 增量扰动模型的更新
Vector3d update_so3(1e-4, 0, 0); //假设更新量为这么多
Sophus::SO3d SO3_updated = Sophus::SO3d::exp(update_so3)*SO3_R;
cout<<"SO3 updated = "<<SO3_updated.log()<<endl;
/********************萌萌的分割线*****************************/
cout<<"************我是分割线*************"<<endl;
// 对SE(3)操作大同小异
Vector3d t(1,0,0); // 沿X轴平移1
Sophus::SE3d SE3_Rt(R, t); // 从R,t构造SE(3)
Sophus::SE3d 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 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::SE3d::hat(se3)<<endl;
cout<<"se3 hat vee = "<<Sophus::SE3d::vee( Sophus::SE3d::hat(se3) ).transpose()<<endl;
// 最后,演示一下更新
Vector6d update_se3; //更新量
update_se3.setZero();
update_se3(0,0) = 1e-4;
Sophus::SE3d SE3_updated = Sophus::SE3d::exp(update_se3)*SE3_Rt;
cout<<"SE3 updated = "<<endl<<SE3_updated.matrix()<<endl;
return 0;
}