写在前面
1、本文内容
在自己的程序中使用Eigen库,用cmake实现
2、平台
windows,linux
3、转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/126319996
准备
下载Eigen源码放在想要的位置,此处为source_code,并解压
https://eigen.tuxfamily.org/index.php?title=Main_Page
以当前下载的cmake3.4为例,解压后的目录结构为source_code/eigen-3.4.0
通过cmake安装
cd source_code
cmake -S ./eigen-3.4.0 -B ./build_eigen -DCMAKE_INSTALL_PREFIX="D:/carlos/install/Eigen"
cmake --build ./build_eigen --target install
其中-DCMAKE_INSTALL_PREFIX="D:/carlos/install/Eigen"就是你想安装的位置,成功安装完成之后,会在相应位置找到Eigen
使用
实现欧拉角转旋转矩阵的程序euler2rt.cpp
#include <Eigen/Core>
#include <Eigen/Dense>
#include <iostream>
#define PI 3.1415926
int main(int argc, char* argv[]){
std::cout<<PI<<std::endl;
if(argc<4){
std::cout<<"please input a 3x1 vector,for example:\neuler2rt 45 30 60"<<std::endl;
return 0;
}
Eigen::Vector3d eulerAngle(atof(argv[1]),atof(argv[2]),atof(argv[3]));
std::cout<<"eulerAngle:\nx: "<<eulerAngle[0]<<" y: "<<eulerAngle[1]<<" z: "<<eulerAngle[2]<<std::endl;
eulerAngle=eulerAngle/180*PI;
Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
Eigen::AngleAxisd rollAngle(Eigen::AngleAxisd(eulerAngle[0],Eigen::Vector3d::UnitX()));
Eigen::AngleAxisd pitchAngle(Eigen::AngleAxisd(eulerAngle[1],Eigen::Vector3d::UnitY()));
Eigen::AngleAxisd yawAngle(Eigen::AngleAxisd(eulerAngle[2],Eigen::Vector3d::UnitZ()));
rotation_matrix=rollAngle*pitchAngle*yawAngle;
std::cout<<"rotation_matrix:\n"<<rotation_matrix<<std::endl;
Eigen::Vector3d eulerAngle2=rotation_matrix.eulerAngles(0,1,2);
std::cout<<"eulerAngle:\nx: "<<eulerAngle2[0]/PI*180<<" y: "<<eulerAngle2[1]/PI*180<<" z: "<<eulerAngle2[2]/PI*180<<std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(Euler2rt)
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIRS})
add_executable (euler2rt euler2rt.cpp)
编译
cmake -S ./ -B ./build -DEigen3_DIR="D:/carlos/install/Eigen/share/eigen3/cmake"
cmake --build ./build --config Release --parallel 8
其中,-DEigen3_DIR就是指定Eigen安装路径下cmake的路径
参考
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料 https://blog.csdn.net/qq_41102371/article/details/126002167文章来源:https://www.toymoban.com/news/detail-400404.html
完
--------------------------------------------------------------------------------------------202208文章来源地址https://www.toymoban.com/news/detail-400404.html
到了这里,关于cmake+Eigen库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!