Main MRPT website > C++ reference
MRPT logo
CRawlog.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 CRawlog_H
10 #define CRawlog_H
11 
12 #include <mrpt/poses/CPose2D.h>
17 
18 
19 namespace mrpt
20 {
21  namespace slam
22  {
24 
25  using namespace mrpt::utils;
26 
27  typedef std::pair<mrpt::system::TTimeStamp, CObservationPtr> TTimeObservationPair; //!< For usage with CRawlog classes.
28  typedef std::multimap<mrpt::system::TTimeStamp, CObservationPtr> TListTimeAndObservations; //!< For usage with CRawlog classes.
29 
30 
31  /** This class stores a rawlog (robotic datasets) in one of two possible formats:
32  * - Format #1: A sequence of actions and observations. There is a sequence of objects, where each one can be of one type:
33  * - An action: Implemented as a CActionCollection object, the actuation of the robot (i.e. odometry increment).
34  * - Observations: Implemented as a CSensoryFrame, refering to a set of robot observations from the same pose.
35  * - Format #2: A sequence of actions and observations. There is a sequence of objects, where each one can be of one type:
36  *
37  * Refer to the wiki page about <a href="http://www.mrpt.org/Rawlog_Format" >rawlog files</a>.
38  *
39  * See also the application <a href="http://www.mrpt.org/Application:RawLogViewer" >RawLogViewer</a > for the GUI program that visualizes and manages rawlogs.
40  *
41  * This class also publishes a static helper method for loading rawlog files in format #1: see CRawlog::readActionObservationPair
42  *
43  * There is a field for comments and blocks of parameters (in ini-like format) accessible through getCommentText and setCommentText
44  * (introduced in MRPT 0.6.4). When serialized to a rawlog file, the commens are saved as an additional observation of the
45  * type CObservationComments at the beginning of the file, but this observation does not appear after loading for clarity.
46  *
47  * \note Since MRPT version 0.5.5, this class also provides a STL container-like interface (see CRawlog::begin, CRawlog::iterator, ...).
48  * \note The format #2 is supported since MRPT version 0.6.0.
49  * \note There is a static helper method "detectImagesDirectory" for localizing the external images directory of a rawlog.
50  *
51  * \sa CSensoryFrame, CPose2D, <a href="http://www.mrpt.org/Rawlog_Format"> RawLog file format</a>.
52  * \ingroup mrpt_obs_grp
53  */
54  class OBS_IMPEXP CRawlog : public mrpt::utils::CSerializable
55  {
56  // This must be added to any CSerializable derived class:
58 
59  private:
60  typedef std::vector<CSerializablePtr> TListObjects;
61  TListObjects m_seqOfActObs; //!< The list where the objects really are in.
62 
63  CObservationComment m_commentTexts; //!< Comments of the rawlog.
64 
65  public:
66  void getCommentText( std::string &t) const; //!< Returns the block of comment text for the rawlog
67  std::string getCommentText() const; //!< Returns the block of comment text for the rawlog
68  void setCommentText( const std::string &t); //!< Changes the block of comment text for the rawlog
69  void getCommentTextAsConfigFile( mrpt::utils::CConfigFileMemory &memCfg ) const; //!< Saves the block of comment text for the rawlog into the passed config file object.
70 
71  /** The type of each entry in a rawlog.
72  * \sa CRawlog::getType
73  */
75  {
76  etSensoryFrame = 0,
78  etObservation
79  };
80 
81  /** Default constructor */
82  CRawlog();
83 
84 
85  /** Destructor: */
86  virtual ~CRawlog();
87 
88  /** Clear the sequence of actions/observations, freeing the memory of all the objects in the list. */
89  void clear();
90 
91  /** Clear the sequence of actions/observations, without deleting the objects themselves (USE ONLY IF YOU KNOW WHAT YOU DO, NORMALLY YOU'LL CALL "clear" INSTEAD). */
92  void clearWithoutDelete();
93 
94  /** Add an action to the sequence: a collection of just one element is created.
95  * The object is duplicated, so the original one can be free if desired.
96  */
97  void addAction( CAction &action );
98 
99  /** Add a set of actions to the sequence; the object is duplicated, so the original one can be free if desired.
100  * \sa addObservations, addActionsMemoryReference
101  */
102  void addActions( CActionCollection &action );
103 
104  /** Add a set of observations to the sequence; the object is duplicated, so the original one can be free if desired.
105  * \sa addActions, addObservationsMemoryReference
106  */
107  void addObservations( CSensoryFrame &observations );
108 
109  /** Add a set of actions to the sequence, using a smart pointer to the object to add.
110  * \sa addActions, addObservationsMemoryReference, addObservationMemoryReference
111  */
112  void addActionsMemoryReference( const CActionCollectionPtr &action );
113 
114  /** Add a set of observations to the sequence, using a smart pointer to the object to add.
115  * \sa addObservations, addActionsMemoryReference, addObservationMemoryReference
116  */
117  void addObservationsMemoryReference( const CSensoryFramePtr &observations );
118 
119  /** Add a single observation to the sequence, using a smart pointer to the object to add.
120  * \sa addObservations, addActionsMemoryReference
121  */
122  void addObservationMemoryReference( const CObservationPtr &observation );
123 
124  /** Load the contents from a file containing one of these possibilities:
125  * - A "CRawlog" object.
126  * - Directly the sequence of objects (pairs CSensoryFrame/CActionCollection or CObservation* objects). In this case the method stops reading on EOF of an unrecogniced class name.
127  * \returns It returns false if the file does not exists.
128  */
129  bool loadFromRawLogFile( const std::string &fileName );
130 
131  /** Saves the contents to a rawlog-file, compatible with RawlogViewer (As the sequence of internal objects).
132  * The file is saved with gz-commpressed if MRPT has gz-streams.
133  * \returns It returns false if any error is found while writing/creating the target file.
134  */
135  bool saveToRawLogFile( const std::string &fileName ) const;
136 
137  /** Returns the number of actions / observations object in the sequence. */
138  size_t size() const;
139 
140  /** Returns the type of a given element.
141  * \sa isAction, isObservation
142  */
143  TEntryType getType( size_t index ) const;
144 
145  /** Delete the action or observation stored in the given index.
146  * \exception std::exception If index is out of bounds
147  */
148  void remove( size_t index );
149 
150  /** Delete the elements stored in the given range of indices (including both the first and last one).
151  * \exception std::exception If any index is out of bounds
152  */
153  void remove( size_t first_index, size_t last_index );
154 
155  /** Returns the i'th element in the sequence, as being actions, where index=0 is the first object.
156  * If it is not a CActionCollection, it throws an exception. Do neighter modify nor delete the returned pointer.
157  * \sa size, isAction, getAsObservations, getAsObservation
158  * \exception std::exception If index is out of bounds
159  */
160  CActionCollectionPtr getAsAction( size_t index ) const;
161 
162  /** Returns the i'th element in the sequence, as being an action, where index=0 is the first object.
163  * If it is not an CSensoryFrame, it throws an exception. Do neighter modify nor delete the returned pointer.
164  * \sa size, isAction, getAsAction, getAsObservation
165  * \exception std::exception If index is out of bounds
166  */
167  CSensoryFramePtr getAsObservations( size_t index ) const;
168 
169  /** Returns the i'th element in the sequence, being its class whatever.
170  * \sa size, isAction, getAsAction, getAsObservations
171  * \exception std::exception If index is out of bounds
172  */
173  CSerializablePtr getAsGeneric( size_t index ) const;
174 
175  /** Returns the i'th element in the sequence, as being an observation, where index=0 is the first object.
176  * If it is not an CObservation, it throws an exception. Do neighter modify nor delete the returned pointer.
177  * This is the proper method to obtain the objects stored in a "only observations"-rawlog file (named "format #2" above.
178  * \sa size, isAction, getAsAction
179  * \exception std::exception If index is out of bounds
180  */
181  CObservationPtr getAsObservation( size_t index ) const;
182 
183 
184  /** A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequence. */
185  class iterator
186  {
187  protected:
189 
190  public:
191  iterator() : m_it() { }
192  iterator(const TListObjects::iterator& it) : m_it(it) { }
193  virtual ~iterator() { }
194 
195  iterator & operator = (const iterator& o) { m_it = o.m_it; return *this; }
196 
197  bool operator == (const iterator& o) { return m_it == o.m_it; }
198  bool operator != (const iterator& o) { return m_it != o.m_it; }
199 
200  CSerializablePtr operator *() { return *m_it; }
201 
202  inline iterator operator ++(int) { iterator aux =*this; m_it++; return aux; } // Post
203  inline iterator& operator ++() { m_it++; return *this; } // Pre
204  inline iterator operator --(int) { iterator aux = *this; m_it--; return aux; } // Post
205  inline iterator& operator --() { m_it--; return *this; } // Pre
206 
208  {
209  if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CObservation) ) )
210  return etObservation;
211  else if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CSensoryFrame) ) )
212  return etSensoryFrame;
213  else
214  return etActionCollection;
215  }
216 
217  static iterator erase( TListObjects& lst, const iterator &it) { return lst.erase(it.m_it); }
218  };
219 
220  /** A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequence. */
222  {
223  protected:
225 
226  public:
227  const_iterator() : m_it() { }
229  virtual ~const_iterator() { }
230 
231  bool operator == (const const_iterator& o) { return m_it == o.m_it; }
232  bool operator != (const const_iterator& o) { return m_it != o.m_it; }
233 
234  const CSerializablePtr operator *() const { return *m_it; }
235 
236  inline const_iterator operator ++(int) { const_iterator aux =*this; m_it++; return aux; } // Post
237  inline const_iterator& operator ++() { m_it++; return *this; } // Pre
238  inline const_iterator operator --(int) { const_iterator aux = *this; m_it--; return aux; } // Post
239  inline const_iterator& operator --() { m_it--; return *this; } // Pre
240 
242  {
243  if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CObservation) ) )
244  return etObservation;
245  else if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CSensoryFrame) ) )
246  return etSensoryFrame;
247  else
248  return etActionCollection;
249  }
250 
251  };
252 
253 
254  const_iterator begin() const { return m_seqOfActObs.begin(); }
255  iterator begin() { return m_seqOfActObs.begin(); }
256  const_iterator end() const { return m_seqOfActObs.end(); }
257  iterator end() { return m_seqOfActObs.end(); }
258 
259  iterator erase(const iterator &it) { return iterator::erase(m_seqOfActObs, it); }
260 
261  /** Returns the sub-set of observations of a given class whose time-stamp t fulfills time_start <= t < time_end.
262  * This method requires the timestamps of the sensors to be in strict ascending order (which should be the normal situation).
263  * Otherwise, the output is undeterminate.
264  * \sa findClosestObservationsByClass
265  */
266  void findObservationsByClassInRange(
267  mrpt::system::TTimeStamp time_start,
268  mrpt::system::TTimeStamp time_end,
269  const mrpt::utils::TRuntimeClassId *class_type,
270  TListTimeAndObservations &out_found,
271  size_t guess_start_position = 0
272  ) const;
273 
274  /** Efficiently copy the contents from other existing object, and remove the data from the origin (after calling this, the original object will have no actions/observations).
275  */
276  void moveFrom( CRawlog &obj);
277 
278  /** Efficiently swap the contents of two existing objects.
279  */
280  void swap( CRawlog &obj);
281 
282  /** Reads a consecutive pair action / observation from the rawlog opened at some input stream.
283  * Previous contents of action and observations are discarded (using stlplus::smart_ptr::clear_unique), and
284  * at exit they contain the new objects read from the rawlog file.
285  * The input/output variable "rawlogEntry" is just a counter of the last rawlog entry read, for logging or monitoring purposes.
286  * \return false if there was some error, true otherwise.
287  * \sa getActionObservationPair, getActionObservationPairOrObservation
288  */
289  static bool readActionObservationPair(
290  CStream &inStream,
291  CActionCollectionPtr &action,
292  CSensoryFramePtr &observations,
293  size_t & rawlogEntry );
294 
295  /** Reads a consecutive pair action/sensory_frame OR an observation, depending of the rawlog format, from the rawlog opened at some input stream.
296  * Previous contents of action and observations are discarded (using stlplus::smart_ptr::clear_unique), and
297  * at exit they contain the new objects read from the rawlog file.
298  *
299  * At return, one of this will happen:
300  * - action/observations contain objects (i.e. action.present() evaluates as true).
301  * - observation contains an object (i.e. observation.present() evaluates as true).
302  *
303  * The input/output variable "rawlogEntry" is just a counter of the last rawlog entry read, for logging or monitoring purposes.
304  * \return false if there was some error, true otherwise.
305  * \sa getActionObservationPair
306  */
307  static bool getActionObservationPairOrObservation(
308  CStream &inStream,
309  CActionCollectionPtr &action,
310  CSensoryFramePtr &observations,
311  CObservationPtr &observation,
312  size_t & rawlogEntry );
313 
314  /** Gets the next consecutive pair action / observation from the rawlog loaded into this object.
315  * Previous contents of action and observations are discarded (using stlplus::smart_ptr::clear_unique), and
316  * at exit they contain the new objects read from the rawlog file.
317  * The input/output variable "rawlogEntry" is just a counter of the last rawlog entry read, for logging or monitoring purposes.
318  * \return false if there was some error, true otherwise.
319  * \sa readActionObservationPair
320  */
321  bool getActionObservationPair(
322  CActionCollectionPtr &action,
323  CSensoryFramePtr &observations,
324  size_t &rawlogEntry ) const;
325 
326  /** Tries to auto-detect the external-images directory of the given rawlog file.
327  * This searches for the existence of the directories:
328  * - "<rawlog_file_path>/<rawlog_filename>_Images"
329  * - "<rawlog_file_path>/<rawlog_filename>_images"
330  * - "<rawlog_file_path>/<rawlog_filename>_IMAGES"
331  * - "<rawlog_file_path>/Images" (This one is returned if none of the choices actually exists).
332  *
333  * The results from this function should be written into mrpt::utils::CImage::IMAGES_PATH_BASE to enable automatic
334  * loading of extenrnally-stored images in rawlogs.
335  */
336  static std::string detectImagesDirectory(const std::string &rawlogFilename);
337 
338  }; // End of class def.
340 
341  } // End of namespace
342 } // End of namespace
343 
344 #endif
const_iterator(const TListObjects::const_iterator &it)
Definition: CRawlog.h:228
bool operator!=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:287
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:35
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
TEntryType getType() const
Definition: CRawlog.h:241
std::vector< CSerializablePtr > TListObjects
Definition: CRawlog.h:60
Scalar * iterator
Definition: eigen_plugins.h:23
TListObjects::const_iterator m_it
Definition: CRawlog.h:224
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
std::multimap< mrpt::system::TTimeStamp, CObservationPtr > TListTimeAndObservations
For usage with CRawlog classes.
Definition: CRawlog.h:28
const_iterator end() const
Definition: CRawlog.h:256
Declares a class for storing a collection of robot actions.
std::pair< mrpt::system::TTimeStamp, CObservationPtr > TTimeObservationPair
For usage with CRawlog classes.
Definition: CRawlog.h:27
This class implements a config file-like interface over a memory-stored string list.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
TEntryType
The type of each entry in a rawlog.
Definition: CRawlog.h:74
Declares a class that represents any robot's observation.
Definition: CObservation.h:52
bool operator==(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:279
TEntryType getType() const
Definition: CRawlog.h:207
iterator(const TListObjects::iterator &it)
Definition: CRawlog.h:192
iterator begin()
Definition: CRawlog.h:255
struct BASE_IMPEXP CSerializablePtr
Definition: CStream.h:24
iterator erase(const iterator &it)
Definition: CRawlog.h:259
#define CLASS_ID(class_name)
Access to runtime class ID for a defined class name.
Definition: CObject.h:88
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:221
size_t size(const MATRIXLIKE &m, int dim)
Definition: bits.h:38
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:53
const_iterator begin() const
Definition: CRawlog.h:254
This "observation" is actually a placeholder for a text block with comments or additional parameters ...
TListObjects::iterator m_it
Definition: CRawlog.h:188
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:185
A structure that holds runtime class type information.
Definition: CObject.h:46
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This class stores a rawlog (robotic datasets) in one of two possible formats:
Definition: CRawlog.h:54
static iterator erase(TListObjects &lst, const iterator &it)
Definition: CRawlog.h:217
iterator end()
Definition: CRawlog.h:257
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:59
Declares a class for storing a robot action.
Definition: CAction.h:33



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