g2o (General Graph Optimization)

repository·master·Indexed 25 days ago

https://github.com/rainerkuemmerle/g2o

An open-source C++ framework for optimizing graph-based nonlinear error functions, widely used in robotics and computer vision for SLAM and Bundle Adjustment (BA). It includes features for parameter blocks, vertex caches, a simulation world for robot and sensor modeling, and OpenGL-based visualization via G2oQGLViewer. The framework supports C++17 and provides Python bindings via the g2o-python package.

Tokens
2.9K
Snippets
7
Records
24
Agent score
85%

What's inside g2o

  1. Use g2o_incremental for incremental SLAM

    master

    The g2o_incremental example implements a variant of the iSAM (Incremental Smoothing and Mapping) algorithm. Unlike standard iSAM, it performs Cholesky factor updates using rank updates provided by the CHOLMOD library.

    By default, the algorithm solves the graph after every 10 nodes are inserted. This frequency can be adjusted using command line options.

  2. Implement Vertex Caches for performance

    master

    Caches store intermediate calculations (like rotation matrices or Jacobians) that depend on a vertex estimate. This avoids redundant computations during computeError() for multiple edges sharing the same vertex.

    Implementation Steps:

    1. Extend VertexCache: Implement the update() method. This must be called whenever the vertex estimate changes (e.g., in oplus() or setEstimate()).
    2. Implement createCache in your Edge: The edge must define how to create a cache for a vertex at a specific position in Edge::_vertices.
    3. Configure Cache IDs in the Edge: Before adding an edge to the graph, assign a cache_id to each vertex using setCacheId(index, id). An ID of -1 indicates no cache.
    4. Retrieve Cache in computeError: Use v->getCache(cache_id) to access the cached data.

    Important: To maintain consistency, any vertex type using a cache must call updateCache() in every method that modifies its estimate.

  3. Compile g2o from source (Linux/Generic)

    master

    The recommended way to build g2o is an out-of-source build. This requires a C++17 compiler, CMake, and Eigen3. The resulting binaries will be in the bin directory and libraries in the lib directory within your build folder.

    mkdir build
    cd build
    cmake ../
    make
  4. Use the interactive SLAM interface for g2o

    master

    The examples/interactive_slam directory provides an interface to g2o that processes SLAM input data via stdin and outputs the current estimates to stdout upon request. This interface follows the grammar proposed for the RSS'11 workshop "Automated SLAM Evaluation".

    Available tools in this directory:

    • slam_parser: The parser for the SLAM protocol.
    • g2o_interactive: Performs batch optimization every N nodes.
    • g2o_incremental: Performs incremental optimization, running a batch every 100 nodes.
  5. Use g2o Python Bindings

    master
    For Python users, you can use the g2o-python package available on PyPI. Note that there is also an experimental branch pymem in the main repository that uses smart pointers instead of raw pointers for improved memory management.
  6. Implement and use Parameter Blocks in a graph

    master

    Parameter blocks are quantities fixed during optimization (e.g., sensor offsets or camera intrinsics). They are identified by a unique integer ID.

    To use parameters, follow these steps:

    1. Extend the Parameters class: Implement read(istream& os) and write(ostream& os).
    2. Register with the metatype system: Use the factory to register your type so it can be saved/loaded.
    3. Insert into the graph: Create the instance, set a unique ID, and add it to the graph using addParameters.
    4. Access parameters: Use OptimizableGraph::parameters(int id) and dynamic_cast to retrieve them.

    Note: Parameters are always saved at the beginning of a graph file.

  7. Generate Doxygen documentation

    master

    To generate the project's documentation using Doxygen, run the doxygen command with the doxy.config configuration file from the current folder. This process requires the dot tool (part of Graphviz) to be installed and available in your system's PATH.

    doxygen doxy.config
  8. Install g2o on Windows using vcpkg

    master

    On Windows, it is recommended to use vcpkg to manage dependencies. Use the provided scripts to build and install dependencies. You can pass the location of vcpkg and the required triplet as CLI arguments.

    To generate build scripts and build the project, use the following sequence (replace %VCPKG_DEFAULT_TRIPLET% and %VCPKG_ROOT_DIR% with your actual values, and ensure your Visual Studio version matches your system):

    mkdir build
    cd build
    cmake -DG2O_BUILD_APPS=ON -DG2O_BUILD_EXAMPLES=ON -DVCPKG_TARGET_TRIPLET="%VCPKG_DEFAULT_TRIPLET%" -DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT_DIR%\scripts\buildsystems\vcpkg.cmake" ..
    cmake --build . --target ALL_BUILD
  9. Install g2o on Ubuntu/Debian

    master

    To install g2o and its dependencies on Ubuntu or Debian, you need to install the required C++17 compiler, CMake, and Eigen3, along with optional dependencies like spdlog, suitesparse, Qt5, and libQGLViewer. Use the following command to install the necessary packages:

    sudo apt install libeigen3-dev libspdlog-dev libsuitesparse-dev qtdeclarative5-dev qt5-qmake libqglviewer-dev-qt5
  10. Cross-Compile g2o for Android

    master

    To cross-compile for Android, use the provided Android toolchain file and specify your NDK path and target ABI (e.g., armeabi-v7a with NEON).

    mkdir build
    cd build
    cmake -DCMAKE_TOOLCHAIN_FILE=../script/android.toolchain.cmake -DANDROID_NDK=<YOUR_PATH_TO_ANDROID_NDK_r10d+> -DCMAKE_BUILD_TYPE=Release -DANDROID_ABI="armeabi-v7a with NEON" -DEIGEN3_INCLUDE_DIR="<YOUR_PATH_TO_EIGEN>" -DEIGEN3_VERSION_OK=ON ..
    cmake --build .