高博SLAM第十章优化代码出错解决办法

第一部分:g2o代码错误修正

**

问题一

**

/home/wh/code/slambook/ch10/g2o_custombundle/ceres/autodiff.h:225:11: error:class ceres::internal::FixedArray<ceres::Jet<double, 12>, 17, Eigen::aligned_allocator<ceres::Jet<double, 12> > >’ has no member named ‘get’
  225 |         x.get() + jet0,
      |         ~~^~~
/home/wh/code/slambook/ch10/g2o_custombundle/ceres/autodiff.h:226:11: error:class ceres::internal::FixedArray<ceres::Jet<double, 12>, 17, Eigen::aligned_allocator<ceres::Jet<double, 12> > >’ has no member named ‘get’
  226 |         x.get() + jet1,
      |         ~~^~~
/home/wh/code/slambook/ch10/g2o_custombundle/ceres/autodiff.h:227:11: error:class ceres::internal::FixedArray<ceres::Jet<double, 12>, 17, Eigen::aligned_allocator<ceres::Jet<double, 12> > >’ has no member named ‘get’
  227 |         x.get() + jet2,

类似上面这种错误,因为版本问题导致的,新的版本中没有get函数,使用data函数取代。
旧版本的源代码:

 // Returns a pointer to the underlying element array.
  inline const T* get() const { return &array_[0].element; }
  inline T* get() { return &array_[0].element; }

新版的源代码如下:

  // FixedArray::data()
  //
  // Returns a const T* pointer to elements of the `FixedArray`. This pointer
  // can be used to access (but not modify) the contained elements.
  const_pointer data() const { return AsValueType(storage_.begin()); }

  // Overload of FixedArray::data() to return a T* pointer to elements of the
  // fixed array. This pointer can be used to access and modify the contained
  // elements.
  pointer data() { return AsValueType(storage_.begin()); }

使用data替代get函数即可。更改后结果如下:

    const JetT *unpacked_parameters[10] = {
        x.data() + jet0,
        x.data() + jet1,
        x.data() + jet2,
        x.data() + jet3,
        x.data() + jet4,
        x.data() + jet5,
        x.data() + jet6,
        x.data() + jet7,
        x.data() + jet8,
        x.data() + jet9,
    };

    JetT* output = x.data() + N0 + N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 + N9;

#define CERES_MAKE_1ST_ORDER_PERTURBATION(i)                            \
    if (N ## i) {                                                       \
      internal::Make1stOrderPerturbation<JetT, T, N ## i>(              \
          jet ## i,                                                     \
          parameters[i],                                                \
          x.data() + jet ## i);                                          \
    }

**

问题二:

**
c++标准问题,将C++11变为C++14 即可。
在CMakeLists.txt文件中修改

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")

**

问题三:

**
share_ptr和unique_ptr的问题,最新版采用unique_ptr,并且需要使用std::move,这属于C++高级知识,有兴趣的同学可以自行查看。
更改方法:
此错误修改方法https://blog.csdn.net/yongniao9185/article/details/112299843

**

问题四:

**
有可能出现未引用的链接错误,此时可能是确实glog库,在CMakeLists.txt中目标程序链接库上将glog加上即可。

target_link_libraries(${PROJECT_NAME} ${G2O_LIBS} ${CHOLMOD_LIBRARIES} BALProblem ParseCmd glog)

有的错误我并没有遇到,在这里就不进行总结。

**

**

第二部分:Ceres优化代码问题修正

**
**

错误一:

**

ceresBundle.cpp:17:14: error:struct ceres::Solver::Options’ has no member named ‘num_linear_solver_threads’ 

版本问题,查看当前版本的源文件,查找Options有关于线程的数据成员
结果如下:

    // Number of threads used by Ceres for evaluating the cost and
    // jacobians.
    int num_threads = 1;

将num_linear_solver_threads改为num_threads即可。

**

其他设置

**
将C++标准设置为C++14
添加Eigen库的目录
以上是发现的问题

PS:

**
请问在第八章直接法代码中,有大佬遇到以下错误吗?有的话还请指导一下,评论私信都可。

direct_semidense: /usr/local/include/g2o/core/hyper_graph.h:210: void g2o::HyperGraph::Edge::setVertex(size_t, g2o::HyperGraph::Vertex*): Assertion `i < _vertices.size() && "index out of bounds"' failed.

程序是可以编译成功的,但是在运行时,迭代12次之后就会报这个错误,进行调试发现是在一下语句中出错(个人猜测)

    cout<<"edges in graph: "<<optimizer.edges().size() <<endl;
    optimizer.initializeOptimization();
    optimizer.optimize ( 30 );
    Tcw = pose->estimate();

是一个断言错误,源代码如下:

    void setVertex(size_t i, Vertex* v) {
      assert(i < _vertices.size() && "index out of bounds");
      _vertices[i] = v;
    }

可能和Eigen库有关,希望大佬赐教。

你可能感兴趣的:(SLAM,c++)