Main MRPT website > C++ reference
MRPT logo
CSensoryFrame.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CSENSORYFRAME_H
29 #define CSENSORYFRAME_H
30 
31 #include <mrpt/slam/CObservation.h>
34 
35 
36 namespace mrpt
37 {
38  namespace slam
39  {
40  class CMetricMap;
41 
42  // This must be added to any CSerializable derived class:
44 
45  /** Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximately at the same time as one "snapshot" of the environment.
46  * It can contain "observations" of many different kinds.
47  *
48  * New observations can be added using:
49  *
50  * \code
51  * CObservationXXXPtr o = CObservationXXX::Create(); // Create a smart pointer containing an object of class "CObservationXXX"
52  * o->(...)
53  *
54  * CSensoryFrame sf;
55  * sf.insert(o);
56  * \endcode
57  *
58  * The following methods are equivalent for adding new observations to a "sensory frame":
59  * - CSensoryFrame::operator +=
60  * - CSensoryFrame::push_back
61  * - CSensoryFrame::insert
62  *
63  * To examine the objects within a sensory frame, the following methods exist:
64  * - CSensoryFrame::getObservationByClass : Looks for some specific observation class.
65  * - CSensoryFrame::begin : To iterate over all observations.
66  * - CSensoryFrame::getObservationByIndex : To query by index.
67  *
68  * Notice that contained observations objects are automatically deleted on
69  * this object's destruction or clear.
70  * \sa CObservation
71  * \ingroup mrpt_obs_grp
72  */
73  class OBS_IMPEXP CSensoryFrame : public mrpt::utils::CSerializable
74  {
75  // This must be added to any CSerializable derived class:
77 
78  public:
79  /** Default constructor
80  */
81  CSensoryFrame();
82 
83  /** Copy constructor
84  */
85  CSensoryFrame( const CSensoryFrame &);
86 
87  /** @name Cached points map
88  @{ */
89  protected:
90  /** A points map, build only under demand by the methods getAuxPointsMap() and buildAuxPointsMap().
91  * It's a generic smart pointer to avoid depending here in the library mrpt-obs on classes on other libraries.
92  */
93  mutable mrpt::slam::CMetricMapPtr m_cachedMap;
94 
95  void internal_buildAuxPointsMap( const void *options = NULL ) const; //!< Internal method, used from buildAuxPointsMap()
96 
97  public:
98 
99  /** Returns the cached points map representation of the scan, if already build with buildAuxPointsMap(), or NULL otherwise.
100  * Usage:
101  * \code
102  * mrpt::slam::CPointsMap *map = obs->getAuxPointsMap<mrpt::slam::CPointsMap>();
103  * \endcode
104  * \sa buildAuxPointsMap
105  */
106  template <class POINTSMAP>
107  inline const POINTSMAP* getAuxPointsMap() const {
108  return static_cast<POINTSMAP*>(m_cachedMap.pointer());
109  }
110 
111  /** Returns a cached points map representing this laser scan, building it upon the first call.
112  * \param options Can be NULL to use default point maps' insertion options, or a pointer to a "CPointsMap::TInsertionOptions" structure to override some params.
113  * Usage:
114  * \code
115  * mrpt::slam::CPointsMap *map = sf->buildAuxPointsMap<mrpt::slam::CPointsMap>(&options or NULL);
116  * \endcode
117  * \sa getAuxPointsMap
118  */
119  template <class POINTSMAP>
120  inline const POINTSMAP *buildAuxPointsMap( const void *options = NULL ) const {
121  internal_buildAuxPointsMap(options);
122  return static_cast<POINTSMAP*>(m_cachedMap.pointer());
123  }
124 
125  /** @} */
126 
127 
128  /** Copy
129  */
130  CSensoryFrame& operator =( const CSensoryFrame &o);
131 
132  /** Destructor.
133  */
134  virtual ~CSensoryFrame();
135 
136  /** Clear all current observations.
137  */
138  void clear();
139 
140  /** Insert all the observations in this SF into a metric map or any kind (see mrpt::slam::CMetricMap).
141  * It calls CObservation::insertObservationInto for all stored observation.
142  * \param theMap The map where this observation is to be inserted: the map will be updated.
143  * \param robotPose The pose of the robot base for this observation, relative to the target metric map. Set to NULL (default) to use (0,0,0deg)
144  *
145  * \return Returns true if the map has been updated, or false if this observations
146  * has nothing to do with a metric map (for example, a sound observation).
147  *
148  * \sa mrpt::slam::CMetricMap, CObservation::insertObservationInto, CMetricMap::insertObservation
149  */
150  bool insertObservationsInto( mrpt::slam::CMetricMap *theMap, const CPose3D *robotPose = NULL ) const;
151 
152  /** Insert all the observations in this SF into a metric map or any kind (see mrpt::slam::CMetricMap).
153  * It calls CObservation::insertObservationInto for all stored observation.
154  * \param theMap The map where this observation is to be inserted: the map will be updated.
155  * \param robotPose The pose of the robot base for this observation, relative to the target metric map. Set to NULL (default) to use (0,0,0deg)
156  *
157  * \return Returns true if the map has been updated, or false if this observations
158  * has nothing to do with a metric map (for example, a sound observation).
159  *
160  * \sa mrpt::slam::CMetricMap, CObservation::insertObservationInto, CMetricMap::insertObservation
161  */
162  inline bool insertObservationsInto( mrpt::slam::CMetricMapPtr &theMap, const CPose3D *robotPose = NULL ) const
163  {
164  return insertObservationsInto(theMap.pointer(), robotPose);
165  }
166 
167 
168  /** You can use "sf1+=sf2;" to add observations in sf2 to sf1. Objects are copied, not referenced, thus the source can be safely deleted next.
169  * \sa moveFrom
170  */
171  void operator += (const CSensoryFrame &sf);
172 
173  /** You can use "sf+=obs;" to add the observation "obs" to the "sf1". Objects are copied, using the smart pointer, thus the original pointer can be safely deleted next.
174  * \sa moveFrom
175  */
176  void operator += (const CObservationPtr &obs);
177 
178  /** Copies all the observation from another object, then erase them from the origin object (this method is fast since only pointers are copied); Previous objects in this objects are not deleted.
179  * \sa operator +=
180  */
181  void moveFrom(CSensoryFrame &sf);
182 
183  /** Inserts a new observation to the list: The pointer to the objects is copied, thus DO NOT delete the passed object, this class will do at destructor or when appropriate.
184  */
185  void push_back(const CObservationPtr &obs);
186 
187  /** Inserts a new observation to the list: The pointer to the objects is copied, thus DO NOT delete the passed object, this class will do at destructor or when appropriate.
188  */
189  void insert(const CObservationPtr &obs);
190 
191  /** Returns the i'th observation of a given class (or of a descendant class), or NULL if there is no such observation in the array.
192  * Example:
193  * \code
194  CObservationImagePtr obs = m_SF->getObservationByClass<CObservationImage>();
195  * \endcode
196  * By default (ith=0), the first observation is returned.
197  */
198  template <typename T>
199  typename T::SmartPtr getObservationByClass( const size_t &ith = 0 ) const
200  {
201  MRPT_START
202  size_t foundCount = 0;
203  const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo;
204  for (const_iterator it = begin();it!=end();++it)
205  if ( (*it)->GetRuntimeClass()->derivedFrom( class_ID ) )
206  if (foundCount++ == ith)
207  return typename T::SmartPtr(*it);
208  return typename T::SmartPtr(); // Not found: return empty smart pointer
209  MRPT_END
210  }
211 
212  /** You can use CSensoryFrame::begin to get a iterator to the first element.
213  */
215 
216  /** You can use CSensoryFrame::begin to get a iterator to the first element.
217  */
219 
220  /** Returns a iterator to the first observation: this is an example of usage:
221  * \code
222  * CSensoryFrame sf;
223  * ...
224  * for (CSensoryFrame::iterator it=sf.begin();it!=sf.end();++it)
225  * {
226  * (*it)->... // (*it) is a "CObservation*"
227  * }
228  *
229  * \endcode
230  */
231  const_iterator begin() const { return m_observations.begin(); }
232 
233  /** Returns a iterator to the end of the list of observations: this is an example of usage:
234  * \code
235  * CSensoryFrame sf;
236  * ...
237  * for (CSensoryFrame::iterator it=sf.begin();it!=sf.end();++it)
238  * {
239  * (*it)->... // (*it) is a "CObservation*"
240  * }
241  *
242  * \endcode
243  */
244  const_iterator end() const { return m_observations.end(); }
245 
246  /** Returns a iterator to the first observation: this is an example of usage:
247  * \code
248  * CSensoryFrame sf;
249  * ...
250  * for (CSensoryFrame::iterator it=sf.begin();it!=sf.end();++it)
251  * {
252  * (*it)->... // (*it) is a "CObservation*"
253  * }
254  *
255  * \endcode
256  */
257  iterator begin() { return m_observations.begin(); }
258 
259  /** Returns a iterator to the end of the list of observations: this is an example of usage:
260  * \code
261  * CSensoryFrame sf;
262  * ...
263  * for (CSensoryFrame::iterator it=sf.begin();it!=sf.end();++it)
264  * {
265  * (*it)->... // (*it) is a "CObservation*"
266  * }
267  *
268  * \endcode
269  */
270  inline iterator end() { return m_observations.end(); }
271 
272 
273  /** Returns the number of observations in the list. */
274  inline size_t size() const { return m_observations.size(); }
275 
276  /** Returns true if there are no observations in the list. */
277  inline bool empty() const { return m_observations.empty(); }
278 
279  /** Removes the i'th observation in the list (0=first). */
280  void eraseByIndex(const size_t &idx);
281 
282  /** Removes the given observation in the list, and return an iterator to the next element (or this->end() if it was the last one).
283  */
284  iterator erase( const iterator &it);
285 
286  /** Removes all the observations that match a given sensorLabel.
287  */
288  void eraseByLabel(const std::string &label);
289 
290  /** Returns the i'th observation in the list (0=first).
291  * \sa begin, size
292  */
293  CObservationPtr getObservationByIndex( const size_t &idx ) const;
294 
295  /** Returns the i'th observation in the list (0=first), and as a different smart pointer type:
296  * \code
297  * sf.getObservationByIndexAs<CObservationStereoImagesPtr>(i);
298  * \endcode
299  * \sa begin, size
300  */
301  template <typename T>
302  T getObservationByIndexAs( const size_t &idx ) const
303  {
304  return static_cast<T>(getObservationByIndex(idx));
305  }
306 
307  /** Returns the i'th observation in the list with the given "sensorLabel" (0=first).
308  * \return The observation, or NULL if not found.
309  * \sa begin, size
310  */
311  CObservationPtr getObservationBySensorLabel( const std::string &label, const size_t &idx = 0) const;
312 
313  /** Returns the i'th observation in the list with the given "sensorLabel" (0=first), and as a different smart pointer type:
314  * \code
315  * sf.getObservationBySensorLabelAs<CObservationStereoImagesPtr>(i);
316  * \endcode
317  * \sa begin, size
318  */
319  template <typename T>
320  T getObservationBySensorLabelAs( const std::string &label, const size_t &idx = 0) const
321  {
322  return T(getObservationBySensorLabel(label,idx));
323  }
324 
325  /** Efficiently swaps the contents of two objects.
326  */
327  void swap( CSensoryFrame &sf );
328 
329  protected:
330  /** The set of observations taken at the same time instant. See the top of this page for instructions on accessing this.
331  */
332  //std::deque<CObservation*> m_observations;
333  std::deque<CObservationPtr> m_observations;
334 
335  }; // End of class def.
336 
337 
338  } // End of namespace
339 } // End of namespace
340 
341 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013