Go to the documentation of this file.
9 #ifndef MRPT_DIRECTEDGRAPH_H
10 #define MRPT_DIRECTEDGRAPH_H
66 template <
class TYPE_EDGES,
73 struct edge_t :
public TYPE_EDGES,
public EDGE_ANNOTATIONS
77 template <
typename... Args>
78 inline edge_t(Args&&...
a) : TYPE_EDGES(std::forward<Args>(
a)...)
127 std::make_pair(from_nodeID, to_nodeID), edge_value);
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);
177 if (it ==
edges.end())
180 "Edge %u->%u does not exist", (
unsigned)from_nodeID,
181 (
unsigned)to_nodeID);
192 return edges.equal_range(std::make_pair(from_nodeID, to_nodeID));
196 std::pair<const_iterator, const_iterator>
getEdges(
199 return edges.equal_range(std::make_pair(from_nodeID, to_nodeID));
207 edges.erase(std::make_pair(from_nodeID, to_nodeID));
217 it !=
edges.end(); ++it)
219 lstNode_IDs.insert(it->first.first);
220 lstNode_IDs.insert(it->first.second);
228 std::set<TNodeID> lst;
237 std::set<TNodeID> aux;
239 it !=
edges.end(); ++it)
241 aux.insert(it->first.first);
242 aux.insert(it->first.second);
250 const TNodeID nodeID, std::set<TNodeID>& neighborIDs)
const
254 it !=
edges.end(); ++it)
256 if (it->first.first == nodeID)
257 neighborIDs.insert(it->first.second);
258 else if (it->first.second == nodeID)
259 neighborIDs.insert(it->first.first);
266 std::set<TNodeID> neighborIDs;
280 template <
class MAP_NODEID_SET_NODEIDS>
283 outAdjacency.clear();
285 it !=
edges.end(); ++it)
287 outAdjacency[it->first.first].insert(it->first.second);
288 outAdjacency[it->first.second].insert(it->first.first);
296 template <
class MAP_NODEID_SET_NODEIDS,
class SET_NODEIDS>
298 MAP_NODEID_SET_NODEIDS& outAdjacency,
299 const SET_NODEIDS& onlyForTheseNodes)
const
301 outAdjacency.clear();
303 onlyForTheseNodes.end();
305 it !=
edges.end(); ++it)
307 if (onlyForTheseNodes.find(it->first.first) == setEnd ||
308 onlyForTheseNodes.find(it->first.second) == setEnd)
310 outAdjacency[it->first.first].insert(it->first.second);
311 outAdjacency[it->first.second].insert(it->first.first);
327 o <<
"digraph G {\n";
330 const TNodeID id1 = it->first.first;
331 const TNodeID id2 = it->first.second;
333 if (!
p.node_names.empty())
336 p.node_names.find(id1);
337 if (itNam1 !=
p.node_names.end()) s1 = itNam1->second;
339 p.node_names.find(id2);
340 if (itNam2 !=
p.node_names.end()) s2 = itNam2->second;
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;
A directed graph with the argument of the template specifying the type of the annotations in the edge...
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...
const Scalar * const_iterator
edge_t & getEdge(TNodeID from_nodeID, TNodeID to_nodeID)
Return a reference to the content of a given edge.
constexpr static auto getClassName()
void insertEdge(TNodeID from_nodeID, TNodeID to_nodeID, const edge_t &edge_value)
Insert an edge (from -> to) with the given edge value.
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.
std::set< TNodeID > getAllNodes() const
Less efficient way to get all nodes that returns a copy of the set object.
bool edgeExists(TNodeID from_nodeID, TNodeID to_nodeID) const
Test if the given directed edge exists.
const edge_t & getEdge(TNodeID from_nodeID, TNodeID to_nodeID) const
Return a reference to the content of a given edge.
std::set< TNodeID > getNeighborsOf(const TNodeID nodeID) const
Return the list of all neighbors of "nodeID", by creating a list of their node IDs.
const_iterator rbegin() const
The type of each global pose in nodes: an extension of the TYPE_EDGES pose with any optional user-def...
mrpt::aligned_std_multimap< TPairNodeIDs, edge_t > edges_map_t
The type of the member edges.
std::string std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
CDirectedGraph()
Default constructor.
GLsizei GLsizei GLuint * obj
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
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.
#define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME)
Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration body.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void clearEdges()
Erase all edges.
edges_map_t edges
The public member with the directed edges in the graph.
const_iterator rend() const
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...
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.
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...
bool mark_edges_as_not_constraint
If true (default=false), an "[constraint=false]" will be added to all edges (see Graphviz docs).
const_iterator begin() const
size_t edgeCount() const
The number of edges in the graph.
std::multimap< KEY, VALUE, std::less< KEY >, mrpt::aligned_allocator_cpp11< std::pair< const KEY, VALUE > >> aligned_std_multimap
CDirectedGraph(const edges_map_t &obj)
Copy constructor from a multimap<pair< >, >
size_t countDifferentNodesInEdges() const
Count how many different node IDs appear in the graph edges.
bool saveAsDot(const std::string &fileName, const TGraphvizExportParams &p=TGraphvizExportParams()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const_iterator end() const
typename edges_map_t::iterator iterator
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...
Used in mrpt::graphs export functions to .dot files.
void eraseEdge(TNodeID from_nodeID, TNodeID to_nodeID)
Erase all edges between the given nodes (it has no effect if no edge existed)
GLsizei const GLchar ** string
std::map< TNodeID, std::string > node_props
If provided, an extra line will be added setting Graphviz properties for each node,...
typename edges_map_t::const_iterator const_iterator
typename edges_map_t::const_reverse_iterator const_reverse_iterator
#define MRPT_MAX_ALIGN_BYTES
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...
typename edges_map_t::reverse_iterator reverse_iterator
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...
GLubyte GLubyte GLubyte a
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST | |