blob: 4f8dfb98f70afa29c1889d31caaed59b0c5a59fb [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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: sameeragarwal@google.com (Sameer Agarwal)
30
31#ifndef CERES_INTERNAL_GRAPH_H_
32#define CERES_INTERNAL_GRAPH_H_
33
34#include <limits>
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include <unordered_map>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036#include <unordered_set>
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include <utility>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "ceres/map_util.h"
41#include "ceres/pair_hash.h"
42#include "ceres/types.h"
43#include "glog/logging.h"
44
Austin Schuh3de38b02024-06-25 18:25:10 -070045namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080046
47// A unweighted undirected graph templated over the vertex ids. Vertex
48// should be hashable.
49template <typename Vertex>
Austin Schuh3de38b02024-06-25 18:25:10 -070050class CERES_NO_EXPORT Graph {
Austin Schuh70cc9552019-01-21 19:46:48 -080051 public:
Austin Schuh70cc9552019-01-21 19:46:48 -080052 // Add a vertex.
53 void AddVertex(const Vertex& vertex) {
54 if (vertices_.insert(vertex).second) {
55 edges_[vertex] = std::unordered_set<Vertex>();
56 }
57 }
58
59 bool RemoveVertex(const Vertex& vertex) {
60 if (vertices_.find(vertex) == vertices_.end()) {
61 return false;
62 }
63
64 vertices_.erase(vertex);
65 const std::unordered_set<Vertex>& sinks = edges_[vertex];
66 for (const Vertex& s : sinks) {
67 edges_[s].erase(vertex);
68 }
69
70 edges_.erase(vertex);
71 return true;
72 }
73
74 // Add an edge between the vertex1 and vertex2. Calling AddEdge on a
75 // pair of vertices which do not exist in the graph yet will result
76 // in undefined behavior.
77 //
78 // It is legal to call this method repeatedly for the same set of
79 // vertices.
80 void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
81 DCHECK(vertices_.find(vertex1) != vertices_.end());
82 DCHECK(vertices_.find(vertex2) != vertices_.end());
83
84 if (edges_[vertex1].insert(vertex2).second) {
85 edges_[vertex2].insert(vertex1);
86 }
87 }
88
89 // Calling Neighbors on a vertex not in the graph will result in
90 // undefined behaviour.
91 const std::unordered_set<Vertex>& Neighbors(const Vertex& vertex) const {
92 return FindOrDie(edges_, vertex);
93 }
94
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080095 const std::unordered_set<Vertex>& vertices() const { return vertices_; }
Austin Schuh70cc9552019-01-21 19:46:48 -080096
97 private:
98 std::unordered_set<Vertex> vertices_;
99 std::unordered_map<Vertex, std::unordered_set<Vertex>> edges_;
100};
101
102// A weighted undirected graph templated over the vertex ids. Vertex
103// should be hashable and comparable.
104template <typename Vertex>
105class WeightedGraph {
106 public:
Austin Schuh70cc9552019-01-21 19:46:48 -0800107 // Add a weighted vertex. If the vertex already exists in the graph,
108 // its weight is set to the new weight.
109 void AddVertex(const Vertex& vertex, double weight) {
110 if (vertices_.find(vertex) == vertices_.end()) {
111 vertices_.insert(vertex);
112 edges_[vertex] = std::unordered_set<Vertex>();
113 }
114 vertex_weights_[vertex] = weight;
115 }
116
117 // Uses weight = 1.0. If vertex already exists, its weight is set to
118 // 1.0.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800119 void AddVertex(const Vertex& vertex) { AddVertex(vertex, 1.0); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800120
121 bool RemoveVertex(const Vertex& vertex) {
122 if (vertices_.find(vertex) == vertices_.end()) {
123 return false;
124 }
125
126 vertices_.erase(vertex);
127 vertex_weights_.erase(vertex);
128 const std::unordered_set<Vertex>& sinks = edges_[vertex];
129 for (const Vertex& s : sinks) {
130 if (vertex < s) {
131 edge_weights_.erase(std::make_pair(vertex, s));
132 } else {
133 edge_weights_.erase(std::make_pair(s, vertex));
134 }
135 edges_[s].erase(vertex);
136 }
137
138 edges_.erase(vertex);
139 return true;
140 }
141
142 // Add a weighted edge between the vertex1 and vertex2. Calling
143 // AddEdge on a pair of vertices which do not exist in the graph yet
144 // will result in undefined behavior.
145 //
146 // It is legal to call this method repeatedly for the same set of
147 // vertices.
148 void AddEdge(const Vertex& vertex1, const Vertex& vertex2, double weight) {
149 DCHECK(vertices_.find(vertex1) != vertices_.end());
150 DCHECK(vertices_.find(vertex2) != vertices_.end());
151
152 if (edges_[vertex1].insert(vertex2).second) {
153 edges_[vertex2].insert(vertex1);
154 }
155
156 if (vertex1 < vertex2) {
157 edge_weights_[std::make_pair(vertex1, vertex2)] = weight;
158 } else {
159 edge_weights_[std::make_pair(vertex2, vertex1)] = weight;
160 }
161 }
162
163 // Uses weight = 1.0.
164 void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
165 AddEdge(vertex1, vertex2, 1.0);
166 }
167
168 // Calling VertexWeight on a vertex not in the graph will result in
169 // undefined behavior.
170 double VertexWeight(const Vertex& vertex) const {
171 return FindOrDie(vertex_weights_, vertex);
172 }
173
174 // Calling EdgeWeight on a pair of vertices where either one of the
175 // vertices is not present in the graph will result in undefined
176 // behaviour. If there is no edge connecting vertex1 and vertex2,
177 // the edge weight is zero.
178 double EdgeWeight(const Vertex& vertex1, const Vertex& vertex2) const {
179 if (vertex1 < vertex2) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800180 return FindWithDefault(
181 edge_weights_, std::make_pair(vertex1, vertex2), 0.0);
Austin Schuh70cc9552019-01-21 19:46:48 -0800182 } else {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800183 return FindWithDefault(
184 edge_weights_, std::make_pair(vertex2, vertex1), 0.0);
Austin Schuh70cc9552019-01-21 19:46:48 -0800185 }
186 }
187
188 // Calling Neighbors on a vertex not in the graph will result in
189 // undefined behaviour.
190 const std::unordered_set<Vertex>& Neighbors(const Vertex& vertex) const {
191 return FindOrDie(edges_, vertex);
192 }
193
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800194 const std::unordered_set<Vertex>& vertices() const { return vertices_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800195
196 static double InvalidWeight() {
197 return std::numeric_limits<double>::quiet_NaN();
198 }
199
200 private:
201 std::unordered_set<Vertex> vertices_;
202 std::unordered_map<Vertex, double> vertex_weights_;
203 std::unordered_map<Vertex, std::unordered_set<Vertex>> edges_;
204 std::unordered_map<std::pair<Vertex, Vertex>, double, pair_hash>
205 edge_weights_;
206};
207
Austin Schuh3de38b02024-06-25 18:25:10 -0700208} // namespace ceres::internal
Austin Schuh70cc9552019-01-21 19:46:48 -0800209
210#endif // CERES_INTERNAL_GRAPH_H_