blob: 4aac445215385455c0c912c7fc57fd88b4d0a1ca [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
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
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
36namespace ceres {
37namespace internal {
38
39void DetectStructure(const CompressedRowBlockStructure& bs,
40 const int num_eliminate_blocks,
41 int* row_block_size,
42 int* e_block_size,
43 int* f_block_size) {
44 const int num_row_blocks = bs.rows.size();
45 *row_block_size = 0;
46 *e_block_size = 0;
47 *f_block_size = 0;
48
49 // Iterate over row blocks of the matrix, checking if row_block,
50 // e_block or f_block sizes remain constant.
51 for (int r = 0; r < num_row_blocks; ++r) {
52 const CompressedRow& row = bs.rows[r];
53 // We do not care about the sizes of the blocks in rows which do
54 // not contain e_blocks.
55 if (row.cells.front().block_id >= num_eliminate_blocks) {
56 break;
57 }
58
59 // Detect fixed or dynamic row block size.
60 if (*row_block_size == 0) {
61 *row_block_size = row.block.size;
62 } else if (*row_block_size != Eigen::Dynamic &&
63 *row_block_size != row.block.size) {
64 VLOG(2) << "Dynamic row block size because the block size changed from "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080065 << *row_block_size << " to " << row.block.size;
Austin Schuh70cc9552019-01-21 19:46:48 -080066 *row_block_size = Eigen::Dynamic;
67 }
68
69 // Detect fixed or dynamic e-block size.
70 const int e_block_id = row.cells.front().block_id;
71 if (*e_block_size == 0) {
72 *e_block_size = bs.cols[e_block_id].size;
73 } else if (*e_block_size != Eigen::Dynamic &&
74 *e_block_size != bs.cols[e_block_id].size) {
75 VLOG(2) << "Dynamic e block size because the block size changed from "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080076 << *e_block_size << " to " << bs.cols[e_block_id].size;
Austin Schuh70cc9552019-01-21 19:46:48 -080077 *e_block_size = Eigen::Dynamic;
78 }
79
80 // Detect fixed or dynamic f-block size. We are only interested in
81 // rows with e-blocks, and the e-block is always the first block,
82 // so only rows of size greater than 1 are of interest.
83 if (row.cells.size() > 1) {
84 if (*f_block_size == 0) {
85 const int f_block_id = row.cells[1].block_id;
86 *f_block_size = bs.cols[f_block_id].size;
87 }
88
89 for (int c = 1;
90 (c < row.cells.size()) && (*f_block_size != Eigen::Dynamic);
91 ++c) {
92 const int f_block_id = row.cells[c].block_id;
93 if (*f_block_size != bs.cols[f_block_id].size) {
94 VLOG(2) << "Dynamic f block size because the block size "
95 << "changed from " << *f_block_size << " to "
96 << bs.cols[f_block_id].size;
97 *f_block_size = Eigen::Dynamic;
98 }
99 }
100 }
101
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800102 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800103 const bool is_everything_dynamic = (*row_block_size == Eigen::Dynamic &&
104 *e_block_size == Eigen::Dynamic &&
105 *f_block_size == Eigen::Dynamic);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800106 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800107 if (is_everything_dynamic) {
108 break;
109 }
110 }
111
112 CHECK_NE(*row_block_size, 0) << "No rows found";
113 CHECK_NE(*e_block_size, 0) << "No e type blocks found";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800114 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800115 VLOG(1) << "Schur complement static structure <"
116 << *row_block_size << ","
117 << *e_block_size << ","
118 << *f_block_size << ">.";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800119 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800120}
121
122} // namespace internal
123} // namespace ceres