MRPT  1.9.9
CSensoryFrame.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-2018, 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 CSENSORYFRAME_H
10 #define CSENSORYFRAME_H
11 
13 #include <mrpt/maps/CMetricMap.h>
14 #include <mrpt/obs/CObservation.h>
15 
16 namespace mrpt::obs
17 {
18 /** Declares a class for storing a "sensory frame", a set of "observations"
19  * taken by the robot approximately at the same time as one "snapshot" of the
20  * environment.
21  * It can contain "observations" of many different kinds.
22  *
23  * New observations can be added using:
24  *
25  * \code
26  * CObservationXXX::Ptr o = mrpt::make_aligned_shared<CObservationXXX>();
27  * // Create
28  * a smart pointer containing an object of class "CObservationXXX"
29  * o->(...)
30  *
31  * CSensoryFrame sf;
32  * sf.insert(o);
33  * \endcode
34  *
35  * The following methods are equivalent for adding new observations to a
36  * "sensory frame":
37  * - CSensoryFrame::operator +=
38  * - CSensoryFrame::push_back
39  * - CSensoryFrame::insert
40  *
41  * To examine the objects within a sensory frame, the following methods exist:
42  * - CSensoryFrame::getObservationByClass : Looks for some specific observation
43  * class.
44  * - CSensoryFrame::begin : To iterate over all observations.
45  * - CSensoryFrame::getObservationByIndex : To query by index.
46  *
47  * Notice that contained observations objects are automatically deleted on
48  * this object's destruction or clear.
49  * \sa CObservation
50  * \ingroup mrpt_obs_grp
51  */
53 {
55 
56  public:
57  /** Default constructor
58  */
59  CSensoryFrame() = default;
60 
61  /** Copy constructor
62  */
64 
65  /** @name Cached points map
66  @{ */
67  protected:
68  /** A points map, build only under demand by the methods getAuxPointsMap()
69  * and buildAuxPointsMap().
70  * It's a generic smart pointer to avoid depending here in the library
71  * mrpt-obs on classes on other libraries.
72  */
74 
75  /** Internal method, used from buildAuxPointsMap() */
76  void internal_buildAuxPointsMap(const void* options = nullptr) const;
77 
78  public:
79  /** Returns the cached points map representation of the scan, if already
80  * build with buildAuxPointsMap(), or nullptr otherwise.
81  * Usage:
82  * \code
83  * mrpt::maps::CPointsMap *map =
84  * obs->getAuxPointsMap<mrpt::maps::CPointsMap>();
85  * \endcode
86  * \sa buildAuxPointsMap
87  */
88  template <class POINTSMAP>
89  inline const POINTSMAP* getAuxPointsMap() const
90  {
91  return static_cast<POINTSMAP*>(m_cachedMap.get());
92  }
93 
94  /** Returns a cached points map representing this laser scan, building it
95  * upon the first call.
96  * \param options Can be nullptr to use default point maps' insertion
97  * options, or a pointer to a "CPointsMap::TInsertionOptions" structure to
98  * override some params.
99  * Usage:
100  * \code
101  * mrpt::maps::CPointsMap *map =
102  * sf->buildAuxPointsMap<mrpt::maps::CPointsMap>(&options or nullptr);
103  * \endcode
104  * \sa getAuxPointsMap
105  */
106  template <class POINTSMAP>
107  inline const POINTSMAP* buildAuxPointsMap(
108  const void* options = nullptr) const
109  {
111  return static_cast<POINTSMAP*>(m_cachedMap.get());
112  }
113 
114  /** @} */
115 
116  /** Copy
117  */
119 
120  /** Clear all current observations.
121  */
122  void clear();
123 
124  /** Insert all the observations in this SF into a metric map or any kind
125  *(see mrpt::maps::CMetricMap).
126  * It calls CObservation::insertObservationInto for all stored observation.
127  * \param theMap The map where this observation is to be inserted: the map
128  *will be updated.
129  * \param robotPose The pose of the robot base for this observation,
130  *relative to the target metric map. Set to nullptr (default) to use
131  *(0,0,0deg)
132  *
133  * \return Returns true if the map has been updated, or false if this
134  *observations
135  * has nothing to do with a metric map (for example, a sound
136  *observation).
137  *
138  * \sa mrpt::maps::CMetricMap, CObservation::insertObservationInto,
139  *CMetricMap::insertObservation
140  */
142  mrpt::maps::CMetricMap* theMap,
143  const mrpt::poses::CPose3D* robotPose = nullptr) const;
144 
145  /** Insert all the observations in this SF into a metric map or any kind
146  *(see mrpt::maps::CMetricMap).
147  * It calls CObservation::insertObservationInto for all stored observation.
148  * \param theMap The map where this observation is to be inserted: the map
149  *will be updated.
150  * \param robotPose The pose of the robot base for this observation,
151  *relative to the target metric map. Set to nullptr (default) to use
152  *(0,0,0deg)
153  *
154  * \return Returns true if the map has been updated, or false if this
155  *observations
156  * has nothing to do with a metric map (for example, a sound
157  *observation).
158  *
159  * \sa mrpt::maps::CMetricMap, CObservation::insertObservationInto,
160  *CMetricMap::insertObservation
161  */
164  const mrpt::poses::CPose3D* robotPose = nullptr) const
165  {
166  return insertObservationsInto(theMap.get(), robotPose);
167  }
168 
169  /** You can use "sf1+=sf2;" to add observations in sf2 to sf1. Objects are
170  * copied, not referenced, thus the source can be safely deleted next.
171  * \sa moveFrom
172  */
173  void operator+=(const CSensoryFrame& sf);
174 
175  /** You can use "sf+=obs;" to add the observation "obs" to the "sf1".
176  * Objects are copied, using the smart pointer, thus the original pointer
177  * can be safely deleted next.
178  * \sa moveFrom
179  */
180  void operator+=(const CObservation::Ptr& obs);
181 
182  /** Copies all the observation from another object, then erase them from the
183  * origin object (this method is fast since only pointers are copied);
184  * Previous objects in this objects are not deleted.
185  * \sa operator +=
186  */
187  void moveFrom(CSensoryFrame& sf);
188 
189  /** Inserts a new observation to the list: The pointer to the objects is
190  * copied, thus DO NOT delete the passed object, this class will do at
191  * destructor or when appropriate.
192  */
193  void push_back(const CObservation::Ptr& obs);
194 
195  /** Inserts a new observation to the list: The pointer to the objects is
196  * copied, thus DO NOT delete the passed object, this class will do at
197  * destructor or when appropriate.
198  */
199  void insert(const CObservation::Ptr& obs);
200 
201  /** Returns the i'th observation of a given class (or of a descendant
202  class), or nullptr if there is no such observation in the array.
203  * Example:
204  * \code
205  CObservationImage::Ptr obs =
206  m_SF->getObservationByClass<CObservationImage>();
207  * \endcode
208  * By default (ith=0), the first observation is returned.
209  */
210  template <typename T>
211  typename T::Ptr getObservationByClass(const size_t& ith = 0) const
212  {
213  MRPT_START
214  size_t foundCount = 0;
215  const mrpt::rtti::TRuntimeClassId* class_ID =
216  &T::GetRuntimeClassIdStatic();
217  for (const_iterator it = begin(); it != end(); ++it)
218  if ((*it)->GetRuntimeClass()->derivedFrom(class_ID))
219  if (foundCount++ == ith)
220  return std::dynamic_pointer_cast<T>(*it);
221  return typename T::Ptr(); // Not found: return empty smart pointer
222  MRPT_END
223  }
224 
225  /** You can use CSensoryFrame::begin to get a iterator to the first element.
226  */
228 
229  /** You can use CSensoryFrame::begin to get a iterator to the first element.
230  */
232 
233  /** Returns a constant iterator to the first observation: this is an example
234  *of usage:
235  * \code
236  * CSensoryFrame sf;
237  * ...
238  * for (CSensoryFrame::const_iterator it=sf.begin();it!=sf.end();++it)
239  * {
240  * (*it)->... // (*it) is a "CObservation*"
241  * }
242  *
243  * \endcode
244  */
245  const_iterator begin() const { return m_observations.begin(); }
246  /** Returns a constant iterator to the end of the list of observations: this
247  *is an example of usage:
248  * \code
249  * CSensoryFrame sf;
250  * ...
251  * for (CSensoryFrame::const_iterator it=sf.begin();it!=sf.end();++it)
252  * {
253  * (*it)->... // (*it) is a "CObservation*"
254  * }
255  *
256  * \endcode
257  */
258  const_iterator end() const { return m_observations.end(); }
259  /** Returns a iterator to the first observation: this is an example of
260  *usage:
261  * \code
262  * CSensoryFrame sf;
263  * ...
264  * for (CSensoryFrame::iterator it=sf.begin();it!=sf.end();++it)
265  * {
266  * (*it)->... // (*it) is a "CObservation*"
267  * }
268  *
269  * \endcode
270  */
271  iterator begin() { return m_observations.begin(); }
272  /** Returns a iterator to the end of the list of observations: this is an
273  *example of usage:
274  * \code
275  * CSensoryFrame sf;
276  * ...
277  * for (CSensoryFrame::iterator it=sf.begin();it!=sf.end();++it)
278  * {
279  * (*it)->... // (*it) is a "CObservation*"
280  * }
281  *
282  * \endcode
283  */
284  inline iterator end() { return m_observations.end(); }
285  /** Returns the number of observations in the list. */
286  inline size_t size() const { return m_observations.size(); }
287  /** Returns true if there are no observations in the list. */
288  inline bool empty() const { return m_observations.empty(); }
289  /** Removes the i'th observation in the list (0=first). */
290  void eraseByIndex(const size_t& idx);
291 
292  /** Removes the given observation in the list, and return an iterator to the
293  * next element (or this->end() if it was the last one).
294  */
295  iterator erase(const iterator& it);
296 
297  /** Removes all the observations that match a given sensorLabel.
298  */
299  void eraseByLabel(const std::string& label);
300 
301  /** Returns the i'th observation in the list (0=first).
302  * \sa begin, size
303  */
304  CObservation::Ptr getObservationByIndex(const size_t& idx) const;
305 
306  /** Returns the i'th observation in the list (0=first), and as a different
307  * smart pointer type:
308  * \code
309  * sf.getObservationByIndexAs<CObservationStereoImages::Ptr>(i);
310  * \endcode
311  * \sa begin, size
312  */
313  template <typename T>
314  T getObservationByIndexAs(const size_t& idx) const
315  {
316  return std::dynamic_pointer_cast<typename T::element_type>(
317  getObservationByIndex(idx));
318  }
319 
320  /** Returns the i'th observation in the list with the given "sensorLabel"
321  * (0=first).
322  * \return The observation, or nullptr if not found.
323  * \sa begin, size
324  */
326  const std::string& label, const size_t& idx = 0) const;
327 
328  /** Returns the i'th observation in the list with the given "sensorLabel"
329  * (0=first), and as a different smart pointer type:
330  * \code
331  * sf.getObservationBySensorLabelAs<CObservationStereoImages::Ptr>(i);
332  * \endcode
333  * \sa begin, size
334  */
335  template <typename T>
337  const std::string& label, const size_t& idx = 0) const
338  {
339  return std::dynamic_pointer_cast<typename T::element_type>(
340  getObservationBySensorLabel(label, idx));
341  }
342 
343  /** Efficiently swaps the contents of two objects.
344  */
345  void swap(CSensoryFrame& sf);
346 
347  protected:
348  /** The set of observations taken at the same time instant. See the top of
349  * this page for instructions on accessing this.
350  */
351  // std::deque<CObservation*> m_observations;
352  std::deque<CObservation::Ptr> m_observations;
353 
354 }; // End of class def.
355 
356 }
357 #endif
358 
359 
void clear()
Clear all current observations.
Scalar * iterator
Definition: eigen_plugins.h:26
void insert(const CObservation::Ptr &obs)
Inserts a new observation to the list: The pointer to the objects is copied, thus DO NOT delete the p...
std::deque< CObservation::Ptr >::iterator iterator
You can use CSensoryFrame::begin to get a iterator to the first element.
#define MRPT_START
Definition: exceptions.h:262
void push_back(const CObservation::Ptr &obs)
Inserts a new observation to the list: The pointer to the objects is copied, thus DO NOT delete the p...
void moveFrom(CSensoryFrame &sf)
Copies all the observation from another object, then erase them from the origin object (this method i...
CSensoryFrame & operator=(const CSensoryFrame &o)
Copy.
A structure that holds runtime class type information.
Definition: CObject.h:30
mrpt::maps::CMetricMap::Ptr m_cachedMap
A points map, build only under demand by the methods getAuxPointsMap() and buildAuxPointsMap().
Definition: CSensoryFrame.h:73
iterator erase(const iterator &it)
Removes the given observation in the list, and return an iterator to the next element (or this->end()...
CSensoryFrame()=default
Default constructor.
const_iterator begin() const
Returns a constant iterator to the first observation: this is an example of usage: ...
void swap(CSensoryFrame &sf)
Efficiently swaps the contents of two objects.
T getObservationBySensorLabelAs(const std::string &label, const size_t &idx=0) const
Returns the i&#39;th observation in the list with the given "sensorLabel" (0=first), and as a different s...
void eraseByIndex(const size_t &idx)
Removes the i&#39;th observation in the list (0=first).
void operator+=(const CSensoryFrame &sf)
You can use "sf1+=sf2;" to add observations in sf2 to sf1.
std::deque< CObservation::Ptr >::const_iterator const_iterator
You can use CSensoryFrame::begin to get a iterator to the first element.
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:52
T getObservationByIndexAs(const size_t &idx) const
Returns the i&#39;th observation in the list (0=first), and as a different smart pointer type: ...
GLsizei const GLchar ** string
Definition: glext.h:4101
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
CObservation::Ptr getObservationBySensorLabel(const std::string &label, const size_t &idx=0) const
Returns the i&#39;th observation in the list with the given "sensorLabel" (0=first).
const POINTSMAP * buildAuxPointsMap(const void *options=nullptr) const
Returns a cached points map representing this laser scan, building it upon the first call...
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:54
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
const POINTSMAP * getAuxPointsMap() const
Returns the cached points map representation of the scan, if already build with buildAuxPointsMap(), or nullptr otherwise.
Definition: CSensoryFrame.h:89
#define MRPT_END
Definition: exceptions.h:266
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
iterator begin()
Returns a iterator to the first observation: this is an example of usage:
bool insertObservationsInto(mrpt::maps::CMetricMap::Ptr &theMap, const mrpt::poses::CPose3D *robotPose=nullptr) const
Insert all the observations in this SF into a metric map or any kind (see mrpt::maps::CMetricMap).
const_iterator end() const
Returns a constant iterator to the end of the list of observations: this is an example of usage: ...
CObservation::Ptr getObservationByIndex(const size_t &idx) const
Returns the i&#39;th observation in the list (0=first).
T::Ptr getObservationByClass(const size_t &ith=0) const
Returns the i&#39;th observation of a given class (or of a descendant class), or nullptr if there is no s...
iterator end()
Returns a iterator to the end of the list of observations: this is an example of usage: ...
bool insertObservationsInto(mrpt::maps::CMetricMap *theMap, const mrpt::poses::CPose3D *robotPose=nullptr) const
Insert all the observations in this SF into a metric map or any kind (see mrpt::maps::CMetricMap).
size_t size() const
Returns the number of observations in the list.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
std::deque< CObservation::Ptr > m_observations
The set of observations taken at the same time instant.
void eraseByLabel(const std::string &label)
Removes all the observations that match a given sensorLabel.
bool empty() const
Returns true if there are no observations in the list.
void internal_buildAuxPointsMap(const void *options=nullptr) const
Internal method, used from buildAuxPointsMap()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020