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  /** Get the i'th observation as an observation of the given type.
230  * \exception std::exception If index is out of bounds, or type not
231  * compatible.
232  */
233  template <class T>
234  typename T::ConstPtr asObservation(size_t index) const
235  {
236  MRPT_START
237  auto ptr = std::dynamic_pointer_cast<const T>(getAsObservation(index));
238  ASSERTMSG_(ptr, "Could not convert observation to specified class");
239  return ptr;
240  MRPT_END
241  }
242 
243  template <class T>
244  typename T::Ptr asObservation(size_t index)
245  {
246  MRPT_START
247  auto ptr = std::dynamic_pointer_cast<T>(getAsObservation(index));
248  ASSERTMSG_(ptr, "Could not convert observation to specified class");
249  return ptr;
250  MRPT_END
251  }
252 
253  /** A normal iterator, plus the extra method "getType" to determine the
254  * type of each entry in the sequence. */
255  class iterator
256  {
257  protected:
258  TListObjects::iterator m_it;
259 
260  public:
261  iterator() : m_it() {}
262  iterator(const TListObjects::iterator& it) : m_it(it) {}
263  virtual ~iterator() = default;
264  iterator& operator=(const iterator& o) = default;
265 
266  bool operator==(const iterator& o) { return m_it == o.m_it; }
267  bool operator!=(const iterator& o) { return m_it != o.m_it; }
269  inline iterator operator++(int)
270  {
271  iterator aux = *this;
272  m_it++;
273  return aux;
274  } // Post
276  {
277  m_it++;
278  return *this;
279  } // Pre
280  inline iterator operator--(int)
281  {
282  iterator aux = *this;
283  m_it--;
284  return aux;
285  } // Post
287  {
288  m_it--;
289  return *this;
290  } // Pre
291 
293  {
294  if ((*m_it)->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
295  return etObservation;
296  else if ((*m_it)->GetRuntimeClass()->derivedFrom(
298  return etSensoryFrame;
299  else
300  return etActionCollection;
301  }
302 
303  static iterator erase(TListObjects& lst, const iterator& it)
304  {
305  return lst.erase(it.m_it);
306  }
307  };
308 
309  /** A normal iterator, plus the extra method "getType" to determine the type
310  * of each entry in the sequence. */
312  {
313  protected:
314  TListObjects::const_iterator m_it;
315 
316  public:
318  const_iterator(const TListObjects::const_iterator& it) : m_it(it) {}
319  virtual ~const_iterator() = default;
320  bool operator==(const const_iterator& o) { return m_it == o.m_it; }
321  bool operator!=(const const_iterator& o) { return m_it != o.m_it; }
323  {
324  return *m_it;
325  }
326 
328  {
329  const_iterator aux = *this;
330  m_it++;
331  return aux;
332  } // Post
334  {
335  m_it++;
336  return *this;
337  } // Pre
339  {
340  const_iterator aux = *this;
341  m_it--;
342  return aux;
343  } // Post
345  {
346  m_it--;
347  return *this;
348  } // Pre
349 
351  {
352  if ((*m_it)->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
353  return etObservation;
354  else if ((*m_it)->GetRuntimeClass()->derivedFrom(
356  return etSensoryFrame;
357  else
358  return etActionCollection;
359  }
360  };
361 
362  const_iterator begin() const { return m_seqOfActObs.begin(); }
363  iterator begin() { return m_seqOfActObs.begin(); }
364  const_iterator end() const { return m_seqOfActObs.end(); }
365  iterator end() { return m_seqOfActObs.end(); }
367  {
368  return iterator::erase(m_seqOfActObs, it);
369  }
370 
371  /** Returns the sub-set of observations of a given class whose time-stamp t
372  * fulfills time_start <= t < time_end.
373  * This method requires the timestamps of the sensors to be in strict
374  * ascending order (which should be the normal situation).
375  * Otherwise, the output is undeterminate.
376  * \sa findClosestObservationsByClass
377  */
380  const mrpt::rtti::TRuntimeClassId* class_type,
381  TListTimeAndObservations& out_found,
382  size_t guess_start_position = 0) const;
383 
384  /** Efficiently swap the contents of two existing objects.
385  */
386  void swap(CRawlog& obj);
387 
388  /** Reads a consecutive pair action / observation from the rawlog opened at
389  * some input stream.
390  * Previous contents of action and observations are discarded (using
391  * stlplus::smart_ptr::reset), and
392  * at exit they contain the new objects read from the rawlog file.
393  * The input/output variable "rawlogEntry" is just a counter of the last
394  * rawlog entry read, for logging or monitoring purposes.
395  * \return false if there was some error, true otherwise.
396  * \sa getActionObservationPair, getActionObservationPairOrObservation
397  */
398  static bool readActionObservationPair(
400  CSensoryFrame::Ptr& observations, size_t& rawlogEntry);
401 
402  /** Reads a consecutive pair action/sensory_frame OR an observation,
403  *depending of the rawlog format, from the rawlog opened at some input
404  *stream.
405  * Previous contents of action and observations are discarded (using
406  *stlplus::smart_ptr::reset), and
407  * at exit they contain the new objects read from the rawlog file.
408  *
409  * At return, one of this will happen:
410  * - action/observations contain objects (i.e. action() evaluates as
411  *true).
412  * - observation contains an object (i.e. observation evaluates as
413  *true).
414  *
415  * The input/output variable "rawlogEntry" is just a counter of the last
416  *rawlog entry read, for logging or monitoring purposes.
417  * \return false if there was some error, true otherwise.
418  * \sa getActionObservationPair
419  */
422  CSensoryFrame::Ptr& observations, CObservation::Ptr& observation,
423  size_t& rawlogEntry);
424 
425  /** Gets the next consecutive pair action / observation from the rawlog
426  * loaded into this object.
427  * Previous contents of action and observations are discarded (using
428  * stlplus::smart_ptr::reset), and
429  * at exit they contain the new objects read from the rawlog file.
430  * The input/output variable "rawlogEntry" is just a counter of the last
431  * rawlog entry read, for logging or monitoring purposes.
432  * \return false if there was some error, true otherwise.
433  * \sa readActionObservationPair
434  */
436  CActionCollection::Ptr& action, CSensoryFrame::Ptr& observations,
437  size_t& rawlogEntry) const;
438 
439  /** Tries to auto-detect the external-images directory of the given rawlog
440  *file.
441  * This searches for the existence of the directories:
442  * - "<rawlog_file_path>/<rawlog_filename>_Images"
443  * - "<rawlog_file_path>/<rawlog_filename>_images"
444  * - "<rawlog_file_path>/<rawlog_filename>_IMAGES"
445  * - "<rawlog_file_path>/Images" (This one is returned if none of the
446  *choices actually exists).
447  *
448  * The results from this function should be written into
449  *mrpt::img::CImage::getImagesPathBase() to enable automatic
450  * loading of extenrnally-stored images in rawlogs.
451  */
452  static std::string detectImagesDirectory(const std::string& rawlogFilename);
453 
454 }; // End of class def.
455 
456 } // 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:266
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
#define MRPT_START
Definition: exceptions.h:241
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:364
std::vector< mrpt::serialization::CSerializable::Ptr > TListObjects
Definition: CRawlog.h:70
iterator begin()
Definition: CRawlog.h:363
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
T::ConstPtr asObservation(size_t index) const
Get the i&#39;th observation as an observation of the given type.
Definition: CRawlog.h:234
iterator erase(const iterator &it)
Definition: CRawlog.h:366
const_iterator & operator++()
Definition: CRawlog.h:333
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:255
const_iterator & operator--()
Definition: CRawlog.h:344
const_iterator operator--(int)
Definition: CRawlog.h:338
A structure that holds runtime class type information.
Definition: CObject.h:31
mrpt::serialization::CSerializable::Ptr operator*()
Definition: CRawlog.h:268
const_iterator(const TListObjects::const_iterator &it)
Definition: CRawlog.h:318
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:314
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:292
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:280
bool operator!=(const const_iterator &o)
Definition: CRawlog.h:321
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:102
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:327
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
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
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:311
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:350
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
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
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:258
#define MRPT_END
Definition: exceptions.h:245
bool operator!=(const iterator &o)
Definition: CRawlog.h:267
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
iterator end()
Definition: CRawlog.h:365
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
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
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:269
iterator(const TListObjects::iterator &it)
Definition: CRawlog.h:262
T::Ptr asObservation(size_t index)
Definition: CRawlog.h:244
static iterator erase(TListObjects &lst, const iterator &it)
Definition: CRawlog.h:303
~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:320
const mrpt::serialization::CSerializable::Ptr operator*() const
Definition: CRawlog.h:322
const_iterator begin() const
Definition: CRawlog.h:362
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: 479715d5b Tue Nov 12 07:26:21 2019 +0100 at mar nov 12 07:30:12 CET 2019