Main MRPT website > C++ reference for MRPT 1.9.9
CSensoryFrame.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, 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 
10 #include "obs-precomp.h" // Precompiled headers
11 
12 #include <mrpt/obs/CSensoryFrame.h>
14 #include <mrpt/utils/CStream.h>
15 #include <mrpt/system/os.h>
16 #include <iterator>
17 
18 using namespace mrpt::obs;
19 using namespace mrpt::poses;
20 using namespace mrpt::utils;
21 using namespace mrpt::system;
22 using namespace std;
23 
25 using namespace mrpt::utils::metaprogramming;
26 
28 
29 /*---------------------------------------------------------------
30  Default constructor
31  ---------------------------------------------------------------*/
32 CSensoryFrame::CSensoryFrame() : m_cachedMap(), m_observations() {}
33 /*---------------------------------------------------------------
34  Copy constructor
35  ---------------------------------------------------------------*/
36 CSensoryFrame::CSensoryFrame(const CSensoryFrame& o) : m_observations()
37 {
38  *this = o;
39 }
40 
41 /*---------------------------------------------------------------
42  Copy
43  ---------------------------------------------------------------*/
45 {
47 
48  clear();
49 
50  if (this == &o) return *this; // It may be used sometimes
51 
53 
54  m_cachedMap.reset();
55 
56  return *this;
57 
58  MRPT_END
59 }
60 
61 /*---------------------------------------------------------------
62  Destructor
63  ---------------------------------------------------------------*/
65 /*---------------------------------------------------------------
66  clear
67  ---------------------------------------------------------------*/
69 {
70  m_observations.clear();
71  m_cachedMap.reset();
72 }
73 
74 /*---------------------------------------------------------------
75  writeToStream
76  ---------------------------------------------------------------*/
77 void CSensoryFrame::writeToStream(mrpt::utils::CStream& out, int* version) const
78 {
79  if (version)
80  *version = 2;
81  else
82  {
83  uint32_t i, n;
84 
85  n = static_cast<uint32_t>(m_observations.size());
86  out << n;
87  for (i = 0; i < n; i++) out << *m_observations[i];
88  }
89 }
90 
91 /*---------------------------------------------------------------
92  readFromStream
93  ---------------------------------------------------------------*/
95 {
97 
98  switch (version)
99  {
100  case 0:
101  case 1:
102  case 2:
103  {
104  uint32_t i, n;
106 
107  clear();
108  if (version < 2) // ID was removed in version 2
109  {
110  uint32_t ID;
111  in >> ID;
112  }
113 
114  if (version == 0) in.ReadBufferFixEndianness(&tempTimeStamp, 1);
115 
116  in >> n;
117  m_observations.resize(n);
118  for_each(
119  m_observations.begin(), m_observations.end(),
121 
122  if (version == 0)
123  for (i = 0; i < n; i++)
124  m_observations[i]->timestamp = tempTimeStamp;
125  }
126  break;
127  default:
129  };
130 
131  m_cachedMap.reset();
132 
133  MRPT_END
134 }
135 
136 /*---------------------------------------------------------------
137  operator +=
138  ---------------------------------------------------------------*/
140 {
141  MRPT_UNUSED_PARAM(sf);
142  m_cachedMap.reset();
143  for (const_iterator it = begin(); it != end(); ++it)
144  {
145  CObservation::Ptr newObs = *it;
146  newObs.reset(dynamic_cast<CObservation*>(newObs->clone()));
147  m_observations.push_back(
148  newObs); // static_cast<CObservation*>( (*it)->clone()) );
149  }
150 }
151 
152 /*---------------------------------------------------------------
153  operator +=
154  ---------------------------------------------------------------*/
156 {
157  m_cachedMap.reset();
158  m_observations.push_back(obs);
159 }
160 
161 /*---------------------------------------------------------------
162  push_back
163  ---------------------------------------------------------------*/
165 {
166  m_cachedMap.reset();
167  m_observations.push_back(obs);
168 }
169 
170 /*---------------------------------------------------------------
171  insert
172  ---------------------------------------------------------------*/
174 {
175  m_cachedMap.reset();
176  m_observations.push_back(obs);
177 }
178 
179 /*---------------------------------------------------------------
180  eraseByIndex
181  ---------------------------------------------------------------*/
182 void CSensoryFrame::eraseByIndex(const size_t& idx)
183 {
184  MRPT_START
185  if (idx >= size())
187  "Index %u out of range.", static_cast<unsigned>(idx));
188 
189  m_cachedMap.reset();
190  iterator it = begin() + idx;
191  ASSERT_(!*it);
192  // delete (*it);
193  m_observations.erase(it);
194  MRPT_END
195 }
196 
197 /*---------------------------------------------------------------
198  getObservationByIndex
199  ---------------------------------------------------------------*/
201 {
202  MRPT_START
203  if (idx >= size())
205  "Index %u out of range.", static_cast<unsigned>(idx));
206 
207  const_iterator it = begin() + idx;
208  return *it;
209 
210  MRPT_END
211 }
212 
213 /*---------------------------------------------------------------
214  erase
215  ---------------------------------------------------------------*/
217 {
218  MRPT_START
219  ASSERT_(it != end())
220 
221  m_cachedMap.reset();
222 
223  return m_observations.erase(it);
224  MRPT_END
225 }
226 
227 /*---------------------------------------------------------------
228  getObservationBySensorLabel
229  ---------------------------------------------------------------*/
231  const std::string& label, const size_t& idx) const
232 {
233  MRPT_START
234 
235  size_t foundCount = 0;
236  for (const_iterator it = begin(); it != end(); ++it)
237  if (!os::_strcmpi((*it)->sensorLabel.c_str(), label.c_str()))
238  if (foundCount++ == idx) return *it;
239 
240  return CObservation::Ptr();
241 
242  MRPT_END
243 }
244 
245 /*---------------------------------------------------------------
246  moveFrom
247  ---------------------------------------------------------------*/
249 {
250  copy(
251  sf.m_observations.begin(), sf.m_observations.end(),
252  back_inserter(m_observations));
253  sf.m_observations.clear();
254  m_cachedMap.reset();
255 }
256 
257 /*---------------------------------------------------------------
258  swap
259  ---------------------------------------------------------------*/
261 {
263  std::swap(m_cachedMap, sf.m_cachedMap);
264 }
265 
266 /*---------------------------------------------------------------
267  eraseByLabel
268  ---------------------------------------------------------------*/
270 {
271  for (iterator it = begin(); it != end();)
272  {
273  if (!os::_strcmpi((*it)->sensorLabel.c_str(), label.c_str()))
274  {
275  it = erase(it);
276  }
277  else
278  it++;
279  }
280  m_cachedMap.reset();
281 }
282 
283 namespace mrpt
284 {
285 namespace obs
286 {
287 // Tricky way to call to a library that depends on us, a sort of "run-time"
288 // linking: ptr_internal_build_points_map_from_scan2D is a functor in
289 // "mrpt-obs", set by "mrpt-maps" at its startup.
290 using scan2pts_functor = void(*)(
292  mrpt::maps::CMetricMap::Ptr& out_map, const void* insertOps);
293 extern scan2pts_functor ptr_internal_build_points_map_from_scan2D; // impl in CObservation2DRangeScan.cpp
294 }
295 }
296 
297 /*---------------------------------------------------------------
298  internal_buildAuxPointsMap
299  ---------------------------------------------------------------*/
300 void CSensoryFrame::internal_buildAuxPointsMap(const void* options) const
301 {
303  throw std::runtime_error(
304  "[CSensoryFrame::buildAuxPointsMap] ERROR: This function needs "
305  "linking against mrpt-maps.\n");
306 
307  for (const_iterator it = begin(); it != end(); ++it)
310  dynamic_cast<CObservation2DRangeScan&>(*it->get()), m_cachedMap,
311  options);
312 }
313 
315  mrpt::maps::CMetricMap* theMap, const CPose3D* robotPose) const
316 {
317  bool anyone = false;
318  for (const_iterator it = begin(); it != end(); ++it)
319  anyone |= (*it)->insertObservationInto(theMap, robotPose);
320  return anyone;
321 }
void clear()
Clear all current observations.
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:32
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...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
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...
CSensoryFrame()
Default constructor.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
scan2pts_functor ptr_internal_build_points_map_from_scan2D
void moveFrom(CSensoryFrame &sf)
Copies all the observation from another object, then erase them from the origin object (this method i...
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
CSensoryFrame & operator=(const CSensoryFrame &o)
Copy.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLenum GLsizei n
Definition: glext.h:5074
mrpt::maps::CMetricMap::Ptr m_cachedMap
A points map, build only under demand by the methods getAuxPointsMap() and buildAuxPointsMap().
Definition: CSensoryFrame.h:75
STL namespace.
iterator erase(const iterator &it)
Removes the given observation in the list, and return an iterator to the next element (or this->end()...
const_iterator begin() const
Returns a constant iterator to the first observation: this is an example of usage: ...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
void swap(CSensoryFrame &sf)
Efficiently swaps the contents of two objects.
std::shared_ptr< CMetricMap > Ptr
Definition: CMetricMap.h:58
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
virtual ~CSensoryFrame()
Destructor.
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.
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:54
An object for reading objects from a stream, intended for being used in STL algorithms.
A set of utility objects for metaprogramming with STL algorithms.
std::shared_ptr< CObservation > Ptr
Definition: CObservation.h:43
GLsizei const GLchar ** string
Definition: glext.h:4101
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:16
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
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).
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:55
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
GLuint in
Definition: glext.h:7274
std::deque< CObservation::Ptr >::const_iterator const_iterator
You can use CSensoryFrame::begin to get a iterator to the first element.
#define ASSERT_(f)
void(*)(const mrpt::obs::CObservation2DRangeScan &obs, mrpt::maps::CMetricMap::Ptr &out_map, const void *insertOps) scan2pts_functor
std::deque< CObservation::Ptr >::iterator iterator
You can use CSensoryFrame::begin to get a iterator to the first element.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
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).
unsigned __int32 uint32_t
Definition: rptypes.h:47
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.
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.
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:319
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: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019