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)...)
128 std::make_pair(from_nodeID, to_nodeID), edge_value);
139 std::make_pair(from_nodeID, to_nodeID), edge_value);
146 return edges.find(std::make_pair(from_nodeID, to_nodeID)) !=
158 iterator it =
edges.find(std::make_pair(from_nodeID, to_nodeID));
159 if (it ==
edges.end())
162 "Edge %u->%u does not exist", (
unsigned)from_nodeID,
163 (
unsigned)to_nodeID);
178 if (it ==
edges.end())
181 "Edge %u->%u does not exist", (
unsigned)from_nodeID,
182 (
unsigned)to_nodeID);
193 return edges.equal_range(std::make_pair(from_nodeID, to_nodeID));
197 std::pair<const_iterator, const_iterator>
getEdges(
200 return edges.equal_range(std::make_pair(from_nodeID, to_nodeID));
208 edges.erase(std::make_pair(from_nodeID, to_nodeID));
218 it !=
edges.end(); ++it)
220 lstNode_IDs.insert(it->first.first);
221 lstNode_IDs.insert(it->first.second);
229 std::set<TNodeID> lst;
238 std::set<TNodeID> aux;
240 it !=
edges.end(); ++it)
242 aux.insert(it->first.first);
243 aux.insert(it->first.second);
251 const TNodeID nodeID, std::set<TNodeID>& neighborIDs)
const 255 it !=
edges.end(); ++it)
257 if (it->first.first == nodeID)
258 neighborIDs.insert(it->first.second);
259 else if (it->first.second == nodeID)
260 neighborIDs.insert(it->first.first);
267 std::set<TNodeID> neighborIDs;
281 template <
class MAP_NODEID_SET_NODEIDS>
284 outAdjacency.clear();
286 it !=
edges.end(); ++it)
288 outAdjacency[it->first.first].insert(it->first.second);
289 outAdjacency[it->first.second].insert(it->first.first);
297 template <
class MAP_NODEID_SET_NODEIDS,
class SET_NODEIDS>
299 MAP_NODEID_SET_NODEIDS& outAdjacency,
300 const SET_NODEIDS& onlyForTheseNodes)
const 302 outAdjacency.clear();
304 onlyForTheseNodes.end();
306 it !=
edges.end(); ++it)
308 if (onlyForTheseNodes.find(it->first.first) == setEnd ||
309 onlyForTheseNodes.find(it->first.second) == setEnd)
311 outAdjacency[it->first.first].insert(it->first.second);
312 outAdjacency[it->first.second].insert(it->first.first);
328 o <<
"digraph G {\n";
331 const TNodeID id1 = it->first.first;
332 const TNodeID id2 = it->first.second;
334 if (!
p.node_names.empty())
337 p.node_names.find(id1);
338 if (itNam1 !=
p.node_names.end()) s1 = itNam1->second;
340 p.node_names.find(id2);
341 if (itNam2 !=
p.node_names.end()) s2 = itNam2->second;
345 if (
p.node_props.empty())
348 p.node_props.find(id1);
349 if (itP1 !=
p.node_props.end())
350 o <<
"\"" << s1 <<
"\"" 351 <<
" [" << itP1->second <<
"];\n";
353 p.node_props.find(id2);
354 if (itP2 !=
p.node_props.end())
355 o <<
"\"" << s2 <<
"\"" 356 <<
" [" << itP2->second <<
"];\n";
358 o <<
" \"" << s1 <<
"\" -> \"" << s2 <<
"\"";
359 if (
p.mark_edges_as_not_constraint) o <<
" [constraint=false]";
362 return !((o <<
"}\n").fail());
370 std::ofstream f(fileName.c_str());
371 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...
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.
#define MRPT_MAX_ALIGN_BYTES
mrpt::aligned_std_multimap< TPairNodeIDs, edge_t > edges_map_t
The type of the member edges.
const_iterator rbegin() const
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.
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.
typename edges_map_t::const_reverse_iterator const_reverse_iterator
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
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
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< >, >
static constexpr auto getClassName()
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...
const_iterator rend() const
#define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME)
Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration body.
void insertEdge(TNodeID from_nodeID, TNodeID to_nodeID, const edge_t &edge_value)
Insert an edge (from -> to) with the given edge value.
bool saveAsDot(const std::string &fileName, const TGraphvizExportParams &p=TGraphvizExportParams()) const
CPOSE edge_underlying_t
Underlying type for edge_t = TYPE_EDGES + annotations.
GLsizei const GLchar ** string
typename edges_map_t::reverse_iterator reverse_iterator
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.
typename edges_map_t::const_iterator const_iterator
bool mark_edges_as_not_constraint
If true (default=false), an "[constraint=false]" will be added to all edges (see Graphviz docs)...
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
std::string std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
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...
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLubyte GLubyte GLubyte a
const Scalar * const_iterator
typename edges_map_t::iterator 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...