티스토리 뷰

728x90
반응형

우분투에 설치하기

우분투 18.04에 직접 다음 자료를 참조하여 설치하거나 도커상에서 설치하면 된다. 

 

http://ceres-solver.org/installation.html#linux

 

Installation — Ceres Solver

Although a full tutorial on CMake is outside the scope of this guide, here we cover some of the most common CMake misunderstandings that crop up when using Ceres. For more detailed CMake usage, the following references are very useful: When a project like

ceres-solver.org

실행을 위해 필요한 라이브러리들을 다음 설치 명령을 사용하여 설치한다. 

# CMake
sudo apt-get install cmake
# google-glog + gflags
sudo apt-get install libgoogle-glog-dev
# BLAS & LAPACK
sudo apt-get install libatlas-base-dev
# Eigen3
sudo apt-get install libeigen3-dev
# SuiteSparse and CXSparse (optional)
# - If you want to build Ceres as a *static* library (the default)
#   you can use the SuiteSparse package in the main Ubuntu package
#   repository:
sudo apt-get install libsuitesparse-dev
# - However, if you want to build Ceres as a *shared* library, you must
#   add the following PPA:
sudo add-apt-repository ppa:bzindovic/suitesparse-bugfix-1319687
sudo apt-get update
sudo apt-get install libsuitesparse-dev

 

특정 버전의 소스파일을 다운받던가 github 에서 파일을 클론받아 사용한다. 

git clone https://ceres-solver.googlesource.com/ceres-solver

cmake를 기반으로 파일을 다운받아 빌드하는 방식이다. 방금 git 명령으로 소스를 다운 받은 동일한 폴더에서 다음 명령들을 수행하면 설치가 완료된다.  

mkdir ceres-bin
cd ceres-bin
cmake ../ceres-solver
make -j3
make test
make install

 

helloworld.cc 작성하고 돌려보기 

소스코드 작성하기

tutorial page에 있는 helloworld.cc파일을 작성하거나 다운받는다.

https://ceres-solver.googlesource.com/ceres-solver/+/master/examples/helloworld.cc

 

examples/helloworld.cc - ceres-solver - Git at Google

// Ceres Solver - A fast non-linear least squares minimizer // Copyright 2015 Google Inc. All rights reserved. // http://ceres-solver.org/ // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that th

ceres-solver.googlesource.com

#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;

struct CostFunctor {
  template <typename T> bool operator()(const T* const x, T* residual) const {
    residual[0] = 10.0 - x[0];
    return true;
  }
};

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);
  // The variable to solve for with its initial value. It will be
  // mutated in place by the solver.
  double x = 0.5;
  const double initial_x = x;
  // Build the problem.
  Problem problem;
  // Set up the only cost function (also known as residual). This uses
  // auto-differentiation to obtain the derivative (jacobian).
  CostFunction* cost_function =
      new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
  problem.AddResidualBlock(cost_function, NULL, &x);
  // Run the solver!
  Solver::Options options;
  options.minimizer_progress_to_stdout = true;
  Solver::Summary summary;
  Solve(options, &problem, &summary);
  std::cout << summary.BriefReport() << "\n";
  std::cout << "x : " << initial_x
            << " -> " << x << "\n";
  return 0;
}

 

CMakeLists.txt 파일 작성하기

cmake기반으로 컴파일하면 문제없이 컴파일 되는 것을 알 수 있었다. 

CMakeLists.txt에 다음과 같이 작성하여 파일을 생성하고 helloworld.cc와 동일한 폴더에 넣는다.

cmake_minimum_required(VERSION 2.8)

project(helloworld)

find_package(Ceres REQUIRED)
include_directories(${CERES_INCLUDE_DIRS})

# helloworld
add_executable(helloworld helloworld.cc)
target_link_libraries(helloworld ${CERES_LIBRARIES})

 

 

컴파일/빌드하기 

CMakeLists.txt와  helloworld.cc파일을 준비되면 다음 명령을 차례로 입력하면 helloworld 실행 파일이 생성된다.

mkdir build
cd build
cmake ..
make

 

실행 해보기

helloworld실행 파일이 생성되므로 다음과 같이 실행명령을 실행하면 결과가 나오는 것을 알 수 있다. 

$ ./helloworld
iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
   0  4.512500e+01    0.00e+00    9.50e+00   0.00e+00   0.00e+00  1.00e+04        0    9.32e-05    2.52e-03
   1  4.511598e-07    4.51e+01    9.50e-04   9.50e+00   1.00e+00  3.00e+04        1    7.67e-04    3.40e-03
   2  5.012552e-16    4.51e-07    3.17e-08   9.50e-04   1.00e+00  9.00e+04        1    1.15e-04    3.55e-03
Ceres Solver Report: Iterations: 3, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 0.5 -> 10

 

 

 

 

 

 

 

728x90
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함