| 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 | // clang-format off |
| 28 | #include "g2o/config.h" |
| 29 | // clang-format on |
| 30 | |
| 31 | #include "g2o/solvers/dense/linear_solver_dense.h" |
| 32 | #include "g2o/solvers/eigen/linear_solver_eigen.h" |
| 33 | #ifdef G2O_HAVE_CSPARSE |
| 34 | #include "g2o/solvers/csparse/linear_solver_csparse.h" |
| 35 | #endif |
| 36 | #ifdef G2O_HAVE_CHOLMOD |
| 37 | #include "g2o/solvers/cholmod/linear_solver_cholmod.h" |
| 38 | #endif |
| 39 | #include "gtest/gtest.h" |
| 40 | #include "sparse_system_helper.h" |
| 41 | |
| 42 | namespace { |
| 43 | struct BlockOrdering { |
| 44 | template <typename T> |
| 45 | void setOrdering(T& solver) { |
| 46 | solver.setBlockOrdering(true); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | struct NoBlockOrdering { |
| 51 | template <typename T> |
| 52 | void setOrdering(T& solver) { |
| 53 | solver.setBlockOrdering(false); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | struct NoooOrdering { |
| 58 | template <typename T> |
| 59 | void setOrdering(T& solver) { |
| 60 | (void)solver; |
| 61 | } |
| 62 | }; |
| 63 | } // namespace |
| 64 | |
| 65 | /** |
| 66 | * Type parameterized class for a fixture to setup a linear solver along with |
| 67 | * some data of a linear system to be solved. |
| 68 | */ |
| 69 | template <typename T> |
| 70 | class LS : public testing::Test { |
| 71 | public: |
| 72 | using LinearSolverType = typename T::first_type; |
| 73 | using OrderingType = typename T::second_type; |
| 74 | |
| 75 | LS() : linearsolver(std::make_unique<LinearSolverType>()) { |
| 76 | g2o::internal::fillTestMatrix(sparse_matrix); |
| 77 | } |
| 78 | |
| 79 | protected: |
| 80 | std::unique_ptr<LinearSolverType> linearsolver; |
| 81 | OrderingType ordering; |
| 82 | g2o::SparseBlockMatrixX sparse_matrix; |
| 83 | g2o::MatrixX matrix_inverse = g2o::internal::createTestMatrixInverse(); |
| 84 | g2o::VectorX x_vector = g2o::internal::createTestVectorX(); |
| 85 | g2o::VectorX b_vector = g2o::internal::createTestVectorB(); |
| 86 | }; |
| 87 | TYPED_TEST_SUITE_P(LS); |
| 88 | |
| 89 | TYPED_TEST_P(LS, Solve) { |
| 90 | this->ordering.setOrdering(*this->linearsolver); |
| 91 | |
| 92 | g2o::VectorX solver_solution; |
| 93 | for (int solve_iter = 0; solve_iter < 2; ++solve_iter) { |
| 94 | solver_solution.setZero(this->b_vector.size()); |
| 95 | this->linearsolver->solve(this->sparse_matrix, solver_solution.data(), |
| 96 | this->b_vector.data()); |
| 97 | |
| 98 | ASSERT_TRUE(solver_solution.isApprox(this->x_vector, 1e-6)) |
| 99 | << "Solution differs on iteration " << solve_iter; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | TYPED_TEST_P(LS, SolvePattern) { |
| 104 | this->ordering.setOrdering(*this->linearsolver); |
| 105 | |
| 106 | g2o::SparseBlockMatrixX spinv; |
| 107 | std::vector<std::pair<int, int>> blockIndices; |
| 108 | for (int i = 0; |
| 109 | i < static_cast<int>(this->sparse_matrix.rowBlockIndices().size()); ++i) |
| 110 | blockIndices.emplace_back(std::make_pair(i, i)); |
| 111 | |
| 112 | bool state = this->linearsolver->solvePattern(spinv, blockIndices, |
| 113 | this->sparse_matrix); |
| 114 | ASSERT_TRUE(!state || spinv.rowBlockIndices().size() == blockIndices.size()); |
| 115 | if (!state) { // solver does not implement solving for a pattern return in |
| 116 | // this case |
| 117 | std::cerr << "Solver does not support solvePattern()" << std::endl; |
| 118 | SUCCEED(); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | for (const auto& idx : blockIndices) { |
| 123 | int rr = spinv.rowBaseOfBlock(idx.first); |
| 124 | int cc = spinv.colBaseOfBlock(idx.second); |
| 125 | int numRows = spinv.rowsOfBlock(idx.first); |
| 126 | int numCols = spinv.colsOfBlock(idx.second); |
| 127 | |
| 128 | g2o::MatrixX expected = |
| 129 | this->matrix_inverse.block(rr, cc, numRows, numCols); |
| 130 | g2o::MatrixX actual = *spinv.block(idx.first, idx.second); |
| 131 | |
| 132 | EXPECT_TRUE(actual.isApprox(expected, 1e-6)) |
| 133 | << "block " << idx.first << " " << idx.second << " differs"; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | TYPED_TEST_P(LS, SolveBlocks) { |
| 138 | this->ordering.setOrdering(*this->linearsolver); |
| 139 | |
| 140 | double** blocks = nullptr; |
| 141 | bool state = this->linearsolver->solveBlocks(blocks, this->sparse_matrix); |
| 142 | ASSERT_TRUE(!state || blocks != nullptr); |
| 143 | if (!state) { // solver does not implement solving for a pattern return in |
| 144 | // this case |
| 145 | std::cerr << "Solver does not support solveBlocks()" << std::endl; |
| 146 | SUCCEED(); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | for (size_t i = 0; i < this->sparse_matrix.rowBlockIndices().size(); ++i) { |
| 151 | int rr = this->sparse_matrix.rowBaseOfBlock(i); |
| 152 | int cc = this->sparse_matrix.colBaseOfBlock(i); |
| 153 | int numRows = this->sparse_matrix.rowsOfBlock(i); |
| 154 | int numCols = this->sparse_matrix.colsOfBlock(i); |
| 155 | |
| 156 | g2o::MatrixX expected = |
| 157 | this->matrix_inverse.block(rr, cc, numRows, numCols); |
| 158 | g2o::MatrixX::MapType actual(blocks[i], numRows, numCols); |
| 159 | |
| 160 | EXPECT_TRUE(actual.isApprox(expected, 1e-6)) |
| 161 | << "block " << i << " " << i << " differs"; |
| 162 | } |
| 163 | TypeParam::first_type::deallocateBlocks(this->sparse_matrix, blocks); |
| 164 | } |
| 165 | |
| 166 | // registering the test suite and all the types to be tested |
| 167 | REGISTER_TYPED_TEST_SUITE_P(LS, Solve, SolvePattern, SolveBlocks); |
| 168 | |
| 169 | using LinearSolverTypes = ::testing::Types< |
| 170 | #ifdef G2O_HAVE_CSPARSE |
| 171 | std::pair<g2o::LinearSolverCSparse<g2o::MatrixX>, NoBlockOrdering>, |
| 172 | std::pair<g2o::LinearSolverCSparse<g2o::MatrixX>, BlockOrdering>, |
| 173 | #endif |
| 174 | #ifdef G2O_HAVE_CHOLMOD |
| 175 | std::pair<g2o::LinearSolverCholmod<g2o::MatrixX>, NoBlockOrdering>, |
| 176 | std::pair<g2o::LinearSolverCholmod<g2o::MatrixX>, BlockOrdering>, |
| 177 | #endif |
| 178 | std::pair<g2o::LinearSolverEigen<g2o::MatrixX>, NoBlockOrdering>, |
| 179 | std::pair<g2o::LinearSolverEigen<g2o::MatrixX>, BlockOrdering>, |
| 180 | std::pair<g2o::LinearSolverDense<g2o::MatrixX>, NoooOrdering>>; |
| 181 | INSTANTIATE_TYPED_TEST_SUITE_P(LinearSolver, LS, LinearSolverTypes); |