Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // http://ceres-solver.org/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: kushalav@google.com (Avanish Kushal) |
| 30 | // sameeragarwal@google.com (Sameer Agarwal) |
| 31 | |
| 32 | #include "ceres/visibility.h" |
| 33 | |
| 34 | #include <memory> |
| 35 | #include <set> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include "ceres/block_structure.h" |
| 39 | #include "ceres/graph.h" |
| 40 | #include "glog/logging.h" |
| 41 | #include "gtest/gtest.h" |
| 42 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 43 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 45 | class VisibilityTest : public ::testing::Test {}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | |
| 47 | TEST(VisibilityTest, SimpleMatrix) { |
| 48 | // A = [1 0 0 0 0 1 |
| 49 | // 1 0 0 1 0 0 |
| 50 | // 0 1 1 0 0 0 |
| 51 | // 0 1 0 0 1 0] |
| 52 | |
| 53 | int num_cols = 6; |
| 54 | int num_eliminate_blocks = 2; |
| 55 | CompressedRowBlockStructure bs; |
| 56 | |
| 57 | // Row 1 |
| 58 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 59 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 60 | CompressedRow& row = bs.rows.back(); |
| 61 | row.block.size = 2; |
| 62 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 63 | row.cells.emplace_back(0, 0); |
| 64 | row.cells.emplace_back(5, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Row 2 |
| 68 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 69 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | CompressedRow& row = bs.rows.back(); |
| 71 | row.block.size = 2; |
| 72 | row.block.position = 2; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 73 | row.cells.emplace_back(0, 1); |
| 74 | row.cells.emplace_back(3, 1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Row 3 |
| 78 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 79 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 80 | CompressedRow& row = bs.rows.back(); |
| 81 | row.block.size = 2; |
| 82 | row.block.position = 4; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 83 | row.cells.emplace_back(1, 2); |
| 84 | row.cells.emplace_back(2, 2); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Row 4 |
| 88 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 89 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 90 | CompressedRow& row = bs.rows.back(); |
| 91 | row.block.size = 2; |
| 92 | row.block.position = 6; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 93 | row.cells.emplace_back(1, 3); |
| 94 | row.cells.emplace_back(4, 3); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 95 | } |
| 96 | bs.cols.resize(num_cols); |
| 97 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 98 | std::vector<std::set<int>> visibility; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 99 | ComputeVisibility(bs, num_eliminate_blocks, &visibility); |
| 100 | ASSERT_EQ(visibility.size(), num_cols - num_eliminate_blocks); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 101 | for (const auto& visible : visibility) { |
| 102 | ASSERT_EQ(visible.size(), 1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 105 | std::unique_ptr<WeightedGraph<int>> graph( |
| 106 | CreateSchurComplementGraph(visibility)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 107 | EXPECT_EQ(graph->vertices().size(), visibility.size()); |
| 108 | for (int i = 0; i < visibility.size(); ++i) { |
| 109 | EXPECT_EQ(graph->VertexWeight(i), 1.0); |
| 110 | } |
| 111 | |
| 112 | for (int i = 0; i < visibility.size(); ++i) { |
| 113 | for (int j = i; j < visibility.size(); ++j) { |
| 114 | double edge_weight = 0.0; |
| 115 | if ((i == 1 && j == 3) || (i == 0 && j == 2) || (i == j)) { |
| 116 | edge_weight = 1.0; |
| 117 | } |
| 118 | |
| 119 | EXPECT_EQ(graph->EdgeWeight(i, j), edge_weight) |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 120 | << "Edge: " << i << " " << j << " weight: " << graph->EdgeWeight(i, j) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 121 | << " expected weight: " << edge_weight; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 126 | TEST(VisibilityTest, NoEBlocks) { |
| 127 | // A = [1 0 0 0 0 0 |
| 128 | // 1 0 0 0 0 0 |
| 129 | // 0 1 0 0 0 0 |
| 130 | // 0 1 0 0 0 0] |
| 131 | |
| 132 | int num_cols = 6; |
| 133 | int num_eliminate_blocks = 2; |
| 134 | CompressedRowBlockStructure bs; |
| 135 | |
| 136 | // Row 1 |
| 137 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 138 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 139 | CompressedRow& row = bs.rows.back(); |
| 140 | row.block.size = 2; |
| 141 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 142 | row.cells.emplace_back(0, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Row 2 |
| 146 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 147 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 148 | CompressedRow& row = bs.rows.back(); |
| 149 | row.block.size = 2; |
| 150 | row.block.position = 2; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 151 | row.cells.emplace_back(0, 1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Row 3 |
| 155 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 156 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 157 | CompressedRow& row = bs.rows.back(); |
| 158 | row.block.size = 2; |
| 159 | row.block.position = 4; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 160 | row.cells.emplace_back(1, 2); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Row 4 |
| 164 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 165 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 166 | CompressedRow& row = bs.rows.back(); |
| 167 | row.block.size = 2; |
| 168 | row.block.position = 6; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 169 | row.cells.emplace_back(1, 3); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 170 | } |
| 171 | bs.cols.resize(num_cols); |
| 172 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 173 | std::vector<std::set<int>> visibility; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 174 | ComputeVisibility(bs, num_eliminate_blocks, &visibility); |
| 175 | ASSERT_EQ(visibility.size(), num_cols - num_eliminate_blocks); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 176 | for (const auto& visible : visibility) { |
| 177 | ASSERT_EQ(visible.size(), 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 180 | std::unique_ptr<WeightedGraph<int>> graph( |
| 181 | CreateSchurComplementGraph(visibility)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 182 | EXPECT_EQ(graph->vertices().size(), visibility.size()); |
| 183 | for (int i = 0; i < visibility.size(); ++i) { |
| 184 | EXPECT_EQ(graph->VertexWeight(i), 1.0); |
| 185 | } |
| 186 | |
| 187 | for (int i = 0; i < visibility.size(); ++i) { |
| 188 | for (int j = i; j < visibility.size(); ++j) { |
| 189 | double edge_weight = 0.0; |
| 190 | if (i == j) { |
| 191 | edge_weight = 1.0; |
| 192 | } |
| 193 | EXPECT_EQ(graph->EdgeWeight(i, j), edge_weight) |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 194 | << "Edge: " << i << " " << j << " weight: " << graph->EdgeWeight(i, j) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 195 | << " expected weight: " << edge_weight; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 200 | } // namespace ceres::internal |