71 struct edge_t :
public TYPE_EDGES,
public EDGE_ANNOTATIONS
75 template <
typename... Args>
76 inline edge_t(Args&&...
a) : TYPE_EDGES(
std::forward<Args>(
a)...)
91 using iterator =
typename edges_map_t::iterator;
125 alignas(MRPT_MAX_STATIC_ALIGN_BYTES)
126 typename edges_map_t::value_type entry(
127 std::make_pair(from_nodeID, to_nodeID), edge_value);
137 alignas(MRPT_MAX_STATIC_ALIGN_BYTES)
138 typename edges_map_t::value_type entry(
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));
217 for (
auto it =
edges.begin(); 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;
238 for (
typename edges_map_t::const_iterator it =
edges.begin();
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 253 for (
typename edges_map_t::const_iterator it =
edges.begin();
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();
284 for (
auto it =
edges.begin(); it !=
edges.end(); ++it)
286 outAdjacency[it->first.first].insert(it->first.second);
287 outAdjacency[it->first.second].insert(it->first.first);
295 template <
class MAP_NODEID_SET_NODEIDS,
class SET_NODEIDS>
297 MAP_NODEID_SET_NODEIDS& outAdjacency,
298 const SET_NODEIDS& onlyForTheseNodes)
const 300 outAdjacency.clear();
301 const typename SET_NODEIDS::const_iterator setEnd =
302 onlyForTheseNodes.end();
303 for (
typename edges_map_t::const_iterator it =
edges.begin();
304 it !=
edges.end(); ++it)
306 if (onlyForTheseNodes.find(it->first.first) == setEnd ||
307 onlyForTheseNodes.find(it->first.second) == setEnd)
309 outAdjacency[it->first.first].insert(it->first.second);
310 outAdjacency[it->first.second].insert(it->first.first);
326 o <<
"digraph G {\n";
329 const TNodeID id1 = it->first.first;
330 const TNodeID id2 = it->first.second;
332 if (!
p.node_names.empty())
334 auto itNam1 =
p.node_names.find(id1);
335 if (itNam1 !=
p.node_names.end()) s1 = itNam1->second;
336 auto itNam2 =
p.node_names.find(id2);
337 if (itNam2 !=
p.node_names.end()) s2 = itNam2->second;
341 if (
p.node_props.empty())
343 auto itP1 =
p.node_props.find(id1);
344 if (itP1 !=
p.node_props.end())
345 o <<
"\"" << s1 <<
"\"" 346 <<
" [" << itP1->second <<
"];\n";
347 auto itP2 =
p.node_props.find(id2);
348 if (itP2 !=
p.node_props.end())
349 o <<
"\"" << s2 <<
"\"" 350 <<
" [" << itP2->second <<
"];\n";
352 o <<
" \"" << s1 <<
"\" -> \"" << s2 <<
"\"";
353 if (
p.mark_edges_as_not_constraint) o <<
" [constraint=false]";
356 return !((o <<
"}\n").fail());
364 std::ofstream f(fileName.c_str());
365 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.
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
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)...
std::multimap< TPairNodeIDs, edge_t > edges_map_t
The type of the member edges.
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
TGraphvizExportParams()=default
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
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...