blob: e82d70fb21ac2889f74b7574cf87a8b39d77e066 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
31#include "ceres/detect_structure.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080032
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include "ceres/internal/eigen.h"
34#include "glog/logging.h"
35
Austin Schuh3de38b02024-06-25 18:25:10 -070036namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080037
38void DetectStructure(const CompressedRowBlockStructure& bs,
39 const int num_eliminate_blocks,
40 int* row_block_size,
41 int* e_block_size,
42 int* f_block_size) {
43 const int num_row_blocks = bs.rows.size();
44 *row_block_size = 0;
45 *e_block_size = 0;
46 *f_block_size = 0;
47
48 // Iterate over row blocks of the matrix, checking if row_block,
49 // e_block or f_block sizes remain constant.
50 for (int r = 0; r < num_row_blocks; ++r) {
51 const CompressedRow& row = bs.rows[r];
52 // We do not care about the sizes of the blocks in rows which do
53 // not contain e_blocks.
54 if (row.cells.front().block_id >= num_eliminate_blocks) {
55 break;
56 }
57
58 // Detect fixed or dynamic row block size.
59 if (*row_block_size == 0) {
60 *row_block_size = row.block.size;
61 } else if (*row_block_size != Eigen::Dynamic &&
62 *row_block_size != row.block.size) {
63 VLOG(2) << "Dynamic row block size because the block size changed from "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080064 << *row_block_size << " to " << row.block.size;
Austin Schuh70cc9552019-01-21 19:46:48 -080065 *row_block_size = Eigen::Dynamic;
66 }
67
68 // Detect fixed or dynamic e-block size.
69 const int e_block_id = row.cells.front().block_id;
70 if (*e_block_size == 0) {
71 *e_block_size = bs.cols[e_block_id].size;
72 } else if (*e_block_size != Eigen::Dynamic &&
73 *e_block_size != bs.cols[e_block_id].size) {
74 VLOG(2) << "Dynamic e block size because the block size changed from "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080075 << *e_block_size << " to " << bs.cols[e_block_id].size;
Austin Schuh70cc9552019-01-21 19:46:48 -080076 *e_block_size = Eigen::Dynamic;
77 }
78
79 // Detect fixed or dynamic f-block size. We are only interested in
80 // rows with e-blocks, and the e-block is always the first block,
81 // so only rows of size greater than 1 are of interest.
82 if (row.cells.size() > 1) {
83 if (*f_block_size == 0) {
84 const int f_block_id = row.cells[1].block_id;
85 *f_block_size = bs.cols[f_block_id].size;
86 }
87
88 for (int c = 1;
89 (c < row.cells.size()) && (*f_block_size != Eigen::Dynamic);
90 ++c) {
91 const int f_block_id = row.cells[c].block_id;
92 if (*f_block_size != bs.cols[f_block_id].size) {
93 VLOG(2) << "Dynamic f block size because the block size "
94 << "changed from " << *f_block_size << " to "
95 << bs.cols[f_block_id].size;
96 *f_block_size = Eigen::Dynamic;
97 }
98 }
99 }
100
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800101 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800102 const bool is_everything_dynamic = (*row_block_size == Eigen::Dynamic &&
103 *e_block_size == Eigen::Dynamic &&
104 *f_block_size == Eigen::Dynamic);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800105 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800106 if (is_everything_dynamic) {
107 break;
108 }
109 }
110
111 CHECK_NE(*row_block_size, 0) << "No rows found";
112 CHECK_NE(*e_block_size, 0) << "No e type blocks found";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800113 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800114 VLOG(1) << "Schur complement static structure <"
115 << *row_block_size << ","
116 << *e_block_size << ","
117 << *f_block_size << ">.";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800118 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800119}
120
Austin Schuh3de38b02024-06-25 18:25:10 -0700121} // namespace ceres::internal