DHART
|
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node. More...
#include <graph.h>
Public Member Functions | |
Graph (const std::vector< std::vector< int > > &edges, const std::vector< std::vector< float > > &distances, const std::vector< Node > &Nodes, const std::string &default_cost="Distance") | |
Construct a graph from a list of nodes, edges, and distances. More... | |
Graph (const std::string &default_cost_name="Distance") | |
Construct an empty graph. More... | |
bool | HasEdge (const std::array< float, 3 > &parent, const std::array< float, 3 > &child, bool undirected=false) const |
Determine if the graph has an edge from parent to child. More... | |
bool | HasEdge (const Node &parent, const Node &child, const bool undirected=false, const std::string cost_type="") const |
Determine if the graph has an edge from parent to child. More... | |
bool | HasEdge (int parent, int child, bool undirected=false, const std::string &cost_type="") const |
std::vector< Node > | Nodes () const |
Get a list of nodes from the graph sorted by ID. More... | |
std::vector< Edge > | GetUndirectedEdges (const Node &N, const std::string &cost_type="") const |
Get a list of all edges to and from node N. More... | |
std::vector< EdgeSet > | GetEdges () const |
Get every in the given graph as IDs. More... | |
std::vector< IntEdge > | GetIntEdges (int parent) const |
Get children of a specific node as integers. More... | |
std::vector< float > | AggregateGraph (COST_AGGREGATE agg_type, bool directed=true, const std::string &cost_type="") const |
Summarize the costs of every outgoing edge for every node in the graph. More... | |
const std::vector< Edge > | operator[] (const Node &n) const |
void | addEdge (const Node &parent, const Node &child, float score=1.0f, const std::string &cost_type="") |
Add a new edge to the graph from parent to child. More... | |
void | addEdge (int parent_id, int child_id, float score, const std::string &cost_type="") |
Add a new edge to the graph from parent to child. More... | |
bool | hasKey (const Node &n) const |
Determine if n exists in the graph. More... | |
std::vector< std::array< float, 3 > > | NodesAsFloat3 () const |
Get a list of nodes as float arrays. More... | |
int | size () const |
Determine how many nodes are in the graph. More... | |
int | MaxID () const |
Calculate the maximum ID of any node in the graph. More... | |
int | getID (const Node &node) const |
Retrieve the ID for node in this graph. More... | |
void | Compress () |
Compress the graph to a CSR and enable the usage of several functions. More... | |
CSRPtrs | GetCSRPointers (const std::string &cost_type="") |
Obtain the size of and pointers to the 3 arrays that comprise this graph's CSR. graph if it isn't compressed already More... | |
Node | NodeFromID (int id) const |
Retrieve the node that corresponds to id. More... | |
void | Clear () |
Clear all nodes and edges from the graph. More... | |
std::vector< Node > | GetChildren (const Node &n) const |
Retrieve n's child nodes - n is a parent node More... | |
std::vector< Node > | GetChildren (const int parent_id) const |
Retrieve node parent_id's child nodes More... | |
Subgraph | GetSubgraph (const Node &parent_node, const std::string &cost_type="") const |
Retrieves a Subgraph using a Node. More... | |
Subgraph | GetSubgraph (int parent_id, const std::string &cost_type="") const |
Retrieves a Subgraph using a parent node ID. More... | |
void | AddNodeAttribute (int id, const std::string &attribute, const std::string &score) |
Add an attribute to the node at id More... | |
void | AddNodeAttributes (const std::vector< int > &id, const std::string &name, const std::vector< std::string > &scores) |
Add an attribute to the node at id. If the node at id already has a score for the attribute at name, then existing score should be overwritten More... | |
std::vector< std::string > | GetNodeAttributes (std::string attribute) const |
Get the score for the given attribute of every node in the graph. Nodes that do not have a score for this attribute should return an empty string for this array. More... | |
void | ClearNodeAttributes (std::string name) |
Clears the attribute at name and all of its contents from the internal hashmap More... | |
bool | DumpToJson (const std::string &path) |
void | AddEdges (const EdgeSet &edges, const std::string &cost_name="") |
Add multiple edges to the graph. More... | |
void | AddEdges (const std::vector< EdgeSet > &edges, const std::string &cost_name="") |
Add an array of edges to the graph. More... | |
std::vector< EdgeSet > | GetEdges (const std::string &cost_name) const |
Get the edges of a specfic cost type. More... | |
std::vector< std::string > | GetCostTypes () const |
Get an array of all cost names within this graph. More... | |
float | GetCost (int parent_id, int child_id, const std::string &cost_type="") const |
get the cost from parent_id to child_id in the given cost_type. More... | |
void | Graph::AddEdges (const std::vector< std::vector< IntEdge > > &edges, const std::string &cost_type) |
Add a set of intedges to the graph. More... | |
void | ClearCostArrays (const std::string &cost_name="") |
Clear one or more cost arrays from the graph. More... | |
void | AttrToCost (const std::string &node_attribute, const std::string &cost_to_store_as, Direction consider=Direction::INCOMING) |
Generate edge costs from a set of node attributes. More... | |
Private Types | |
using | NodeAttributeValueMap = robin_hood::unordered_map< int, std::string > |
Private Member Functions | |
int | getOrAssignID (const Node &input_node) |
Get the unique ID for this x, y, z position and assign it an new one if it doesn't already exist. More... | |
int | getOrAssignID (int input_int) |
Add an ID to the graph if it doesn't exist already. More... | |
bool | checkForEdge (int parent, int child) const |
Determine if an edge between parent and child exists in the graph. More... | |
void | CSRAddOrUpdateEdge (int parent_id, int child_id, float cost) |
Add a new edge cost to the CSR or update if if a cost already exists. More... | |
void | TripletsAddOrUpdateEdge (int parent_id, int child_id, float cost) |
Add a new edge to the triplets list. More... | |
void | ResizeIfNeeded () |
Resize the CSR to fit all the nodes in ordered_nodes if needed. More... | |
bool | hasKey (int id) const |
Check if this ID has already been assigned. More... | |
bool | HasCostArray (const std::string &key) const |
Check if we have this edge matrix already defined. More... | |
EdgeCostSet & | GetCostArray (const std::string &key) |
Get a reference to the edge matrix at the given key. More... | |
EdgeCostSet & | GetOrCreateCostType (const std::string &name) |
Get a reference to the edge matrix, or create a new one if it doesn't exist. More... | |
EdgeCostSet & | CreateCostArray (const std::string &name) |
Create a new edge matrix. More... | |
const EdgeCostSet & | GetCostArray (const std::string &key) const |
Get a reference to the edge matrix at the given key. More... | |
bool | IsDefaultName (const std::string &name) const |
Check if this name belongs to the default graph. More... | |
int | FindValueArrayIndex (int parent_id, int child_id) const |
Get the index of the cost at parent/child. More... | |
void | InsertEdgeIntoCostSet (int parent_id, int child_id, float score, EdgeCostSet &cost_set) |
Add an edge to a cost set between parent_id and child_id. More... | |
void | InsertEdgesIntoCostSet (EdgeCostSet &cost_set, const std::vector< EdgeSet > &es) |
Insert edges for a specific cost type into a cost set. More... | |
void | InsertOrUpdateEdge (int parent_id, int child_id, float score, const std::string &cost_type) |
Insert an edge into the default cost array or a new cost array. More... | |
float | GetCostForSet (const EdgeCostSet &set, int parent_id, int child_id) const |
Get the cost of traversing the edge between parent and child using set. More... | |
std::vector< Edge > | GetEdgesForNode (int parent_id, bool undirected=false, const std::string &cost_type="") const |
Get the edges for the given node. More... | |
TempMatrix | MapCostMatrix (const std::string &cost_type) const |
Construct a temp matrix for the specific cost type. More... | |
bool | HasNodeAttribute (const std::string &key) const |
Check if this graph has a specific node attribute. More... | |
Private Attributes | |
int | next_id = 0 |
The id for the next unique node. More... | |
std::vector< Node > | ordered_nodes |
A list of nodes contained by the graph. More... | |
robin_hood::unordered_map< Node, int > | idmap |
Maps a list of X,Y,Z positions to positions in ordered_nodes. More... | |
std::vector< Eigen::Triplet< float > > | triplets |
Edges to be converted to a CSR when Graph::Compress() is called. More... | |
bool | needs_compression = true |
If true, the CSR is inaccurate and requires compression. More... | |
robin_hood::unordered_map< std::string, NodeAttributeValueMap > | node_attr_map |
Node attribute type : Map of node id to node attribute. More... | |
std::string | active_cost_type |
The active edge matrix to use for the graph. More... | |
EdgeMatrix | edge_matrix |
The underlying CSR containing edge information. More... | |
std::string | default_cost = "Distance" |
std::unordered_map< std::string, EdgeCostSet > | edge_cost_maps |
< The default cost type of the graph. More... | |
bool | has_cost_arrays = false |
Indicates that the graph has cost arrays. More... | |
bool | nodes_out_of_order = false |
Determines whether or not the graph is using integer nodes. More... | |
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node.
Internally, this object uses Eigen (https://eigen.tuxfamily.org/dox/group__TutorialSparse.html) to store and maintain a CSR matrix. The CSR is always stored as a n by n sparse matrix where n is the number of nodes in ordered_nodes.
Distance
which can be accessed explicitly by the key "Distance" or leaving the cost_type field blank. Alternate costs have corresponding edges in the default cost set, but different costs to traverse from the parent to the child node.
|
private |
HF::SpatialStructures::Graph::Graph | ( | const std::vector< std::vector< int > > & | edges, |
const std::vector< std::vector< float > > & | distances, | ||
const std::vector< Node > & | Nodes, | ||
const std::string & | default_cost = "Distance" |
||
) |
Construct a graph from a list of nodes, edges, and distances.
edges | Ordered array of arrays of edges for each node in nodes. |
distances | Ordered array of distance from parent to child for each edge in edges. |
Nodes | Ordered array of nodes to act as a parent to all children in it's array in edges. |
default_cost | Default cost of the graph. This is the name of the first used cost. |
Preallocates the matrix it in element by element and compresses it.
(edges.size() == nodes.size() && nodes.size() == distances.size())
nodes[i]
, edges[i]
should contain an array for the id of all nodes that nodes[i]
has an edge from and, and distances[i]
should contain an array of the the distance from nodes[i]
to one of the nodes it has an edge to in edges[i]
.HF::SpatialStructures::Graph::Graph | ( | const std::string & | default_cost_name = "Distance" | ) |
Construct an empty graph.
void HF::SpatialStructures::Graph::addEdge | ( | const Node & | parent, |
const Node & | child, | ||
float | score = 1.0f , |
||
const std::string & | cost_type = "" |
||
) |
Add a new edge to the graph from parent to child.
parent | Parent node of the edge. |
child | Child node of the edge. |
score | Cost of traversing from aprent to child. |
If the parent or child node do not have an ID. An ID will be assigned automatically.
Referenced by AddEdgeFromNodeIDs(), AddEdgeFromNodes(), HF::GraphGenerator::GraphGenerator::CrawlGeom(), and HF::GraphGenerator::GraphGenerator::CrawlGeomParallel().
void HF::SpatialStructures::Graph::addEdge | ( | int | parent_id, |
int | child_id, | ||
float | score, | ||
const std::string & | cost_type = "" |
||
) |
Add a new edge to the graph from parent to child.
parent | Parent node of the edge. |
child | Child node of the edge. |
score | Cost of traversing from aprent to child. |
cost_type | Type of cost to add this edge to |
If the parent or child ids don't exist in the dictionary, they will be added.
std::logic_error | Tried to add an edge to an alternate cost type when the graph isnt compressed |
std::out_of_range | Tried to add an edge to an alternate cost type when it hasn't been added to the default graph |
void HF::SpatialStructures::Graph::AddEdges | ( | const EdgeSet & | edges, |
const std::string & | cost_name = "" |
||
) |
Add multiple edges to the graph.
edges | The set of edges to add to the graph |
cost_name | The cost_type to add the edges to. If this cost type doesn't exist in the graph yet, then it will be created. If left blank or set to the default name, then the edges will be added to the default cost type. |
std::logic_error | Trying to add an edge to an alternate cost type when it's not compressed |
std::out_of_range | Trying to add an edge to an alternate cost type when it hasn't already been added to the default graph2) If adding an alternate edge to the graph, the graph must already be compressed |
Referenced by CalculateAndStoreCrossSlope(), and CalculateAndStoreEnergyExpenditure().
void HF::SpatialStructures::Graph::AddEdges | ( | const std::vector< EdgeSet > & | edges, |
const std::string & | cost_name = "" |
||
) |
Add an array of edges to the graph.
void HF::SpatialStructures::Graph::AddNodeAttribute | ( | int | id, |
const std::string & | attribute, | ||
const std::string & | score | ||
) |
Add an attribute to the node at id
id | The ID of the node that will receive attribute |
attribute | The attribute that the node at ID will receive |
score | The weight, or distance that extends from the node at id |
void HF::SpatialStructures::Graph::AddNodeAttributes | ( | const std::vector< int > & | id, |
const std::string & | name, | ||
const std::vector< std::string > & | scores | ||
) |
Add an attribute to the node at id. If the node at id already has a score for the attribute at name, then existing score should be overwritten
id | The container of IDs from which nodes will be retrieved and given attributes |
name | The attribute that each node will receive |
scores | The container of score, ordered by the container of node IDs |
std::logic_error | The length of scores and the length of ID do not match. |
Referenced by AddNodeAttributes().
std::vector< float > HF::SpatialStructures::Graph::AggregateGraph | ( | COST_AGGREGATE | agg_type, |
bool | directed = true , |
||
const std::string & | cost_type = "" |
||
) | const |
Summarize the costs of every outgoing edge for every node in the graph.
agg_type | Type of aggregation to use. |
directed | If true, include both incoming and outgoing edges for calculating a node's score. |
std::out_of_range | if agg_type doesn't match any value of COST_AGGREGATE. |
Std::exception | if the graph isn't compressed. |
O(k)
where k is the total number of edges in the graph.O(n)
where n is the total number of nodes in the graph.Referenced by AggregateCosts().
void HF::SpatialStructures::Graph::AttrToCost | ( | const std::string & | node_attribute, |
const std::string & | cost_to_store_as, | ||
Direction | consider = Direction::INCOMING |
||
) |
Generate edge costs from a set of node attributes.
attr_key | Attribute to create a new cost set from. |
cost_string | Name of the new cost set. |
dir | Direction that the cost of the edge should be calculated in. For example INCOMING will use the cost of the node being traveled to by the edge. |
std::out_of_range | if node_attribute could not be found. |
0->1: 111.000000
Referenced by GraphAttrsToCosts().
|
private |
Determine if an edge between parent and child exists in the graph.
Iterates through every row in the parent's column to find child. If child is not found in this column, false is returned. If child can be found in this column, then true is returned.
void HF::SpatialStructures::Graph::Clear | ( | ) |
Clear all nodes and edges from the graph.
Referenced by ClearGraph().
void HF::SpatialStructures::Graph::ClearCostArrays | ( | const std::string & | cost_name = "" | ) |
Clear one or more cost arrays from the graph.
cost_name | Name of the cost array to clear. If equal to the default cost of this graph or empty string, will clear all existing cost arrays (except for the default) |
NoCost | if the costname specified not match either the default cost or any other cost type held by the graph. |
Referenced by ClearGraph().
void HF::SpatialStructures::Graph::ClearNodeAttributes | ( | std::string | name | ) |
Clears the attribute at name and all of its contents from the internal hashmap
name | The attribute that will be cleared from this graph's internal hashmap |
name |
Referenced by ClearAttributeType().
void HF::SpatialStructures::Graph::Compress | ( | ) |
Compress the graph to a CSR and enable the usage of several functions.
This won't do anything if called on an already compressed graph. The graph is "compressed" by resizing the edge matrix to the maximum ID of any node in triplets, then calling setFromTriplets().
Referenced by Compress().
|
private |
Create a new edge matrix.
name | Unique name of the new cost array |
name
does not already belong to another cost array in the graph. name
is not the default name in the graph or an empty string.
|
private |
Add a new edge cost to the CSR or update if if a cost already exists.
parent_id | ID of the parent node. |
child_id | ID of the child node |
cost | Cost of traversing from parent to child |
bool HF::SpatialStructures::Graph::DumpToJson | ( | const std::string & | path | ) |
|
private |
Get the index of the cost at parent/child.
parent_id | ID of the edge's parent. |
child_id | ID of the edge's child. |
Determines the start and end bounds of the row belonging to parent_id
using the CSR's outer_index_ptr, then searches these bounds in inner_indices for child_id
. If a match is found, the distance from child_id to the beginning of the csr's inner_index array is calculated and returned.
parent_id
is a valid node in the graph. std::vector< Node > HF::SpatialStructures::Graph::GetChildren | ( | const int | parent_id | ) | const |
Retrieve node parent_id's child nodes
parent_id | The parent node ID from which child nodes will be derived |
Retrieve n's child nodes - n is a parent node
n | The parent node from which child nodes will be derived |
float HF::SpatialStructures::Graph::GetCost | ( | int | parent_id, |
int | child_id, | ||
const std::string & | cost_type = "" |
||
) | const |
get the cost from parent_id to child_id in the given cost_type.
parent_id | Node that's being traversed from. |
child_id | Node that's being traversed to. |
cost_type | Type of cost to get for this edge. If blank, the graph's default cost type will be used. |
parent_id
to child_id
for cost_type
.Referenced by GetEdgeCost().
|
private |
Get a reference to the edge matrix at the given key.
key | Name of the cost to retrieve. |
key
.HF::Exceptions::NoCost | if the given key doesn't exist |
key
is not the default graph name or an empty string.
|
private |
Get a reference to the edge matrix at the given key.
key | Name of the cost to retrieve. |
key
.HF::Exceptions::NoCost | if the given cost doesn't exist. |
|
private |
Get the cost of traversing the edge between parent and child using set.
set | The set of edges to get this cost in |
parent_id | ID of the edge's parent node. |
child_id | ID of the edge's child node |
set
between parent_id
and child_id
. If an edge does exist betwen parent_id
and child_id
in set
then returns the coststd::vector< std::string > HF::SpatialStructures::Graph::GetCostTypes | ( | ) | const |
Get an array of all cost names within this graph.
CSRPtrs HF::SpatialStructures::Graph::GetCSRPointers | ( | const std::string & | cost_type = "" | ) |
Obtain the size of and pointers to the 3 arrays that comprise this graph's CSR. graph if it isn't compressed already
This will automatically call Compress if it hasn't been called already.
Referenced by GetCSRPointers().
std::vector< EdgeSet > HF::SpatialStructures::Graph::GetEdges | ( | ) | const |
Get every in the given graph as IDs.
std::exception | if the graph hasn't been compressed. compressed. |
Referenced by HF::Pathfinding::BoostGraph::BoostGraph().
std::vector< EdgeSet > HF::SpatialStructures::Graph::GetEdges | ( | const std::string & | cost_name | ) | const |
Get the edges of a specfic cost type.
cost_name | The name of the cost to get edges for |
cost_name
HF::Exceptions::NoCost | The cost at `cost_name` didn't exist in the graph |
|
private |
Get the edges for the given node.
parent_id | Node to get the outgoing edges of |
undirected | If this is true, then get both outgoing and incoming edges of parent_id |
cost_type | Cost type to use for retrieved edges |
NoCost | if cost_type isn't eh default graph and doesn't exist in the graph's cost types |
int HF::SpatialStructures::Graph::getID | ( | const Node & | node | ) | const |
Retrieve the ID for node in this graph.
Referenced by GetNodeID().
std::vector< IntEdge > HF::SpatialStructures::Graph::GetIntEdges | ( | int | parent | ) | const |
Get children of a specific node as integers.
std::vector< std::string > HF::SpatialStructures::Graph::GetNodeAttributes | ( | std::string | attribute | ) | const |
Get the score for the given attribute of every node in the graph. Nodes that do not have a score for this attribute should return an empty string for this array.
attribute | The attribute from which a container of scores will be obtained |
Referenced by GetNodeAttributes().
|
private |
Get the unique ID for this x, y, z position and assign it an new one if it doesn't already exist.
If the node has not yet been seen by the graph, next_id will be assigned to it and incremented, then the node node will and its new id will be added to idmap. If the node has already been assigned an ID, then the ID will be returned directly from idmap.
input_node | Node to retrieve and potentially assign a new ID for. |
|
private |
Add an ID to the graph if it doesn't exist already.
|
private |
Get a reference to the edge matrix, or create a new one if it doesn't exist.
name | Name of the cost matrix to create or retrieve. |
key
is not the default name in the graph or an empty string. Subgraph HF::SpatialStructures::Graph::GetSubgraph | ( | const Node & | parent_node, |
const std::string & | cost_type = "" |
||
) | const |
Retrieves a Subgraph using a Node.
parent_node | The parent node from which the Subgraph will be derived |
Referenced by HF::SpatialStructures::CostAlgorithms::CalculateCrossSlope(), and HF::SpatialStructures::CostAlgorithms::CalculateEnergyExpenditure().
Subgraph HF::SpatialStructures::Graph::GetSubgraph | ( | int | parent_id, |
const std::string & | cost_type = "" |
||
) | const |
std::vector< Edge > HF::SpatialStructures::Graph::GetUndirectedEdges | ( | const Node & | N, |
const std::string & | cost_type = "" |
||
) | const |
Get a list of all edges to and from node N.
N | The Node to get edges from and to. |
O(k)
where k is the number of edges in the graph since it needs to iterate through every edge in the graph to find the edges to this node.void HF::SpatialStructures::Graph::Graph::AddEdges | ( | const std::vector< std::vector< IntEdge > > & | edges, |
const std::string & | cost_type | ||
) |
Add a set of intedges to the graph.
edges | An ordered vector of vectors in which each outer vector holds a vector of edges for the node at the ID of that index. For example the vector at index 0 would hold a vector of intedges for the node at ID 0.a |
cost_type | The type of cost to add these edges to. If this cost type does not yet exist, then it will be created. |
std::logic_error | Trying to add an edge to an alternate cost type when it's not compressed |
std::out_of_range | Trying to add an edge to an alternate cost type when it hasn't already been added to the default graph2) If adding an alternate edge to the graph, the graph must already be compressed |
|
private |
Check if we have this edge matrix already defined.
Unique | key of the cost type to search for in the cost map. |
bool HF::SpatialStructures::Graph::HasEdge | ( | const Node & | parent, |
const Node & | child, | ||
const bool | undirected = false , |
||
const std::string | cost_type = "" |
||
) | const |
Determine if the graph has an edge from parent to child.
parent | Parent of the edge to check for. |
child | Child of the edge to check for. |
undirected | If true, look for an edge from child to parent as well. |
std::exception | if the graph is uncompressed. // be sure to #include "graph.h"
// Create the nodes
HF::SpatialStructures::Node node_0(1.0f, 1.0f, 2.0f);
HF::SpatialStructures::Node node_1(2.0f, 3.0f, 4.0f, 5);
HF::SpatialStructures::Node node_2(11.0f, 22.0f, 140.0f);
// Create a container (vector) of nodes
std::vector<HF::SpatialStructures::Node> nodes = { node_0, node_1, node_2 };
// Create matrices for edges and distances, edges.size() == distances().size()
std::vector<std::vector<int>> edges = { { 1, 2 }, { 2 }, { 1 } };
std::vector<std::vector<float>> distances = { { 1.0f, 2.5f }, { 54.0f }, { 39.0f } };
// Now you can create a Graph - note that nodes, edges, and distances are passed by reference
HF::SpatialStructures::Graph graph(edges, distances, nodes);
// last argument can be true/false for undirected/directed graph respectively
bool has_edge = graph.HasEdge(node_1, node_2, true);
|
bool HF::SpatialStructures::Graph::HasEdge | ( | const std::array< float, 3 > & | parent, |
const std::array< float, 3 > & | child, | ||
bool | undirected = false |
||
) | const |
Determine if the graph has an edge from parent to child.
parent | Parent of the edge to check for. |
child | Child of the edge to check for. |
undirected | If true, look for an edge from child to parent as well. |
std::exception | if the matrix is uncompressed. // be sure to #include "graph.h"
// Create the nodes
HF::SpatialStructures::Node node_0(1.0f, 1.0f, 2.0f);
HF::SpatialStructures::Node node_1(2.0f, 3.0f, 4.0f, 5);
HF::SpatialStructures::Node node_2(11.0f, 22.0f, 140.0f);
// Create a container (vector) of nodes
std::vector<HF::SpatialStructures::Node> nodes = { node_0, node_1, node_2 };
// Create matrices for edges and distances, edges.size() == distances().size()
std::vector<std::vector<int>> edges = { { 1, 2 }, { 2 }, { 1 } };
std::vector<std::vector<float>> distances = { { 1.0f, 2.5f }, { 54.0f }, { 39.0f } };
// Now you can create a Graph - note that nodes, edges, and distances are passed by reference
HF::SpatialStructures::Graph graph(edges, distances, nodes);
// Prepare {x, y, z} coordinates (positions)
auto parent_pos = node_1.getArray(); // (2.0, 3.0, 4.0)
auto child_pos = node_2.getArray(); // (11.0, 22.0, 140.0)
// last argument can be true/false for undirected/directed graph respectively
bool has_edge = graph.HasEdge(parent_pos, child_pos, true);
|
bool HF::SpatialStructures::Graph::HasEdge | ( | int | parent, |
int | child, | ||
bool | undirected = false , |
||
const std::string & | cost_type = "" |
||
) | const |
bool HF::SpatialStructures::Graph::hasKey | ( | const Node & | n | ) | const |
Determine if n exists in the graph.
n | Node to look for. |
Performs a single hash to check if n exists in the hashmap.
|
private |
Check if this ID has already been assigned.
id | Id of the node to check |
|
private |
Check if this graph has a specific node attribute.
|
private |
Add an edge to a cost set between parent_id and child_id.
parent_id | The id of the parent node in the graph |
child_id | the ID of the child node in the graph |
std::out_of_range | no edge from parent_id to child_id exists in the default cost type. |
|
private |
Insert edges for a specific cost type into a cost set.
cost_set | Set of costs to insert edges into. |
es | An array of edge_sets containing parent and child nodes to add to the graph. |
|
private |
Insert an edge into the default cost array or a new cost array.
parent_id | ID of the edge's parent node |
child_id | ID of the edge's child node |
score | Cost of traversing from parent to child |
cost_type | The type of cost to add this edge to |
If the graph isn't compressed, calls TripletsAddOrUpdateEdge(). If the graph is compressed calls CSRAddOrUpdateEdge(). If the cost at cost_type doesn't exist, then it will be created.
std::logic_error | If trying to add an edge to an alternate cost type when the graph hasn't been compressed. |
std::logic_error | If trying to add an edge between nodes that doesn't exist already in the graph. |
|
private |
Check if this name belongs to the default graph.
name | The name to test against the default. |
|
private |
Construct a temp matrix for the specific cost type.
cost_type | The type of cost to generate the temp matrix for |
cost_type
.int HF::SpatialStructures::Graph::MaxID | ( | ) | const |
Calculate the maximum ID of any node in the graph.
Referenced by HF::Pathfinding::BoostGraph::BoostGraph().
Node HF::SpatialStructures::Graph::NodeFromID | ( | int | id | ) | const |
Retrieve the node that corresponds to id.
id | The ID of the node to get. |
std::out_of_range | id didn't belong to any node in the graph. // be sure to #include "graph.h"
// Create the nodes
HF::SpatialStructures::Node node_0(1.0f, 1.0f, 2.0f, 4);
HF::SpatialStructures::Node node_1(2.0f, 3.0f, 4.0f, 5);
HF::SpatialStructures::Node node_2(11.0f, 22.0f, 140.0f, 6);
// Create a container (vector) of nodes
std::vector<HF::SpatialStructures::Node> nodes = { node_0, node_1, node_2 };
// Create matrices for edges and distances, edges.size() == distances().size()
std::vector<std::vector<int>> edges = { { 1, 2 }, { 2 }, { 1 } };
std::vector<std::vector<float>> distances = { { 1.0f, 2.5f }, { 54.0f }, { 39.0f } };
// Now you can create a Graph - note that nodes, edges, and distances are passed by reference
// Note: graph is compressed upon instantiation
HF::SpatialStructures::Graph graph(edges, distances, nodes);
// Let's retrieve node_1.
int desired_node_id = 2;
HF::SpatialStructures::Node node_from_id = graph.NodeFromID(desired_node_id);
// Note that NodeFromID ceases to work if the id argument provided does not exist as
// an ID among the nodes within graph
|
std::vector< Node > HF::SpatialStructures::Graph::Nodes | ( | ) | const |
Get a list of nodes from the graph sorted by ID.
Referenced by HF::SpatialStructures::CostAlgorithms::CalculateCrossSlope(), HF::SpatialStructures::CostAlgorithms::CalculateEnergyExpenditure(), GenerateGraph(), GenerateGraphObstacles(), and GetAllNodesFromGraph().
std::vector< std::array< float, 3 > > HF::SpatialStructures::Graph::NodesAsFloat3 | ( | ) | const |
Get a list of nodes as float arrays.
|
private |
Resize the CSR to fit all the nodes in ordered_nodes if needed.
If the CSR can already fit all of the ndoes in ordered_nodes then this won't do anything.
int HF::SpatialStructures::Graph::size | ( | ) | const |
Determine how many nodes are in the graph.
Size is directly returned from id_to_nodes.size()
.
Referenced by HF::Pathfinding::BoostGraph::BoostGraph(), HF::VisibilityGraph::AllToAll(), HF::VisibilityGraph::AllToAllUndirected(), GetSizeOfGraph(), and HF::VisibilityGraph::GroupToGroup().
|
private |
Add a new edge to the triplets list.
parent_id | Id of the parent node. |
child_id | Id of the child node. |
cost | Cost of traversing from parent to child. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
Edges to be converted to a CSR when Graph::Compress() is called.