blob: 72a1c337e7a0fe3789fc90bc1a04c533bb9162fe [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: kushalav@google.com (Avanish Kushal)
30
31#include "ceres/visibility.h"
32
33#include <cmath>
34#include <ctime>
35#include <algorithm>
36#include <set>
37#include <vector>
38#include <unordered_map>
39#include <utility>
40#include "ceres/block_structure.h"
41#include "ceres/graph.h"
42#include "ceres/pair_hash.h"
43#include "glog/logging.h"
44
45namespace ceres {
46namespace internal {
47
48using std::make_pair;
49using std::max;
50using std::pair;
51using std::set;
52using std::vector;
53
54void ComputeVisibility(const CompressedRowBlockStructure& block_structure,
55 const int num_eliminate_blocks,
56 vector<set<int>>* visibility) {
57 CHECK(visibility != nullptr);
58
59 // Clear the visibility vector and resize it to hold a
60 // vector for each camera.
61 visibility->resize(0);
62 visibility->resize(block_structure.cols.size() - num_eliminate_blocks);
63
64 for (int i = 0; i < block_structure.rows.size(); ++i) {
65 const vector<Cell>& cells = block_structure.rows[i].cells;
66 int block_id = cells[0].block_id;
67 // If the first block is not an e_block, then skip this row block.
68 if (block_id >= num_eliminate_blocks) {
69 continue;
70 }
71
72 for (int j = 1; j < cells.size(); ++j) {
73 int camera_block_id = cells[j].block_id - num_eliminate_blocks;
74 DCHECK_GE(camera_block_id, 0);
75 DCHECK_LT(camera_block_id, visibility->size());
76 (*visibility)[camera_block_id].insert(block_id);
77 }
78 }
79}
80
81WeightedGraph<int>* CreateSchurComplementGraph(
82 const vector<set<int>>& visibility) {
83 const time_t start_time = time(NULL);
84 // Compute the number of e_blocks/point blocks. Since the visibility
85 // set for each e_block/camera contains the set of e_blocks/points
86 // visible to it, we find the maximum across all visibility sets.
87 int num_points = 0;
88 for (int i = 0; i < visibility.size(); i++) {
89 if (visibility[i].size() > 0) {
90 num_points = max(num_points, (*visibility[i].rbegin()) + 1);
91 }
92 }
93
94 // Invert the visibility. The input is a camera->point mapping,
95 // which tells us which points are visible in which
96 // cameras. However, to compute the sparsity structure of the Schur
97 // Complement efficiently, its better to have the point->camera
98 // mapping.
99 vector<set<int>> inverse_visibility(num_points);
100 for (int i = 0; i < visibility.size(); i++) {
101 const set<int>& visibility_set = visibility[i];
102 for (const int v : visibility_set) {
103 inverse_visibility[v].insert(i);
104 }
105 }
106
107 // Map from camera pairs to number of points visible to both cameras
108 // in the pair.
109 std::unordered_map<pair<int, int>, int, pair_hash> camera_pairs;
110
111 // Count the number of points visible to each camera/f_block pair.
112 for (const auto& inverse_visibility_set : inverse_visibility) {
113 for (set<int>::const_iterator camera1 = inverse_visibility_set.begin();
114 camera1 != inverse_visibility_set.end();
115 ++camera1) {
116 set<int>::const_iterator camera2 = camera1;
117 for (++camera2; camera2 != inverse_visibility_set.end(); ++camera2) {
118 ++(camera_pairs[make_pair(*camera1, *camera2)]);
119 }
120 }
121 }
122
123 WeightedGraph<int>* graph = new WeightedGraph<int>;
124
125 // Add vertices and initialize the pairs for self edges so that self
126 // edges are guaranteed. This is needed for the Canonical views
127 // algorithm to work correctly.
128 static const double kSelfEdgeWeight = 1.0;
129 for (int i = 0; i < visibility.size(); ++i) {
130 graph->AddVertex(i);
131 graph->AddEdge(i, i, kSelfEdgeWeight);
132 }
133
134 // Add an edge for each camera pair.
135 for (const auto& camera_pair_count : camera_pairs) {
136 const int camera1 = camera_pair_count.first.first;
137 const int camera2 = camera_pair_count.first.second;
138 const int count = camera_pair_count.second;
139 DCHECK_NE(camera1, camera2);
140 // Static cast necessary for Windows.
141 const double weight = static_cast<double>(count) /
142 (sqrt(static_cast<double>(
143 visibility[camera1].size() * visibility[camera2].size())));
144 graph->AddEdge(camera1, camera2, weight);
145 }
146
147 VLOG(2) << "Schur complement graph time: " << (time(NULL) - start_time);
148 return graph;
149}
150
151} // namespace internal
152} // namespace ceres