9 #ifndef MRPT_DIRECTEDGRAPH_H 10 #define MRPT_DIRECTEDGRAPH_H 68 template <
class TYPE_EDGES,
75 struct edge_t :
public TYPE_EDGES,
public EDGE_ANNOTATIONS
79 template <
typename ARG1>
83 template <
typename ARG1,
typename ARG2>
126 alignas(16)
typename edges_map_t::value_type entry(
127 std::make_pair(from_nodeID, to_nodeID), edge_value);
137 alignas(16)
typename edges_map_t::value_type entry(
138 std::make_pair(from_nodeID, to_nodeID), edge_value);
145 return edges.find(std::make_pair(from_nodeID, to_nodeID)) !=
157 iterator it =
edges.find(std::make_pair(from_nodeID, to_nodeID));
158 if (it ==
edges.end())
161 "Edge %u->%u does not exist", (
unsigned)from_nodeID,
162 (
unsigned)to_nodeID))
176 if (it ==
edges.end())
179 "Edge %u->%u does not exist", (
unsigned)from_nodeID,
180 (
unsigned)to_nodeID))
190 return edges.equal_range(std::make_pair(from_nodeID, to_nodeID));
194 std::pair<const_iterator, const_iterator>
getEdges(
197 return edges.equal_range(std::make_pair(from_nodeID, to_nodeID));
205 edges.erase(std::make_pair(from_nodeID, to_nodeID));
215 it !=
edges.end(); ++it)
217 lstNode_IDs.insert(it->first.first);
218 lstNode_IDs.insert(it->first.second);
226 std::set<TNodeID> lst;
235 std::set<TNodeID> aux;
237 it !=
edges.end(); ++it)
239 aux.insert(it->first.first);
240 aux.insert(it->first.second);
248 const TNodeID nodeID, std::set<TNodeID>& neighborIDs)
const 252 it !=
edges.end(); ++it)
254 if (it->first.first == nodeID)
255 neighborIDs.insert(it->first.second);
256 else if (it->first.second == nodeID)
257 neighborIDs.insert(it->first.first);
264 std::set<TNodeID> neighborIDs;
278 template <
class MAP_NODEID_SET_NODEIDS>
281 outAdjacency.clear();
283 it !=
edges.end(); ++it)
285 outAdjacency[it->first.first].insert(it->first.second);
286 outAdjacency[it->first.second].insert(it->first.first);
294 template <
class MAP_NODEID_SET_NODEIDS,
class SET_NODEIDS>
296 MAP_NODEID_SET_NODEIDS& outAdjacency,
297 const SET_NODEIDS& onlyForTheseNodes)
const 299 outAdjacency.clear();
301 onlyForTheseNodes.end();
303 it !=
edges.end(); ++it)
305 if (onlyForTheseNodes.find(it->first.first) == setEnd ||
306 onlyForTheseNodes.find(it->first.second) == setEnd)
308 outAdjacency[it->first.first].insert(it->first.second);
309 outAdjacency[it->first.second].insert(it->first.first);
325 o <<
"digraph G {\n";
328 const TNodeID id1 = it->first.first;
329 const TNodeID id2 = it->first.second;
331 if (!
p.node_names.empty())
334 p.node_names.find(id1);
335 if (itNam1 !=
p.node_names.end()) s1 = itNam1->second;
337 p.node_names.find(id2);
338 if (itNam2 !=
p.node_names.end()) s2 = itNam2->second;
341 s1 =
mrpt::format(
"%u", static_cast<unsigned int>(id1));
343 s2 =
mrpt::format(
"%u", static_cast<unsigned int>(id2));
344 if (
p.node_props.empty())
347 p.node_props.find(id1);
348 if (itP1 !=
p.node_props.end())
349 o <<
"\"" << s1 <<
"\"" 350 <<
" [" << itP1->second <<
"];\n";
352 p.node_props.find(id2);
353 if (itP2 !=
p.node_props.end())
354 o <<
"\"" << s2 <<
"\"" 355 <<
" [" << itP2->second <<
"];\n";
357 o <<
" \"" << s1 <<
"\" -> \"" << s2 <<
"\"";
358 if (
p.mark_edges_as_not_constraint) o <<
" [constraint=false]";
361 return !((o <<
"}\n").fail());
369 std::ofstream f(fileName.c_str());
370 if (!f.is_open())
return false;
std::pair< const_iterator, const_iterator > getEdges(TNodeID from_nodeID, TNodeID to_nodeID) const
Return a pair<first,last> of const iterators to the range of edges between two given nodes...
A directed graph with the argument of the template specifying the type of the annotations in the edge...
std::multimap< TYPE1, TYPE2, std::less< TYPE1 >, Eigen::aligned_allocator< std::pair< const TYPE1, TYPE2 > > > multimap_t
The type of each global pose in nodes: an extension of the TYPE_EDGES pose with any optional user-def...
const edge_t & getEdge(TNodeID from_nodeID, TNodeID to_nodeID) const
Return a reference to the content of a given edge.
std::pair< TNodeID, TNodeID > TPairNodeIDs
A pair of node IDs.
const_iterator rbegin() const
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::map< TNodeID, std::string > node_names
If provided, these textual names will be used for naming the nodes instead of their numeric IDs given...
edges_map_t edges
The public member with the directed edges in the graph.
#define THROW_EXCEPTION(msg)
void getNeighborsOf(const TNodeID nodeID, std::set< TNodeID > &neighborIDs) const
Return the list of all neighbors of "nodeID", by creating a list of their node IDs.
void getAdjacencyMatrix(MAP_NODEID_SET_NODEIDS &outAdjacency, const SET_NODEIDS &onlyForTheseNodes) const
Just like getAdjacencyMatrix but return only the adjacency for those node_ids in the set onlyForThese...
std::set< TNodeID > getAllNodes() const
Less efficient way to get all nodes that returns a copy of the set object.
std::map< TNodeID, std::string > node_props
If provided, an extra line will be added setting Graphviz properties for each node, e.g.
const_iterator end() const
const Scalar * const_iterator
GLsizei GLsizei GLuint * obj
std::pair< iterator, iterator > getEdges(TNodeID from_nodeID, TNodeID to_nodeID)
Return a pair<first,last> of iterators to the range of edges between two given nodes.
void clearEdges()
Erase all edges.
bool edgeExists(TNodeID from_nodeID, TNodeID to_nodeID) const
Test if the given directed edge exists.
const_iterator begin() const
CDirectedGraph(const edges_map_t &obj)
Copy constructor from a multimap<pair< >, >
edges_map_t::iterator iterator
CDirectedGraph()
Default constructor.
void getAdjacencyMatrix(MAP_NODEID_SET_NODEIDS &outAdjacency) const
Return a map from node IDs to all its neighbors (that is, connected nodes, regardless of the edge dir...
uint64_t TNodeID
The type for node IDs in graphs of different types.
const_iterator rend() const
edges_map_t::const_reverse_iterator const_reverse_iterator
void insertEdge(TNodeID from_nodeID, TNodeID to_nodeID, const edge_t &edge_value)
Insert an edge (from -> to) with the given edge value.
CDirectedGraph< TYPE_EDGES, EDGE_ANNOTATIONS > self_t
Handy self type.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
bool saveAsDot(const std::string &fileName, const TGraphvizExportParams &p=TGraphvizExportParams()) const
uint64_t TNodeID
The type for node IDs in graphs of different types.
edge_t(const ARG1 &a1, const ARG2 &a2)
GLsizei const GLchar ** string
mrpt::aligned_containers< TPairNodeIDs, edge_t >::multimap_t edges_map_t
The type of the member edges.
size_t countDifferentNodesInEdges() const
Count how many different node IDs appear in the graph edges.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void getAllNodes(std::set< TNodeID > &lstNode_IDs) const
Return a list of all the node_ID's of the graph, generated from all the nodes that appear in the list...
Used in mrpt::graphs export functions to .dot files.
edge_t & getEdge(TNodeID from_nodeID, TNodeID to_nodeID)
Return a reference to the content of a given edge.
edges_map_t::reverse_iterator reverse_iterator
bool mark_edges_as_not_constraint
If true (default=false), an "[constraint=false]" will be added to all edges (see Graphviz docs)...
size_t edgeCount() const
The number of edges in the graph.
void eraseEdge(TNodeID from_nodeID, TNodeID to_nodeID)
Erase all edges between the given nodes (it has no effect if no edge existed)
void insertEdgeAtEnd(TNodeID from_nodeID, TNodeID to_nodeID, const edge_t &edge_value)
Insert an edge (from -> to) with the given edge value (more efficient version to be called if you kno...
edges_map_t::const_iterator const_iterator
std::set< TNodeID > getNeighborsOf(const TNodeID nodeID) const
Return the list of all neighbors of "nodeID", by creating a list of their node IDs.
bool saveAsDot(std::ostream &o, const TGraphvizExportParams &p=TGraphvizExportParams()) const
Save the graph in a Graphviz (.dot files) text format; useful for quickly rendering the graph with "d...
#define MRPT_DECLARE_TTYPENAME(_TYPE)