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