9 #ifndef MRPT_DIRECTEDGRAPH_H 10 #define MRPT_DIRECTEDGRAPH_H 54 template<
class TYPE_EDGES,
class EDGE_ANNOTATIONS = detail::edge_annotations_empty>
59 struct edge_t :
public TYPE_EDGES,
public EDGE_ANNOTATIONS
63 template <
typename ARG1>
inline edge_t (
const ARG1 &
a1) : TYPE_EDGES(
a1) { }
64 template <
typename ARG1,
typename ARG2>
inline edge_t (
const ARG1 &
a1,
const ARG2 &
a2) : TYPE_EDGES(
a1,
a2) { }
103 std::make_pair(from_nodeID,to_nodeID),
112 std::make_pair(from_nodeID,to_nodeID),
119 return edges.find(std::make_pair(from_nodeID,to_nodeID))!=
edges.end();
129 iterator it =
edges.find(std::make_pair(from_nodeID,to_nodeID));
132 else return it->second;
145 else return it->second;
150 return edges.equal_range(std::make_pair(from_nodeID,to_nodeID));
154 return edges.equal_range(std::make_pair(from_nodeID,to_nodeID));
160 edges.erase(std::make_pair(from_nodeID,to_nodeID));
170 lstNode_IDs.insert(it->first.first);
171 lstNode_IDs.insert(it->first.second);
182 std::set<TNodeID> aux;
185 aux.insert(it->first.first);
186 aux.insert(it->first.second);
197 if (it->first.first==nodeID)
198 neighborIDs.insert(it->first.second);
199 else if (it->first.second==nodeID)
200 neighborIDs.insert(it->first.first);
205 std::set<TNodeID> neighborIDs;
217 template <
class MAP_NODEID_SET_NODEIDS>
220 outAdjacency.clear();
223 outAdjacency[it->first.first].insert(it->first.second);
224 outAdjacency[it->first.second].insert(it->first.first);
230 template <
class MAP_NODEID_SET_NODEIDS,
class SET_NODEIDS>
231 void getAdjacencyMatrix( MAP_NODEID_SET_NODEIDS &outAdjacency,
const SET_NODEIDS &onlyForTheseNodes )
const 233 outAdjacency.clear();
237 if (onlyForTheseNodes.find(it->first.first)==setEnd || onlyForTheseNodes.find(it->first.second)==setEnd)
239 outAdjacency[it->first.first].insert(it->first.second);
240 outAdjacency[it->first.second].insert(it->first.first);
254 o <<
"digraph G {\n";
257 const TNodeID id1 = it->first.first;
258 const TNodeID id2 = it->first.second;
260 if (!
p.node_names.empty())
263 if (itNam1!=
p.node_names.end()) s1 =itNam1->second;
265 if (itNam2!=
p.node_names.end()) s2 =itNam2->second;
267 if (s1.empty()) s1 =
mrpt::format(
"%u",static_cast<unsigned int>(id1));
268 if (s2.empty()) s2 =
mrpt::format(
"%u",static_cast<unsigned int>(id2));
269 if (
p.node_props.empty())
272 if (itP1!=
p.node_props.end()) o <<
"\""<<s1<<
"\""<<
" [" << itP1->second <<
"];\n";
274 if (itP2!=
p.node_props.end()) o <<
"\""<<s2<<
"\""<<
" [" << itP2->second <<
"];\n";
276 o <<
" \"" << s1 <<
"\" -> \"" << s2 <<
"\"";
277 if (
p.mark_edges_as_not_constraint) o <<
" [constraint=false]";
280 return !((o <<
"}\n").fail());
286 std::ofstream f(fileName.c_str());
287 if (!f.is_open())
return false;
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
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::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...
std::set< TNodeID > getAllNodes() const
Less efficient way to get all nodes that returns a copy of the set object.
const_iterator end() const
std::map< TNodeID, std::string > node_props
If provided, an extra line will be added setting Graphviz properties for each node, e.g. to set a node position, use the string "pos = \"x,y"".
const Scalar * const_iterator
GLsizei GLsizei GLuint * obj
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
TYPE_EDGES edge_underlying_t
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 BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::pair< TNodeID, TNodeID > TPairNodeIDs
A pair of node IDs.
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)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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)...
std::multimap< TYPE1, TYPE2, std::less< TYPE1 >, Eigen::aligned_allocator< std::pair< const TYPE1, TYPE2 > > > multimap_t
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...
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.
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...
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...
#define MRPT_DECLARE_TTYPENAME(_TYPE)