win10+vs2019+g2o 安装教程

2020-03-28  本文已影响0人  离群土拨鼠
  • 电脑:Dell vostro 3480
  • 操作系统:win10
  • 内存:8GB
  • 显卡:英特尔 HD Graphics 620
  • CPU: 英特尔 酷睿 i5-8265U

一、安装vs2019

二、安装Eigen3

下载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编译

生成

配置

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 ;
}

测试结果

常见问题

四、安装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->生成->会出现相应的libdll文件。环境变量添加dll所在目录。

五、安装g2o:

下载g2o

CMake编译

生成:

打开build文件下的g2o.sln。All_BULLD生成。


配置:

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

测试

#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;
}

测试结果

上一篇下一篇

猜你喜欢

热点阅读