DHART
Loading...
Searching...
No Matches
boost_graph.cpp
Go to the documentation of this file.
1
7
8#include <boost_graph.h>
9
10#include <graph.h>
11#include <node.h>
12#include <Edge.h>
13#include <assert.h>
14
17using std::vector;
18using std::string;
19
20namespace HF::Pathfinding {
21
22
23 BoostGraph::BoostGraph(const Graph& graph, const string & cost_type)
24 {
25
26 // Allocate a vector of integers with an element for every node in
27 // nodes.
28 int num_nodes = graph.size();
29 vector<int> nodes(num_nodes);
30
31 std::vector<pair> edges;
32 std::vector<Edge_Cost> weights;
33
34 // Get a set of all edges from the graph with the passed cost type.
35 // This will return the set of costs that the graph was constructed
36 // with in the case that no cost_type was specified.
37 auto edge_sets = graph.GetEdges(cost_type);
38
39 // Iterate through every set of edges to convert them
40 // to a format usable with Boost
41 for (const auto& edgeset : edge_sets) {
42 const int parent_id = edgeset.parent;
43
44 // Iterate through all children of this parent
45 for (const auto& edge : edgeset.children) {
46
47 // Add the edge to the boost graph's edge array and the weight
48 // to the boost graph's weights array
49 int child_id = edge.child;
50 float cost = edge.weight;
51 weights.emplace_back(Edge_Cost{ edge.weight });
52 edges.emplace_back(pair{ parent_id,child_id });
53 }
54 }
55
56 // Calculate the maximum id held by the graph.
57 unsigned int max_node = graph.MaxID() + 1;
58
59 // Create the boost graph from the two input arrays
60 g = graph_t(boost::edges_are_unsorted, edges.begin(), edges.end(), weights.begin(), max_node);
61
62 int n = num_vertices(g);
63
64 // Resize predecessor and distance arrays to maximum size.
65 p.resize(n);
66 d.resize(n);
67 }
68
69 BoostGraph::~BoostGraph() = default;
70}
Contains definitions for the BoostGraph class.
Contains definitions for the Edge structure.
Contains definitions for the Graph class.
Contains definitions for the Node structure.
Algorithms to find the shortest path between nodes in a HF::SpatialStructures::Graph.
Definition: boost_graph.cpp:20
Data stored for every edge in the BoostGraph.
Definition: boost_graph.h:99
boost::compressed_sparse_row_graph< boost::directedS, vertex_data, Edge_Cost > graph_t
Type of graph held by the BoostGraph.
Definition: boost_graph.h:135
std::pair< int, int > pair
Shorten std::pair to simplify graph construction.
Definition: boost_graph.h:142
float weight
Cost of traversing this edge.
Definition: boost_graph.h:100
std::vector< double > d
Distance array preallocated to the number of nodes in the graph.
Definition: boost_graph.h:170
graph_t g
The underlying graph in boost.
Definition: boost_graph.h:168
~BoostGraph()
Explicit Destructor required for BoostGraphDeleter to work in path_finder.h.
BoostGraph(const HF::SpatialStructures::Graph &graph, const std::string &cost_type="")
Create a boost graph from a HF::SpatialStructures::Graph.
Definition: boost_graph.cpp:23
std::vector< vertex_descriptor > p
Vertex array preallocated to the number of nodes in the graph.
Definition: boost_graph.h:169
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node.
Definition: graph.h:486
std::vector< EdgeSet > GetEdges() const
Get every in the given graph as IDs.
int size() const
Determine how many nodes are in the graph.
int MaxID() const
Calculate the maximum ID of any node in the graph.
A point in space with an ID.
Definition: node.h:38