Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 36 | #include <memory> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 37 | #include <set> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include <unordered_map> |
| 39 | #include <utility> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | #include "ceres/block_structure.h" |
| 43 | #include "ceres/graph.h" |
| 44 | #include "ceres/pair_hash.h" |
| 45 | #include "glog/logging.h" |
| 46 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 47 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 48 | |
| 49 | void ComputeVisibility(const CompressedRowBlockStructure& block_structure, |
| 50 | const int num_eliminate_blocks, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 51 | std::vector<std::set<int>>* visibility) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 52 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 59 | for (const auto& row : block_structure.rows) { |
| 60 | const std::vector<Cell>& cells = row.cells; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 61 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 76 | std::unique_ptr<WeightedGraph<int>> CreateSchurComplementGraph( |
| 77 | const std::vector<std::set<int>>& visibility) { |
| 78 | const time_t start_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 79 | // 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 83 | for (const auto& visible : visibility) { |
| 84 | if (!visible.empty()) { |
| 85 | num_points = std::max(num_points, (*visible.rbegin()) + 1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 86 | } |
| 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 94 | std::vector<std::set<int>> inverse_visibility(num_points); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 95 | for (int i = 0; i < visibility.size(); i++) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 96 | const std::set<int>& visibility_set = visibility[i]; |
| 97 | for (int v : visibility_set) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 98 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 104 | std::unordered_map<std::pair<int, int>, int, pair_hash> camera_pairs; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 105 | |
| 106 | // Count the number of points visible to each camera/f_block pair. |
| 107 | for (const auto& inverse_visibility_set : inverse_visibility) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 108 | for (auto camera1 = inverse_visibility_set.begin(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 109 | camera1 != inverse_visibility_set.end(); |
| 110 | ++camera1) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 111 | auto camera2 = camera1; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 112 | for (++camera2; camera2 != inverse_visibility_set.end(); ++camera2) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 113 | ++(camera_pairs[std::make_pair(*camera1, *camera2)]); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 118 | auto graph = std::make_unique<WeightedGraph<int>>(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 119 | |
| 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 Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 123 | static constexpr double kSelfEdgeWeight = 1.0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 124 | 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 Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 136 | const double weight = |
| 137 | static_cast<double>(count) / |
| 138 | (sqrt(static_cast<double>(visibility[camera1].size() * |
| 139 | visibility[camera2].size()))); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 140 | graph->AddEdge(camera1, camera2, weight); |
| 141 | } |
| 142 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 143 | VLOG(2) << "Schur complement graph time: " << (time(nullptr) - start_time); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | return graph; |
| 145 | } |
| 146 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 147 | } // namespace ceres::internal |