| 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 "allocate_optimizer.h" |
| 28 | #include "g2o/core/sparse_optimizer.h" |
| 29 | #include "g2o/core/sparse_optimizer_terminate_action.h" |
| 30 | #include "g2o/types/slam3d/types_slam3d.h" |
| 31 | #include "gtest/gtest.h" |
| 32 | |
| 33 | TEST(General, ClearAndRedo) { |
| 34 | // Initialize the SparseOptimizer |
| 35 | std::unique_ptr<g2o::SparseOptimizer> mOptimizerPtr; |
| 36 | mOptimizerPtr.reset(g2o::internal::createOptimizerForTests()); |
| 37 | g2o::SparseOptimizer& mOptimizer = *mOptimizerPtr; |
| 38 | |
| 39 | // Set the default terminate action |
| 40 | g2o::SparseOptimizerTerminateAction* terminateAction = |
| 41 | new g2o::SparseOptimizerTerminateAction; |
| 42 | mOptimizer.addPostIterationAction(terminateAction); |
| 43 | |
| 44 | for (int i = 0; i < 2; i++) { |
| 45 | // Add vertices |
| 46 | g2o::VertexSE3* v0 = new g2o::VertexSE3; |
| 47 | v0->setEstimate( |
| 48 | Eigen::Transform<double, 3, 1>(Eigen::Translation<double, 3>(0, 0, 0))); |
| 49 | v0->setId(0); |
| 50 | mOptimizer.addVertex(v0); |
| 51 | |
| 52 | g2o::VertexSE3* v1 = new g2o::VertexSE3; |
| 53 | v1->setEstimate( |
| 54 | Eigen::Transform<double, 3, 1>(Eigen::Translation<double, 3>(0, 0, 0))); |
| 55 | v1->setId(1); |
| 56 | mOptimizer.addVertex(v1); |
| 57 | |
| 58 | g2o::VertexSE3* v2 = new g2o::VertexSE3; |
| 59 | v2->setEstimate( |
| 60 | Eigen::Transform<double, 3, 1>(Eigen::Translation<double, 3>(0, 0, 0))); |
| 61 | v2->setId(2); |
| 62 | mOptimizer.addVertex(v2); |
| 63 | |
| 64 | // Add edges |
| 65 | g2o::EdgeSE3* e1 = new g2o::EdgeSE3(); |
| 66 | e1->vertices()[0] = mOptimizer.vertex(0); |
| 67 | e1->vertices()[1] = mOptimizer.vertex(1); |
| 68 | e1->setMeasurement(g2o::Isometry3(Eigen::Translation<double, 3>(1, 0, 0))); |
| 69 | e1->setInformation(g2o::MatrixN<6>::Identity()); |
| 70 | mOptimizer.addEdge(e1); |
| 71 | |
| 72 | g2o::EdgeSE3* e2 = new g2o::EdgeSE3(); |
| 73 | e2->vertices()[0] = mOptimizer.vertex(1); |
| 74 | e2->vertices()[1] = mOptimizer.vertex(2); |
| 75 | e2->setMeasurement(g2o::Isometry3(Eigen::Translation<double, 3>(0, 1, 0))); |
| 76 | e2->setInformation(g2o::MatrixN<6>::Identity()); |
| 77 | mOptimizer.addEdge(e2); |
| 78 | |
| 79 | g2o::EdgeSE3* e3 = new g2o::EdgeSE3(); |
| 80 | e3->vertices()[0] = mOptimizer.vertex(2); |
| 81 | e3->vertices()[1] = mOptimizer.vertex(0); |
| 82 | e3->setMeasurement( |
| 83 | g2o::Isometry3(Eigen::Translation<double, 3>(-0.8, -0.7, 0.1))); |
| 84 | e3->setInformation(g2o::MatrixN<6>::Identity()); |
| 85 | mOptimizer.addEdge(e3); |
| 86 | |
| 87 | v0->setFixed(true); |
| 88 | |
| 89 | // mOptimizer.setVerbose(true); |
| 90 | mOptimizer.initializeOptimization(); |
| 91 | mOptimizer.computeInitialGuess(); |
| 92 | mOptimizer.computeActiveErrors(); |
| 93 | int iter = mOptimizer.optimize(10); |
| 94 | if (iter <= 0) { |
| 95 | ADD_FAILURE() << "Optimization did not happen"; |
| 96 | } else { |
| 97 | SUCCEED(); |
| 98 | } |
| 99 | |
| 100 | mOptimizer.clear(); |
| 101 | } |
| 102 | delete terminateAction; |
| 103 | } |