MRPT  2.0.4
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-2020, 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  CRawlog() = default;
79  virtual ~CRawlog() override = default;
80 
81  /** Returns the block of comment text for the rawlog */
82  void getCommentText(std::string& t) const;
83  /** Returns the block of comment text for the rawlog */
84  std::string getCommentText() const;
85  /** Changes the block of comment text for the rawlog */
86  void setCommentText(const std::string& t);
87  /** Saves the block of comment text for the rawlog into the passed config
88  * file object. */
90  mrpt::config::CConfigFileMemory& memCfg) const;
91 
92  /** The type of each entry in a rawlog.
93  * \sa CRawlog::getType
94  */
96  {
101  };
102 
103  /** Clear the sequence of actions/observations. Smart pointers to objects
104  * previously in the rawlog will remain being valid. */
105  void clear();
106 
107  /** Add an action to the sequence: a collection of just one element is
108  * created.
109  * The object is duplicated, so the original one can be freed if desired.
110  */
111  void insert(CAction& action);
112 
113  /** Add a set of actions to the sequence; the object is duplicated, so the
114  * original one can be freed if desired.
115  * \sa insert, insert
116  */
117  void insert(CActionCollection& action);
118 
119  /** Add a set of observations to the sequence; the object is duplicated, so
120  * the original one can be free if desired.
121  */
122  void insert(CSensoryFrame& observations);
123 
124  /** Generic add for a smart pointer to a CSerializable object:
125  */
127 
128  /** Load the contents from a file containing one of these possibilities:
129  * - A "CRawlog" object.
130  * - Directly the sequence of objects (pairs
131  * `CSensoryFrame`/`CActionCollection` or `CObservation*` objects). In this
132  * case the method stops reading on EOF of an unrecogniced class name.
133  * - Only if `non_obs_objects_are_legal` is true, any `CSerializable`
134  * object is allowed in the log file. Otherwise, the read stops on classes
135  * different from the ones listed in the item above.
136  * \returns It returns false upon error reading or accessing the file.
137  */
138  bool loadFromRawLogFile(
139  const std::string& fileName, bool non_obs_objects_are_legal = false);
140 
141  /** Saves the contents to a rawlog-file, compatible with RawlogViewer (As
142  * the sequence of internal objects).
143  * The file is saved with gz-commpressed if MRPT has gz-streams.
144  * \returns It returns false if any error is found while writing/creating
145  * the target file.
146  */
147  bool saveToRawLogFile(const std::string& fileName) const;
148 
149  /** Returns the number of actions / observations object in the sequence. */
150  size_t size() const;
151 
152  /** Returns the type of a given element.
153  * \sa isAction, isObservation
154  */
155  TEntryType getType(size_t index) const;
156 
157  /** Delete the action or observation stored in the given index.
158  * \exception std::exception If index is out of bounds
159  */
160  void remove(size_t index);
161 
162  /** Delete the elements stored in the given range of indices (including both
163  * the first and last one).
164  * \exception std::exception If any index is out of bounds
165  */
166  void remove(size_t first_index, size_t last_index);
167 
168  /** Returns the i'th element in the sequence, as being actions, where
169  * index=0 is the first object.
170  * If it is not a CActionCollection, it throws an exception. Do neighter
171  * modify nor delete the returned pointer.
172  * \sa size, isAction, getAsObservations, getAsObservation
173  * \exception std::exception If index is out of bounds
174  */
175  CActionCollection::Ptr getAsAction(size_t index) const;
176 
177  /** Returns the i'th element in the sequence, as being an action, where
178  * index=0 is the first object.
179  * If it is not an CSensoryFrame, it throws an exception. Do neighter
180  * modify nor delete the returned pointer.
181  * \sa size, isAction, getAsAction, getAsObservation
182  * \exception std::exception If index is out of bounds
183  */
184  CSensoryFrame::Ptr getAsObservations(size_t index) const;
185 
186  /** Returns the i'th element in the sequence, being its class whatever.
187  * \sa size, isAction, getAsAction, getAsObservations
188  * \exception std::exception If index is out of bounds
189  */
191 
192  /** Returns the i'th element in the sequence, as being an observation, where
193  * index=0 is the first object.
194  * If it is not an CObservation, it throws an exception. Do neighter
195  * modify nor delete the returned pointer.
196  * This is the proper method to obtain the objects stored in a "only
197  * observations"-rawlog file (named "format #2" above.
198  * \sa size, isAction, getAsAction
199  * \exception std::exception If index is out of bounds
200  */
201  CObservation::Ptr getAsObservation(size_t index) const;
202 
203  /** Get the i'th observation as an observation of the given type.
204  * \exception std::exception If index is out of bounds, or type not
205  * compatible.
206  */
207  template <class T>
208  typename T::ConstPtr asObservation(size_t index) const
209  {
210  MRPT_START
211  auto ptr = std::dynamic_pointer_cast<const T>(getAsObservation(index));
212  ASSERTMSG_(ptr, "Could not convert observation to specified class");
213  return ptr;
214  MRPT_END
215  }
216 
217  template <class T>
218  typename T::Ptr asObservation(size_t index)
219  {
220  MRPT_START
221  auto ptr = std::dynamic_pointer_cast<T>(getAsObservation(index));
222  ASSERTMSG_(ptr, "Could not convert observation to specified class");
223  return ptr;
224  MRPT_END
225  }
226 
227  /** A normal iterator, plus the extra method "getType" to determine the
228  * type of each entry in the sequence. */
229  class iterator
230  {
231  protected:
232  TListObjects::iterator m_it;
233 
234  public:
235  iterator() : m_it() {}
236  iterator(const TListObjects::iterator& it) : m_it(it) {}
237  virtual ~iterator() = default;
238  iterator& operator=(const iterator& o) = default;
239 
240  bool operator==(const iterator& o) { return m_it == o.m_it; }
241  bool operator!=(const iterator& o) { return m_it != o.m_it; }
243  inline iterator operator++(int)
244  {
245  iterator aux = *this;
246  m_it++;
247  return aux;
248  } // Post
250  {
251  m_it++;
252  return *this;
253  } // Pre
254  inline iterator operator--(int)
255  {
256  iterator aux = *this;
257  m_it--;
258  return aux;
259  } // Post
261  {
262  m_it--;
263  return *this;
264  } // Pre
265 
267  {
268  if ((*m_it)->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
269  return etObservation;
270  else if ((*m_it)->GetRuntimeClass()->derivedFrom(
272  return etSensoryFrame;
273  else
274  return etActionCollection;
275  }
276 
277  static iterator erase(TListObjects& lst, const iterator& it)
278  {
279  return lst.erase(it.m_it);
280  }
281  };
282 
283  /** A normal iterator, plus the extra method "getType" to determine the type
284  * of each entry in the sequence. */
286  {
287  protected:
288  TListObjects::const_iterator m_it;
289 
290  public:
292  const_iterator(const TListObjects::const_iterator& it) : m_it(it) {}
293  virtual ~const_iterator() = default;
294  bool operator==(const const_iterator& o) { return m_it == o.m_it; }
295  bool operator!=(const const_iterator& o) { return m_it != o.m_it; }
297  {
298  return *m_it;
299  }
300 
302  {
303  const_iterator aux = *this;
304  m_it++;
305  return aux;
306  } // Post
308  {
309  m_it++;
310  return *this;
311  } // Pre
313  {
314  const_iterator aux = *this;
315  m_it--;
316  return aux;
317  } // Post
319  {
320  m_it--;
321  return *this;
322  } // Pre
323 
325  {
326  if ((*m_it)->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
327  return etObservation;
328  else if ((*m_it)->GetRuntimeClass()->derivedFrom(
330  return etSensoryFrame;
331  else
332  return etActionCollection;
333  }
334  };
335 
336  const_iterator begin() const { return m_seqOfActObs.begin(); }
337  iterator begin() { return m_seqOfActObs.begin(); }
338  const_iterator end() const { return m_seqOfActObs.end(); }
339  iterator end() { return m_seqOfActObs.end(); }
341  {
342  return iterator::erase(m_seqOfActObs, it);
343  }
344 
345  /** Returns the sub-set of observations of a given class whose time-stamp t
346  * fulfills time_start <= t < time_end.
347  * This method requires the timestamps of the sensors to be in strict
348  * ascending order (which should be the normal situation).
349  * Otherwise, the output is undeterminate.
350  * \sa findClosestObservationsByClass
351  */
354  const mrpt::rtti::TRuntimeClassId* class_type,
355  TListTimeAndObservations& out_found,
356  size_t guess_start_position = 0) const;
357 
358  /** Efficiently swap the contents of two existing objects.
359  */
360  void swap(CRawlog& obj);
361 
362  /** Reads a consecutive pair action / observation from the rawlog opened at
363  * some input stream.
364  * Previous contents of action and observations are discarded (using
365  * stlplus::smart_ptr::reset), and
366  * at exit they contain the new objects read from the rawlog file.
367  * The input/output variable "rawlogEntry" is just a counter of the last
368  * rawlog entry read, for logging or monitoring purposes.
369  * \return false if there was some error, true otherwise.
370  * \sa getActionObservationPair, getActionObservationPairOrObservation
371  */
372  static bool readActionObservationPair(
374  CSensoryFrame::Ptr& observations, size_t& rawlogEntry);
375 
376  /** Reads a consecutive pair action/sensory_frame OR an observation,
377  *depending of the rawlog format, from the rawlog opened at some input
378  *stream.
379  * Previous contents of action and observations are discarded (using
380  *stlplus::smart_ptr::reset), and
381  * at exit they contain the new objects read from the rawlog file.
382  *
383  * At return, one of this will happen:
384  * - action/observations contain objects (i.e. action() evaluates as
385  *true).
386  * - observation contains an object (i.e. observation evaluates as
387  *true).
388  *
389  * The input/output variable "rawlogEntry" is just a counter of the last
390  *rawlog entry read, for logging or monitoring purposes.
391  * \return false if there was some error, true otherwise.
392  * \sa getActionObservationPair
393  */
396  CSensoryFrame::Ptr& observations, CObservation::Ptr& observation,
397  size_t& rawlogEntry);
398 
399  /** Gets the next consecutive pair action / observation from the rawlog
400  * loaded into this object.
401  * Previous contents of action and observations are discarded (using
402  * stlplus::smart_ptr::reset), and
403  * at exit they contain the new objects read from the rawlog file.
404  * The input/output variable "rawlogEntry" is just a counter of the last
405  * rawlog entry read, for logging or monitoring purposes.
406  * \return false if there was some error, true otherwise.
407  * \sa readActionObservationPair
408  */
410  CActionCollection::Ptr& action, CSensoryFrame::Ptr& observations,
411  size_t& rawlogEntry) const;
412 
413  /** Tries to auto-detect the external-images directory of the given rawlog
414  *file.
415  * This searches for the existence of the directories:
416  * - "<rawlog_file_path>/<rawlog_filename>_Images"
417  * - "<rawlog_file_path>/<rawlog_filename>_images"
418  * - "<rawlog_file_path>/<rawlog_filename>_IMAGES"
419  * - "<rawlog_file_path>/Images" (This one is returned if none of the
420  *choices actually exists).
421  *
422  * The results from this function should be written into
423  *mrpt::img::CImage::getImagesPathBase() to enable automatic
424  * loading of extenrnally-stored images in rawlogs.
425  */
426  static std::string detectImagesDirectory(const std::string& rawlogFilename);
427 
428 }; // End of class def.
429 
430 } // 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:240
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:411
#define MRPT_START
Definition: exceptions.h:241
TListObjects m_seqOfActObs
The list where the objects really are in.
Definition: CRawlog.h:72
const_iterator end() const
Definition: CRawlog.h:338
std::vector< mrpt::serialization::CSerializable::Ptr > TListObjects
Definition: CRawlog.h:70
iterator begin()
Definition: CRawlog.h:337
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:492
static std::string detectImagesDirectory(const std::string &rawlogFilename)
Tries to auto-detect the external-images directory of the given rawlog file.
Definition: CRawlog.cpp:534
T::ConstPtr asObservation(size_t index) const
Get the i&#39;th observation as an observation of the given type.
Definition: CRawlog.h:208
iterator erase(const iterator &it)
Definition: CRawlog.h:340
const_iterator & operator++()
Definition: CRawlog.h:307
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:229
const_iterator & operator--()
Definition: CRawlog.h:318
const_iterator operator--(int)
Definition: CRawlog.h:312
A structure that holds runtime class type information.
Definition: CObject.h:31
virtual ~CRawlog() override=default
mrpt::serialization::CSerializable::Ptr operator*()
Definition: CRawlog.h:242
const_iterator(const TListObjects::const_iterator &it)
Definition: CRawlog.h:292
TListObjects::const_iterator m_it
Definition: CRawlog.h:288
TEntryType getType() const
Definition: CRawlog.h:266
iterator operator--(int)
Definition: CRawlog.h:254
bool operator!=(const const_iterator &o)
Definition: CRawlog.h:295
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:349
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:266
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:65
std::string getCommentText() const
Returns the block of comment text for the rawlog.
Definition: CRawlog.cpp:526
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:166
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:97
#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:81
const_iterator operator++(int)
Definition: CRawlog.h:301
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:533
A normal iterator, plus the extra method "getType" to determine the type of each entry in the sequenc...
Definition: CRawlog.h:285
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:125
Declares a class for storing a robot action.
Definition: CAction.h:24
TEntryType getType() const
Definition: CRawlog.h:324
virtual ~iterator()=default
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:95
TListObjects::iterator m_it
Definition: CRawlog.h:232
#define MRPT_END
Definition: exceptions.h:245
void insert(CAction &action)
Add an action to the sequence: a collection of just one element is created.
Definition: CRawlog.cpp:57
bool operator!=(const iterator &o)
Definition: CRawlog.h:241
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
iterator end()
Definition: CRawlog.h:339
void swap(CRawlog &obj)
Efficiently swap the contents of two existing objects.
Definition: CRawlog.cpp:283
void clear()
Clear the sequence of actions/observations.
Definition: CRawlog.cpp:28
#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:106
iterator operator++(int)
Definition: CRawlog.h:243
iterator(const TListObjects::iterator &it)
Definition: CRawlog.h:236
T::Ptr asObservation(size_t index)
Definition: CRawlog.h:218
static iterator erase(TListObjects &lst, const iterator &it)
Definition: CRawlog.h:277
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:290
size_t size() const
Returns the number of actions / observations object in the sequence.
Definition: CRawlog.cpp:64
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:527
CObservationComment m_commentTexts
Comments of the rawlog.
Definition: CRawlog.h:75
bool operator==(const const_iterator &o)
Definition: CRawlog.h:294
const mrpt::serialization::CSerializable::Ptr operator*() const
Definition: CRawlog.h:296
const_iterator begin() const
Definition: CRawlog.h:336
iterator & operator=(const iterator &o)=default



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020