MRPT  1.9.9
CRawlog.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
14 #include <mrpt/obs/CSensoryFrame.h>
15 #include <mrpt/poses/CPose2D.h>
16 
17 namespace mrpt::obs
18 {
19 /** For usage with CRawlog classes. */
21  std::pair<mrpt::system::TTimeStamp, CObservation::Ptr>;
22 /** For usage with CRawlog classes. */
24  std::multimap<mrpt::system::TTimeStamp, CObservation::Ptr>;
25 
26 /** This class stores a rawlog (robotic datasets) in one of two possible
27  *formats:
28  * - Format #1: A sequence of actions and observations. There is a sequence
29  *of objects, where each one can be of one type:
30  * - An action: Implemented as a CActionCollection object, the
31  *actuation
32  *of the robot (i.e. odometry increment).
33  * - Observations: Implemented as a CSensoryFrame, refering to a set of
34  *robot observations from the same pose.
35  * - Format #2: A sequence of actions and observations. There is a sequence
36  *of objects, where each one can be of one type:
37  *
38  * Refer to the wiki page about <a href="http://www.mrpt.org/Rawlog_Format"
39  *>rawlog files</a>.
40  *
41  * See also the application <a
42  *href="http://www.mrpt.org/Application:RawLogViewer" >RawLogViewer</a > for the
43  *GUI program that visualizes and manages rawlogs.
44  *
45  * This class also publishes a static helper method for loading rawlog files in
46  *format #1: see CRawlog::readActionObservationPair
47  *
48  * There is a field for comments and blocks of parameters (in ini-like format)
49  *accessible through getCommentText and setCommentText
50  * (introduced in MRPT 0.6.4). When serialized to a rawlog file, the comments
51  *are saved as an additional observation of the
52  * type CObservationComments at the beginning of the file, but this
53  *observation does not appear after loading for clarity.
54  *
55  * \note Since MRPT version 0.5.5, this class also provides a STL container-like
56  *interface (see CRawlog::begin, CRawlog::iterator, ...).
57  * \note The format #2 is supported since MRPT version 0.6.0.
58  * \note There is a static helper method "detectImagesDirectory" for localizing
59  *the external images directory of a rawlog.
60  *
61  * \sa CSensoryFrame, CPose2D, <a href="http://www.mrpt.org/Rawlog_Format">
62  *RawLog file format</a>.
63  * \ingroup mrpt_obs_grp
64  */
66 {
68 
69  private:
70  using TListObjects = std::vector<mrpt::serialization::CSerializable::Ptr>;
71  /** The list where the objects really are in. */
73 
74  /** Comments of the rawlog. */
76 
77  public:
78  /** Returns the block of comment text for the rawlog */
79  void getCommentText(std::string& t) const;
80  /** Returns the block of comment text for the rawlog */
82  /** Changes the block of comment text for the rawlog */
83  void setCommentText(const std::string& t);
84  /** Saves the block of comment text for the rawlog into the passed config
85  * file object. */
87  mrpt::config::CConfigFileMemory& memCfg) const;
88 
89  /** The type of each entry in a rawlog.
90  * \sa CRawlog::getType
91  */
93  {
98  };
99 
100  /** Default constructor */
101  CRawlog();
102 
103  /** Destructor: */
104  ~CRawlog() override;
105 
106  /** Clear the sequence of actions/observations. Smart pointers to objects
107  * previously in the rawlog will remain being valid. */
108  void clear();
109 
110  /** Add an action to the sequence: a collection of just one element is
111  * created.
112  * The object is duplicated, so the original one can be freed if desired.
113  */
114  void addAction(CAction& action);
115 
116  /** Add a set of actions to the sequence; the object is duplicated, so the
117  * original one can be freed if desired.
118  * \sa addObservations, addActionsMemoryReference
119  */
120  void addActions(CActionCollection& action);
121 
122  /** Add a set of observations to the sequence; the object is duplicated, so
123  * the original one can be free if desired.
124  * \sa addActions, addObservationsMemoryReference
125  */
126  void addObservations(CSensoryFrame& observations);
127 
128  /** Add a set of actions to the sequence, using a smart pointer to the
129  * object to add.
130  * \sa addActions, addObservationsMemoryReference,
131  * addObservationMemoryReference
132  */
134 
135  /** Add a set of observations to the sequence, using a smart pointer to the
136  * object to add.
137  * \sa addObservations, addActionsMemoryReference,
138  * addObservationMemoryReference
139  */
140  void addObservationsMemoryReference(const CSensoryFrame::Ptr& observations);
141 
142  /** Add a single observation to the sequence, using a smart pointer to the
143  * object to add.
144  * \sa addObservations, addActionsMemoryReference
145  */
146  void addObservationMemoryReference(const CObservation::Ptr& observation);
147 
148  /** Generic add for a smart pointer to a CSerializable object:
149  * \sa addObservations, addActionsMemoryReference,
150  * addObservationMemoryReference
151  */
153 
154  /** Load the contents from a file containing one of these possibilities:
155  * - A "CRawlog" object.
156  * - Directly the sequence of objects (pairs
157  * `CSensoryFrame`/`CActionCollection` or `CObservation*` objects). In this
158  * case the method stops reading on EOF of an unrecogniced class name.
159  * - Only if `non_obs_objects_are_legal` is true, any `CSerializable`
160  * object is allowed in the log file. Otherwise, the read stops on classes
161  * different from the ones listed in the item above.
162  * \returns It returns false upon error reading or accessing the file.
163  */
164  bool loadFromRawLogFile(
165  const std::string& fileName, bool non_obs_objects_are_legal = false);
166 
167  /** Saves the contents to a rawlog-file, compatible with RawlogViewer (As
168  * the sequence of internal objects).
169  * The file is saved with gz-commpressed if MRPT has gz-streams.
170  * \returns It returns false if any error is found while writing/creating
171  * the target file.
172  */
173  bool saveToRawLogFile(const std::string& fileName) const;
174 
175  /** Returns the number of actions / observations object in the sequence. */
176  size_t size() const;
177 
178  /** Returns the type of a given element.
179  * \sa isAction, isObservation
180  */
181  TEntryType getType(size_t index) const;
182 
183  /** Delete the action or observation stored in the given index.
184  * \exception std::exception If index is out of bounds
185  */
186  void remove(size_t index);
187 
188  /** Delete the elements stored in the given range of indices (including both
189  * the first and last one).
190  * \exception std::exception If any index is out of bounds
191  */
192  void remove(size_t first_index, size_t last_index);
193 
194  /** Returns the i'th element in the sequence, as being actions, where
195  * index=0 is the first object.
196  * If it is not a CActionCollection, it throws an exception. Do neighter
197  * modify nor delete the returned pointer.
198  * \sa size, isAction, getAsObservations, getAsObservation
199  * \exception std::exception If index is out of bounds
200  */
202 
203  /** Returns the i'th element in the sequence, as being an action, where
204  * index=0 is the first object.
205  * If it is not an CSensoryFrame, it throws an exception. Do neighter
206  * modify nor delete the returned pointer.
207  * \sa size, isAction, getAsAction, getAsObservation
208  * \exception std::exception If index is out of bounds
209  */
211 
212  /** Returns the i'th element in the sequence, being its class whatever.
213  * \sa size, isAction, getAsAction, getAsObservations
214  * \exception std::exception If index is out of bounds
215  */
217 
218  /** Returns the i'th element in the sequence, as being an observation, where
219  * index=0 is the first object.
220  * If it is not an CObservation, it throws an exception. Do neighter
221  * modify nor delete the returned pointer.
222  * This is the proper method to obtain the objects stored in a "only
223  * observations"-rawlog file (named "format #2" above.
224  * \sa size, isAction, getAsAction
225  * \exception std::exception If index is out of bounds
226  */
228 
229  /** A normal iterator, plus the extra method "getType" to determine the type
230  * of each entry in the sequence. */
231  class iterator
232  {
233  protected:
234  TListObjects::iterator m_it;
235 
236  public:
237  iterator() : m_it() {}
238  iterator(const TListObjects::iterator& it) : m_it(it) {}
239  virtual ~iterator() = default;
240  iterator& operator=(const iterator& o) = default;
241 
242  bool operator==(const iterator& o) { return m_it == o.m_it; }
243  bool operator!=(const iterator& o) { return m_it != o.m_it; }
245  inline iterator operator++(int)
246  {
247  iterator aux = *this;
248  m_it++;
249  return aux;
250  } // Post
252  {
253  m_it++;
254  return *this;
255  } // Pre
256  inline iterator operator--(int)
257  {
258  iterator aux = *this;
259  m_it--;
260  return aux;
261  } // Post
263  {
264  m_it--;
265  return *this;
266  } // Pre
267 
269  {
270  if ((*m_it)->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
271  return etObservation;
272  else if ((*m_it)->GetRuntimeClass()->derivedFrom(
274  return etSensoryFrame;
275  else
276  return etActionCollection;
277  }
278 
279  static iterator erase(TListObjects& lst, const iterator& it)
280  {
281  return lst.erase(it.m_it);
282  }
283  };
284 
285  /** A normal iterator, plus the extra method "getType" to determine the type
286  * of each entry in the sequence. */
288  {
289  protected:
290  TListObjects::const_iterator m_it;
291 
292  public:
294  const_iterator(const TListObjects::const_iterator& it) : m_it(it) {}
295  virtual ~const_iterator() = default;
296  bool operator==(const const_iterator& o) { return m_it == o.m_it; }
297  bool operator!=(const const_iterator& o) { return m_it != o.m_it; }
299  {
300  return *m_it;
301  }
302 
304  {
305  const_iterator aux = *this;
306  m_it++;
307  return aux;
308  } // Post
310  {
311  m_it++;
312  return *this;
313  } // Pre
315  {
316  const_iterator aux = *this;
317  m_it--;
318  return aux;
319  } // Post
321  {
322  m_it--;
323  return *this;
324  } // Pre
325 
327  {
328  if ((*m_it)->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
329  return etObservation;
330  else if ((*m_it)->GetRuntimeClass()->derivedFrom(
332  return etSensoryFrame;
333  else
334  return etActionCollection;
335  }
336  };
337 
338  const_iterator begin() const { return m_seqOfActObs.begin(); }
339  iterator begin() { return m_seqOfActObs.begin(); }
340  const_iterator end() const { return m_seqOfActObs.end(); }
341  iterator end() { return m_seqOfActObs.end(); }
343  {
344  return iterator::erase(m_seqOfActObs, it);
345  }
346 
347  /** Returns the sub-set of observations of a given class whose time-stamp t
348  * fulfills time_start <= t < time_end.
349  * This method requires the timestamps of the sensors to be in strict
350  * ascending order (which should be the normal situation).
351  * Otherwise, the output is undeterminate.
352  * \sa findClosestObservationsByClass
353  */
356  const mrpt::rtti::TRuntimeClassId* class_type,
357  TListTimeAndObservations& out_found,
358  size_t guess_start_position = 0) const;
359 
360  /** Efficiently swap the contents of two existing objects.
361  */
362  void swap(CRawlog& obj);
363 
364  /** Reads a consecutive pair action / observation from the rawlog opened at
365  * some input stream.
366  * Previous contents of action and observations are discarded (using
367  * stlplus::smart_ptr::reset), and
368  * at exit they contain the new objects read from the rawlog file.
369  * The input/output variable "rawlogEntry" is just a counter of the last
370  * rawlog entry read, for logging or monitoring purposes.
371  * \return false if there was some error, true otherwise.
372  * \sa getActionObservationPair, getActionObservationPairOrObservation
373  */
374  static bool readActionObservationPair(
376  CSensoryFrame::Ptr& observations, size_t& rawlogEntry);
377 
378  /** Reads a consecutive pair action/sensory_frame OR an observation,
379  *depending of the rawlog format, from the rawlog opened at some input
380  *stream.
381  * Previous contents of action and observations are discarded (using
382  *stlplus::smart_ptr::reset), and
383  * at exit they contain the new objects read from the rawlog file.
384  *
385  * At return, one of this will happen:
386  * - action/observations contain objects (i.e. action() evaluates as
387  *true).
388  * - observation contains an object (i.e. observation evaluates as
389  *true).
390  *
391  * The input/output variable "rawlogEntry" is just a counter of the last
392  *rawlog entry read, for logging or monitoring purposes.
393  * \return false if there was some error, true otherwise.
394  * \sa getActionObservationPair
395  */
398  CSensoryFrame::Ptr& observations, CObservation::Ptr& observation,
399  size_t& rawlogEntry);
400 
401  /** Gets the next consecutive pair action / observation from the rawlog
402  * loaded into this object.
403  * Previous contents of action and observations are discarded (using
404  * stlplus::smart_ptr::reset), and
405  * at exit they contain the new objects read from the rawlog file.
406  * The input/output variable "rawlogEntry" is just a counter of the last
407  * rawlog entry read, for logging or monitoring purposes.
408  * \return false if there was some error, true otherwise.
409  * \sa readActionObservationPair
410  */
412  CActionCollection::Ptr& action, CSensoryFrame::Ptr& observations,
413  size_t& rawlogEntry) const;
414 
415  /** Tries to auto-detect the external-images directory of the given rawlog
416  *file.
417  * This searches for the existence of the directories:
418  * - "<rawlog_file_path>/<rawlog_filename>_Images"
419  * - "<rawlog_file_path>/<rawlog_filename>_images"
420  * - "<rawlog_file_path>/<rawlog_filename>_IMAGES"
421  * - "<rawlog_file_path>/Images" (This one is returned if none of the
422  *choices actually exists).
423  *
424  * The results from this function should be written into
425  *mrpt::img::CImage::getImagesPathBase() to enable automatic
426  * loading of extenrnally-stored images in rawlogs.
427  */
428  static std::string detectImagesDirectory(const std::string& rawlogFilename);
429 
430 }; // End of class def.
431 
432 } // namespace mrpt::obs
This class implements a config file-like interface over a memory-stored string list.
bool operator==(const iterator &o)
Definition: CRawlog.h:242
This "observation" is actually a placeholder for a text block with comments or additional parameters ...
void findObservationsByClassInRange(mrpt::system::TTimeStamp time_start, mrpt::system::TTimeStamp time_end, const mrpt::rtti::TRuntimeClassId *class_type, TListTimeAndObservations &out_found, size_t guess_start_position=0) const
Returns the sub-set of observations of a given class whose time-stamp t fulfills time_start <= t < ti...
Definition: CRawlog.cpp:432
GLdouble GLdouble t
Definition: glext.h:3695
TListObjects m_seqOfActObs
The list where the objects really are in.
Definition: CRawlog.h:72
const_iterator end() const
Definition: CRawlog.h:340
std::vector< mrpt::serialization::CSerializable::Ptr > TListObjects
Definition: CRawlog.h:70
iterator begin()
Definition: CRawlog.h:339
bool getActionObservationPair(CActionCollection::Ptr &action, CSensoryFrame::Ptr &observations, size_t &rawlogEntry) const
Gets the next consecutive pair action / observation from the rawlog loaded into this object...
Definition: CRawlog.cpp:513
static std::string detectImagesDirectory(const std::string &rawlogFilename)
Tries to auto-detect the external-images directory of the given rawlog file.
Definition: CRawlog.cpp:555
iterator erase(const iterator &it)
Definition: CRawlog.h:342
const_iterator & operator++()
Definition: CRawlog.h:309
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:231
const_iterator & operator--()
Definition: CRawlog.h:320
const_iterator operator--(int)
Definition: CRawlog.h:314
A structure that holds runtime class type information.
Definition: CObject.h:31
mrpt::serialization::CSerializable::Ptr operator*()
Definition: CRawlog.h:244
const_iterator(const TListObjects::const_iterator &it)
Definition: CRawlog.h:294
void addObservationsMemoryReference(const CSensoryFrame::Ptr &observations)
Add a set of observations to the sequence, using a smart pointer to the object to add...
Definition: CRawlog.cpp:55
void addActions(CActionCollection &action)
Add a set of actions to the sequence; the object is duplicated, so the original one can be freed if d...
Definition: CRawlog.cpp:44
TListObjects::const_iterator m_it
Definition: CRawlog.h:290
void addGenericObject(const mrpt::serialization::CSerializable::Ptr &obj)
Generic add for a smart pointer to a CSerializable object:
Definition: CRawlog.cpp:60
TEntryType getType() const
Definition: CRawlog.h:268
CRawlog()
Default constructor.
Definition: CRawlog.cpp:29
void addObservations(CSensoryFrame &observations)
Add a set of observations to the sequence; the object is duplicated, so the original one can be free ...
Definition: CRawlog.cpp:38
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
iterator operator--(int)
Definition: CRawlog.h:256
bool operator!=(const const_iterator &o)
Definition: CRawlog.h:297
Declares a class for storing a collection of robot actions.
static bool getActionObservationPairOrObservation(mrpt::serialization::CArchive &inStream, CActionCollection::Ptr &action, CSensoryFrame::Ptr &observations, CObservation::Ptr &observation, size_t &rawlogEntry)
Reads a consecutive pair action/sensory_frame OR an observation, depending of the rawlog format...
Definition: CRawlog.cpp:370
bool saveToRawLogFile(const std::string &fileName) const
Saves the contents to a rawlog-file, compatible with RawlogViewer (As the sequence of internal object...
Definition: CRawlog.cpp:287
CActionCollection::Ptr getAsAction(size_t index) const
Returns the i&#39;th element in the sequence, as being actions, where index=0 is the first object...
Definition: CRawlog.cpp:86
std::string getCommentText() const
Returns the block of comment text for the rawlog.
Definition: CRawlog.cpp:547
std::pair< mrpt::system::TTimeStamp, CObservation::Ptr > TTimeObservationPair
For usage with CRawlog classes.
Definition: CRawlog.h:21
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
bool loadFromRawLogFile(const std::string &fileName, bool non_obs_objects_are_legal=false)
Load the contents from a file containing one of these possibilities:
Definition: CRawlog.cpp:187
mrpt::serialization::CSerializable::Ptr getAsGeneric(size_t index) const
Returns the i&#39;th element in the sequence, being its class whatever.
Definition: CRawlog.cpp:118
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:89
std::multimap< mrpt::system::TTimeStamp, CObservation::Ptr > TListTimeAndObservations
For usage with CRawlog classes.
Definition: CRawlog.h:24
CObservation::Ptr getAsObservation(size_t index) const
Returns the i&#39;th element in the sequence, as being an observation, where index=0 is the first object...
Definition: CRawlog.cpp:102
GLuint index
Definition: glext.h:4068
const_iterator operator++(int)
Definition: CRawlog.h:303
This class stores a rawlog (robotic datasets) in one of two possible formats:
Definition: CRawlog.h:65
This namespace contains representation of robot actions and observations.
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:51
void setCommentText(const std::string &t)
Changes the block of comment text for the rawlog.
Definition: CRawlog.cpp:554
GLsizei const GLchar ** string
Definition: glext.h:4116
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:287
CSensoryFrame::Ptr getAsObservations(size_t index) const
Returns the i&#39;th element in the sequence, as being an action, where index=0 is the first object...
Definition: CRawlog.cpp:146
Declares a class for storing a robot action.
Definition: CAction.h:24
TEntryType getType() const
Definition: CRawlog.h:326
virtual ~iterator()=default
void addActionsMemoryReference(const CActionCollection::Ptr &action)
Add a set of actions to the sequence, using a smart pointer to the object to add. ...
Definition: CRawlog.cpp:50
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
TEntryType
The type of each entry in a rawlog.
Definition: CRawlog.h:92
TListObjects::iterator m_it
Definition: CRawlog.h:234
bool operator!=(const iterator &o)
Definition: CRawlog.h:243
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
iterator end()
Definition: CRawlog.h:341
void swap(CRawlog &obj)
Efficiently swap the contents of two existing objects.
Definition: CRawlog.cpp:304
void clear()
Clear the sequence of actions/observations.
Definition: CRawlog.cpp:32
TEntryType getType(size_t index) const
Returns the type of a given element.
Definition: CRawlog.cpp:127
void addAction(CAction &action)
Add an action to the sequence: a collection of just one element is created.
Definition: CRawlog.cpp:78
iterator operator++(int)
Definition: CRawlog.h:245
iterator(const TListObjects::iterator &it)
Definition: CRawlog.h:238
static iterator erase(TListObjects &lst, const iterator &it)
Definition: CRawlog.h:279
~CRawlog() override
Destructor:
Definition: CRawlog.cpp:31
static bool readActionObservationPair(mrpt::serialization::CArchive &inStream, CActionCollection::Ptr &action, CSensoryFrame::Ptr &observations, size_t &rawlogEntry)
Reads a consecutive pair action / observation from the rawlog opened at some input stream...
Definition: CRawlog.cpp:311
size_t size() const
Returns the number of actions / observations object in the sequence.
Definition: CRawlog.cpp:85
void getCommentTextAsConfigFile(mrpt::config::CConfigFileMemory &memCfg) const
Saves the block of comment text for the rawlog into the passed config file object.
Definition: CRawlog.cpp:548
CObservationComment m_commentTexts
Comments of the rawlog.
Definition: CRawlog.h:75
bool operator==(const const_iterator &o)
Definition: CRawlog.h:296
const mrpt::serialization::CSerializable::Ptr operator*() const
Definition: CRawlog.h:298
const_iterator begin() const
Definition: CRawlog.h:338
void addObservationMemoryReference(const CObservation::Ptr &observation)
Add a single observation to the sequence, using a smart pointer to the object to add.
Definition: CRawlog.cpp:65
iterator & operator=(const iterator &o)=default



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019