Main MRPT website > C++ reference for MRPT 1.9.9
CMultiMetricMap.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 "slam-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/CConfigFile.h>
13 #include <mrpt/poses/CPoint2D.h>
16 #include <mrpt/utils/CStream.h>
17 
18 using namespace mrpt::maps;
19 using namespace mrpt::utils;
20 using namespace mrpt::poses;
21 using namespace mrpt::obs;
22 using namespace mrpt::utils::metaprogramming;
23 
25 
26 // ------------------------------------------------------------------------
27 // A few words explaining how all this works:
28 // The main hub for operating with all the maps in the internal list
29 // if MapExecutor.
30 //
31 // All operations go thru MapExecutor::run<OP>() with OP being one of the
32 // possible map operations (clear, matching, likelihood, etc.). The
33 // idea is that when adding new map types to the internal list of
34 // CMultiMetricMap, *only* "MapExecutor" methods must be updated.
35 // (The only exception are readFromStream() & writeToStream())
36 //
37 // The map-specific operations all go into template specializations of
38 // other helper structures or in overloaded methods.
39 // JLBC (7-AUG-2011)
40 // ------------------------------------------------------------------------
41 
43 {
44  // Apply operation to maps in the same order as declared in
45  // CMultiMetricMap.h:
46  template <typename OP>
47  static void run(const CMultiMetricMap& _mmm, OP& op)
48  {
50  CMultiMetricMap& mmm =
51  const_cast<CMultiMetricMap&>(_mmm); // This is to avoid duplicating
52  // "::run()" for const and
53  // non-const.
54  for_each(mmm.maps.begin(), mmm.maps.end(), op);
55  MRPT_END
56  }
57 }; // end of MapExecutor
58 
59 // ------------------- Begin of map-operations helper templates
60 // -------------------
62 {
63  template <typename T>
64  inline void operator()(T& container)
65  {
66  container.clear();
67  }
68 };
69 
71 {
72  const CObservation* obs;
74  double& total_log_lik;
75 
77  const CMultiMetricMap& m, const CObservation* _obs,
78  const CPose3D& _takenFrom, double& _total_log_lik)
79  : obs(_obs), takenFrom(_takenFrom), total_log_lik(_total_log_lik)
80  {
81  total_log_lik = 0;
82  }
83 
84  template <typename PTR>
85  inline void operator()(PTR& ptr)
86  {
87  total_log_lik += ptr->computeObservationLikelihood(obs, takenFrom);
88  }
89 
90 }; // end of MapComputeLikelihood
91 
93 {
94  const CObservation* obs;
95  bool& can;
96 
98  const CMultiMetricMap& m, const CObservation* _obs, bool& _can)
99  : obs(_obs), can(_can)
100  {
101  can = false;
102  }
103 
104  template <typename PTR>
105  inline void operator()(PTR& ptr)
106  {
107  can = can || ptr->canComputeObservationLikelihood(obs);
108  }
109 
110 }; // end of MapCanComputeLikelihood
111 
113 {
117 
119  const CMultiMetricMap& m, const CObservation* _obs,
120  const CPose3D* _robot_pose, int& _total_insert)
121  : obs(_obs), robot_pose(_robot_pose), total_insert(_total_insert)
122  {
123  total_insert = 0;
124  }
125 
126  template <typename PTR>
127  inline void operator()(PTR& ptr)
128  {
129  bool ret = ptr->insertObservation(obs, robot_pose);
130  if (ret) total_insert++;
131  }
132 }; // end of MapInsertObservation
133 
135 {
137 
139  : obj_gl(_obj_gl)
140  {
141  }
142 
143  template <typename PTR>
144  inline void operator()(PTR& ptr)
145  {
146  if (ptr) ptr->getAs3DObject(obj_gl);
147  }
148 }; // end of MapGetAs3DObject
149 
151 {
153  template <typename PTR>
154  inline void operator()(PTR& ptr)
155  {
156  if (ptr) ptr->auxParticleFilterCleanUp();
157  }
158 }; // end of MapAuxPFCleanup
159 
161 {
162  bool& is_empty;
163 
164  MapIsEmpty(bool& _is_empty) : is_empty(_is_empty) { is_empty = true; }
165  template <typename PTR>
166  inline void operator()(PTR& ptr)
167  {
168  if (ptr) is_empty = is_empty && ptr->isEmpty();
169  }
170 }; // end of MapIsEmpty
171 
172 // ------------------- End of map-operations helper templates
173 // -------------------
174 
175 #define ALL_PROXIES_INIT \
176  m_pointsMaps(maps), m_gridMaps(maps), m_octoMaps(maps), \
177  m_colourOctoMaps(maps), m_gasGridMaps(maps), m_wifiGridMaps(maps), \
178  m_heightMaps(maps), m_heightMRFMaps(maps), m_reflectivityMaps(maps), \
179  m_colourPointsMap(maps), m_weightedPointsMap(maps), \
180  m_landmarksMap(maps), m_beaconMap(maps)
181 
182 // Ctor
184  const TSetOfMetricMapInitializers* initializers)
185  : maps(), ALL_PROXIES_INIT, m_ID(0)
186 {
187  MRPT_START
188  setListOfMaps(initializers);
189  MRPT_END
190 }
191 
193  : maps(o.maps), ALL_PROXIES_INIT, m_ID(o.m_ID)
194 {
195 }
196 
198 {
199  maps = o.maps;
200  m_ID = o.m_ID;
201  return *this;
202 }
203 
205  : maps(std::move(o.maps)), ALL_PROXIES_INIT, m_ID(o.m_ID)
206 {
207 }
208 
210 {
211  maps = std::move(o.maps);
212  m_ID = o.m_ID;
213  return *this;
214 }
215 
216 /*---------------------------------------------------------------
217  setListOfMaps
218  ---------------------------------------------------------------*/
220  const mrpt::maps::TSetOfMetricMapInitializers* initializers)
221 {
222  MRPT_START
223 
224  // Erase current list of maps:
225  deleteAllMaps();
226 
229 
230  // Do we have any initializer?
231  if (initializers != nullptr)
232  {
233  // Process each entry in the "initializers" and create maps accordingly:
235  initializers->begin();
236  it != initializers->end(); ++it)
237  {
238  // Create map from the list of all params:
239  mrpt::maps::CMetricMap* theMap =
240  mmr.factoryMapObjectFromDefinition(*it->get());
241  ASSERT_(theMap)
242 
243  // Add to the list of maps:
244  this->maps.push_back(mrpt::maps::CMetricMap::Ptr(theMap));
245  }
246 
247  } // end if initializers!=nullptr
248 
249  MRPT_END
250 }
251 
252 /*---------------------------------------------------------------
253  clear
254  ---------------------------------------------------------------*/
256 {
257  ObjectClear op;
258  MapExecutor::run(*this, op);
259 }
260 
261 // Deletes all maps and clears the internal lists of maps (with reset(), so user
262 // copies remain alive)
264 {
265  // Clear smart pointers:
267  op_reset;
268  MapExecutor::run(*this, op_reset);
269 
270  // Clear list:
271  maps.clear();
272  m_ID = 0;
273 }
274 
275 /*---------------------------------------------------------------
276  Implements the writing to a CStream capability of CSerializable objects
277  ---------------------------------------------------------------*/
279  mrpt::utils::CStream& out, int* version) const
280 {
281  if (version)
282  *version = 11;
283  else
284  {
285  // Version 11: simply the list of maps:
286  out << static_cast<uint32_t>(m_ID);
287 
288  const uint32_t n = static_cast<uint32_t>(maps.size());
289  for (uint32_t i = 0; i < n; i++) out << *maps[i];
290  }
291 }
292 
293 /*---------------------------------------------------------------
294  Implements the reading from a CStream capability of CSerializable objects
295  ---------------------------------------------------------------*/
297 {
298  switch (version)
299  {
300  case 11:
301  {
302  // ID:
303  {
304  uint32_t ID;
305  in >> ID;
306  m_ID = ID;
307  }
308 
309  // List of maps:
310  uint32_t n;
311  in >> n;
312  this->maps.resize(n);
313  for_each(
314  maps.begin(), maps.end(),
316  }
317  break;
318  default:
320  };
321 }
322 
323 // Read docs in base class
325  const CObservation* obs, const CPose3D& takenFrom)
326 {
327  double ret_log_lik;
328  MapComputeLikelihood op_likelihood(*this, obs, takenFrom, ret_log_lik);
329 
330  MapExecutor::run(*this, op_likelihood);
331 
332  MRPT_CHECK_NORMAL_NUMBER(ret_log_lik) //-V614
333  return ret_log_lik;
334 }
335 
336 // Read docs in base class
338  const CObservation* obs) const
339 {
340  bool can_comp;
341 
342  MapCanComputeLikelihood op_can_likelihood(*this, obs, can_comp);
343  MapExecutor::run(*this, op_can_likelihood);
344  return can_comp; //-V614
345 }
346 
347 /*---------------------------------------------------------------
348  insertObservation
349 
350 Insert the observation information into this map.
351  ---------------------------------------------------------------*/
353  const CObservation* obs, const CPose3D* robotPose)
354 {
355  int total_insert;
356  MapInsertObservation op_insert_obs(*this, obs, robotPose, total_insert);
357  MapExecutor::run(*this, op_insert_obs);
358  return total_insert != 0; //-V614
359 }
360 
361 /*---------------------------------------------------------------
362  computeMatchingWith2D
363  ---------------------------------------------------------------*/
365  const mrpt::maps::CMetricMap* otherMap, const CPose2D& otherMapPose,
366  TMatchingPairList& correspondences, const TMatchingParams& params,
367  TMatchingExtraResults& extraResults) const
368 {
369  MRPT_START
370  ASSERTMSG_(
371  m_pointsMaps.size() == 1,
372  "There is not exactly 1 points maps in the multimetric map.");
373  m_pointsMaps[0]->determineMatching2D(
374  otherMap, otherMapPose, correspondences, params, extraResults);
375  MRPT_END
376 }
377 
378 /*---------------------------------------------------------------
379  isEmpty
380  ---------------------------------------------------------------*/
382 {
383  bool is_empty;
384  MapIsEmpty op_insert_obs(is_empty); //-V614
385  MapExecutor::run(*this, op_insert_obs);
386  return is_empty;
387 }
388 
390  const std::string& filNamePrefix) const
391 {
392  MRPT_START
393 
394  for (size_t idx = 0; idx < maps.size(); idx++)
395  {
396  const mrpt::maps::CMetricMap* m = maps[idx].get();
397  ASSERT_(m)
398 
399  std::string fil = filNamePrefix;
400  fil += format(
401  "_%s_%02u", m->GetRuntimeClass()->className,
402  static_cast<unsigned int>(idx));
404  }
405 
406  MRPT_END
407 }
408 
409 /*---------------------------------------------------------------
410  getAs3DObject
411 ---------------------------------------------------------------*/
413  mrpt::opengl::CSetOfObjects::Ptr& outObj) const
414 {
415  MRPT_START
416  MapGetAs3DObject op_get_3D(outObj);
417  MapExecutor::run(*this, op_get_3D);
418  MRPT_END
419 }
420 
421 // See docs in base class
423  const mrpt::maps::CMetricMap* otherMap,
424  const mrpt::poses::CPose3D& otherMapPose,
425  const TMatchingRatioParams& params) const
426 {
427  MRPT_START
428 
429  float accumResult = 0;
430 
431  for (size_t idx = 0; idx < maps.size(); idx++)
432  {
433  const mrpt::maps::CMetricMap* m = maps[idx].get();
434  ASSERT_(m)
435  accumResult +=
436  m->compute3DMatchingRatio(otherMap, otherMapPose, params);
437  }
438 
439  // Return average:
440  const size_t nMapsComputed = maps.size();
441  if (nMapsComputed) accumResult /= nMapsComputed;
442  return accumResult;
443 
444  MRPT_END
445 }
446 
447 /*---------------------------------------------------------------
448  auxParticleFilterCleanUp
449  ---------------------------------------------------------------*/
451 {
452  MRPT_START
453  MapAuxPFCleanup op_cleanup;
454  MapExecutor::run(*this, op_cleanup);
455  MRPT_END
456 }
457 
458 /** If the map is a simple points map or it's a multi-metric map that contains
459 * EXACTLY one simple points map, return it.
460 * Otherwise, return NULL
461 */
463 {
464  MRPT_START
465  ASSERT_(m_pointsMaps.size() == 1 || m_pointsMaps.size() == 0)
466  if (m_pointsMaps.empty())
467  return nullptr;
468  else
469  return m_pointsMaps[0].get();
470  MRPT_END
471 }
473 {
474  MRPT_START
475  ASSERT_(m_pointsMaps.size() == 1 || m_pointsMaps.size() == 0)
476  if (m_pointsMaps.empty())
477  return nullptr;
478  else
479  return m_pointsMaps[0].get();
480  MRPT_END
481 }
482 
483 /** Gets the i-th map \exception std::runtime_error On out-of-bounds */
485 {
486  ASSERT_BELOW_(idx, maps.size())
487  return maps[idx].get_ptr();
488 }
virtual void saveMetricMapRepresentationToFile(const std::string &filNamePrefix) const =0
This virtual method saves the map to a file "filNamePrefix"+< some_file_extension >...
void auxParticleFilterCleanUp() override
This method is called at the end of each "prediction-update-map insertion" cycle within "mrpt::slam::...
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map.
Parameters for CMetricMap::compute3DMatchingRatio()
void operator()(PTR &ptr)
void deleteAllMaps()
Deletes all maps and clears the internal lists of maps (with clear_unique(), so user copies remain al...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
An object for making smart pointers unique (ie, making copies if necessary), intended for being used ...
ProxyFilterContainerByClass< mrpt::maps::CSimplePointsMap::Ptr, TListMaps > m_pointsMaps
STL-like proxy to access this kind of maps in maps.
mrpt::maps::CMetricMap * factoryMapObjectFromDefinition(const mrpt::maps::TMetricMapInitializer &mi) const
Return nullptr if not found.
MapGetAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &_obj_gl)
void operator()(PTR &ptr)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define MRPT_CHECK_NORMAL_NUMBER(val)
#define ASSERT_BELOW_(__A, __B)
GLenum GLsizei n
Definition: glext.h:5074
const CPose3D * robot_pose
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
double internal_computeObservationLikelihood(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D &takenFrom) override
Internal method called by computeObservationLikelihood()
void operator()(PTR &ptr)
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans. ...
STL namespace.
const CObservation * obs
mrpt::opengl::CSetOfObjects::Ptr & obj_gl
void operator()(T &container)
A set of TMetricMapInitializer structures, passed to the constructor CMultiMetricMap::CMultiMetricMap...
#define ALL_PROXIES_INIT
MapCanComputeLikelihood(const CMultiMetricMap &m, const CObservation *_obs, bool &_can)
const CPose3D & takenFrom
bool isEmpty() const override
Returns true if all maps returns true to their isEmpty() method, which is map-dependent.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
float compute3DMatchingRatio(const mrpt::maps::CMetricMap *otherMap, const mrpt::poses::CPose3D &otherMapPose, const TMatchingRatioParams &params) const override
See the definition in the base class: Calls in this class become a call to every single map in this s...
Additional results from the determination of matchings between point clouds, etc., apart from the pairings themselves.
std::shared_ptr< CMetricMap > Ptr
Definition: CMetricMap.h:58
#define MRPT_END
void operator()(PTR &ptr)
void internal_clear() override
Clear all elements of the map.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
A list of TMatchingPair.
Definition: TMatchingPair.h:93
CMultiMetricMap & operator=(const CMultiMetricMap &o)
bool internal_insertObservation(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D *robotPose=nullptr) override
Internal method called by insertObservation()
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:32
This namespace contains representation of robot actions and observations.
CMultiMetricMap(const mrpt::maps::TSetOfMetricMapInitializers *initializers=nullptr)
Constructor.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
A set of utility objects for metaprogramming with STL algorithms.
bool internal_canComputeObservationLikelihood(const mrpt::obs::CObservation *obs) const override
Returns true if any of the inner maps is able to compute a sensible likelihood function for this obse...
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
static void run(const CMultiMetricMap &_mmm, OP &op)
mrpt::maps::CMetricMap::Ptr getMapByIndex(size_t idx) const
Gets the i-th map.
An object for clearing an object (invokes its method "->clear()") given a pointer or smart-pointer...
MapComputeLikelihood(const CMultiMetricMap &m, const CObservation *_obs, const CPose3D &_takenFrom, double &_total_log_lik)
#define MRPT_START
const CObservation * obs
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:55
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
virtual const mrpt::maps::CSimplePointsMap * getAsSimplePointsMap() const override
If the map is a simple point map or it&#39;s a multi-metric map that contains EXACTLY one simple point ma...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:41
void setListOfMaps(const mrpt::maps::TSetOfMetricMapInitializers *initializers)
Sets the list of internal map according to the passed list of map initializers (Current maps&#39; content...
MapIsEmpty(bool &_is_empty)
void operator()(PTR &ptr)
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
unsigned int m_ID
An auxiliary variable that can be used freely by the users (this will be copied to other maps using t...
TListMaps maps
The list of MRPT metric maps in this object.
This class stores any customizable set of metric maps.
const char * className
Definition: CObject.h:34
Class factory & registry for map classes.
const CObservation * obs
Parameters for the determination of matchings between point clouds, etc.
unsigned __int32 uint32_t
Definition: rptypes.h:47
virtual void determineMatching2D(const mrpt::maps::CMetricMap *otherMap, const mrpt::poses::CPose2D &otherMapPose, mrpt::utils::TMatchingPairList &correspondences, const mrpt::maps::TMatchingParams &params, mrpt::maps::TMatchingExtraResults &extraResults) const override
Computes the matching between this and another 2D point map, which includes finding: ...
#define ASSERTMSG_(f, __ERROR_MSG)
GLenum const GLfloat * params
Definition: glext.h:3534
virtual float compute3DMatchingRatio(const mrpt::maps::CMetricMap *otherMap, const mrpt::poses::CPose3D &otherMapPose, const TMatchingRatioParams &params) const
Computes the ratio in [0,1] of correspondences between "this" and the "otherMap" map, whose 6D pose relative to "this" is "otherMapPose" In the case of a multi-metric map, this returns the average between the maps.
Definition: CMetricMap.cpp:157
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
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...
std::deque< TMetricMapInitializer::Ptr >::const_iterator const_iterator
MapInsertObservation(const CMultiMetricMap &m, const CObservation *_obs, const CPose3D *_robot_pose, int &_total_insert)
void saveMetricMapRepresentationToFile(const std::string &filNamePrefix) const override
The implementation in this class just calls all the corresponding method of the contained metric maps...



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