Main MRPT website > C++ reference for MRPT 1.9.9
CDirectedTree.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef MRPT_DIRECTED_TREE_H
10 #define MRPT_DIRECTED_TREE_H
11 
12 #include <list>
13 #include <mrpt/graphs/TNodeID.h>
14 #include <sstream>
15 
16 namespace mrpt
17 {
18 namespace graphs
19 {
20 /** A special kind of graph in the form of a tree with directed edges and
21  *optional edge annotations of templatized type "TYPE_EDGES".
22  * The tree is represented by means of:
23  * - \a root: The ID of the root node.
24  * - \a edges_to_children: A map from node ID to all the edges to its
25  *children.
26  *
27  * Note that nodes are *not* explicitly listed anywhere: their existence is
28  *only inferred from their ID numbers in the list
29  * of edges in the \a edges_to_children data structure. If you want to include
30  *information for each node, derive from this class
31  * and create a separate container for that data.
32  *
33  * This class is less general than CDirectedGraph but more efficient to
34  *traverse (see \a visitDepthFirst and \a visitBreadthFirst).
35  *
36  * If annotations in edges are not required, you can leave TYPE_EDGES to its
37  *default type "uint8_t".
38  *
39  * Example of insertion of a new edge:
40  * \code
41  * using my_tree_t = CDirectedTree<edge_t> ;
42  * my_tree_t tree;
43  * TNodeID id_root = XXX;
44  * TNodeID id_child = XXX;
45  * my_tree_t::TListEdges & edges_of_root = tree.edges_to_children[id_root];
46  * edges_of_root.push_back( my_tree_t::TEdgeInfo(id_child,false, edge_t(...) )
47  *);
48  * \endcode
49  *
50  * \sa CDirectedGraph, CDijkstra, mrpt::graphs::CNetworkOfPoses
51  * \ingroup mrpt_graphs_grp
52  */
53 template <class TYPE_EDGES = uint8_t>
55 {
56  public:
57  struct TEdgeInfo
58  {
59  /** The ID of the child node. */
61  /** True if edge direction is child->parent, false if it's
62  * parent->child. */
63  bool reverse;
64  /** User data for this edge. */
65  TYPE_EDGES data;
66 
67  /** Edge constructor from data */
69  TNodeID child_id_, bool direction_child_to_parent = false,
70  const TYPE_EDGES& edge_data = TYPE_EDGES())
71  : id(child_id_), reverse(direction_child_to_parent), data(edge_data)
72  {
73  }
74  };
75 
76  using TListEdges = std::list<TEdgeInfo>;
77  using TMapNode2ListEdges = std::map<TNodeID, TListEdges>;
78 
79  /** @name Data
80  @{ */
81  /** The root of the tree */
83  /** The edges of each node */
85  /** @} */
86 
87  /** @name Utilities
88  @{ */
89 
90  /** Empty all edge data and set "root" to INVALID_NODEID */
91  void clear()
92  {
93  edges_to_children.clear();
95  }
96 
97  /** Virtual base class for user-defined visitors */
98  struct Visitor
99  {
101 
102  /** Virtual method to be implemented by the user and which will be
103  * called during the visit to a graph with visitDepthFirst or
104  * visitBreadthFirst
105  * Specifically, the method will be called once for each <b>edge</b>
106  * in the tree.
107  * \param parent [IN] The ID of the parent node.
108  * \param edge_to_child [IN] The edge information from the parent to
109  * "edge_to_child.id"
110  * \param depth_level [IN] The "depth level" of the child node
111  * "edge_to_child.id" (root node is at 0, its children are at 1, etc.).
112  */
113  virtual void OnVisitNode(
114  const TNodeID parent,
115  const typename tree_t::TEdgeInfo& edge_to_child,
116  const size_t depth_level) = 0;
117  };
118 
119  /** Depth-first visit of all children nodes of a given root (itself excluded
120  * from the visit), invoking a user-provided function for each node/edge.
121  * \sa visitBreadthFirst */
123  const TNodeID vroot, Visitor& user_visitor,
124  const size_t root_depth_level = 0) const
125  {
126  const size_t next_depth_level = root_depth_level + 1;
127  typename TMapNode2ListEdges::const_iterator itChildren =
128  edges_to_children.find(vroot);
129  if (itChildren == edges_to_children.end()) return; // No children
130  const TListEdges& children = itChildren->second;
131  for (typename TListEdges::const_iterator itEdge = children.begin();
132  itEdge != children.end(); ++itEdge)
133  {
134  user_visitor.OnVisitNode(vroot, *itEdge, next_depth_level);
136  itEdge->id, user_visitor,
137  next_depth_level); // Recursive depth-first call.
138  }
139  }
140 
141  /** Breadth-first visit of all children nodes of a given root (itself
142  * excluded from the visit), invoking a user-provided function for each
143  * node/edge. \sa visitDepthFirst */
145  const TNodeID vroot, Visitor& user_visitor,
146  const size_t root_depth_level = 0) const
147  {
148  const size_t next_depth_level = root_depth_level + 1;
149  typename TMapNode2ListEdges::const_iterator itChildren =
150  edges_to_children.find(vroot);
151  if (itChildren == edges_to_children.end()) return; // No children
152  const TListEdges& children = itChildren->second;
153  for (typename TListEdges::const_iterator itEdge = children.begin();
154  itEdge != children.end(); ++itEdge)
155  user_visitor.OnVisitNode(vroot, *itEdge, next_depth_level);
156  for (typename TListEdges::const_iterator itEdge = children.begin();
157  itEdge != children.end(); ++itEdge)
159  itEdge->id, user_visitor,
160  next_depth_level); // Recursive breath-first call.
161  }
162 
163  /** Return a text representation of the tree spanned in a depth-first view,
164  * as in this example:
165  * \code
166  * 0
167  * -> 1
168  * -> 2
169  * -> 4
170  * -> 5
171  * -> 3
172  * \endcode
173  */
175  {
176  std::stringstream s;
177  struct CMyVisitor
178  : public mrpt::graphs::CDirectedTree<TYPE_EDGES>::Visitor
179  {
180  std::stringstream& m_s;
181  CMyVisitor(std::stringstream& s) : m_s(s) {}
182  virtual void OnVisitNode(
183  const TNodeID parent,
184  const typename mrpt::graphs::CDirectedTree<
185  TYPE_EDGES>::Visitor::tree_t::TEdgeInfo& edge_to_child,
186  const size_t depth_level) override
187  {
188  m_s << std::string(depth_level * 5, ' ')
189  << (edge_to_child.reverse ? "<-" : "->") //;
190  << edge_to_child.id << std::endl;
191  }
192  };
193  CMyVisitor myVisitor(s);
194  s << root << std::endl;
195  visitDepthFirst(root, myVisitor);
196  return s.str();
197  }
198 };
199 
200 /** @} */
201 } // namespace graphs
202 } // namespace mrpt
203 #endif
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
s
GLdouble s
Definition: glext.h:3676
mrpt::graphs::CDirectedTree::Visitor
Virtual base class for user-defined visitors.
Definition: CDirectedTree.h:98
mrpt::graphs::CDirectedTree::visitDepthFirst
void visitDepthFirst(const TNodeID vroot, Visitor &user_visitor, const size_t root_depth_level=0) const
Depth-first visit of all children nodes of a given root (itself excluded from the visit),...
Definition: CDirectedTree.h:122
mrpt::graphs::TNodeID
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
Definition: TNodeID.h:17
mrpt::graphs::CDirectedTree::TEdgeInfo::reverse
bool reverse
True if edge direction is child->parent, false if it's parent->child.
Definition: CDirectedTree.h:63
mrpt::graphs::CDirectedTree::TEdgeInfo::TEdgeInfo
TEdgeInfo(TNodeID child_id_, bool direction_child_to_parent=false, const TYPE_EDGES &edge_data=TYPE_EDGES())
Edge constructor from data.
Definition: CDirectedTree.h:68
mrpt::graphs::CDirectedTree::TEdgeInfo
Definition: CDirectedTree.h:57
mrpt::graphs::CDirectedTree::TEdgeInfo::id
TNodeID id
The ID of the child node.
Definition: CDirectedTree.h:60
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::graphs::CDirectedTree::getAsTextDescription
std::string getAsTextDescription() const
Return a text representation of the tree spanned in a depth-first view, as in this example:
Definition: CDirectedTree.h:174
mrpt::graphs::CDirectedTree::clear
void clear()
Empty all edge data and set "root" to INVALID_NODEID.
Definition: CDirectedTree.h:91
mrpt::graphs::CDirectedTree::visitBreadthFirst
void visitBreadthFirst(const TNodeID vroot, Visitor &user_visitor, const size_t root_depth_level=0) const
Breadth-first visit of all children nodes of a given root (itself excluded from the visit),...
Definition: CDirectedTree.h:144
mrpt::graphs::CDirectedTree
A special kind of graph in the form of a tree with directed edges and optional edge annotations of te...
Definition: CDirectedTree.h:54
mrpt::graphs::CDirectedTree::Visitor::OnVisitNode
virtual void OnVisitNode(const TNodeID parent, const typename tree_t::TEdgeInfo &edge_to_child, const size_t depth_level)=0
Virtual method to be implemented by the user and which will be called during the visit to a graph wit...
mrpt::graphs::CDirectedTree::TEdgeInfo::data
TYPE_EDGES data
User data for this edge.
Definition: CDirectedTree.h:65
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
id
GLuint id
Definition: glext.h:3909
mrpt::graphs::CDirectedTree::edges_to_children
TMapNode2ListEdges edges_to_children
The edges of each node.
Definition: CDirectedTree.h:84
mrpt::graphs::CDirectedTree< EDGE_TYPE >::TMapNode2ListEdges
std::map< TNodeID, TListEdges > TMapNode2ListEdges
Definition: CDirectedTree.h:77
mrpt::graphs::CDirectedTree::root
TNodeID root
The root of the tree.
Definition: CDirectedTree.h:82
TNodeID.h
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::graphs::CDirectedTree< EDGE_TYPE >::TListEdges
std::list< TEdgeInfo > TListEdges
Definition: CDirectedTree.h:76
INVALID_NODEID
#define INVALID_NODEID
Definition: TNodeID.h:20



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