添加Sophus库时CMake找不到Sophus的问题
刚开始使用Sophus时,会遇到这样的错误,
CMake Error at CMakeLists.txt:5 (find_package):
By not providing "FindSophus.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Sophus", but
CMake did not find one.
Could not find a package configuration file provided by "Sophus" with any
of the following names:
SophusConfig.cmake
sophus-config.cmake
Add the installation prefix of "Sophus" to CMAKE_PREFIX_PATH or set
"Sophus_DIR" to a directory containing one of the above files. If "Sophus"
provides a separate development package or SDK, be sure it has been
installed.
=
-- Configuring incomplete, errors occurred!
参考这篇博客的思路,https://blog.youkuaiyun.com/weixin_38213410/article/details/98114423
进到自己Sophus 源程序文件夹,打开SophusConfig.cmake 文件,文件位置如图1所示。
打开该文件,看到Sophus_DIR的路径。在cmakelist 文件中设置如下:
cmake_minimum_required(VERSION 3.19)
project(DataStructure)
set(CMAKE_CXX_STANDARD 14)
set(Sophus_DIR "/home/xiujie/SLAM_Lib/Sophus/build")
# 为使用 sophus,您需要使用find_package命令找到它
find_package( Sophus REQUIRED )
include_directories( ${Sophus_INCLUDE_DIRS} )
include_directories("/usr/include/eigen3")
add_executable(DataStructure main.cpp)
target_link_libraries( DataStructure ${Sophus_LIBRARIES} )
此处set 命令行中的路径即为查找到的路径。
测试代码源自:《视觉SLAM14讲》-ch4:useSophus,直接可用。
main.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;
return 0;
}
result
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
Process finished with exit code 0