Graph2D.cpp
Go to the documentation of this file.
1 //=============================================================================
2 // Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 // Author: Gonzalo Mier
4 // BSD-3 License
5 //=============================================================================
6 
7 #include <numeric>
9 
10 namespace f2c::types {
11 
13  const Point& from, const Point& to, int64_t cost) {
14  size_t p_from = this->nodes_to_index_.insert(
15  std::make_pair(from, nodes_to_index_.size())).first->second;
16  size_t p_to = this->nodes_to_index_.insert(
17  std::make_pair(to, nodes_to_index_.size())).first->second;
18  this->index_to_nodes_.insert({{p_from, from}, {p_to, to}});
19  this->addDirectedEdge(p_from, p_to, cost);
20  return *this;
21 }
22 
23 Graph2D& Graph2D::addEdge(const Point& i, const Point& j, int64_t cost) {
24  this->addDirectedEdge(i, j, cost);
25  return this->addDirectedEdge(j, i, cost);
26 }
27 
28 Graph2D& Graph2D::addDirectedEdge(const Point& from, const Point& to) {
29  return addDirectedEdge(from, to, int64_t(scale_ * from.distance(to)));
30 }
31 
32 Graph2D& Graph2D::addEdge(const Point& i, const Point& j) {
33  return addEdge(i, j, int64_t(scale_ * i.distance(j)));
34 }
35 
37  const Point& from, const Point& to, Graph2D& short_path_g) {
38  return addDirectedEdge(from, to, short_path_g.shortestPathCost(from, to));
39 }
40 
42  const Point& i, const Point& j, Graph2D& short_path_g) {
43  addDirectedEdge(i, j, short_path_g.shortestPathCost(i, j));
44  return addDirectedEdge(j, i, short_path_g.shortestPathCost(j, i));
45 }
46 
47 
48 Graph2D& Graph2D::removeDirectedEdge(const Point& from, const Point& to) {
49  this->removeDirectedEdge(nodeToIndex(from), nodeToIndex(to));
50  return *this;
51 }
52 
53 Graph2D& Graph2D::removeEdge(const Point& i, const Point& j) {
54  return this->removeDirectedEdge(i, j).removeDirectedEdge(j, i);
55 }
56 
57 size_t Graph2D::numNodes() const {
58  return this->nodes_to_index_.size();
59 }
60 
61 std::vector<Point> Graph2D::getNodes() const {
62  std::vector<Point> nodes;
63  for (auto&& n : nodes_to_index_) {
64  nodes.emplace_back(n.first);
65  }
66  return nodes;
67 }
68 
69 size_t Graph2D::nodeToIndex(const Point& p) const {
70  return this->nodes_to_index_.at(p);
71 }
72 
73 Point Graph2D::indexToNode(size_t id) const {
74  return this->index_to_nodes_.at(id);
75 }
76 
77 std::vector<std::vector<Point>> Graph2D::allPathsBetween(
78  const Point& from, const Point& to) const {
79  auto int_routes = this->allPathsBetween(nodeToIndex(from), nodeToIndex(to));
80 
81  std::vector<std::vector<Point>> routes;
82  for (auto&& i_route : int_routes) {
83  routes.emplace_back();
84  for (auto&& i : i_route) {
85  routes.back().emplace_back(indexToNode(i));
86  }
87  }
88  return routes;
89 }
90 
91 std::vector<Point> Graph2D::shortestPath(
92  const Point& from, const Point& to, int64_t INF) {
93  auto i_path = this->shortestPath(nodeToIndex(from), nodeToIndex(to), INF);
94  std::vector<Point> path;
95  for (auto&& i : i_path) {
96  path.emplace_back(indexToNode(i));
97  }
98  return path;
99 }
100 
102  const Point& from, const Point& to, int64_t INF) {
103  return this->shortestPathCost(nodeToIndex(from), nodeToIndex(to), INF);
104 }
105 
106 
107 } // namespace f2c::types
108 
109 
f2c::types::Graph2D
Definition: Graph2D.h:18
f2c::types::Graph2D::scale_
double scale_
Definition: Graph2D.h:60
f2c::types::Graph2D::addEdge
Graph2D & addEdge(const Point &i, const Point &j, int64_t cost)
Definition: Graph2D.cpp:23
f2c::types
Types used by fields2cover library.
Definition: Cell.h:20
Graph2D.h
2_objective_functions.path
path
Definition: 2_objective_functions.py:88
f2c::types::Graph2D::shortestPathCost
int64_t shortestPathCost(const Point &from, const Point &to, int64_t INF=1<< 30)
Definition: Graph2D.cpp:101
f2c::types::Graph2D::numNodes
size_t numNodes() const
Definition: Graph2D.cpp:57
f2c::types::Graph2D::indexToNode
Point indexToNode(size_t id) const
Definition: Graph2D.cpp:73
f2c::types::Graph2D::removeDirectedEdge
Graph2D & removeDirectedEdge(const Point &from, const Point &to)
Definition: Graph2D.cpp:48
f2c::types::Graph2D::addDirectedEdge
Graph2D & addDirectedEdge(const Point &from, const Point &to, int64_t cost)
Definition: Graph2D.cpp:12
f2c::types::Graph2D::getNodes
std::vector< Point > getNodes() const
Definition: Graph2D.cpp:61
f2c::types::Graph2D::nodeToIndex
size_t nodeToIndex(const Point &p) const
Definition: Graph2D.cpp:69
f2c::types::Graph2D::index_to_nodes_
std::unordered_map< size_t, Point > index_to_nodes_
Definition: Graph2D.h:57
f2c::types::Graph2D::shortestPath
std::vector< Point > shortestPath(const Point &from, const Point &to, int64_t INF=1<< 30)
Definition: Graph2D.cpp:91
f2c::types::Point
Definition: Point.h:21
f2c::types::Graph2D::removeEdge
Graph2D & removeEdge(const Point &i, const Point &j)
Definition: Graph2D.cpp:53
f2c::types::Geometry::distance
double distance(const Geometry< T2, R2 > &p) const
Compute shortest distance between this and another geometry.
Definition: Geometry_impl.hpp:139
f2c::types::Graph2D::nodes_to_index_
std::unordered_map< Point, size_t > nodes_to_index_
Definition: Graph2D.h:56
f2c::types::Graph2D::allPathsBetween
std::vector< std::vector< Point > > allPathsBetween(const Point &from, const Point &to) const
Definition: Graph2D.cpp:77


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31