- 电脑:Dell vostro 3480
- 操作系统:win10
- 内存:8GB
- 显卡:英特尔 HD Graphics 620
- CPU: 英特尔 酷睿 i5-8265U
一、安装vs2019
- 访问网址:https://visualstudio.microsoft.com/zh-hans/vs/,下载vs2019社区版,并按需要进行安装
二、安装Eigen3
下载Eigen3
- 访问网址:http://eigen.tuxfamily.org/index.php?title=Main_Page,下载并解压。
-
将Eigen文件夹重新复制到自定义文件夹中:
配置
- 新建Eigen属性表:
- VC++目录->包含目录:F:\opensources\Eigen3
测试Eigen
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
Matrix2d a;
a << 1, 2,
3, 4;
MatrixXd b(2,2);
b << 2, 3,
1, 4;
std::cout << "a + b =\n" << a + b << std::endl;
std::cout << "a - b =\n" << a - b << std::endl;
std::cout << "Doing a += b;" << std::endl;
a += b;
std::cout << "Now a =\n" << a << std::endl;
Vector3d v(1,2,3);
Vector3d w(1,0,0);
std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
return 0;
}
测试结果
三、安装suitesparse-metis-for-windows-1.5.0
下载suitesparse-metis-for-windows-1.5.0
CMake编译
- 首先下载CMake:https://cmake.org/download/
- 填写Source code :
F:/opensources/suitesparse-metis-for-windows-1.5.0/suitesparse-metis-for-windows-1.5.0 - 填写Build the binaries:
F:/opensources/suitesparse-metis-for-windows-1.5.0/suitesparse-metis-for-windows-1.5.0/build - 第一次 Condigure:
遇到错误:Can ‘t found lapack line 241.不要慌,将LAPACK_DIR路径换为:
F:/opensources/suitesparse-metis-for-windows-1.5.0/suitesparse-metis-for-windows-1.5.0/lapack_windows/x64 - 第二次点击 Condigure:
HAVE_COMPLEX 为红色不要勾选!不要勾选!不要勾选!再次点击Condigure,然后点击Generate.
生成
-
打开VS2019,将INSTALL设置为启动项,右键生成
配置
- 新建属性表:Suitsparse
- C/C++->附加包含目录:
F:\opensources\suitesparse-metis-for-windows-1.5.0\suitesparse-metis-for-windows-1.5.0\build\install\include
F:\opensources\suitesparse-metis-for-windows-1.5.0\suitesparse-metis-for-windows-1.5.0\build\install\include\suitesparse - 链接器->常规->附加库目录:
F:\opensources\suitesparse-metis-for-windows-1.5.0\suitesparse-metis-for-windows-1.5.0\build\lib
F:\opensources\suitesparse-metis-for-windows-1.5.0\suitesparse-metis-for-windows-1.5.0\build\lib\Debug
F:\opensources\suitesparse-metis-for-windows-1.5.0\suitesparse-metis-for-windows-1.5.0\lapack_windows\x64
F:\opensources\suitesparse-metis-for-windows-1.5.0\suitesparse-metis-for-windows-1.5.0\build\install\lib - 链接器->输入->附加依赖项:
libamdd.lib
libbtfd.lib
libcamdd.lib
libccolamdd.lib
libcholmodd.lib
libcolamdd.lib
libcxsparsed.lib
libklud.lib
libldld.lib
libspqrd.lib
libumfpackd.lib
suitesparseconfigd.lib
libblas.lib
liblapack.lib
metisd.lib
测试
#include <iostream>
#include "Eigen/Eigen"
#include "Eigen/SPQRSupport"
using namespace Eigen ;
int main ( ) {
SparseMatrix < double > A ( 4 , 4 ) ;
std :: vector < Triplet < double > > triplets ;
// 初始化非零元素
int r [ 3 ] = { 0 , 1 , 2 } ;
int c [ 3 ] = { 1 , 2 , 2 } ;
double val [ 3 ] = { 6.1 , 7.2 , 8.3 } ;
for ( int i = 0 ; i < 3 ; ++ i )
triplets . push_back( Triplet < double >(r [ i ] , c [ i ] , val [ i ]) ) ;
// 初始化稀疏矩阵
A . setFromTriplets ( triplets . begin ( ) , triplets . end ( ) ) ;
std :: cout << "A = \n" << A << std :: endl ;
// 一个QR分解的实例
SPQR < SparseMatrix < double > > qr ;
// 计算分解
qr . compute ( A ) ;
// 求一个A x = b
Vector4d b ( 1 , 2 , 3 , 4 ) ;
Vector4d x = qr . solve ( b ) ;
std :: cout << "x = \n" << x ;
std :: cout << "A x = \n" << A * x ;
return 0 ;
}
测试结果
常见问题
- 测试代码中Vector4d b(1, 2, 3, 4);报错,解决方法是将报错的位置注释掉。。。。
四、安装QGLViewer-2.7.2
下载QGLViewer-2.7.2
生成
主要是为了在Qt中显示g2o的运算结果。方法为下载->利用VS打开->扩展->Qt vs tool –>Open Qt Project File->打开\libQGLViewer-2.7.1\QGLViewer\ QGLViewer.pro->生成->会出现相应的lib和dll文件。环境变量添加dll所在目录。
五、安装g2o:
下载g2o
CMake编译
- 填写Source code: F:/opensources/g2o-20170730_git/g2o-20170730_git
- 填写 Build binaries: F:/opensources/g2o-20170730_git/g2o-20170730_git/build
- 第一次Configure:
会出现错误,不要慌,是因为没有找到Eigen3的路径,手动找到EIGEN3_INCLUDE_DIR,改为重新复制后的路径。再次点击Configure
- 第二次Configure:
第二次Configure后还是会有红色的行出现。此时需要将
QGLVIEWER_INCLUDE_DIR
路径换为
F:/opensources/libQGLViewer-2.7.2/libQGLViewer-2.7.2/QGLViewer
QGLVIEWER_LIBRARY_DEBUG
路径换为
F:/opensources/libQGLViewer-2.7.2/libQGLViewer-2.7.2/QGLViewer/lib/QGLViewerd2.lib
QGLVIEWER_LIBRARY_RELEASE
路径换为
F:/opensources/libQGLViewer-2.7.2/libQGLViewer-2.7.2/QGLViewer/lib/QGLViewer2.lib
CHOLMOD_INCLUDE_DIR
路径换为
F:/opensources/suitesparse-metis-for-windows-1.5.0/suitesparse-metis-for-windows-1.5.0/build/install/include
CHOLMOD_LIBRARY
路径换为
F:/opensources/suitesparse-metis-for-windows-1.5.0/suitesparse-metis-for-windows-1.5.0/build/install/lib/libcholmodd.lib
再次点击Configure(后面会多次出现红色行(如:AMD_LIBRARY
,CAMD_LIBRARY
…..),都要根据上面两个替换方式替换),直到没有红色行出现。 - 第N次Configure:
Cmake界面变为白色。但是会提示你一个warning,不用管它。恭喜你离成功又进了一步。
生成:
打开build文件下的g2o.sln。All_BULLD生成。
配置:
- 独特的部分:
将:F:\opensources\g2o-20170730_git\g2o-20170730_git\EXTERNAL\csparse下的cs.h
和cs_api.h
复制到F:\opensources\g2o-20170730_git\g2o-20170730_git\g2o\solvers\csparse下。然后打开本文件夹下的csparse_helper.h
文件,把#include <cs.h>
改成#include "cs.h"
将F:\opensources\g2o-20170730_git\g2o-20170730_git\build\g2o下config.h
复制到F:\opensources\g2o-20170730_git\g2o-20170730_git\g2o下。 - 新建属性表
- VC++目录->包含目录:F:\opensources\g2o-20170730_git\g2o-20170730_git
- VC++目录->库目录:F:\opensources\g2o-20170730_git\g2o-20170730_git\bin\Debug
- 链接器->输入->附加依赖项:
g2o_interface_d.lib
g2o_parser_d.lib
g2o_simulator_d.lib
g2o_types_sim3_d.lib
g2o_types_icp_d.lib
g2o_types_slam3d_addons_d.lib
g2o_types_sba_d.lib
g2o_types_slam2d_addons_d.lib
g2o_types_slam3d_d.lib
g2o_calibration_odom_laser_d.lib
g2o_solver_slam2d_linear_d.lib
g2o_types_data_d.lib
g2o_types_sclam2d_d.lib
g2o_solver_structure_only_d.lib
g2o_types_slam2d_d.lib
g2o_solver_pcg_d.lib
g2o_solver_dense_d.lib
g2o_solver_eigen_d.lib
g2o_tutorial_slam2d_d.lib
g2o_hierarchical_d.lib
g2o_solver_csparse_d.lib
g2o_cli_d.lib
g2o_core_d.lib
g2o_stuff_d.lib
g2o_csparse_extension_d.lib
g2o_ext_freeglut_minimal_d.lib
g2o_opengl_helper_d.lib
g2o_ext_csparse_d.lib
- C/C++->预处理器->预处理器定义:_WINDOWS
测试
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/StdVector>
#include <Eigen/Geometry>
#include "g2o/types/slam3d/vertex_se3.h"
#include "g2o/types/slam3d/edge_se3.h"
#include "g2o/stuff/sampler.h"
#include "g2o/core/factory.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/core/block_solver.h"
#include "g2o/solvers/dense/linear_solver_dense.h"
#include "g2o/core/optimization_algorithm_levenberg.h"
#include "g2o/types/sba/types_six_dof_expmap.h"
using namespace std;
using namespace g2o;
using namespace Eigen;
int main()
{
g2o::SparseOptimizer optimizer;//全局优化器
optimizer.setVerbose(true);//调试信息输出
g2o::BlockSolver_6_3::LinearSolverType* linearSolver;//6代表pose的自由度 3代表landmark的自由度
linearSolver = new g2o::LinearSolverDense<g2o::BlockSolver_6_3::PoseMatrixType>();
g2o::BlockSolver_6_3* solver_ptr
= new g2o::BlockSolver_6_3(linearSolver);
//优化方法LM
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg(solver_ptr);
optimizer.setAlgorithm(solver);
vector<VertexSE3*> vertices;//pose
vector<EdgeSE3*> odometryEdges;
vector<EdgeSE3*> edges;
//设置pose
int id = 0;
for (int i = 0; i < 10; i++)
{
VertexSE3* v = new VertexSE3;
v->setId(id++);
Eigen::Isometry3d T = Eigen::Isometry3d::Identity();;//旋转和平移的集合,4*4的矩阵
Eigen::Matrix3d rot = Eigen::Matrix3d::Identity();
Eigen::Vector3d trans = Eigen::Vector3d(i, 0, 0);
T.rotate(rot);
T.translate(trans);
//cout << "Transform matrix = \n" << T.matrix() <<endl;
v->setEstimate(T);
vertices.push_back(v);
optimizer.addVertex(v);
}
//生成里程计的边
for (int i = 1; i < vertices.size(); i++)
{
VertexSE3* pre = vertices[i - 1];
VertexSE3* cur = vertices[i];
Eigen::Isometry3d T = pre->estimate().inverse() * cur->estimate();
EdgeSE3* e = new EdgeSE3;
e->setVertex(0, pre);
e->setVertex(1, cur);
e->setMeasurement(T);
Eigen::Matrix<double, 6, 6> information = Eigen::Matrix<double, 6, 6>::Zero();
information.block<3, 3>(0, 0) = 0.01 * Eigen::Matrix3d::Identity();
information.block<3, 3>(3, 3) = 0.01 * Eigen::Matrix3d::Identity();;
e->setInformation(information);
odometryEdges.push_back(e);
edges.push_back(e);
optimizer.addEdge(e);
}
//添加一条首尾相连的边,从尾巴指向头
{
EdgeSE3* e = new EdgeSE3;
Eigen::Isometry3d T = vertices[vertices.size() - 1]->estimate().inverse() * vertices[0]->estimate();
e->setVertex(0, vertices[vertices.size() - 1]);
e->setVertex(1, vertices[0]);
e->setMeasurement(T);
Eigen::Matrix<double, 6, 6> information = Eigen::Matrix<double, 6, 6>::Zero();
information.block<3, 3>(0, 0) = 0.01 * Eigen::Matrix3d::Identity();
information.block<3, 3>(3, 3) = 0.01 * Eigen::Matrix3d::Identity();;
e->setInformation(information);
odometryEdges.push_back(e);
edges.push_back(e);
optimizer.addEdge(e);
}
optimizer.save("test.g2o");
return 0;
}
测试结果
- 参考:https://blog.csdn.net/ye_shen_wei_mian/article/details/51864239
还有没正确安装的吗!要是有,我当场。。。