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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 31 | #include "ceres/detect_structure.h" |
| 32 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 33 | #include "Eigen/Core" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 34 | #include "ceres/block_structure.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 35 | #include "glog/logging.h" |
| 36 | #include "gtest/gtest.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 37 | |
| 38 | namespace ceres { |
| 39 | namespace internal { |
| 40 | |
| 41 | TEST(DetectStructure, EverythingStatic) { |
| 42 | const int expected_row_block_size = 2; |
| 43 | const int expected_e_block_size = 3; |
| 44 | const int expected_f_block_size = 4; |
| 45 | |
| 46 | CompressedRowBlockStructure bs; |
| 47 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 48 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | bs.cols.back().size = 3; |
| 50 | bs.cols.back().position = 0; |
| 51 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 52 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 53 | bs.cols.back().size = 4; |
| 54 | bs.cols.back().position = 3; |
| 55 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 56 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 57 | bs.cols.back().size = 4; |
| 58 | bs.cols.back().position = 7; |
| 59 | |
| 60 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 61 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 62 | CompressedRow& row = bs.rows.back(); |
| 63 | row.block.size = 2; |
| 64 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 65 | row.cells.emplace_back(0, 0); |
| 66 | row.cells.emplace_back(1, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 70 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 71 | CompressedRow& row = bs.rows.back(); |
| 72 | row.block.size = 2; |
| 73 | row.block.position = 2; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 74 | row.cells.emplace_back(0, 0); |
| 75 | row.cells.emplace_back(2, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int row_block_size = 0; |
| 79 | int e_block_size = 0; |
| 80 | int f_block_size = 0; |
| 81 | const int num_eliminate_blocks = 1; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 82 | DetectStructure( |
| 83 | bs, num_eliminate_blocks, &row_block_size, &e_block_size, &f_block_size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 84 | |
| 85 | EXPECT_EQ(row_block_size, expected_row_block_size); |
| 86 | EXPECT_EQ(e_block_size, expected_e_block_size); |
| 87 | EXPECT_EQ(f_block_size, expected_f_block_size); |
| 88 | } |
| 89 | |
| 90 | TEST(DetectStructure, DynamicRow) { |
| 91 | const int expected_row_block_size = Eigen::Dynamic; |
| 92 | const int expected_e_block_size = 3; |
| 93 | const int expected_f_block_size = 4; |
| 94 | |
| 95 | CompressedRowBlockStructure bs; |
| 96 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 97 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 98 | bs.cols.back().size = 3; |
| 99 | bs.cols.back().position = 0; |
| 100 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 101 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 102 | bs.cols.back().size = 4; |
| 103 | bs.cols.back().position = 3; |
| 104 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 105 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | bs.cols.back().size = 4; |
| 107 | bs.cols.back().position = 7; |
| 108 | |
| 109 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 110 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 111 | CompressedRow& row = bs.rows.back(); |
| 112 | row.block.size = 2; |
| 113 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 114 | row.cells.emplace_back(0, 0); |
| 115 | row.cells.emplace_back(1, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 119 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 120 | CompressedRow& row = bs.rows.back(); |
| 121 | row.block.size = 1; |
| 122 | row.block.position = 2; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 123 | row.cells.emplace_back(0, 0); |
| 124 | row.cells.emplace_back(2, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int row_block_size = 0; |
| 128 | int e_block_size = 0; |
| 129 | int f_block_size = 0; |
| 130 | const int num_eliminate_blocks = 1; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 131 | DetectStructure( |
| 132 | bs, num_eliminate_blocks, &row_block_size, &e_block_size, &f_block_size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 133 | |
| 134 | EXPECT_EQ(row_block_size, expected_row_block_size); |
| 135 | EXPECT_EQ(e_block_size, expected_e_block_size); |
| 136 | EXPECT_EQ(f_block_size, expected_f_block_size); |
| 137 | } |
| 138 | |
| 139 | TEST(DetectStructure, DynamicFBlockDifferentRows) { |
| 140 | const int expected_row_block_size = 2; |
| 141 | const int expected_e_block_size = 3; |
| 142 | const int expected_f_block_size = Eigen::Dynamic; |
| 143 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | CompressedRowBlockStructure bs; |
| 145 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 146 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 147 | bs.cols.back().size = 3; |
| 148 | bs.cols.back().position = 0; |
| 149 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 150 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 151 | bs.cols.back().size = 4; |
| 152 | bs.cols.back().position = 3; |
| 153 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 154 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 155 | bs.cols.back().size = 3; |
| 156 | bs.cols.back().position = 7; |
| 157 | |
| 158 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 159 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 160 | CompressedRow& row = bs.rows.back(); |
| 161 | row.block.size = 2; |
| 162 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 163 | row.cells.emplace_back(0, 0); |
| 164 | row.cells.emplace_back(1, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 168 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 169 | CompressedRow& row = bs.rows.back(); |
| 170 | row.block.size = 2; |
| 171 | row.block.position = 2; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 172 | row.cells.emplace_back(0, 0); |
| 173 | row.cells.emplace_back(2, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int row_block_size = 0; |
| 177 | int e_block_size = 0; |
| 178 | int f_block_size = 0; |
| 179 | const int num_eliminate_blocks = 1; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 180 | DetectStructure( |
| 181 | bs, num_eliminate_blocks, &row_block_size, &e_block_size, &f_block_size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 182 | |
| 183 | EXPECT_EQ(row_block_size, expected_row_block_size); |
| 184 | EXPECT_EQ(e_block_size, expected_e_block_size); |
| 185 | EXPECT_EQ(f_block_size, expected_f_block_size); |
| 186 | } |
| 187 | |
| 188 | TEST(DetectStructure, DynamicEBlock) { |
| 189 | const int expected_row_block_size = 2; |
| 190 | const int expected_e_block_size = Eigen::Dynamic; |
| 191 | const int expected_f_block_size = 3; |
| 192 | |
| 193 | CompressedRowBlockStructure bs; |
| 194 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 195 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 196 | bs.cols.back().size = 3; |
| 197 | bs.cols.back().position = 0; |
| 198 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 199 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 200 | bs.cols.back().size = 4; |
| 201 | bs.cols.back().position = 3; |
| 202 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 203 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 204 | bs.cols.back().size = 3; |
| 205 | bs.cols.back().position = 7; |
| 206 | |
| 207 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 208 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 209 | CompressedRow& row = bs.rows.back(); |
| 210 | row.block.size = 2; |
| 211 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 212 | row.cells.emplace_back(0, 0); |
| 213 | row.cells.emplace_back(2, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 217 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 218 | CompressedRow& row = bs.rows.back(); |
| 219 | row.block.size = 2; |
| 220 | row.block.position = 2; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 221 | row.cells.emplace_back(1, 0); |
| 222 | row.cells.emplace_back(2, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int row_block_size = 0; |
| 226 | int e_block_size = 0; |
| 227 | int f_block_size = 0; |
| 228 | const int num_eliminate_blocks = 2; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 229 | DetectStructure( |
| 230 | bs, num_eliminate_blocks, &row_block_size, &e_block_size, &f_block_size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 231 | |
| 232 | EXPECT_EQ(row_block_size, expected_row_block_size); |
| 233 | EXPECT_EQ(e_block_size, expected_e_block_size); |
| 234 | EXPECT_EQ(f_block_size, expected_f_block_size); |
| 235 | } |
| 236 | |
| 237 | TEST(DetectStructure, DynamicFBlockSameRow) { |
| 238 | const int expected_row_block_size = 2; |
| 239 | const int expected_e_block_size = 3; |
| 240 | const int expected_f_block_size = Eigen::Dynamic; |
| 241 | |
| 242 | CompressedRowBlockStructure bs; |
| 243 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 244 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 245 | bs.cols.back().size = 3; |
| 246 | bs.cols.back().position = 0; |
| 247 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 248 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 249 | bs.cols.back().size = 4; |
| 250 | bs.cols.back().position = 3; |
| 251 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 252 | bs.cols.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 253 | bs.cols.back().size = 3; |
| 254 | bs.cols.back().position = 7; |
| 255 | |
| 256 | { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 257 | bs.rows.emplace_back(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 258 | CompressedRow& row = bs.rows.back(); |
| 259 | row.block.size = 2; |
| 260 | row.block.position = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 261 | row.cells.emplace_back(0, 0); |
| 262 | row.cells.emplace_back(1, 0); |
| 263 | row.cells.emplace_back(2, 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int row_block_size = 0; |
| 267 | int e_block_size = 0; |
| 268 | int f_block_size = 0; |
| 269 | const int num_eliminate_blocks = 1; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 270 | DetectStructure( |
| 271 | bs, num_eliminate_blocks, &row_block_size, &e_block_size, &f_block_size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 272 | |
| 273 | EXPECT_EQ(row_block_size, expected_row_block_size); |
| 274 | EXPECT_EQ(e_block_size, expected_e_block_size); |
| 275 | EXPECT_EQ(f_block_size, expected_f_block_size); |
| 276 | } |
| 277 | |
| 278 | } // namespace internal |
| 279 | } // namespace ceres |