| 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 "gtest/gtest.h" |
| 34 |
|
| 35 |
using namespace std; |
| 36 |
using namespace g2o; |
| 37 |
|
| 38 |
typedef g2o::LinearSolverEigen<g2o::BlockSolverX::PoseMatrixType> |
| 39 |
SlamLinearSolver; |
| 40 |
|
| 41 |
template <typename T> |
| 42 |
class Slam3DOptimization : public testing::Test { |
| 43 |
public: |
| 44 |
using OptimizationAlgo = T; |
| 45 |
|
| 46 |
Slam3DOptimization() { |
| 47 |
auto linearSolver = std::make_unique<SlamLinearSolver>(); |
| 48 |
linearSolver->setBlockOrdering(false); |
| 49 |
auto blockSolver = |
| 50 |
std::make_unique<g2o::BlockSolverX>(std::move(linearSolver)); |
| 51 |
OptimizationAlgo* algorithm = new OptimizationAlgo(std::move(blockSolver)); |
| 52 |
|
| 53 |
optimizer.setAlgorithm(algorithm); |
| 54 |
} |
| 55 |
|
| 56 |
protected: |
| 57 |
g2o::SparseOptimizer optimizer; |
| 58 |
}; |
| 59 |
TYPED_TEST_SUITE_P(Slam3DOptimization); |
| 60 |
|
| 61 |
TYPED_TEST_P(Slam3DOptimization, Translation) { |
| 62 |
g2o::VertexSE3* v = new g2o::VertexSE3(); |
| 63 |
v->setId(0); |
| 64 |
v->setEstimate(g2o::Isometry3::Identity()); |
| 65 |
v->setFixed(true); |
| 66 |
this->optimizer.addVertex(v); |
| 67 |
|
| 68 |
v = new g2o::VertexSE3(); |
| 69 |
v->setId(1); |
| 70 |
// move vertex away from origin |
| 71 |
g2o::Isometry3 p2 = g2o::Isometry3::Identity(); |
| 72 |
p2.translation() << 10., 10., 10.; |
| 73 |
v->setEstimate(p2); |
| 74 |
v->setFixed(false); |
| 75 |
this->optimizer.addVertex(v); |
| 76 |
|
| 77 |
g2o::EdgeSE3* e = new g2o::EdgeSE3(); |
| 78 |
e->setInformation(g2o::EdgeSE3::InformationType::Identity()); |
| 79 |
e->setMeasurement(g2o::Isometry3::Identity()); |
| 80 |
e->vertices()[0] = this->optimizer.vertex(0); |
| 81 |
e->vertices()[1] = this->optimizer.vertex(1); |
| 82 |
this->optimizer.addEdge(e); |
| 83 |
|
| 84 |
this->optimizer.initializeOptimization(); |
| 85 |
this->optimizer.computeActiveErrors(); |
| 86 |
ASSERT_LT(0., this->optimizer.chi2()); |
| 87 |
int numOptimization = this->optimizer.optimize(100); |
| 88 |
ASSERT_LT(0, numOptimization); |
| 89 |
ASSERT_GT(1e-6, this->optimizer.chi2()); |
| 90 |
ASSERT_GT(1e-6, this->optimizer.activeChi2()); |
| 91 |
|
| 92 |
g2o::VertexSE3* v2AfterOpti = |
| 93 |
dynamic_cast<g2o::VertexSE3*>(this->optimizer.vertex(1)); |
| 94 |
ASSERT_TRUE( |
| 95 |
v2AfterOpti->estimate().translation().isApprox(g2o::Vector3::Zero())); |
| 96 |
ASSERT_TRUE(v2AfterOpti->estimate().rotation().diagonal().isApprox( |
| 97 |
g2o::Vector3::Ones())); |
| 98 |
} |
| 99 |
|
| 100 |
TYPED_TEST_P(Slam3DOptimization, Rotation) { |
| 101 |
g2o::VertexSE3* v = new g2o::VertexSE3(); |
| 102 |
v->setId(0); |
| 103 |
v->setEstimate(g2o::Isometry3::Identity()); |
| 104 |
v->setFixed(true); |
| 105 |
this->optimizer.addVertex(v); |
| 106 |
|
| 107 |
v = new g2o::VertexSE3(); |
| 108 |
v->setId(1); |
| 109 |
// rotate vertex a bit |
| 110 |
g2o::Isometry3 p2 = g2o::Isometry3::Identity(); |
| 111 |
p2 *= g2o::AngleAxis(g2o::deg2rad(2), g2o::Vector3::Ones().normalized()); |
| 112 |
v->setEstimate(p2); |
| 113 |
v->setFixed(false); |
| 114 |
this->optimizer.addVertex(v); |
| 115 |
|
| 116 |
g2o::EdgeSE3* e = new g2o::EdgeSE3(); |
| 117 |
e->setInformation(g2o::EdgeSE3::InformationType::Identity()); |
| 118 |
e->setMeasurement(g2o::Isometry3::Identity()); |
| 119 |
e->vertices()[0] = this->optimizer.vertex(0); |
| 120 |
e->vertices()[1] = this->optimizer.vertex(1); |
| 121 |
this->optimizer.addEdge(e); |
| 122 |
|
| 123 |
this->optimizer.initializeOptimization(); |
| 124 |
this->optimizer.computeActiveErrors(); |
| 125 |
ASSERT_LT(0., this->optimizer.chi2()); |
| 126 |
int numOptimization = this->optimizer.optimize(100); |
| 127 |
ASSERT_LT(0, numOptimization); |
| 128 |
ASSERT_GT(1e-6, this->optimizer.chi2()); |
| 129 |
ASSERT_GT(1e-6, this->optimizer.activeChi2()); |
| 130 |
|
| 131 |
g2o::VertexSE3* v2AfterOpti = |
| 132 |
dynamic_cast<g2o::VertexSE3*>(this->optimizer.vertex(1)); |
| 133 |
ASSERT_TRUE( |
| 134 |
v2AfterOpti->estimate().translation().isApprox(g2o::Vector3::Zero())); |
| 135 |
ASSERT_TRUE(v2AfterOpti->estimate().rotation().diagonal().isApprox( |
| 136 |
g2o::Vector3::Ones())); |
| 137 |
} |
| 138 |
|
| 139 |
// registering the test suite and all the types to be tested |
| 140 |
REGISTER_TYPED_TEST_SUITE_P(Slam3DOptimization, Translation, Rotation); |
| 141 |
|
| 142 |
using OptimizationAlgorithmTypes = |
| 143 |
::testing::Types<OptimizationAlgorithmGaussNewton, |
| 144 |
OptimizationAlgorithmLevenberg, |
| 145 |
OptimizationAlgorithmDogleg>; |
| 146 |
INSTANTIATE_TYPED_TEST_SUITE_P(Slam3D, Slam3DOptimization, |
| 147 |
OptimizationAlgorithmTypes); |