10#include <robin_hood.h>
130 inline float* CSRPtrs::data_end()
const {
242 float* begin =
nullptr;
245 if (row_number >= 0 && row_number <
rows) {
275 float* end =
nullptr;
276 const int next_row = row_number + 1;
279 if (next_row > 0 && next_row <
rows) {
282 else if (next_row > 0 && next_row ==
rows) {
313 int* begin =
nullptr;
316 if (row_number >= 0 && row_number <
rows) {
346 const int next_row = row_number + 1;
349 if (next_row > 0 && next_row <
rows) {
352 else if (next_row > 0 && next_row ==
rows) {
394 inline int size()
const {
return this->costs.size(); };
404 const int old_size = this->
size();
405 if (this->
size() < new_size)
408 this->costs.resize(new_size);
411 const auto start_offset =
costs.begin() + old_size;
412 std::fill(start_offset,
costs.end(), NAN);
417 inline void Clear() { this->costs.clear(); }
426 return this->costs[i];
430 return !(i >= this->
size()) || (i < 0);
440 return this->costs[i];
449 assert(this->costs.size() > 0);
450 return this->costs.data();
459 assert(this->costs.size() > 0);
460 return this->costs.data();
494 robin_hood::unordered_map<Node, int>
idmap;
499 robin_hood::unordered_map<std::string, NodeAttributeValueMap>
node_attr_map;
807 bool undirected =
false,
808 const std::string& cost_type =
""
880 const std::vector<std::vector<int>>& edges,
881 const std::vector<std::vector<float>>& distances,
882 const std::vector<Node>&
Nodes,
902 Graph(
const std::string& default_cost_name =
"Distance");
943 const std::array<float, 3>& parent,
944 const std::array<float, 3>& child,
945 bool undirected =
false
985 const bool undirected =
false,
986 const std::string cost_type =
""
1021 bool HasEdge(
int parent,
int child,
bool undirected =
false,
const std::string& cost_type =
"")
const;
1303 void addEdge(
const Node& parent,
const Node& child,
float score = 1.0f,
const std::string& cost_type =
"");
1368 void addEdge(
int parent_id,
int child_id,
float score,
const std::string& cost_type =
"");
1832 void AddNodeAttributes(
const std::vector<int> &
id,
const std::string & name,
const std::vector<std::string> & scores);
1892 void AddEdges(
const std::vector<EdgeSet>& edges,
const std::string& cost_name =
"");
1901 std::vector<EdgeSet>
GetEdges(
const std::string& cost_name)
const;
1924 float GetCost(
int parent_id,
int child_id,
const std::string& cost_type =
"")
const;
1943 void Graph::AddEdges(
const std::vector<std::vector<IntEdge>>& edges,
const std::string& cost_type);
1972 const std::string & node_attribute,
1973 const std::string & cost_to_store_as,
Contains definitions for the Edge structure.
Contains definitions for the Node structure.
Contains standard fundamental data structures for representing space used throughout DHARTAPI.
A Subgraph consists of a parent Node m_parent and a container of Edge m_edges such that all Edge in m...
Node m_parent
The parent node from which all Edge in m_edges extend.
Eigen::SparseMatrix< float, 1 > EdgeMatrix
The type of matrix the graph uses internally.
COST_AGGREGATE
Methods of aggregating the costs for edges for each node in the graph.
@ AVERAGE
Average the cost of all edges.
@ COUNT
Count how many edges this node has.
@ SUM
Add the cost of all edges.
Eigen::Map< const EdgeMatrix > TempMatrix
A mapped matrix of EdgeMatrix. Only owns pointers to memory.
std::vector< Edge > m_edges
The edges that extend from m_parent.
Direction
Node to use for calculating the cost of an edge when converting node attributes to edge costs.
Eigen a C++ template library for linear algebra: matrices, vectors, numerical solvers,...
A collection of edges and a parent.
A struct to hold all necessary information for a CSR.
int * CSRPtrs::col_end(int row_number) const
Returns the address of the element that denotes the end of a 'subarray' within inner_indices
float * CSRPtrs::row_begin(int row_number) const
Returns the address of the first non-zero element of row_number within the CSR data buffer
int * CSRPtrs::col_begin(int row_number) const
Returns the address of the element that determines the column where the first non-zero value begins w...
int * CSRPtrs::inner_end() const
Returns the address of one-past the last element within the inner_indices buffer
int * CSRPtrs::inner_begin() const
Returns the address of one-past the last element within the data buffer
float * CSRPtrs::data_begin() const
Returns the base address of the data buffer
bool AreValid()
Verify the CSR referenced by this instance is valid.
int * outer_indices
Stores for each column (resp. row) the index of the first non-zero in the previous two arrays.
int cols
Number of columns in this CSR.
int nnz
Number of non-zeros contained by the CSR.
int * inner_indices
Stores the row (resp. column) indices of the non-zeros.
int * CSRPtrs::outer_end() const
Returns the address of one-past the last element within the outer_indices buffer
float * data
Stores the coefficient values of the non-zeros.
int rows
Number of rows in this CSR.
float * CSRPtrs::row_end(int row_number) const
Returns the address of the first non-zero element of row_number + 1, i.e. the base address of the nex...
int * CSRPtrs::outer_begin() const
Returns the base address of the outer_indices buffer
A set of edge costs for a graph.
std::vector< float > costs
Array of costs to be used like eigen's internal indices array.
float * GetPtr()
Get the pointer to the start of this array.
void ResizeIfNeeded(int new_size)
Resize this edge matrix if needed.
EdgeCostSet(int size)
Create an edge cost set and allocate a specific size.
float & operator[](int i)
Index internal values array.
float operator[](int i) const
Index internal values array.
void Clear()
Clear all values from this edge cost set.
bool bounds_check(int i) const
EdgeCostSet()
Construct an empty edge cost set.
int size() const
Get the size of this edge matrix.
const float * GetPtr() const
Get the pointer to the start of this array.
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node.
void InsertEdgesIntoCostSet(EdgeCostSet &cost_set, const std::vector< EdgeSet > &es)
Insert edges for a specific cost type into a cost set.
void ClearNodeAttributes(std::string name)
Clears the attribute at name and all of its contents from the internal hashmap
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.
bool DumpToJson(const std::string &path)
EdgeMatrix edge_matrix
The underlying CSR containing edge information.
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.
int getOrAssignID(int input_int)
Add an ID to the graph if it doesn't exist already.
void ResizeIfNeeded()
Resize the CSR to fit all the nodes in ordered_nodes if needed.
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.
std::vector< EdgeSet > GetEdges() const
Get every in the given graph as IDs.
void TripletsAddOrUpdateEdge(int parent_id, int child_id, float cost)
Add a new edge to the triplets list.
bool IsDefaultName(const std::string &name) const
Check if this name belongs to the default graph.
bool needs_compression
If true, the CSR is inaccurate and requires compression.
std::vector< Eigen::Triplet< float > > triplets
Edges to be converted to a CSR when Graph::Compress() is called.
Graph(const std::string &default_cost_name="Distance")
Construct an empty graph.
int size() const
Determine how many nodes are in the graph.
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.
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,...
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.
TempMatrix MapCostMatrix(const std::string &cost_type) const
Construct a temp matrix for the specific cost type.
void Compress()
Compress the graph to a CSR and enable the usage of several functions.
bool HasCostArray(const std::string &key) const
Check if we have this edge matrix already defined.
bool HasNodeAttribute(const std::string &key) const
Check if this graph has a specific node attribute.
bool nodes_out_of_order
Determines whether or not the graph is using integer nodes.
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.
robin_hood::unordered_map< Node, int > idmap
Maps a list of X,Y,Z positions to positions in ordered_nodes.
std::unordered_map< std::string, EdgeCostSet > edge_cost_maps
< The default cost type of the graph.
bool hasKey(const Node &n) const
Determine if n exists in the graph.
void AddEdges(const std::vector< EdgeSet > &edges, const std::string &cost_name="")
Add an array of edges to the graph.
std::vector< Node > GetChildren(const int parent_id) const
Retrieve node parent_id's child nodes
int MaxID() const
Calculate the maximum ID of any node in the graph.
void AddEdges(const EdgeSet &edges, const std::string &cost_name="")
Add multiple edges to the 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.
std::string active_cost_type
The active edge matrix to use for the graph.
Node NodeFromID(int id) const
Retrieve the node that corresponds to id.
const std::vector< Edge > operator[](const Node &n) const
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 com...
void AddNodeAttribute(int id, const std::string &attribute, const std::string &score)
Add an attribute to the node at id
EdgeCostSet & GetOrCreateCostType(const std::string &name)
Get a reference to the edge matrix, or create a new one if it doesn't exist.
std::vector< std::string > GetCostTypes() const
Get an array of all cost names within this graph.
Subgraph GetSubgraph(const Node &parent_node, const std::string &cost_type="") const
Retrieves a Subgraph using a Node.
bool HasEdge(int parent, int child, bool undirected=false, const std::string &cost_type="") const
void ClearCostArrays(const std::string &cost_name="")
Clear one or more cost arrays from the graph.
bool checkForEdge(int parent, int child) const
Determine if an edge between parent and child exists in the graph.
Subgraph GetSubgraph(int parent_id, const std::string &cost_type="") const
Retrieves a Subgraph using a parent node ID.
void Clear()
Clear all nodes and edges from the graph.
bool has_cost_arrays
Indicates that the graph has cost arrays.
std::vector< Edge > GetEdgesForNode(int parent_id, bool undirected=false, const std::string &cost_type="") const
Get the edges for the given node.
std::vector< Node > Nodes() const
Get a list of nodes from the graph sorted by ID.
int FindValueArrayIndex(int parent_id, int child_id) const
Get the index of the cost at parent/child.
int next_id
The id for the next unique node.
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.
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.
EdgeCostSet & CreateCostArray(const std::string &name)
Create a new edge matrix.
EdgeCostSet & GetCostArray(const std::string &key)
Get a reference to the edge matrix at the given key.
std::vector< Edge > GetUndirectedEdges(const Node &N, const std::string &cost_type="") const
Get a list of all edges to and from node N.
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.
robin_hood::unordered_map< int, std::string > NodeAttributeValueMap
std::vector< EdgeSet > GetEdges(const std::string &cost_name) const
Get the edges of a specfic cost type.
std::vector< IntEdge > GetIntEdges(int parent) const
Get children of a specific node as integers.
std::vector< Node > ordered_nodes
A list of nodes contained by the graph.
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.
std::vector< Node > GetChildren(const Node &n) const
Retrieve n's child nodes - n is a parent node
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 ...
std::vector< std::array< float, 3 > > NodesAsFloat3() const
Get a list of nodes as float arrays.
robin_hood::unordered_map< std::string, NodeAttributeValueMap > node_attr_map
Node attribute type : Map of node id to node attribute.
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.
int getID(const Node &node) const
Retrieve the ID for node in this graph.
const EdgeCostSet & GetCostArray(const std::string &key) const
Get a reference to the edge matrix at the given key.
bool hasKey(int id) const
Check if this ID has already been assigned.
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.
A point in space with an ID.