|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_INTERNAL_GRAPH_H_
|
|
Packit |
ea1746 |
#define CERES_INTERNAL_GRAPH_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <limits>
|
|
Packit |
ea1746 |
#include <utility>
|
|
Packit |
ea1746 |
#include "ceres/integral_types.h"
|
|
Packit |
ea1746 |
#include "ceres/map_util.h"
|
|
Packit |
ea1746 |
#include "ceres/collections_port.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/macros.h"
|
|
Packit |
ea1746 |
#include "ceres/types.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// A unweighted undirected graph templated over the vertex ids. Vertex
|
|
Packit |
ea1746 |
// should be hashable.
|
|
Packit |
ea1746 |
template <typename Vertex>
|
|
Packit |
ea1746 |
class Graph {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
Graph() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Add a vertex.
|
|
Packit |
ea1746 |
void AddVertex(const Vertex& vertex) {
|
|
Packit |
ea1746 |
if (vertices_.insert(vertex).second) {
|
|
Packit |
ea1746 |
edges_[vertex] = HashSet<Vertex>();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
bool RemoveVertex(const Vertex& vertex) {
|
|
Packit |
ea1746 |
if (vertices_.find(vertex) == vertices_.end()) {
|
|
Packit |
ea1746 |
return false;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
vertices_.erase(vertex);
|
|
Packit |
ea1746 |
const HashSet<Vertex>& sinks = edges_[vertex];
|
|
Packit |
ea1746 |
for (typename HashSet<Vertex>::const_iterator it = sinks.begin();
|
|
Packit |
ea1746 |
it != sinks.end(); ++it) {
|
|
Packit |
ea1746 |
edges_[*it].erase(vertex);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
edges_.erase(vertex);
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Add an edge between the vertex1 and vertex2. Calling AddEdge on a
|
|
Packit |
ea1746 |
// pair of vertices which do not exist in the graph yet will result
|
|
Packit |
ea1746 |
// in undefined behavior.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// It is legal to call this method repeatedly for the same set of
|
|
Packit |
ea1746 |
// vertices.
|
|
Packit |
ea1746 |
void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
|
|
Packit |
ea1746 |
DCHECK(vertices_.find(vertex1) != vertices_.end());
|
|
Packit |
ea1746 |
DCHECK(vertices_.find(vertex2) != vertices_.end());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (edges_[vertex1].insert(vertex2).second) {
|
|
Packit |
ea1746 |
edges_[vertex2].insert(vertex1);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Calling Neighbors on a vertex not in the graph will result in
|
|
Packit |
ea1746 |
// undefined behaviour.
|
|
Packit |
ea1746 |
const HashSet<Vertex>& Neighbors(const Vertex& vertex) const {
|
|
Packit |
ea1746 |
return FindOrDie(edges_, vertex);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const HashSet<Vertex>& vertices() const {
|
|
Packit |
ea1746 |
return vertices_;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
HashSet<Vertex> vertices_;
|
|
Packit |
ea1746 |
HashMap<Vertex, HashSet<Vertex> > edges_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CERES_DISALLOW_COPY_AND_ASSIGN(Graph);
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// A weighted undirected graph templated over the vertex ids. Vertex
|
|
Packit |
ea1746 |
// should be hashable and comparable.
|
|
Packit |
ea1746 |
template <typename Vertex>
|
|
Packit |
ea1746 |
class WeightedGraph {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
WeightedGraph() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Add a weighted vertex. If the vertex already exists in the graph,
|
|
Packit |
ea1746 |
// its weight is set to the new weight.
|
|
Packit |
ea1746 |
void AddVertex(const Vertex& vertex, double weight) {
|
|
Packit |
ea1746 |
if (vertices_.find(vertex) == vertices_.end()) {
|
|
Packit |
ea1746 |
vertices_.insert(vertex);
|
|
Packit |
ea1746 |
edges_[vertex] = HashSet<Vertex>();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
vertex_weights_[vertex] = weight;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Uses weight = 1.0. If vertex already exists, its weight is set to
|
|
Packit |
ea1746 |
// 1.0.
|
|
Packit |
ea1746 |
void AddVertex(const Vertex& vertex) {
|
|
Packit |
ea1746 |
AddVertex(vertex, 1.0);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
bool RemoveVertex(const Vertex& vertex) {
|
|
Packit |
ea1746 |
if (vertices_.find(vertex) == vertices_.end()) {
|
|
Packit |
ea1746 |
return false;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
vertices_.erase(vertex);
|
|
Packit |
ea1746 |
vertex_weights_.erase(vertex);
|
|
Packit |
ea1746 |
const HashSet<Vertex>& sinks = edges_[vertex];
|
|
Packit |
ea1746 |
for (typename HashSet<Vertex>::const_iterator it = sinks.begin();
|
|
Packit |
ea1746 |
it != sinks.end(); ++it) {
|
|
Packit |
ea1746 |
if (vertex < *it) {
|
|
Packit |
ea1746 |
edge_weights_.erase(std::make_pair(vertex, *it));
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
edge_weights_.erase(std::make_pair(*it, vertex));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
edges_[*it].erase(vertex);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
edges_.erase(vertex);
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Add a weighted edge between the vertex1 and vertex2. Calling
|
|
Packit |
ea1746 |
// AddEdge on a pair of vertices which do not exist in the graph yet
|
|
Packit |
ea1746 |
// will result in undefined behavior.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// It is legal to call this method repeatedly for the same set of
|
|
Packit |
ea1746 |
// vertices.
|
|
Packit |
ea1746 |
void AddEdge(const Vertex& vertex1, const Vertex& vertex2, double weight) {
|
|
Packit |
ea1746 |
DCHECK(vertices_.find(vertex1) != vertices_.end());
|
|
Packit |
ea1746 |
DCHECK(vertices_.find(vertex2) != vertices_.end());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (edges_[vertex1].insert(vertex2).second) {
|
|
Packit |
ea1746 |
edges_[vertex2].insert(vertex1);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (vertex1 < vertex2) {
|
|
Packit |
ea1746 |
edge_weights_[std::make_pair(vertex1, vertex2)] = weight;
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
edge_weights_[std::make_pair(vertex2, vertex1)] = weight;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Uses weight = 1.0.
|
|
Packit |
ea1746 |
void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
|
|
Packit |
ea1746 |
AddEdge(vertex1, vertex2, 1.0);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Calling VertexWeight on a vertex not in the graph will result in
|
|
Packit |
ea1746 |
// undefined behavior.
|
|
Packit |
ea1746 |
double VertexWeight(const Vertex& vertex) const {
|
|
Packit |
ea1746 |
return FindOrDie(vertex_weights_, vertex);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Calling EdgeWeight on a pair of vertices where either one of the
|
|
Packit |
ea1746 |
// vertices is not present in the graph will result in undefined
|
|
Packit |
ea1746 |
// behaviour. If there is no edge connecting vertex1 and vertex2,
|
|
Packit |
ea1746 |
// the edge weight is zero.
|
|
Packit |
ea1746 |
double EdgeWeight(const Vertex& vertex1, const Vertex& vertex2) const {
|
|
Packit |
ea1746 |
if (vertex1 < vertex2) {
|
|
Packit |
ea1746 |
return FindWithDefault(edge_weights_,
|
|
Packit |
ea1746 |
std::make_pair(vertex1, vertex2), 0.0);
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
return FindWithDefault(edge_weights_,
|
|
Packit |
ea1746 |
std::make_pair(vertex2, vertex1), 0.0);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Calling Neighbors on a vertex not in the graph will result in
|
|
Packit |
ea1746 |
// undefined behaviour.
|
|
Packit |
ea1746 |
const HashSet<Vertex>& Neighbors(const Vertex& vertex) const {
|
|
Packit |
ea1746 |
return FindOrDie(edges_, vertex);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const HashSet<Vertex>& vertices() const {
|
|
Packit |
ea1746 |
return vertices_;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
static double InvalidWeight() {
|
|
Packit |
ea1746 |
return std::numeric_limits<double>::quiet_NaN();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
HashSet<Vertex> vertices_;
|
|
Packit |
ea1746 |
HashMap<Vertex, double> vertex_weights_;
|
|
Packit |
ea1746 |
HashMap<Vertex, HashSet<Vertex> > edges_;
|
|
Packit |
ea1746 |
HashMap<std::pair<Vertex, Vertex>, double> edge_weights_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CERES_DISALLOW_COPY_AND_ASSIGN(WeightedGraph);
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_INTERNAL_GRAPH_H_
|