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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020