blob: 6c10fb250b0578fefbe0a3e5af60c1cc771bc45e [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: kushalav@google.com (Avanish Kushal)
30
31#include "ceres/visibility.h"
32
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080033#include <algorithm>
Austin Schuh70cc9552019-01-21 19:46:48 -080034#include <cmath>
35#include <ctime>
Austin Schuh3de38b02024-06-25 18:25:10 -070036#include <memory>
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include <set>
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include <unordered_map>
39#include <utility>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040#include <vector>
41
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include "ceres/block_structure.h"
43#include "ceres/graph.h"
44#include "ceres/pair_hash.h"
45#include "glog/logging.h"
46
Austin Schuh3de38b02024-06-25 18:25:10 -070047namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080048
49void ComputeVisibility(const CompressedRowBlockStructure& block_structure,
50 const int num_eliminate_blocks,
Austin Schuh3de38b02024-06-25 18:25:10 -070051 std::vector<std::set<int>>* visibility) {
Austin Schuh70cc9552019-01-21 19:46:48 -080052 CHECK(visibility != nullptr);
53
54 // Clear the visibility vector and resize it to hold a
55 // vector for each camera.
56 visibility->resize(0);
57 visibility->resize(block_structure.cols.size() - num_eliminate_blocks);
58
Austin Schuh3de38b02024-06-25 18:25:10 -070059 for (const auto& row : block_structure.rows) {
60 const std::vector<Cell>& cells = row.cells;
Austin Schuh70cc9552019-01-21 19:46:48 -080061 int block_id = cells[0].block_id;
62 // If the first block is not an e_block, then skip this row block.
63 if (block_id >= num_eliminate_blocks) {
64 continue;
65 }
66
67 for (int j = 1; j < cells.size(); ++j) {
68 int camera_block_id = cells[j].block_id - num_eliminate_blocks;
69 DCHECK_GE(camera_block_id, 0);
70 DCHECK_LT(camera_block_id, visibility->size());
71 (*visibility)[camera_block_id].insert(block_id);
72 }
73 }
74}
75
Austin Schuh3de38b02024-06-25 18:25:10 -070076std::unique_ptr<WeightedGraph<int>> CreateSchurComplementGraph(
77 const std::vector<std::set<int>>& visibility) {
78 const time_t start_time = time(nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -080079 // Compute the number of e_blocks/point blocks. Since the visibility
80 // set for each e_block/camera contains the set of e_blocks/points
81 // visible to it, we find the maximum across all visibility sets.
82 int num_points = 0;
Austin Schuh3de38b02024-06-25 18:25:10 -070083 for (const auto& visible : visibility) {
84 if (!visible.empty()) {
85 num_points = std::max(num_points, (*visible.rbegin()) + 1);
Austin Schuh70cc9552019-01-21 19:46:48 -080086 }
87 }
88
89 // Invert the visibility. The input is a camera->point mapping,
90 // which tells us which points are visible in which
91 // cameras. However, to compute the sparsity structure of the Schur
92 // Complement efficiently, its better to have the point->camera
93 // mapping.
Austin Schuh3de38b02024-06-25 18:25:10 -070094 std::vector<std::set<int>> inverse_visibility(num_points);
Austin Schuh70cc9552019-01-21 19:46:48 -080095 for (int i = 0; i < visibility.size(); i++) {
Austin Schuh3de38b02024-06-25 18:25:10 -070096 const std::set<int>& visibility_set = visibility[i];
97 for (int v : visibility_set) {
Austin Schuh70cc9552019-01-21 19:46:48 -080098 inverse_visibility[v].insert(i);
99 }
100 }
101
102 // Map from camera pairs to number of points visible to both cameras
103 // in the pair.
Austin Schuh3de38b02024-06-25 18:25:10 -0700104 std::unordered_map<std::pair<int, int>, int, pair_hash> camera_pairs;
Austin Schuh70cc9552019-01-21 19:46:48 -0800105
106 // Count the number of points visible to each camera/f_block pair.
107 for (const auto& inverse_visibility_set : inverse_visibility) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700108 for (auto camera1 = inverse_visibility_set.begin();
Austin Schuh70cc9552019-01-21 19:46:48 -0800109 camera1 != inverse_visibility_set.end();
110 ++camera1) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700111 auto camera2 = camera1;
Austin Schuh70cc9552019-01-21 19:46:48 -0800112 for (++camera2; camera2 != inverse_visibility_set.end(); ++camera2) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700113 ++(camera_pairs[std::make_pair(*camera1, *camera2)]);
Austin Schuh70cc9552019-01-21 19:46:48 -0800114 }
115 }
116 }
117
Austin Schuh3de38b02024-06-25 18:25:10 -0700118 auto graph = std::make_unique<WeightedGraph<int>>();
Austin Schuh70cc9552019-01-21 19:46:48 -0800119
120 // Add vertices and initialize the pairs for self edges so that self
121 // edges are guaranteed. This is needed for the Canonical views
122 // algorithm to work correctly.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123 static constexpr double kSelfEdgeWeight = 1.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800124 for (int i = 0; i < visibility.size(); ++i) {
125 graph->AddVertex(i);
126 graph->AddEdge(i, i, kSelfEdgeWeight);
127 }
128
129 // Add an edge for each camera pair.
130 for (const auto& camera_pair_count : camera_pairs) {
131 const int camera1 = camera_pair_count.first.first;
132 const int camera2 = camera_pair_count.first.second;
133 const int count = camera_pair_count.second;
134 DCHECK_NE(camera1, camera2);
135 // Static cast necessary for Windows.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800136 const double weight =
137 static_cast<double>(count) /
138 (sqrt(static_cast<double>(visibility[camera1].size() *
139 visibility[camera2].size())));
Austin Schuh70cc9552019-01-21 19:46:48 -0800140 graph->AddEdge(camera1, camera2, weight);
141 }
142
Austin Schuh3de38b02024-06-25 18:25:10 -0700143 VLOG(2) << "Schur complement graph time: " << (time(nullptr) - start_time);
Austin Schuh70cc9552019-01-21 19:46:48 -0800144 return graph;
145}
146
Austin Schuh3de38b02024-06-25 18:25:10 -0700147} // namespace ceres::internal