visibility.cc 5.6 KB

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