Main MRPT website > C++ reference
MRPT logo
CNetworkOfPoses.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-2015, 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 CONSTRAINED_POSE_NETWORK_H
10 #define CONSTRAINED_POSE_NETWORK_H
11 
12 /** \file The main class in this file is mrpt::poses::CNetworkOfPoses<>, a generic
13  basic template for predefined 2D/3D graphs of pose contraints.
14 */
15 
18 
22 
23 #include <mrpt/utils/traits_map.h>
25 
26 namespace mrpt
27 {
28  // Fwd decls:
29  namespace poses {
30  class CPose2D; class CPose3D;
31  class CPosePDFGaussian; class CPose3DPDFGaussian;
32  class CPosePDFGaussianInf; class CPose3DPDFGaussianInf;
33  }
34 
35  namespace graphs
36  {
37  /** Internal functions for MRPT */
38  namespace detail
39  {
40  template <class GRAPH_T> struct graph_ops;
41  /** An empty structure */
43  }
44 
45  /** A directed graph of pose constraints, with edges being the relative pose between pairs of nodes indentified by their numeric IDs (of type mrpt::utils::TNodeID).
46  * A link or edge between two nodes "i" and "j", that is, the pose \f$ p_{ij} \f$, holds the relative position of "j" with respect to "i".
47  * These poses are stored in the edges in the format specified by the template argument CPOSE. Users should employ the following derived classes
48  * depending on the desired representation of edges:
49  * - mrpt::graphs::CNetworkOfPoses2D : 2D edges as a simple CPose2D (x y phi)
50  * - mrpt::graphs::CNetworkOfPoses3D : 3D edges as a simple mrpt::poses::CPose3D (x y z yaw pitch roll)
51  * - mrpt::graphs::CNetworkOfPoses2DInf : 2D edges as a Gaussian PDF with information matrix ( CPosePDFGaussianInf )
52  * - mrpt::graphs::CNetworkOfPoses3DInf : 3D edges as a Gaussian PDF with information matrix ( CPose3DPDFGaussianInf )
53  * - mrpt::graphs::CNetworkOfPoses2DCov : 2D edges as a Gaussian PDF with covariance matrix ( CPosePDFGaussian ). It's more efficient to use the information matrix version instead!
54  * - mrpt::graphs::CNetworkOfPoses3DCov : 3D edges as a Gaussian PDF with covariance matrix ( CPose3DPDFGaussian ). It's more efficient to use the information matrix version instead!
55  *
56  * Two main members store all the information in this class:
57  * - \a edges (in the base class mrpt::graphs::CDirectedGraph::edges): A map from pairs of node ID -> pose constraints.
58  * - \a nodes : A map from node ID -> estimated pose of that node (actually, read below on the template argument MAPS_IMPLEMENTATION).
59  *
60  * Graphs can be loaded and saved to text file in the format used by TORO & HoG-man (more on the format <a href="http://www.mrpt.org/Robotics_file_formats" >here</a> ),
61  * using \a loadFromTextFile and \a saveToTextFile.
62  *
63  * This class is the base for representing networks of poses, which are the main data type of a series
64  * of SLAM algorithms implemented in the library mrpt-slam, in the namespace mrpt::graphslam.
65  *
66  * For tools to visualize graphs as 2D/3D plots, see the namespace mrpt::opengl::graph_tools in the library mrpt-opengl.
67  *
68  * The template arguments are:
69  * - CPOSE: The type of the edges, which hold a relative pose (2D/3D, just a value or a Gaussian, etc.)
70  * - MAPS_IMPLEMENTATION: Can be either mrpt::utils::map_traits_stdmap or mrpt::utils::map_traits_map_as_vector. Determines the type of the list of global poses (member \a nodes).
71  *
72  * \sa mrpt::graphslam
73  * \ingroup mrpt_graphs_grp
74  */
75  template<
76  class CPOSE, // Type of edges
77  class MAPS_IMPLEMENTATION = mrpt::utils::map_traits_stdmap, // Use std::map<> vs. std::vector<>
78  class NODE_ANNOTATIONS = mrpt::graphs::detail::node_annotations_empty,
79  class EDGE_ANNOTATIONS = mrpt::graphs::detail::edge_annotations_empty
80  >
81  class CNetworkOfPoses : public mrpt::graphs::CDirectedGraph< CPOSE, EDGE_ANNOTATIONS >
82  {
83  public:
84  /** @name Typedef's
85  @{ */
86  typedef mrpt::graphs::CDirectedGraph<CPOSE,EDGE_ANNOTATIONS> BASE; //!< The base class "CDirectedGraph<CPOSE,EDGE_ANNOTATIONS>" */
88 
89  typedef CPOSE constraint_t; //!< The type of PDF poses in the contraints (edges) (=CPOSE template argument)
90  typedef NODE_ANNOTATIONS node_annotations_t; //!< The extra annotations in nodes, apart from a \a constraint_no_pdf_t
91  typedef EDGE_ANNOTATIONS edge_annotations_t; //!< The extra annotations in edges, apart from a \a constraint_t
92 
93  typedef MAPS_IMPLEMENTATION maps_implementation_t; //!< The type of map's implementation (=MAPS_IMPLEMENTATION template argument)
94  typedef typename CPOSE::type_value constraint_no_pdf_t; //!< The type of edges or their means if they are PDFs (that is, a simple "edge" value)
95 
96  /** The type of each global pose in \a nodes: an extension of the \a constraint_no_pdf_t pose with any optional user-defined data */
97  struct global_pose_t : public constraint_no_pdf_t, public NODE_ANNOTATIONS
98  {
99  // Replicate possible constructors:
100  inline global_pose_t() : constraint_no_pdf_t() { }
101  template <typename ARG1> inline global_pose_t(const ARG1 &a1) : constraint_no_pdf_t(a1) { }
102  template <typename ARG1,typename ARG2> inline global_pose_t(const ARG1 &a1,const ARG2 &a2) : constraint_no_pdf_t(a1,a2) { }
103  };
104 
105  /** A map from pose IDs to their global coordinates estimates, with uncertainty */
106  typedef typename MAPS_IMPLEMENTATION::template map<mrpt::utils::TNodeID,CPOSE> global_poses_pdf_t;
107 
108  /** A map from pose IDs to their global coordinates estimates, without uncertainty (the "most-likely value") */
109  typedef typename MAPS_IMPLEMENTATION::template map<mrpt::utils::TNodeID,global_pose_t> global_poses_t;
110 
111  /** @} */
112 
113 
114  /** @name Data members
115  @{ */
116 
117  /** The nodes (vertices) of the graph, with their estimated "global" (with respect to \a root) position, without an associated covariance.
118  * \sa dijkstra_nodes_estimate
119  */
120  global_poses_t nodes;
121 
122  /** The ID of the node that is the origin of coordinates, used as reference by all coordinates in \nodes. By default, root is the ID "0". */
124 
125  /** False (default) if an edge i->j stores the normal relative pose of j as seen from i: \f$ \Delta_i^j = j \ominus i \f$
126  * True if an edge i->j stores the inverse relateive pose, that is, i as seen from j: \f$ \Delta_i^j = i \ominus j \f$
127  */
129 
130  /** @} */
131 
132 
133  /** @name I/O file methods
134  @{ */
135 
136  /** Saves to a text file in the format used by TORO & HoG-man (more on the format <a href="http://www.mrpt.org/Robotics_file_formats" >here</a> )
137  * For 2D graphs only VERTEX2 & EDGE2 entries will be saved, and VERTEX3 & EDGE3 entries for 3D graphs.
138  * Note that EQUIV entries will not be saved, but instead several EDGEs will be stored between the same node IDs.
139  * \sa saveToBinaryFile, loadFromTextFile
140  * \exception On any error
141  */
142  inline void saveToTextFile( const std::string &fileName ) const {
144  }
145 
146  /** Loads from a text file in the format used by TORO & HoG-man (more on the format <a href="http://www.mrpt.org/Robotics_file_formats" >here</a> )
147  * Recognized line entries are: VERTEX2, VERTEX3, EDGE2, EDGE3, EQUIV.
148  * If an unknown entry is found, a warning is dumped to std::cerr (only once for each unknown keyword).
149  * An exception will be raised if trying to load a 3D graph into a 2D class (in the opposite case, missing 3D data will default to zero).
150  * \param fileName The file to load.
151  * \param collapse_dup_edges If true, \a collapseDuplicatedEdges will be called automatically after loading (note that this operation may take significant time for very large graphs).
152  * \sa loadFromBinaryFile, saveToTextFile
153  * \exception On any error, as a malformed line or loading a 3D graph in a 2D graph.
154  */
155  inline void loadFromTextFile( const std::string &fileName, bool collapse_dup_edges = true ) {
157  if (collapse_dup_edges) this->collapseDuplicatedEdges();
158  }
159 
160  /** @} */
161 
162  /** @name Utility methods
163  @{ */
164 
165  /** Spanning tree computation of a simple estimation of the global coordinates of each node just from the information in all edges, sorted in a Dijkstra tree based on the current "root" node.
166  * Note that "global" coordinates are with respect to the node with the ID specified in \a root.
167  * \note This method takes into account the value of \a edges_store_inverse_poses
168  * \sa node, root
169  */
171 
172  /** Look for duplicated edges (even in opposite directions) between all pairs of nodes and fuse them.
173  * Upon return, only one edge remains between each pair of nodes with the mean & covariance (or information matrix) corresponding to the Bayesian fusion of all the Gaussians.
174  * \return Overall number of removed edges.
175  */
177 
178  /** Computes the overall square error from all the pose constraints (edges) with respect to the global poses in \nodes
179  * If \a ignoreCovariances is false, the squared Mahalanobis distance will be computed instead of the straight square error.
180  * \sa getEdgeSquareError
181  * \exception std::exception On global poses not in \a nodes
182  */
183  double getGlobalSquareError(bool ignoreCovariances = true) const {
184  double sqErr=0;
185  const typename BASE::edges_map_t::const_iterator last_it=BASE::edges.end();
186  for (typename BASE::edges_map_t::const_iterator itEdge=BASE::edges.begin();itEdge!=last_it;++itEdge)
187  sqErr+=detail::graph_ops<self_t>::graph_edge_sqerror(this,itEdge,ignoreCovariances);
188  return sqErr;
189  }
190 
191  /** Computes the square error of one pose constraints (edge) with respect to the global poses in \nodes
192  * If \a ignoreCovariances is false, the squared Mahalanobis distance will be computed instead of the straight square error.
193  * \exception std::exception On global poses not in \a nodes
194  */
195  inline double getEdgeSquareError(const typename BASE::edges_map_t::const_iterator &itEdge, bool ignoreCovariances = true) const { return detail::graph_ops<self_t>::graph_edge_sqerror(this,itEdge,ignoreCovariances); }
196 
197  /** Computes the square error of one pose constraints (edge) with respect to the global poses in \nodes
198  * If \a ignoreCovariances is false, the squared Mahalanobis distance will be computed instead of the straight square error.
199  * \exception std::exception On edge not existing or global poses not in \a nodes
200  */
201  double getEdgeSquareError(const mrpt::utils::TNodeID from_id, const mrpt::utils::TNodeID to_id, bool ignoreCovariances = true ) const
202  {
203  const typename BASE::edges_map_t::const_iterator itEdge = BASE::edges.find( std::make_pair(from_id,to_id) );
204  ASSERTMSG_(itEdge!=BASE::edges.end(),format("Request for edge %u->%u that doesn't exist in graph.",static_cast<unsigned int>(from_id),static_cast<unsigned int>(to_id)));
205  return getEdgeSquareError(itEdge,ignoreCovariances);
206  }
207 
208  /** Empty all edges, nodes and set root to ID 0. */
209  inline void clear() {
210  BASE::edges.clear();
211  nodes.clear();
212  root = 0;
213  edges_store_inverse_poses = false;
214  }
215 
216  /** Return number of nodes in the list \nodes of global coordinates (may be differente that all nodes appearing in edges)
217  * \sa mrpt::graphs::CDirectedGraph::countDifferentNodesInEdges
218  */
219  inline size_t nodeCount() const { return nodes.size(); }
220 
221  /** @} */
222 
223  /** @name Ctors & Dtors
224  @{ */
225 
226  /** Default constructor (just sets root to "0" and edges_store_inverse_poses to "false") */
227  inline CNetworkOfPoses() : root(0), edges_store_inverse_poses(false) { }
229  /** @} */
230  };
231 
232 
233  /** Binary serialization (write) operator "stream << graph" */
234  template <class CPOSE,class MAPS_IMPLEMENTATION,class NODE_ANNOTATIONS,class EDGE_ANNOTATIONS>
235  mrpt::utils::CStream & operator << (mrpt::utils::CStream&out, const CNetworkOfPoses<CPOSE,MAPS_IMPLEMENTATION,NODE_ANNOTATIONS,EDGE_ANNOTATIONS> &obj)
236  {
239  return out;
240  }
241 
242  /** Binary serialization (read) operator "stream >> graph" */
243  template <class CPOSE,class MAPS_IMPLEMENTATION,class NODE_ANNOTATIONS,class EDGE_ANNOTATIONS>
245  {
248  return in;
249  }
250 
251  /** \addtogroup mrpt_graphs_grp
252  @{ */
253 
254  typedef CNetworkOfPoses<mrpt::poses::CPose2D,mrpt::utils::map_traits_stdmap> CNetworkOfPoses2D; //!< The specialization of CNetworkOfPoses for poses of type CPose2D (not a PDF!), also implementing serialization.
255  typedef CNetworkOfPoses<mrpt::poses::CPose3D,mrpt::utils::map_traits_stdmap> CNetworkOfPoses3D; //!< The specialization of CNetworkOfPoses for poses of type mrpt::poses::CPose3D (not a PDF!), also implementing serialization.
256  typedef CNetworkOfPoses<mrpt::poses::CPosePDFGaussian,mrpt::utils::map_traits_stdmap> CNetworkOfPoses2DCov; //!< The specialization of CNetworkOfPoses for poses of type CPosePDFGaussian, also implementing serialization.
257  typedef CNetworkOfPoses<mrpt::poses::CPose3DPDFGaussian,mrpt::utils::map_traits_stdmap> CNetworkOfPoses3DCov; //!< The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussian, also implementing serialization.
258  typedef CNetworkOfPoses<mrpt::poses::CPosePDFGaussianInf,mrpt::utils::map_traits_stdmap> CNetworkOfPoses2DInf; //!< The specialization of CNetworkOfPoses for poses of type CPosePDFGaussianInf, also implementing serialization.
259  typedef CNetworkOfPoses<mrpt::poses::CPose3DPDFGaussianInf,mrpt::utils::map_traits_stdmap> CNetworkOfPoses3DInf; //!< The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussianInf, also implementing serialization.
260 
261  /** @} */ // end of grouping
262 
263  } // End of namespace
264 
265  // Specialization of TTypeName must occur in the same namespace:
266  namespace utils
267  {
268  // Extensions to mrpt::utils::TTypeName for matrices:
269  template<
270  class CPOSE,
271  class MAPS_IMPLEMENTATION,
272  class NODE_ANNOTATIONS,
273  class EDGE_ANNOTATIONS
274  >
275  struct TTypeName <mrpt::graphs::CNetworkOfPoses<CPOSE,MAPS_IMPLEMENTATION,NODE_ANNOTATIONS,EDGE_ANNOTATIONS> >
276  {
277  static std::string get()
278  {
279  return std::string("mrpt::graphs::CNetworkOfPoses<")
280  +TTypeName<CPOSE>::get() + std::string(",")
281  +TTypeName<MAPS_IMPLEMENTATION>::get() + std::string(",")
282  +TTypeName<NODE_ANNOTATIONS>::get() + std::string(",")
284  +std::string(">");
285  }
286  };
287 
288 
290  MRPT_DECLARE_TTYPENAME(mrpt::utils::map_traits_stdmap)
291  MRPT_DECLARE_TTYPENAME(mrpt::utils::map_traits_map_as_vector)
292 
293  }
294 
295 } // End of namespace
296 
297 
298 // Implementation of templates (in a separate file for clarity)
299 #include "CNetworkOfPoses_impl.h"
300 
301 #endif
A directed graph with the argument of the template specifying the type of the annotations in the edge...
MAPS_IMPLEMENTATION::template map< mrpt::utils::TNodeID, global_pose_t > global_poses_t
A map from pose IDs to their global coordinates estimates, without uncertainty (the "most-likely valu...
void dijkstra_nodes_estimate()
Spanning tree computation of a simple estimation of the global coordinates of each node just from the...
CNetworkOfPoses< CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS, EDGE_ANNOTATIONS > self_t
My own type.
NODE_ANNOTATIONS node_annotations_t
The extra annotations in nodes, apart from a constraint_no_pdf_t.
CNetworkOfPoses< mrpt::poses::CPose3DPDFGaussianInf, mrpt::utils::map_traits_stdmap > CNetworkOfPoses3DInf
The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussianInf, also implementing seri...
void clear()
Empty all edges, nodes and set root to ID 0.
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:47
size_t nodeCount() const
Return number of nodes in the list of global coordinates (may be differente that all nodes appearing...
size_t collapseDuplicatedEdges()
Look for duplicated edges (even in opposite directions) between all pairs of nodes and fuse them...
edges_map_t edges
The public member with the directed edges in the graph.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void loadFromTextFile(const std::string &fileName, bool collapse_dup_edges=true)
Loads from a text file in the format used by TORO & HoG-man (more on the format here ) Recognized lin...
void saveToTextFile(const std::string &fileName) const
Saves to a text file in the format used by TORO & HoG-man (more on the format here ) For 2D graphs on...
static void read_graph_of_poses_from_binary_file(graph_t *g, mrpt::utils::CStream &in)
double getEdgeSquareError(const typename BASE::edges_map_t::const_iterator &itEdge, bool ignoreCovariances=true) const
Computes the square error of one pose constraints (edge) with respect to the global poses in If igno...
static void graph_of_poses_dijkstra_init(graph_t *g)
A directed graph of pose constraints, with edges being the relative pose between pairs of nodes inden...
CNetworkOfPoses< mrpt::poses::CPosePDFGaussianInf, mrpt::utils::map_traits_stdmap > CNetworkOfPoses2DInf
The specialization of CNetworkOfPoses for poses of type CPosePDFGaussianInf, also implementing serial...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
static void save_graph_of_poses_to_binary_file(const graph_t *g, mrpt::utils::CStream &out)
CNetworkOfPoses()
Default constructor (just sets root to "0" and edges_store_inverse_poses to "false") ...
The type of each global pose in nodes: an extension of the constraint_no_pdf_t pose with any optional...
static std::string get()
Definition: TTypeName.h:49
CPOSE::type_value constraint_no_pdf_t
The type of edges or their means if they are PDFs (that is, a simple "edge" value) ...
static size_t graph_of_poses_collapse_dup_edges(graph_t *g)
static void save_graph_of_poses_to_text_file(const graph_t *g, const std::string &fil)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CPOSE constraint_t
The type of PDF poses in the contraints (edges) (=CPOSE template argument)
uint64_t TNodeID
The type for node IDs in graphs of different types.
MAPS_IMPLEMENTATION::template map< mrpt::utils::TNodeID, CPOSE > global_poses_pdf_t
A map from pose IDs to their global coordinates estimates, with uncertainty.
static double graph_edge_sqerror(const graph_t *g, const typename mrpt::graphs::CDirectedGraph< typename graph_t::constraint_t >::edges_map_t::const_iterator &itEdge, bool ignoreCovariances)
a helper struct with static template functions
mrpt::graphs::CDirectedGraph< CPOSE, EDGE_ANNOTATIONS > BASE
The base class "CDirectedGraph" */.
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CNetworkOfPoses< CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS, EDGE_ANNOTATIONS > &obj)
Binary serialization (read) operator "stream >> graph".
static void load_graph_of_poses_from_text_file(graph_t *g, const std::string &fil)
double getEdgeSquareError(const mrpt::utils::TNodeID from_id, const mrpt::utils::TNodeID to_id, bool ignoreCovariances=true) const
Computes the square error of one pose constraints (edge) with respect to the global poses in If igno...
CNetworkOfPoses< mrpt::poses::CPose3DPDFGaussian, mrpt::utils::map_traits_stdmap > CNetworkOfPoses3DCov
The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussian, also implementing seriali...
global_poses_t nodes
The nodes (vertices) of the graph, with their estimated "global" (with respect to root) position...
global_pose_t(const ARG1 &a1, const ARG2 &a2)
mrpt::utils::TNodeID root
The ID of the node that is the origin of coordinates, used as reference by all coordinates in ...
Traits for using a std::map<> (sparse representation)
Definition: traits_map.h:25
bool edges_store_inverse_poses
False (default) if an edge i->j stores the normal relative pose of j as seen from i: True if an edge...
EDGE_ANNOTATIONS edge_annotations_t
The extra annotations in edges, apart from a constraint_t.
CNetworkOfPoses< mrpt::poses::CPose3D, mrpt::utils::map_traits_stdmap > CNetworkOfPoses3D
The specialization of CNetworkOfPoses for poses of type mrpt::poses::CPose3D (not a PDF!)...
double getGlobalSquareError(bool ignoreCovariances=true) const
Computes the overall square error from all the pose constraints (edges) with respect to the global po...
#define ASSERTMSG_(f, __ERROR_MSG)
CNetworkOfPoses< mrpt::poses::CPosePDFGaussian, mrpt::utils::map_traits_stdmap > CNetworkOfPoses2DCov
The specialization of CNetworkOfPoses for poses of type CPosePDFGaussian, also implementing serializa...
MAPS_IMPLEMENTATION maps_implementation_t
The type of map's implementation (=MAPS_IMPLEMENTATION template argument)
CNetworkOfPoses< mrpt::poses::CPose2D, mrpt::utils::map_traits_stdmap > CNetworkOfPoses2D
The specialization of CNetworkOfPoses for poses of type CPose2D (not a PDF!), also implementing seria...
#define MRPT_DECLARE_TTYPENAME(_TYPE)
Definition: TTypeName.h:60



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015