g2o
Loading...
Searching...
No Matches
Functions
optimize_sphere_by_sim3.cpp File Reference
#include <Eigen/Core>
#include <Eigen/StdVector>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include "g2o/core/block_solver.h"
#include "g2o/core/factory.h"
#include "g2o/core/optimization_algorithm_gauss_newton.h"
#include "g2o/core/optimization_algorithm_levenberg.h"
#include "g2o/core/robust_kernel_impl.h"
#include "g2o/core/solver.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/solvers/dense/linear_solver_dense.h"
#include "g2o/solvers/eigen/linear_solver_eigen.h"
#include "g2o/types/sim3/types_seven_dof_expmap.h"
#include "g2o/types/slam3d/edge_se3.h"
#include "g2o/types/slam3d/types_slam3d.h"
#include "g2o/types/slam3d/vertex_se3.h"
Include dependency graph for optimize_sphere_by_sim3.cpp:

Go to the source code of this file.

Functions

void G2O_FACTORY_EXPORT g2o_type_VertexSE3 (void)
 
void ToVertexSim3 (const g2o::VertexSE3 &v_se3, g2o::VertexSim3Expmap *const v_sim3)
 
void ToVertexSE3 (const g2o::VertexSim3Expmap &v_sim3, g2o::VertexSE3 *const v_se3)
 
void ToEdgeSim3 (const g2o::EdgeSE3 &e_se3, g2o::EdgeSim3 *const e_sim3)
 
int main (int argc, char **argv)
 

Function Documentation

◆ g2o_type_VertexSE3()

void G2O_FACTORY_EXPORT g2o_type_VertexSE3 ( void  )

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 93 of file optimize_sphere_by_sim3.cpp.

93 {
95 if (argc != 2) {
96 cout << "Usage: pose_graph_g2o_SE3 sphere.g2o" << endl;
97 return 1;
98 }
99 ifstream fin(argv[1]);
100 if (!fin) {
101 cout << "file " << argv[1] << " does not exist." << endl;
102 return 1;
103 }
104
105 // define the optimizer
106 typedef g2o::BlockSolver<g2o::BlockSolverTraits<7, 7>> BlockSolverType;
108 LinearSolverType;
109 auto solver = new g2o::OptimizationAlgorithmLevenberg(
110 std::make_unique<BlockSolverType>(std::make_unique<LinearSolverType>()));
111
112 g2o::SparseOptimizer optimizer;
113 optimizer.setAlgorithm(solver);
114 optimizer.setVerbose(true);
115
116 // Load and Save in SE3
117 g2o::SparseOptimizer interface;
118 interface.load(argv[1]);
119
120 // Convert all vertices
121 for (auto& tmp : interface.vertices()) {
122 const int& id = tmp.first;
123 g2o::VertexSE3* v_se3 = static_cast<g2o::VertexSE3*>(tmp.second);
125 v_sim3->setId(id);
126 v_sim3->setMarginalized(false);
127
128 ToVertexSim3(*v_se3, v_sim3);
129 optimizer.addVertex(v_sim3);
130 if (id == 0) {
131 v_sim3->setFixed(true);
132 }
133 }
134
135 // Convert all edges
136 int edge_index = 0;
137 for (auto& tmp : interface.edges()) {
138 g2o::EdgeSE3* e_se3 = static_cast<g2o::EdgeSE3*>(tmp);
139 int idx0 = e_se3->vertex(0)->id();
140 int idx1 = e_se3->vertex(1)->id();
141 g2o::EdgeSim3* e_sim3 = new g2o::EdgeSim3();
142
143 ToEdgeSim3(*e_se3, e_sim3);
144 e_sim3->setId(edge_index++);
145 e_sim3->setVertex(0, optimizer.vertices()[idx0]);
146 e_sim3->setVertex(1, optimizer.vertices()[idx1]);
147 e_sim3->information() = Eigen::Matrix<double, 7, 7>::Identity();
148
149 optimizer.addEdge(e_sim3);
150 }
151
152 cout << "optimizing ..." << endl;
153 optimizer.initializeOptimization();
154 optimizer.optimize(30);
155
156 cout << "saving optimization results in VertexSE3..." << endl;
157 auto vertices_sim3 = optimizer.vertices();
158 auto vertices_se3 = interface.vertices();
159
160 for (auto& tmp : vertices_sim3) {
161 const int& id = tmp.first;
162 g2o::VertexSim3Expmap* v_sim3 =
163 static_cast<g2o::VertexSim3Expmap*>(tmp.second);
164 g2o::VertexSE3* v_se3 = static_cast<g2o::VertexSE3*>(vertices_se3[id]);
165
166 ToVertexSE3(*v_sim3, v_se3);
167 }
168
169 interface.save("result.g2o");
170 return 0;
171}
EIGEN_STRONG_INLINE const InformationType & information() const
information matrix of the constraint
Definition base_edge.h:107
Implementation of a solver operating on the blocks of the Hessian.
Edge between two 3D pose vertices.
Definition edge_se3.h:44
7D edge between two Vertex7
void setVertex(size_t i, Vertex *v)
const Vertex * vertex(size_t i) const
int id() const
returns the id
const VertexIDMap & vertices() const
linear solver which uses the sparse Cholesky solver from Eigen
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
void setMarginalized(bool marginalized)
true => this node should be marginalized out during the optimization
Implementation of the Levenberg Algorithm.
int optimize(int iterations, bool online=false)
void setVerbose(bool verbose)
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
void setAlgorithm(OptimizationAlgorithm *algorithm)
3D pose Vertex, represented as an Isometry3
Definition vertex_se3.h:50
Sim3 Vertex, (x,y,z,qw,qx,qy,qz) the parameterization for the increments constructed is a 7d vector (...
void ToVertexSim3(const g2o::VertexSE3 &v_se3, g2o::VertexSim3Expmap *const v_sim3)
void ToEdgeSim3(const g2o::EdgeSE3 &e_se3, g2o::EdgeSim3 *const e_sim3)
void G2O_FACTORY_EXPORT g2o_type_VertexSE3(void)
void ToVertexSE3(const g2o::VertexSim3Expmap &v_sim3, g2o::VertexSE3 *const v_se3)
virtual bool addEdge(HyperGraph::Edge *e)
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
virtual bool addVertex(HyperGraph::Vertex *v, Data *userData)
virtual bool load(std::istream &is)

References g2o::OptimizableGraph::addEdge(), g2o::OptimizableGraph::addVertex(), g2o::HyperGraph::edges(), g2o_type_VertexSE3(), g2o::HyperGraph::Vertex::id(), g2o::BaseEdge< D, E >::information(), g2o::SparseOptimizer::initializeOptimization(), g2o::OptimizableGraph::load(), g2o::SparseOptimizer::optimize(), g2o::OptimizableGraph::save(), g2o::SparseOptimizer::setAlgorithm(), g2o::OptimizableGraph::Vertex::setFixed(), g2o::HyperGraph::Edge::setId(), g2o::OptimizableGraph::Vertex::setId(), g2o::OptimizableGraph::Vertex::setMarginalized(), g2o::SparseOptimizer::setVerbose(), g2o::HyperGraph::Edge::setVertex(), ToEdgeSim3(), ToVertexSE3(), ToVertexSim3(), g2o::HyperGraph::Edge::vertex(), and g2o::HyperGraph::vertices().

◆ ToEdgeSim3()

void ToEdgeSim3 ( const g2o::EdgeSE3 e_se3,
g2o::EdgeSim3 *const  e_sim3 
)

Definition at line 78 of file optimize_sphere_by_sim3.cpp.

78 {
79 Eigen::Isometry3d se3 = e_se3.measurement().inverse();
80 Eigen::Matrix3d r = se3.rotation();
81 Eigen::Vector3d t = se3.translation();
82 g2o::Sim3 sim3(r, t, 1.0);
83
84 e_sim3->setMeasurement(sim3);
85}
EIGEN_STRONG_INLINE const Measurement & measurement() const
accessor functions for the measurement represented by the edge
Definition base_edge.h:119
virtual void setMeasurement(const Measurement &m)
Definition base_edge.h:122

References g2o::BaseEdge< D, E >::measurement(), and g2o::BaseEdge< D, E >::setMeasurement().

Referenced by main().

◆ ToVertexSE3()

void ToVertexSE3 ( const g2o::VertexSim3Expmap v_sim3,
g2o::VertexSE3 *const  v_se3 
)

Definition at line 65 of file optimize_sphere_by_sim3.cpp.

66 {
67 g2o::Sim3 sim3 = v_sim3.estimate().inverse();
68 Eigen::Matrix3d r = sim3.rotation().toRotationMatrix();
69 Eigen::Vector3d t = sim3.translation();
70 Eigen::Isometry3d se3;
71 se3 = r;
72 se3.translation() = t;
73
74 v_se3->setEstimate(se3);
75}
const EstimateType & estimate() const
return the current estimate of the vertex
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
const Quaternion & rotation() const
Definition sim3.h:249
Sim3 inverse() const
Definition sim3.h:200
const Vector3 & translation() const
Definition sim3.h:245

References g2o::BaseVertex< D, T >::estimate(), g2o::Sim3::inverse(), g2o::Sim3::rotation(), g2o::BaseVertex< D, T >::setEstimate(), and g2o::Sim3::translation().

Referenced by main().

◆ ToVertexSim3()

void ToVertexSim3 ( const g2o::VertexSE3 v_se3,
g2o::VertexSim3Expmap *const  v_sim3 
)

Definition at line 54 of file optimize_sphere_by_sim3.cpp.

55 {
56 Eigen::Isometry3d se3 = v_se3.estimate().inverse();
57 Eigen::Matrix3d r = se3.rotation();
58 Eigen::Vector3d t = se3.translation();
59 g2o::Sim3 sim3(r, t, 1.0);
60
61 v_sim3->setEstimate(sim3);
62}

References g2o::BaseVertex< D, T >::estimate(), and g2o::BaseVertex< D, T >::setEstimate().

Referenced by main().