g2o初始化一些
今天看了一下智能指针的东西,发现更简单的思路:
就是Block和solver构造时,需要传递unique_ptr,那我们将普通指针转换成unique_ptr不就可以了么:
// 初始化g2o
//第一步:
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose 维度为 6, landmark 维度为 3
//第二步:
Block::LinearSolverType* linearSolver = new g2o::LinearSolverCSparse<Block::PoseMatrixType>(); // 线性方程求解器
//std::unique_ptr<Block::LinearSolverType> linearSolver ( new g2o::LinearSolverCSparse<Block::PoseMatrixType>());
//第三步
Block* solver_ptr = new Block ( std::unique_ptr<Block::LinearSolverType>(linearSolver) );
//std::unique_ptr<Block> solver_ptr ( new Block ( linearSolver));
//std::unique_ptr<Block> solver_ptr ( new Block ( std::move(linearSolver))); // 矩阵块求解器
//第四步:
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::unique_ptr<Block>(solver_ptr));
//g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::move(solver_ptr));
//第五步:
g2o::SparseOptimizer optimizer;
//第六步:
optimizer.setAlgorithm ( solver );
变化在第三步和第四步的new构造参数里,运行没毛病。
版权声明:本文为serser原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。