Debian Codegraph @

unit_test/slam3d/optimization_slam3d.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "g2o/core/block_solver.h"
28 #include "g2o/core/optimization_algorithm_dogleg.h"
29 #include "g2o/core/optimization_algorithm_gauss_newton.h"
30 #include "g2o/core/optimization_algorithm_levenberg.h"
31 #include "g2o/solvers/eigen/linear_solver_eigen.h"
32 #include "g2o/types/slam3d/edge_se3.h"
•33 #include "gmock/gmock.h"
34 #include "unit_test/test_helper/eigen_matcher.h"
35
36 using namespace std;
37 using namespace g2o;
38
39 typedef g2o::LinearSolverEigen<g2o::BlockSolverX::PoseMatrixType>
40 SlamLinearSolver;
41
42 template <typename T>
43 class Slam3DOptimization : public testing::Test {
44 public:
45 using OptimizationAlgo = T;
46
47 Slam3DOptimization() {
48 auto linearSolver = std::make_unique<SlamLinearSolver>();
49 linearSolver->setBlockOrdering(false);
50 auto blockSolver =
51 std::make_unique<g2o::BlockSolverX>(std::move(linearSolver));
52 OptimizationAlgo* algorithm = new OptimizationAlgo(std::move(blockSolver));
53
54 optimizer.setAlgorithm(algorithm);
55 }
56
57 protected:
58 g2o::SparseOptimizer optimizer;
59 };
60 TYPED_TEST_SUITE_P(Slam3DOptimization);
61
62 TYPED_TEST_P(Slam3DOptimization, Translation) {
63 g2o::VertexSE3* v = new g2o::VertexSE3();
64 v->setId(0);
65 v->setEstimate(g2o::Isometry3::Identity());
66 v->setFixed(true);
67 this->optimizer.addVertex(v);
68
69 v = new g2o::VertexSE3();
70 v->setId(1);
71 // move vertex away from origin
72 g2o::Isometry3 p2 = g2o::Isometry3::Identity();
73 p2.translation() << 10., 10., 10.;
74 v->setEstimate(p2);
75 v->setFixed(false);
76 this->optimizer.addVertex(v);
77
78 g2o::EdgeSE3* e = new g2o::EdgeSE3();
79 e->setInformation(g2o::EdgeSE3::InformationType::Identity());
80 e->setMeasurement(g2o::Isometry3::Identity());
81 e->vertices()[0] = this->optimizer.vertex(0);
82 e->vertices()[1] = this->optimizer.vertex(1);
83 this->optimizer.addEdge(e);
84
85 this->optimizer.initializeOptimization();
86 this->optimizer.computeActiveErrors();
87 ASSERT_LT(0., this->optimizer.chi2());
88 int numOptimization = this->optimizer.optimize(100);
89 ASSERT_LT(0, numOptimization);
90 ASSERT_GT(1e-6, this->optimizer.chi2());
91 ASSERT_GT(1e-6, this->optimizer.activeChi2());
92
93 g2o::VertexSE3* v2AfterOpti =
94 dynamic_cast<g2o::VertexSE3*>(this->optimizer.vertex(1));
•95 EXPECT_THAT(print_wrap(v2AfterOpti->estimate().translation()),
96 EigenMaxDiff(print_wrap(g2o::Vector3::Zero()), 1e-3));
97 EXPECT_THAT(print_wrap(v2AfterOpti->estimate().rotation().diagonal()),
98 EigenMaxDiff(print_wrap(g2o::Vector3::Ones()), 1e-3));
99 }
100
101 TYPED_TEST_P(Slam3DOptimization, Rotation) {
102 g2o::VertexSE3* v = new g2o::VertexSE3();
103 v->setId(0);
104 v->setEstimate(g2o::Isometry3::Identity());
105 v->setFixed(true);
106 this->optimizer.addVertex(v);
107
108 v = new g2o::VertexSE3();
109 v->setId(1);
110 // rotate vertex a bit
111 g2o::Isometry3 p2 = g2o::Isometry3::Identity();
112 p2 *= g2o::AngleAxis(g2o::deg2rad(2), g2o::Vector3::Ones().normalized());
113 v->setEstimate(p2);
114 v->setFixed(false);
115 this->optimizer.addVertex(v);
116
117 g2o::EdgeSE3* e = new g2o::EdgeSE3();
118 e->setInformation(g2o::EdgeSE3::InformationType::Identity());
119 e->setMeasurement(g2o::Isometry3::Identity());
120 e->vertices()[0] = this->optimizer.vertex(0);
121 e->vertices()[1] = this->optimizer.vertex(1);
122 this->optimizer.addEdge(e);
123
124 this->optimizer.initializeOptimization();
125 this->optimizer.computeActiveErrors();
126 ASSERT_LT(0., this->optimizer.chi2());
127 int numOptimization = this->optimizer.optimize(100);
128 ASSERT_LT(0, numOptimization);
129 ASSERT_GT(1e-6, this->optimizer.chi2());
130 ASSERT_GT(1e-6, this->optimizer.activeChi2());
131
132 g2o::VertexSE3* v2AfterOpti =
133 dynamic_cast<g2o::VertexSE3*>(this->optimizer.vertex(1));
•134 EXPECT_THAT(print_wrap(v2AfterOpti->estimate().translation()),
135 EigenMaxDiff(print_wrap(g2o::Vector3::Zero()), 1e-3));
136 EXPECT_THAT(print_wrap(v2AfterOpti->estimate().rotation().diagonal()),
137 EigenMaxDiff(print_wrap(g2o::Vector3::Ones()), 1e-3));
138 }
139
140 // registering the test suite and all the types to be tested
141 REGISTER_TYPED_TEST_SUITE_P(Slam3DOptimization, Translation, Rotation);
142
143 using OptimizationAlgorithmTypes =
144 ::testing::Types<OptimizationAlgorithmGaussNewton,
145 OptimizationAlgorithmLevenberg,
146 OptimizationAlgorithmDogleg>;
147 INSTANTIATE_TYPED_TEST_SUITE_P(Slam3D, Slam3DOptimization,
148 OptimizationAlgorithmTypes);