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-2014, 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  {
38  using namespace mrpt::utils;
39 
40  /** Internal functions for MRPT */
41  namespace detail
42  {
43  template <class GRAPH_T> struct graph_ops;
44  /** An empty structure */
46  }
47 
48  /** A directed graph of pose constraints, with edges being the relative pose between pairs of nodes indentified by their numeric IDs (of type TNodeID).
49  * 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".
50  * These poses are stored in the edges in the format specified by the template argument CPOSE. Users should employ the following derived classes
51  * depending on the desired representation of edges:
52  * - mrpt::graphs::CNetworkOfPoses2D : 2D edges as a simple CPose2D (x y phi)
53  * - mrpt::graphs::CNetworkOfPoses3D : 3D edges as a simple CPose3D (x y z yaw pitch roll)
54  * - mrpt::graphs::CNetworkOfPoses2DInf : 2D edges as a Gaussian PDF with information matrix ( CPosePDFGaussianInf )
55  * - mrpt::graphs::CNetworkOfPoses3DInf : 3D edges as a Gaussian PDF with information matrix ( CPose3DPDFGaussianInf )
56  * - mrpt::graphs::CNetworkOfPoses2DCov : 2D edges as a Gaussian PDF with covariance matrix ( CPosePDFGaussian ). It's more efficient to use the information matrix version instead!
57  * - mrpt::graphs::CNetworkOfPoses3DCov : 3D edges as a Gaussian PDF with covariance matrix ( CPose3DPDFGaussian ). It's more efficient to use the information matrix version instead!
58  *
59  * Two main members store all the information in this class:
60  * - \a edges (in the base class mrpt::graphs::CDirectedGraph::edges): A map from pairs of node ID -> pose constraints.
61  * - \a nodes : A map from node ID -> estimated pose of that node (actually, read below on the template argument MAPS_IMPLEMENTATION).
62  *
63  * 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> ),
64  * using \a loadFromTextFile and \a saveToTextFile.
65  *
66  * This class is the base for representing networks of poses, which are the main data type of a series
67  * of SLAM algorithms implemented in the library mrpt-slam, in the namespace mrpt::graphslam.
68  *
69  * For tools to visualize graphs as 2D/3D plots, see the namespace mrpt::opengl::graph_tools in the library mrpt-opengl.
70  *
71  * The template arguments are:
72  * - CPOSE: The type of the edges, which hold a relative pose (2D/3D, just a value or a Gaussian, etc.)
73  * - 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).
74  *
75  * \sa mrpt::graphslam
76  * \ingroup mrpt_graphs_grp
77  */
78  template<
79  class CPOSE, // Type of edges
80  class MAPS_IMPLEMENTATION = map_traits_stdmap, // Use std::map<> vs. std::vector<>
81  class NODE_ANNOTATIONS = mrpt::graphs::detail::node_annotations_empty,
82  class EDGE_ANNOTATIONS = mrpt::graphs::detail::edge_annotations_empty
83  >
84  class CNetworkOfPoses : public mrpt::graphs::CDirectedGraph< CPOSE, EDGE_ANNOTATIONS >
85  {
86  public:
87  /** @name Typedef's
88  @{ */
89  typedef mrpt::graphs::CDirectedGraph<CPOSE,EDGE_ANNOTATIONS> BASE; //!< The base class "CDirectedGraph<CPOSE,EDGE_ANNOTATIONS>" */
91 
92  typedef CPOSE constraint_t; //!< The type of PDF poses in the contraints (edges) (=CPOSE template argument)
93  typedef NODE_ANNOTATIONS node_annotations_t; //!< The extra annotations in nodes, apart from a \a constraint_no_pdf_t
94  typedef EDGE_ANNOTATIONS edge_annotations_t; //!< The extra annotations in edges, apart from a \a constraint_t
95 
96  typedef MAPS_IMPLEMENTATION maps_implementation_t; //!< The type of map's implementation (=MAPS_IMPLEMENTATION template argument)
97  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)
98 
99  /** 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 */
100  struct global_pose_t : public constraint_no_pdf_t, public NODE_ANNOTATIONS
101  {
102  // Replicate possible constructors:
103  inline global_pose_t() : constraint_no_pdf_t() { }
104  template <typename ARG1> inline global_pose_t(const ARG1 &a1) : constraint_no_pdf_t(a1) { }
105  template <typename ARG1,typename ARG2> inline global_pose_t(const ARG1 &a1,const ARG2 &a2) : constraint_no_pdf_t(a1,a2) { }
106  };
107 
108  /** A map from pose IDs to their global coordinates estimates, with uncertainty */
109  typedef typename MAPS_IMPLEMENTATION::template map<TNodeID,CPOSE> global_poses_pdf_t;
110 
111  /** A map from pose IDs to their global coordinates estimates, without uncertainty (the "most-likely value") */
112  typedef typename MAPS_IMPLEMENTATION::template map<TNodeID,global_pose_t> global_poses_t;
113 
114  /** @} */
115 
116 
117  /** @name Data members
118  @{ */
119 
120  /** The nodes (vertices) of the graph, with their estimated "global" (with respect to \a root) position, without an associated covariance.
121  * \sa dijkstra_nodes_estimate
122  */
123  global_poses_t nodes;
124 
125  /** 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". */
127 
128  /** 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$
129  * 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$
130  */
132 
133  /** @} */
134 
135 
136  /** @name I/O file methods
137  @{ */
138 
139  /** 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> )
140  * For 2D graphs only VERTEX2 & EDGE2 entries will be saved, and VERTEX3 & EDGE3 entries for 3D graphs.
141  * Note that EQUIV entries will not be saved, but instead several EDGEs will be stored between the same node IDs.
142  * \sa saveToBinaryFile, loadFromTextFile
143  * \exception On any error
144  */
145  inline void saveToTextFile( const std::string &fileName ) const {
147  }
148 
149  /** 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> )
150  * Recognized line entries are: VERTEX2, VERTEX3, EDGE2, EDGE3, EQUIV.
151  * If an unknown entry is found, a warning is dumped to std::cerr (only once for each unknown keyword).
152  * 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).
153  * \param fileName The file to load.
154  * \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).
155  * \sa loadFromBinaryFile, saveToTextFile
156  * \exception On any error, as a malformed line or loading a 3D graph in a 2D graph.
157  */
158  inline void loadFromTextFile( const std::string &fileName, bool collapse_dup_edges = true ) {
160  if (collapse_dup_edges) this->collapseDuplicatedEdges();
161  }
162 
163  /** @} */
164 
165  /** @name Utility methods
166  @{ */
167 
168  /** 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.
169  * Note that "global" coordinates are with respect to the node with the ID specified in \a root.
170  * \note This method takes into account the value of \a edges_store_inverse_poses
171  * \sa node, root
172  */
174 
175  /** Look for duplicated edges (even in opposite directions) between all pairs of nodes and fuse them.
176  * 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.
177  * \return Overall number of removed edges.
178  */
180 
181  /** Computes the overall square error from all the pose constraints (edges) with respect to the global poses in \nodes
182  * If \a ignoreCovariances is false, the squared Mahalanobis distance will be computed instead of the straight square error.
183  * \sa getEdgeSquareError
184  * \exception std::exception On global poses not in \a nodes
185  */
186  double getGlobalSquareError(bool ignoreCovariances = true) const {
187  double sqErr=0;
188  const typename BASE::edges_map_t::const_iterator last_it=BASE::edges.end();
189  for (typename BASE::edges_map_t::const_iterator itEdge=BASE::edges.begin();itEdge!=last_it;++itEdge)
190  sqErr+=detail::graph_ops<self_t>::graph_edge_sqerror(this,itEdge,ignoreCovariances);
191  return sqErr;
192  }
193 
194  /** Computes the square error of one pose constraints (edge) with respect to the global poses in \nodes
195  * If \a ignoreCovariances is false, the squared Mahalanobis distance will be computed instead of the straight square error.
196  * \exception std::exception On global poses not in \a nodes
197  */
198  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); }
199 
200  /** Computes the square error of one pose constraints (edge) with respect to the global poses in \nodes
201  * If \a ignoreCovariances is false, the squared Mahalanobis distance will be computed instead of the straight square error.
202  * \exception std::exception On edge not existing or global poses not in \a nodes
203  */
204  double getEdgeSquareError(const TNodeID from_id, const TNodeID to_id, bool ignoreCovariances = true ) const
205  {
206  const typename BASE::edges_map_t::const_iterator itEdge = BASE::edges.find( std::make_pair(from_id,to_id) );
207  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)));
208  return getEdgeSquareError(itEdge,ignoreCovariances);
209  }
210 
211  /** Empty all edges, nodes and set root to ID 0. */
212  inline void clear() {
213  BASE::edges.clear();
214  nodes.clear();
215  root = 0;
216  edges_store_inverse_poses = false;
217  }
218 
219  /** Return number of nodes in the list \nodes of global coordinates (may be differente that all nodes appearing in edges)
220  * \sa mrpt::graphs::CDirectedGraph::countDifferentNodesInEdges
221  */
222  inline size_t nodeCount() const { return nodes.size(); }
223 
224  /** @} */
225 
226  /** @name Ctors & Dtors
227  @{ */
228 
229  /** Default constructor (just sets root to "0" and edges_store_inverse_poses to "false") */
230  inline CNetworkOfPoses() : root(0), edges_store_inverse_poses(false) { }
232  /** @} */
233  };
234 
235 
236  /** Binary serialization (write) operator "stream << graph" */
237  template <class CPOSE,class MAPS_IMPLEMENTATION,class NODE_ANNOTATIONS,class EDGE_ANNOTATIONS>
238  CStream & operator << (CStream&out, const CNetworkOfPoses<CPOSE,MAPS_IMPLEMENTATION,NODE_ANNOTATIONS,EDGE_ANNOTATIONS> &obj)
239  {
242  return out;
243  }
244 
245  /** Binary serialization (read) operator "stream >> graph" */
246  template <class CPOSE,class MAPS_IMPLEMENTATION,class NODE_ANNOTATIONS,class EDGE_ANNOTATIONS>
248  {
251  return in;
252  }
253 
254  /** \addtogroup mrpt_graphs_grp
255  @{ */
256 
257  typedef CNetworkOfPoses<mrpt::poses::CPose2D,map_traits_stdmap> CNetworkOfPoses2D; //!< The specialization of CNetworkOfPoses for poses of type CPose2D (not a PDF!), also implementing serialization.
258  typedef CNetworkOfPoses<mrpt::poses::CPose3D,map_traits_stdmap> CNetworkOfPoses3D; //!< The specialization of CNetworkOfPoses for poses of type CPose3D (not a PDF!), also implementing serialization.
259  typedef CNetworkOfPoses<mrpt::poses::CPosePDFGaussian,map_traits_stdmap> CNetworkOfPoses2DCov; //!< The specialization of CNetworkOfPoses for poses of type CPosePDFGaussian, also implementing serialization.
260  typedef CNetworkOfPoses<mrpt::poses::CPose3DPDFGaussian,map_traits_stdmap> CNetworkOfPoses3DCov; //!< The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussian, also implementing serialization.
261  typedef CNetworkOfPoses<mrpt::poses::CPosePDFGaussianInf,map_traits_stdmap> CNetworkOfPoses2DInf; //!< The specialization of CNetworkOfPoses for poses of type CPosePDFGaussianInf, also implementing serialization.
262  typedef CNetworkOfPoses<mrpt::poses::CPose3DPDFGaussianInf,map_traits_stdmap> CNetworkOfPoses3DInf; //!< The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussianInf, also implementing serialization.
263 
264  /** @} */ // end of grouping
265 
266  } // End of namespace
267 
268  // Specialization of TTypeName must occur in the same namespace:
269  namespace utils
270  {
271  // Extensions to mrpt::utils::TTypeName for matrices:
272  template<
273  class CPOSE,
274  class MAPS_IMPLEMENTATION,
275  class NODE_ANNOTATIONS,
276  class EDGE_ANNOTATIONS
277  >
278  struct TTypeName <mrpt::graphs::CNetworkOfPoses<CPOSE,MAPS_IMPLEMENTATION,NODE_ANNOTATIONS,EDGE_ANNOTATIONS> >
279  {
280  static std::string get()
281  {
282  return std::string("mrpt::graphs::CNetworkOfPoses<")
283  +TTypeName<CPOSE>::get() + std::string(",")
284  +TTypeName<MAPS_IMPLEMENTATION>::get() + std::string(",")
285  +TTypeName<NODE_ANNOTATIONS>::get() + std::string(",")
287  +std::string(">");
288  }
289  };
290 
291 
295 
296  }
297 
298 } // End of namespace
299 
300 
301 // Implementation of templates (in a separate file for clarity)
302 #include "CNetworkOfPoses_impl.h"
303 
304 #endif
A directed graph with the argument of the template specifying the type of the annotations in the edge...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
void dijkstra_nodes_estimate()
Spanning tree computation of a simple estimation of the global coordinates of each node just from the...
EIGEN_STRONG_INLINE iterator end()
Definition: eigen_plugins.h:27
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.
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
Traits for using a mrpt::utils::map_as_vector<> (dense, fastest representation)
Definition: traits_map.h:32
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...
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
CNetworkOfPoses< mrpt::poses::CPosePDFGaussian, map_traits_stdmap > CNetworkOfPoses2DCov
The specialization of CNetworkOfPoses for poses of type CPosePDFGaussian, also implementing serializa...
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...
MAPS_IMPLEMENTATION::template map< TNodeID, global_pose_t > global_poses_t
A map from pose IDs to their global coordinates estimates, without uncertainty (the "most-likely valu...
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
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...
MAPS_IMPLEMENTATION::template map< TNodeID, CPOSE > global_poses_pdf_t
A map from pose IDs to their global coordinates estimates, with uncertainty.
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...
double getEdgeSquareError(const TNodeID from_id, const 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...
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.
CNetworkOfPoses< mrpt::poses::CPose3D, map_traits_stdmap > CNetworkOfPoses3D
The specialization of CNetworkOfPoses for poses of type CPose3D (not a PDF!), also implementing seria...
CPOSE constraint_t
The type of PDF poses in the contraints (edges) (=CPOSE template argument)
TNodeID root
The ID of the node that is the origin of coordinates, used as reference by all coordinates in ...
uint64_t TNodeID
The type for node IDs in graphs of different types.
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" */.
static void load_graph_of_poses_from_text_file(graph_t *g, const std::string &fil)
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)
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::CPose3DPDFGaussian, map_traits_stdmap > CNetworkOfPoses3DCov
The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussian, also implementing seriali...
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::CPose3DPDFGaussianInf, map_traits_stdmap > CNetworkOfPoses3DInf
The specialization of CNetworkOfPoses for poses of type CPose3DPDFGaussianInf, also implementing seri...
MAPS_IMPLEMENTATION maps_implementation_t
The type of map's implementation (=MAPS_IMPLEMENTATION template argument)
CStream & operator>>(CStream &in, CNetworkOfPoses< CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS, EDGE_ANNOTATIONS > &obj)
Binary serialization (read) operator "stream >> graph".
CNetworkOfPoses< mrpt::poses::CPose2D, 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
CNetworkOfPoses< mrpt::poses::CPosePDFGaussianInf, map_traits_stdmap > CNetworkOfPoses2DInf
The specialization of CNetworkOfPoses for poses of type CPosePDFGaussianInf, also implementing serial...



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014