blob: 371bdfacd52cda67caca282631ce5fe2b582dcd5 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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
Sergey Sharybinb53c9662013-02-25 01:14:26 +060031#include "ceres/visibility.h"
32
Keir Mierle8ebb0732012-04-30 23:09:08 -070033#include <cmath>
34#include <ctime>
35#include <algorithm>
36#include <set>
37#include <vector>
38#include <utility>
Keir Mierle8ebb0732012-04-30 23:09:08 -070039#include "ceres/block_structure.h"
40#include "ceres/collections_port.h"
41#include "ceres/graph.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070042#include "glog/logging.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070043
44namespace ceres {
45namespace internal {
46
47void ComputeVisibility(const CompressedRowBlockStructure& block_structure,
48 const int num_eliminate_blocks,
49 vector< set<int> >* visibility) {
50 CHECK_NOTNULL(visibility);
51
52 // Clear the visibility vector and resize it to hold a
53 // vector for each camera.
54 visibility->resize(0);
55 visibility->resize(block_structure.cols.size() - num_eliminate_blocks);
56
57 for (int i = 0; i < block_structure.rows.size(); ++i) {
58 const vector<Cell>& cells = block_structure.rows[i].cells;
59 int block_id = cells[0].block_id;
60 // If the first block is not an e_block, then skip this row block.
61 if (block_id >= num_eliminate_blocks) {
62 continue;
63 }
64
65 for (int j = 1; j < cells.size(); ++j) {
66 int camera_block_id = cells[j].block_id - num_eliminate_blocks;
67 DCHECK_GE(camera_block_id, 0);
68 DCHECK_LT(camera_block_id, visibility->size());
69 (*visibility)[camera_block_id].insert(block_id);
70 }
71 }
72}
73
74Graph<int>* CreateSchurComplementGraph(const vector<set<int> >& visibility) {
75 const time_t start_time = time(NULL);
76 // Compute the number of e_blocks/point blocks. Since the visibility
77 // set for each e_block/camera contains the set of e_blocks/points
78 // visible to it, we find the maximum across all visibility sets.
79 int num_points = 0;
80 for (int i = 0; i < visibility.size(); i++) {
81 if (visibility[i].size() > 0) {
82 num_points = max(num_points, (*visibility[i].rbegin()) + 1);
83 }
84 }
85
86 // Invert the visibility. The input is a camera->point mapping,
87 // which tells us which points are visible in which
88 // cameras. However, to compute the sparsity structure of the Schur
89 // Complement efficiently, its better to have the point->camera
90 // mapping.
91 vector<set<int> > inverse_visibility(num_points);
92 for (int i = 0; i < visibility.size(); i++) {
93 const set<int>& visibility_set = visibility[i];
94 for (set<int>::const_iterator it = visibility_set.begin();
95 it != visibility_set.end();
96 ++it) {
97 inverse_visibility[*it].insert(i);
98 }
99 }
100
101 // Map from camera pairs to number of points visible to both cameras
102 // in the pair.
103 HashMap<pair<int, int>, int > camera_pairs;
104
105 // Count the number of points visible to each camera/f_block pair.
106 for (vector<set<int> >::const_iterator it = inverse_visibility.begin();
107 it != inverse_visibility.end();
108 ++it) {
109 const set<int>& inverse_visibility_set = *it;
110 for (set<int>::const_iterator camera1 = inverse_visibility_set.begin();
111 camera1 != inverse_visibility_set.end();
112 ++camera1) {
113 set<int>::const_iterator camera2 = camera1;
114 for (++camera2; camera2 != inverse_visibility_set.end(); ++camera2) {
115 ++(camera_pairs[make_pair(*camera1, *camera2)]);
116 }
117 }
118 }
119
120 Graph<int>* graph = new Graph<int>();
121
122 // Add vertices and initialize the pairs for self edges so that self
123 // edges are guaranteed. This is needed for the Canonical views
124 // algorithm to work correctly.
125 static const double kSelfEdgeWeight = 1.0;
126 for (int i = 0; i < visibility.size(); ++i) {
127 graph->AddVertex(i);
128 graph->AddEdge(i, i, kSelfEdgeWeight);
129 }
130
131 // Add an edge for each camera pair.
132 for (HashMap<pair<int, int>, int>::const_iterator it = camera_pairs.begin();
133 it != camera_pairs.end();
134 ++it) {
135 const int camera1 = it->first.first;
136 const int camera2 = it->first.second;
137 CHECK_NE(camera1, camera2);
138
139 const int count = it->second;
Keir Mierleefe7ac62012-06-24 22:25:28 -0700140 // Static cast necessary for Windows.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700141 const double weight = static_cast<double>(count) /
Sameer Agarwal931c3092013-02-25 09:46:21 -0800142 (sqrt(static_cast<double>(
143 visibility[camera1].size() * visibility[camera2].size())));
Keir Mierle8ebb0732012-04-30 23:09:08 -0700144 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