Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zha0ming1e committed Mar 7, 2021
0 parents commit c6c9bb0
Show file tree
Hide file tree
Showing 46 changed files with 18,763 additions and 0 deletions.
167 changes: 167 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
cmake_minimum_required(VERSION 2.8)
set(CMAKE_LEGACY_CYGWIN_WIN32 0)

project(lego)

include(CPack)

# lib prefix
set(LIB_PREFIX lego_)

set(lego_C_FLAGS)
set(lego_CXX_FLAGS)

# built type
#if(NOT CMAKE_BUILD_TYPE)
# set(CMAKE_BUILD_TYPE Release CACHE STRING
# "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
# FORCE)
#endif(NOT CMAKE_BUILD_TYPE)

set(CMAKE_BUILD_TYPE "Release")

# postfix
set(CMAKE_DEBUG_POSTFIX "_d" CACHE STRING "postfix applied to debug build of libraries")
set(CMAKE_RELEASE_POSTFIX "" CACHE STRING "postfix applied to release build of libraries")
set(CMAKE_RELWITHDEBINFO_POSTFIX "_rd" CACHE STRING "postfix applied to release-with-debug-information libraries")
set(CMAKE_MINSIZEREL_POSTFIX "_s" CACHE STRING "postfix applied to minimium-size-build libraries")

# postfix
if(CMAKE_BUILD_TYPE MATCHES Release)
set(EXE_POSTFIX)
elseif(CMAKE_BUILD_TYPE MATCHES Debug)
set(EXE_POSTFIX ${CMAKE_DEBUG_POSTFIX})
elseif(CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
set(EXE_POSTFIX ${CMAKE_RELWITHDEBINFO_POSTFIX})
elseif(CMAKE_BUILD_TYPE MATCHES MinSizeRel)
set(EXE_POSTFIX ${CMAKE_MINSIZEREL_POSTFIX})
endif(CMAKE_BUILD_TYPE MATCHES Release)

# dynamic or static lib
option (BUILD_SHARED_LIBS "Build Shared Libraries (preferred and required for the lego plugin system)" ON)
set (LEGO_LIB_TYPE STATIC)
if (BUILD_SHARED_LIBS)
set (LEGO_LIB_TYPE SHARED)
endif ()

# output directories
set(lego_RUNTIME_OUTPUT_DIRECTORY ${lego_SOURCE_DIR}/bin CACHE PATH "Target for the binaries")
set(lego_LIBRARY_OUTPUT_DIRECTORY ${lego_SOURCE_DIR}/lib CACHE PATH "Target for the libraries")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${lego_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${lego_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${lego_RUNTIME_OUTPUT_DIRECTORY})

# installation directories
set(RUNTIME_DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
set(LIBRARY_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
set(ARCHIVE_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
set(INCLUDES_DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
set(INCLUDES_INSTALL_DIR ${INCLUDES_DESTINATION}/lego)

# look for SuiteSparse and Eigen3
list(APPEND CMAKE_MODULE_PATH ${lego_SOURCE_DIR}/cmake_modules)

# os
if (UNIX)
add_definitions(-DUNIX)
message(STATUS "Compiling on Unix")
endif (UNIX)

option(BUILD_LGPL_SHARED_LIBS "Build LGPL Code as Shared Libraries (LGPL Code)" ON)
set (LEGO_LGPL_LIB_TYPE STATIC)
if (BUILD_LGPL_SHARED_LIBS)
set (LEGO_LGPL_LIB_TYPE SHARED)
else ()
message(STATUS "Building LGPL code as static library (affects license of the binary)")
endif ()

include(CMakeDependentOption)
CMAKE_DEPENDENT_OPTION(LEGO_BUILD_LINKED_APPS "Build apps linked with the libraries (no plugin system)" OFF
"LEGO_BUILD_APPS" OFF)

# build the examples
set(LEGO_BUILD_EXAMPLES ON CACHE BOOL "Build lego examples")
if(LEGO_BUILD_EXAMPLES)
message(STATUS "Compiling lego examples")
endif(LEGO_BUILD_EXAMPLES)

# gcc
if(CMAKE_COMPILER_IS_GNUCXX)
option (BUILD_WITH_MARCH_NATIVE "Build with \"-march native\"" OFF)
message(STATUS "Compiling with GCC")

# settings for optimisation
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")

# switch off optimization for debug builds
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")

# Linux
if(BUILD_WITH_MARCH_NATIVE AND NOT "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm" AND "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=native")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
endif()
# activate warnings
set(lego_C_FLAGS "${lego_C_FLAGS} -Wall -W")
set(lego_CXX_FLAGS "${lego_CXX_FLAGS} -Wall -W")
endif(CMAKE_COMPILER_IS_GNUCXX)

# c++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# specifying compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${lego_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${lego_C_FLAGS}")

# find Eigen3
# https://eigen.tuxfamily.org/dox/TopicCMakeGuide.html for details
find_package(Eigen3 REQUIRED)
if (TARGET Eigen3::Eigen)
set(LEGO_EIGEN3_EIGEN_TARGET Eigen3::Eigen)
else()
include_directories(${EIGEN3_INCLUDE_DIR})
endif ()

# Set up the top-level include directories
include_directories(${lego_SOURCE_DIR} ${PROJECT_BINARY_DIR})

# config.h
set(LEGO_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(LEGO_LGPL_SHARED_LIBS ${BUILD_LGPL_SHARED_LIBS})
set(LEGO_CXX_COMPILER "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER}")

configure_file(config.h.in "${PROJECT_BINARY_DIR}/lego/config.h")
install(FILES ${PROJECT_BINARY_DIR}/lego/config.h DESTINATION ${INCLUDES_DESTINATION}/lego)

# cmake configuration scripts
set(LEGO_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(LEGO_VERSION_CONFIG "${LEGO_GENERATED_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(LEGO_PROJECT_CONFIG "${LEGO_GENERATED_DIR}/${PROJECT_NAME}Config.cmake")
set(LEGO_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(LEGO_CONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}")
set(LEGO_NAMESPACE "${PROJECT_NAME}::")
set(LEGO_VERSION 1.0.0)

include(CMakePackageConfigHelpers)
WRITE_BASIC_PACKAGE_VERSION_FILE(
"${LEGO_VERSION_CONFIG}" VERSION ${LEGO_VERSION} COMPATIBILITY SameMajorVersion
)

configure_file("${lego_SOURCE_DIR}/cmake_modules/Config.cmake.in" "${LEGO_PROJECT_CONFIG}" @ONLY)

install(
FILES "${LEGO_PROJECT_CONFIG}" "${LEGO_VERSION_CONFIG}"
DESTINATION "${LEGO_CONFIG_INSTALL_DIR}")

install(
EXPORT "${LEGO_TARGETS_EXPORT_NAME}"
NAMESPACE "${LEGO_NAMESPACE}"
DESTINATION "${LEGO_CONFIG_INSTALL_DIR}")

# subdirectories
add_subdirectory(lego)
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@


# LEGO #

**LEGO** is a **L**ight w**E**ight **G**raph-based **O**ptimization library in C++. LEGO is a graph-based non-linear optimization framework for learning and practicing which only depends on Eigen3. LEGO is a generic graph-based optimization library, more importantly, it is designed for visual or visual-inertial SLAM optimization problem. Now, the optimization problem solving in LEGO mainly depends on Levenberg-Marquardt algorithm and it will include more algorithms in the future, such as Dog-leg. This version is only tested on Ubuntu 18.04.



## Installation ##

### Prerequisites

- [**CMake**](https://cmake.org/) and **GCC**

- [**Eigen3**](http://eigen.tuxfamily.org/)

On Ubuntu 18.04, we can install CMake and Eigen3 following:

```bash
# cmake
sudo apt-get install cmake
# Eigen3
sudo apt-get install libeigen3-dev
```

### Build and Install

We can build and install LEGO from this [repository](https://github.com/zha0ming1e/LEGO.git) and follow:

```bash
git clone https://github.com/zha0ming1e/LEGO.git
cd LEGO/
mkdir build/
cmake .. && make -j6
# if you want to install it
sudo make install
```

Now we have already installed the LEGO and then we can read the code and use it.

### Examples

There are two examples which are implemented by LEGO (one of non-linear data fitting and one of pose graph optimization on SE3/se3 with Lie algebra) under the [lego/examples/](./lego/examples/) folder.

- **Non-linear Data Fitting**

**Run**

```bash
./bin/example_nonlinear_fitting
```

**Output**

```bash
Example: Non-linear Fitting start...
==========LEGO OPTIMIZER==========
Iteration = 0, Chi = 185454, Lambda = 0.01
Iteration = 1, Chi = 153661, Lambda = 6990.51
Iteration = 2, Chi = 62936.6, Lambda = 18641.4
Iteration = 3, Chi = 27640.5, Lambda = 12427.6
Iteration = 4, Chi = 1090.47, Lambda = 4142.52
Iteration = 5, Chi = 546.72, Lambda = 1380.84
Iteration = 6, Chi = 526.969, Lambda = 460.28
Iteration = 7, Chi = 505.841, Lambda = 153.427
Iteration = 8, Chi = 490.512, Lambda = 51.1423
Iteration = 9, Chi = 487.068, Lambda = 17.0474
Iteration = 10, Chi = 486.901, Lambda = 5.68247
Iteration = 11, Chi = 486.9, Lambda = 1.89416

Stop the optimization: [last_chi_(486.9) - currentChi_(486.9) = 1.17714e-06] < 1e-5

Info:
TimeCost(SolveProblem) = 8.42891 ms
TimeCost(BuildHessian) = 6.33171 ms

--------Estimates after optimization--------
a, b, c = 0.98179, 2.0277, 0.989624
--------Ground truth--------
a, b, c = 1.0, 2.0, 1.0
```

- **Pose Graph Optimization on SE3/se3**

**Run**

```bash
./bin/example_pose_graph ./lego/examples/pose_graph/sphere.g2o
```

**Output**

```bash
read total 2500 vertices, 9799 edges.
optimizing ...
==========LEGO OPTIMIZER==========
Iteration = 0, Chi = 4.78072e+09, Lambda = 20119.8
Iteration = 1, Chi = 1.92146e+09, Lambda = 13413.2
Iteration = 2, Chi = 1.23104e+09, Lambda = 8942.11
Iteration = 3, Chi = 1.09913e+09, Lambda = 5961.41
Iteration = 4, Chi = 1.03169e+09, Lambda = 3974.27
Iteration = 5, Chi = 9.30259e+08, Lambda = 2649.52
Iteration = 6, Chi = 8.81683e+08, Lambda = 1766.34
...
```



## References ##

- [**g2o**](https://github.com/RainerKuemmerle/g2o): A General Framework for Graph Optimization
- [**ceres-solver**](http://ceres-solver.org/): A Large Scale Non-linear Optimization Library

- [**Sophus**](https://github.com/strasdat/Sophus): C++ implementation of Lie Groups using Eigen
- [**VINS-Mono**](https://github.com/HKUST-Aerial-Robotics/VINS-Mono): A Robust and Versatile Monocular Visual-Inertial State Estimator
- [**VINS-Course**](https://github.com/HeYijia/VINS-Course): VINS-Mono code without Ceres or ROS

6 changes: 6 additions & 0 deletions cmake_modules/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include(CMakeFindDependencyMacro)

find_dependency(Eigen3)

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

Loading

0 comments on commit c6c9bb0

Please sign in to comment.