MRPT  1.9.9
CMultiMetricMap.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, 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 
10 #include "maps-precomp.h" // Precompiled headers
11 
15 #include <mrpt/poses/CPoint2D.h>
18 
19 using namespace mrpt::maps;
20 using namespace mrpt::poses;
21 using namespace mrpt::obs;
22 using namespace mrpt::tfest;
24 
26 
27 // ------------------------------------------------------------------------
28 // A few words explaining how all this works:
29 // The main hub for operating with all the maps in the internal list
30 // if MapExecutor.
31 //
32 // All operations go thru MapExecutor::run<OP>() with OP being one of the
33 // possible map operations (clear, matching, likelihood, etc.). The
34 // idea is that when adding new map types to the internal list of
35 // CMultiMetricMap, *only* "MapExecutor" methods must be updated.
36 // (The only exception are readFromStream() & writeToStream())
37 //
38 // The map-specific operations all go into template specializations of
39 // other helper structures or in overloaded methods.
40 // JLBC (7-AUG-2011)
41 // ------------------------------------------------------------------------
42 
44 {
45  // Apply operation to maps in the same order as declared in
46  // CMultiMetricMap.h:
47  template <typename OP>
48  static void run(const CMultiMetricMap& _mmm, OP op)
49  {
51  // This is to avoid duplicating "::run()" for const and non-const.
52  auto& mmm = const_cast<CMultiMetricMap&>(_mmm);
53  std::for_each(mmm.maps.begin(), mmm.maps.end(), op);
54  MRPT_END
55  }
56 }; // end of MapExecutor
57 
58 // ------------------- Begin of map-operations helper templates
59 
61 {
62  const CObservation* obs;
63  bool& can;
64 
66  const CMultiMetricMap& m, const CObservation* _obs, bool& _can)
67  : obs(_obs), can(_can)
68  {
69  can = false;
70  }
71 
72  template <typename PTR>
73  inline void operator()(PTR& ptr)
74  {
75  can = can || ptr->canComputeObservationLikelihood(obs);
76  }
77 
78 }; // end of MapCanComputeLikelihood
79 
81 {
82  MapAuxPFCleanup() = default;
83  template <typename PTR>
84  inline void operator()(PTR& ptr)
85  {
86  if (ptr) ptr->auxParticleFilterCleanUp();
87  }
88 }; // end of MapAuxPFCleanup
89 
90 struct MapIsEmpty
91 {
92  bool& is_empty;
93 
94  MapIsEmpty(bool& _is_empty) : is_empty(_is_empty) { is_empty = true; }
95  template <typename PTR>
96  inline void operator()(PTR& ptr)
97  {
98  if (ptr) is_empty = is_empty && ptr->isEmpty();
99  }
100 }; // end of MapIsEmpty
101 
102 // ---- End of map-operations helper templates
103 
104 // Ctor
106 {
107  MRPT_START
108  setListOfMaps(i);
109  MRPT_END
110 }
111 
113 {
114  MRPT_START
115  // Erase current list of maps:
116  maps.clear();
117 
119 
120  // Process each entry in the "initializers" and create maps accordingly:
121  for (const auto& i : inits)
122  {
123  // Create map from the list of all params:
124  auto* theMap = mmr.factoryMapObjectFromDefinition(*i.get());
125  ASSERT_(theMap);
126  // Add to the list of maps:
127  this->maps.push_back(mrpt::maps::CMetricMap::Ptr(theMap));
128  }
129  MRPT_END
130 }
131 
133 {
134  std::for_each(maps.begin(), maps.end(), [](auto ptr) {
135  if (ptr) ptr->clear();
136  });
137 }
138 
141 {
142  const auto n = static_cast<uint32_t>(maps.size());
143  out << n;
144  for (uint32_t i = 0; i < n; i++) out << *maps[i];
145 }
146 
149 {
150  switch (version)
151  {
152  case 11:
153  case 12:
154  {
155  // ID:
156  if (version < 12) // ID was removed in v12
157  {
158  uint32_t ID;
159  in >> ID;
160  }
161 
162  // List of maps:
163  uint32_t n;
164  in >> n;
165  this->maps.resize(n);
166  for_each(
167  maps.begin(), maps.end(),
169  }
170  break;
171  default:
173  };
174 }
175 
176 // Read docs in base class
178  const CObservation& obs, const CPose3D& takenFrom)
179 {
180  MRPT_START
181  double ret_log_lik = 0;
182 
183  std::for_each(maps.begin(), maps.end(), [&](auto& ptr) {
184  ret_log_lik += ptr->computeObservationLikelihood(obs, takenFrom);
185  });
186  return ret_log_lik;
187 
188  MRPT_END
189 }
190 
191 // Read docs in base class
193  const CObservation& obs) const
194 {
195  bool can_comp = false;
196  std::for_each(maps.begin(), maps.end(), [&](auto& ptr) {
197  can_comp = can_comp || ptr->canComputeObservationLikelihood(obs);
198  });
199  return can_comp; //-V614
200 }
201 
203  const CObservation& obs, const CPose3D* robotPose)
204 {
205  int total_insert = 0;
206 
207  std::for_each(maps.begin(), maps.end(), [&](auto& ptr) {
208  const bool ret = ptr->insertObservation(obs, robotPose);
209  if (ret) total_insert++;
210  });
211  return total_insert != 0;
212 }
213 
215  const mrpt::maps::CMetricMap* otherMap, const CPose2D& otherMapPose,
216  TMatchingPairList& correspondences, const TMatchingParams& params,
217  TMatchingExtraResults& extraResults) const
218 {
219  MRPT_START
220  const auto numPointsMaps = countMapsByClass<CSimplePointsMap>();
221 
222  ASSERTMSG_(
223  numPointsMaps == 1,
224  "There is not exactly 1 points maps in the multimetric map.");
225  mapByClass<CSimplePointsMap>()->determineMatching2D(
226  otherMap, otherMapPose, correspondences, params, extraResults);
227  MRPT_END
228 }
229 
231 {
232  bool is_empty;
233  MapIsEmpty op_insert_obs(is_empty); //-V614
234  MapExecutor::run(*this, op_insert_obs);
235  return is_empty;
236 }
237 
239  const std::string& filNamePrefix) const
240 {
241  MRPT_START
242 
243  for (size_t idx = 0; idx < maps.size(); idx++)
244  {
245  const mrpt::maps::CMetricMap* m = maps[idx].get();
246  ASSERT_(m);
247  std::string fil = filNamePrefix;
248  fil += format(
249  "_%s_%02u", m->GetRuntimeClass()->className,
250  static_cast<unsigned int>(idx));
252  }
253 
254  MRPT_END
255 }
256 
258  mrpt::opengl::CSetOfObjects::Ptr& outObj) const
259 {
260  MRPT_START
261  std::for_each(maps.begin(), maps.end(), [&](auto& ptr) {
262  ptr->getAs3DObject(outObj);
263  });
264  MRPT_END
265 }
266 
267 // See docs in base class
269  const mrpt::maps::CMetricMap* otherMap,
270  const mrpt::poses::CPose3D& otherMapPose,
271  const TMatchingRatioParams& params) const
272 {
273  MRPT_START
274 
275  float accumResult = 0;
276 
277  for (const auto& map : maps)
278  {
279  const mrpt::maps::CMetricMap* m = map.get();
280  ASSERT_(m);
281  accumResult +=
282  m->compute3DMatchingRatio(otherMap, otherMapPose, params);
283  }
284 
285  // Return average:
286  const size_t nMapsComputed = maps.size();
287  if (nMapsComputed) accumResult /= nMapsComputed;
288  return accumResult;
289 
290  MRPT_END
291 }
292 
294 {
295  MRPT_START
296  std::for_each(maps.begin(), maps.end(), [](auto& ptr) {
297  ptr->auxParticleFilterCleanUp();
298  });
299  MRPT_END
300 }
301 
303 {
304  MRPT_START
305  const auto numPointsMaps = countMapsByClass<CSimplePointsMap>();
306  ASSERT_(numPointsMaps == 1 || numPointsMaps == 0);
307  if (!numPointsMaps)
308  return nullptr;
309  else
310  return this->mapByClass<CSimplePointsMap>(0).get();
311  MRPT_END
312 }
313 
315 {
316  MRPT_START
317  return maps.at(idx).get_ptr();
318  MRPT_END
319 }
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)
#define MRPT_START
Definition: exceptions.h:241
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
bool internal_insertObservation(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D *robotPose=nullptr) override
Internal method called by insertObservation()
GLenum GLsizei n
Definition: glext.h:5136
void operator()(PTR &ptr)
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans. ...
void determineMatching2D(const mrpt::maps::CMetricMap *otherMap, const mrpt::poses::CPose2D &otherMapPose, mrpt::tfest::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: ...
A set of TMetricMapInitializer structures, passed to the constructor CMultiMetricMap::CMultiMetricMap...
unsigned char uint8_t
Definition: rptypes.h:44
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
MapCanComputeLikelihood(const CMultiMetricMap &m, const CObservation *_obs, bool &_can)
bool isEmpty() const override
Returns true if all maps returns true in their isEmpty() method.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
float compute3DMatchingRatio(const mrpt::maps::CMetricMap *otherMap, const mrpt::poses::CPose3D &otherMapPose, const TMatchingRatioParams &params) const override
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.
Additional results from the determination of matchings between point clouds, etc., apart from the pairings themselves.
const char * className
Definition: CObject.h:34
void internal_clear() override
Internal method called by clear()
A list of TMatchingPair.
Definition: TMatchingPair.h:70
This namespace contains representation of robot actions and observations.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
CMultiMetricMap()=default
Default ctor: empty list of maps.
bool internal_canComputeObservationLikelihood(const mrpt::obs::CObservation &obs) const override
Internal method called by canComputeObservationLikelihood()
GLsizei const GLchar ** string
Definition: glext.h:4116
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
double internal_computeObservationLikelihood(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D &takenFrom) override
Internal method called by computeObservationLikelihood()
const CObservation * obs
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:52
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:39
const mrpt::maps::CSimplePointsMap * getAsSimplePointsMap() const override
If the map is a simple points map or it&#39;s a multi-metric map that contains EXACTLY one simple points ...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
MapIsEmpty(bool &_is_empty)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
mrpt::maps::CMetricMap::Ptr mapByIndex(size_t idx) const
Gets the i-th map On out-of-bounds.
This class stores any customizable set of metric maps.
Parameters for the determination of matchings between point clouds, etc.
unsigned __int32 uint32_t
Definition: rptypes.h:50
Functions for estimating the optimal transformation between two frames of references given measuremen...
GLenum const GLfloat * params
Definition: glext.h:3538
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:149
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
void saveMetricMapRepresentationToFile(const std::string &filNamePrefix) const override
This virtual method saves the map to a file "filNamePrefix"+< some_file_extension >...
void setListOfMaps(const mrpt::maps::TSetOfMetricMapInitializers &init)
Sets the list of internal map according to the passed list of map initializers (current maps will be ...
static void run(const CMultiMetricMap &_mmm, OP op)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019