2012-04-30 23:09:08 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2023-09-19 15:29:34 -07:00
|
|
|
// Copyright 2023 Google Inc. All rights reserved.
|
2015-03-17 22:30:16 -07:00
|
|
|
// http://ceres-solver.org/
|
2012-04-30 23:09:08 -07:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
|
|
|
// used to endorse or promote products derived from this software without
|
|
|
|
|
// specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// Author: kushalav@google.com (Avanish Kushal)
|
|
|
|
|
|
2013-02-25 01:14:26 +06:00
|
|
|
#include "ceres/visibility.h"
|
|
|
|
|
|
2020-09-20 21:45:24 +02:00
|
|
|
#include <algorithm>
|
2012-04-30 23:09:08 -07:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <ctime>
|
2022-02-12 08:11:33 -08:00
|
|
|
#include <memory>
|
2012-04-30 23:09:08 -07:00
|
|
|
#include <set>
|
2018-03-29 22:01:29 -07:00
|
|
|
#include <unordered_map>
|
2012-04-30 23:09:08 -07:00
|
|
|
#include <utility>
|
2020-09-20 21:45:24 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
2024-07-07 10:24:18 -07:00
|
|
|
#include "absl/log/check.h"
|
|
|
|
|
#include "absl/log/log.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/block_structure.h"
|
|
|
|
|
#include "ceres/graph.h"
|
2018-03-29 22:01:29 -07:00
|
|
|
#include "ceres/pair_hash.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2022-04-21 17:41:10 -07:00
|
|
|
namespace ceres::internal {
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
void ComputeVisibility(const CompressedRowBlockStructure& block_structure,
|
|
|
|
|
const int num_eliminate_blocks,
|
2023-01-11 16:51:38 +00:00
|
|
|
std::vector<std::set<int>>* visibility) {
|
2018-08-27 07:12:43 -07:00
|
|
|
CHECK(visibility != nullptr);
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Clear the visibility vector and resize it to hold a
|
|
|
|
|
// vector for each camera.
|
|
|
|
|
visibility->resize(0);
|
|
|
|
|
visibility->resize(block_structure.cols.size() - num_eliminate_blocks);
|
|
|
|
|
|
2022-02-20 02:22:17 +01:00
|
|
|
for (const auto& row : block_structure.rows) {
|
2023-01-11 16:51:38 +00:00
|
|
|
const std::vector<Cell>& cells = row.cells;
|
2012-04-30 23:09:08 -07:00
|
|
|
int block_id = cells[0].block_id;
|
|
|
|
|
// If the first block is not an e_block, then skip this row block.
|
|
|
|
|
if (block_id >= num_eliminate_blocks) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int j = 1; j < cells.size(); ++j) {
|
|
|
|
|
int camera_block_id = cells[j].block_id - num_eliminate_blocks;
|
|
|
|
|
DCHECK_GE(camera_block_id, 0);
|
|
|
|
|
DCHECK_LT(camera_block_id, visibility->size());
|
|
|
|
|
(*visibility)[camera_block_id].insert(block_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 16:16:37 -08:00
|
|
|
std::unique_ptr<WeightedGraph<int>> CreateSchurComplementGraph(
|
2023-01-11 16:51:38 +00:00
|
|
|
const std::vector<std::set<int>>& visibility) {
|
2022-02-02 13:17:29 -08:00
|
|
|
const time_t start_time = time(nullptr);
|
2012-04-30 23:09:08 -07:00
|
|
|
// Compute the number of e_blocks/point blocks. Since the visibility
|
|
|
|
|
// set for each e_block/camera contains the set of e_blocks/points
|
|
|
|
|
// visible to it, we find the maximum across all visibility sets.
|
|
|
|
|
int num_points = 0;
|
2022-02-20 02:22:17 +01:00
|
|
|
for (const auto& visible : visibility) {
|
2022-02-27 05:26:07 -08:00
|
|
|
if (!visible.empty()) {
|
2023-01-11 16:51:38 +00:00
|
|
|
num_points = std::max(num_points, (*visible.rbegin()) + 1);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invert the visibility. The input is a camera->point mapping,
|
|
|
|
|
// which tells us which points are visible in which
|
|
|
|
|
// cameras. However, to compute the sparsity structure of the Schur
|
|
|
|
|
// Complement efficiently, its better to have the point->camera
|
|
|
|
|
// mapping.
|
2023-01-11 16:51:38 +00:00
|
|
|
std::vector<std::set<int>> inverse_visibility(num_points);
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int i = 0; i < visibility.size(); i++) {
|
2023-01-11 16:51:38 +00:00
|
|
|
const std::set<int>& visibility_set = visibility[i];
|
2022-02-20 02:22:17 +01:00
|
|
|
for (int v : visibility_set) {
|
2018-03-29 22:01:29 -07:00
|
|
|
inverse_visibility[v].insert(i);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map from camera pairs to number of points visible to both cameras
|
|
|
|
|
// in the pair.
|
2023-01-11 16:51:38 +00:00
|
|
|
std::unordered_map<std::pair<int, int>, int, pair_hash> camera_pairs;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Count the number of points visible to each camera/f_block pair.
|
2018-03-29 22:01:29 -07:00
|
|
|
for (const auto& inverse_visibility_set : inverse_visibility) {
|
2022-02-20 02:22:17 +01:00
|
|
|
for (auto camera1 = inverse_visibility_set.begin();
|
2012-04-30 23:09:08 -07:00
|
|
|
camera1 != inverse_visibility_set.end();
|
|
|
|
|
++camera1) {
|
2022-02-20 02:22:17 +01:00
|
|
|
auto camera2 = camera1;
|
2012-04-30 23:09:08 -07:00
|
|
|
for (++camera2; camera2 != inverse_visibility_set.end(); ++camera2) {
|
2023-01-11 16:51:38 +00:00
|
|
|
++(camera_pairs[std::make_pair(*camera1, *camera2)]);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 16:16:37 -08:00
|
|
|
auto graph = std::make_unique<WeightedGraph<int>>();
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Add vertices and initialize the pairs for self edges so that self
|
|
|
|
|
// edges are guaranteed. This is needed for the Canonical views
|
|
|
|
|
// algorithm to work correctly.
|
2020-04-21 10:10:01 -07:00
|
|
|
static constexpr double kSelfEdgeWeight = 1.0;
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int i = 0; i < visibility.size(); ++i) {
|
|
|
|
|
graph->AddVertex(i);
|
|
|
|
|
graph->AddEdge(i, i, kSelfEdgeWeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add an edge for each camera pair.
|
2018-03-29 22:01:29 -07:00
|
|
|
for (const auto& camera_pair_count : camera_pairs) {
|
|
|
|
|
const int camera1 = camera_pair_count.first.first;
|
|
|
|
|
const int camera2 = camera_pair_count.first.second;
|
|
|
|
|
const int count = camera_pair_count.second;
|
|
|
|
|
DCHECK_NE(camera1, camera2);
|
2012-06-24 22:25:28 -07:00
|
|
|
// Static cast necessary for Windows.
|
2020-09-20 21:45:24 +02:00
|
|
|
const double weight =
|
|
|
|
|
static_cast<double>(count) /
|
|
|
|
|
(sqrt(static_cast<double>(visibility[camera1].size() *
|
|
|
|
|
visibility[camera2].size())));
|
2012-04-30 23:09:08 -07:00
|
|
|
graph->AddEdge(camera1, camera2, weight);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:17:29 -08:00
|
|
|
VLOG(2) << "Schur complement graph time: " << (time(nullptr) - start_time);
|
2012-04-30 23:09:08 -07:00
|
|
|
return graph;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 17:41:10 -07:00
|
|
|
} // namespace ceres::internal
|