MRPT  1.9.9
CHeightGridMap2D_MRF.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-2020, 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" // Precomp header
11 
13 #include <mrpt/poses/CPose2D.h>
14 #include <mrpt/poses/CPose3D.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::maps;
18 using namespace mrpt::obs;
19 using namespace mrpt::poses;
20 using namespace std;
21 using namespace mrpt::math;
22 
23 // =========== Begin of Map definition ============
25  "mrpt::maps::CHeightGridMap2D_MRF,dem_mrf",
27 
28 MRPT_TODO("Improvement: Rewrite to avoid union -> variant");
29 
31 
32  = default;
33 
35  const mrpt::config::CConfigFileBase& source,
36  const std::string& sectionNamePrefix)
37 {
38  // [<sectionNamePrefix>+"_creationOpts"]
39  const std::string sSectCreation =
40  sectionNamePrefix + string("_creationOpts");
42  run_map_estimation_at_ctor, bool, source, sSectCreation);
43  MRPT_LOAD_CONFIG_VAR(min_x, double, source, sSectCreation);
44  MRPT_LOAD_CONFIG_VAR(max_x, double, source, sSectCreation);
45  MRPT_LOAD_CONFIG_VAR(min_y, double, source, sSectCreation);
46  MRPT_LOAD_CONFIG_VAR(max_y, double, source, sSectCreation);
47  MRPT_LOAD_CONFIG_VAR(resolution, double, source, sSectCreation);
49  sSectCreation, "mapType", mapType);
50 
51  insertionOpts.loadFromConfigFile(
52  source, sectionNamePrefix + string("_insertOpts"));
53 }
54 
56  std::ostream& out) const
57 {
58  out << mrpt::format(
59  "MAP TYPE = %s\n",
61  CHeightGridMap2D_MRF::TMapRepresentation>::value2name(mapType)
62  .c_str());
63  LOADABLEOPTS_DUMP_VAR(run_map_estimation_at_ctor, bool);
64  LOADABLEOPTS_DUMP_VAR(min_x, double);
65  LOADABLEOPTS_DUMP_VAR(max_x, double);
66  LOADABLEOPTS_DUMP_VAR(min_y, double);
67  LOADABLEOPTS_DUMP_VAR(max_y, double);
68  LOADABLEOPTS_DUMP_VAR(resolution, double);
69 
70  this->insertionOpts.dumpToTextStream(out);
71 }
72 
75 {
77  *dynamic_cast<const CHeightGridMap2D_MRF::TMapDefinition*>(&_def);
78  auto* obj = new CHeightGridMap2D_MRF(
79  def.mapType, def.min_x, def.max_x, def.min_y, def.max_y, def.resolution,
81  obj->insertionOptions = def.insertionOpts;
82  return obj;
83 }
84 // =========== End of Map definition Block =========
85 
87 
88 // Constructor
90  TMapRepresentation mapType, double x_min, double x_max, double y_min,
91  double y_max, double resolution, bool run_first_map_estimation_now)
92  : CRandomFieldGridMap2D(mapType, x_min, x_max, y_min, y_max, resolution),
93  insertionOptions()
94 {
95  m_rfgm_run_update_upon_clear = run_first_map_estimation_now;
96  // Set the grid to initial values (and adjusts the KF covariance matrix!)
97  // Also, calling clear() is mandatory to end initialization of our base
98  // class (read note in CRandomFieldGridMap2D::CRandomFieldGridMap2D)
100 }
101 
103  const double x, const double y, const double z,
105 {
106  const TRandomFieldCell* cell = cellByPos(x, y);
107  if (!cell) return false;
108  this->insertIndividualReading(
109  z, mrpt::math::TPoint2D(x, y), params.update_map_after_insertion,
110  true /*time invariant*/, params.pt_z_std);
111  return true;
112 }
113 double CHeightGridMap2D_MRF::dem_get_resolution() const { return m_resolution; }
114 size_t CHeightGridMap2D_MRF::dem_get_size_x() const { return m_size_x; }
115 size_t CHeightGridMap2D_MRF::dem_get_size_y() const { return m_size_y; }
117  const size_t cx, const size_t cy, double& z_out) const
118 {
119  const TRandomFieldCell* cell = cellByIndex(cx, cy);
120  if (cell && cell->kf_mean)
121  {
122  z_out = cell->kf_mean;
123  return true;
124  }
125  else
126  return false;
127 }
129  const double x, const double y, double& z_out) const
130 {
131  const TRandomFieldCell* cell = cellByPos(x, y);
132  if (cell && cell->kf_mean)
133  {
134  z_out = cell->kf_mean;
135  return true;
136  }
137  else
138  return false;
139 }
140 void CHeightGridMap2D_MRF::dem_update_map() { this->updateMapEstimation(); }
142 {
143  // Just do the generic clear:
145  // Anything else special for this derived class?
146 }
147 
149  const CObservation& obs, const CPose3D* robotPose)
150 {
151  return dem_internal_insertObservation(obs, robotPose);
152 }
153 
154 /*---------------------------------------------------------------
155  computeObservationLikelihood
156  ---------------------------------------------------------------*/
158  const CObservation& obs, const CPose3D& takenFrom)
159 {
160  MRPT_UNUSED_PARAM(obs);
161  MRPT_UNUSED_PARAM(takenFrom);
162  THROW_EXCEPTION("Not implemented yet!");
163 }
164 
165 uint8_t CHeightGridMap2D_MRF::serializeGetVersion() const { return 0; }
167 {
168  dyngridcommon_writeToStream(out);
169 
170  // To assure compatibility: The size of each cell:
171  auto n = static_cast<uint32_t>(sizeof(TRandomFieldCell));
172  out << n;
173 
174  // Save the map contents:
175  n = static_cast<uint32_t>(m_map.size());
176  out << n;
177 
178 // Save the "m_map": This requires special handling for big endian systems:
179 #if MRPT_IS_BIG_ENDIAN
180  for (uint32_t i = 0; i < n; i++)
181  {
182  out << m_map[i].kf_mean << m_map[i].dm_mean << m_map[i].dmv_var_mean;
183  }
184 #else
185  // Little endian: just write all at once:
186  out.WriteBuffer(&m_map[0], sizeof(m_map[0]) * m_map.size());
187 #endif
188 
189  // Save the insertion options:
190  out << uint8_t(m_mapType) << m_cov << m_stackedCov;
191 
192  out << insertionOptions.sigma << insertionOptions.cutoffRadius
193  << insertionOptions.R_min << insertionOptions.R_max
194  << insertionOptions.KF_covSigma << insertionOptions.KF_initialCellStd
195  << insertionOptions.KF_observationModelNoise
196  << insertionOptions.KF_defaultCellMeanValue
197  << insertionOptions.KF_W_size;
198 
199  out << m_average_normreadings_mean << m_average_normreadings_var
200  << uint64_t(m_average_normreadings_count);
201 
202  out << genericMapParams;
203 }
204 
206  mrpt::serialization::CArchive& in, uint8_t version)
207 {
208  switch (version)
209  {
210  case 0:
211  {
212  dyngridcommon_readFromStream(in);
213 
214  // To assure compatibility: The size of each cell:
215  uint32_t n;
216  in >> n;
217 
218  ASSERT_EQUAL_(n, static_cast<uint32_t>(sizeof(TRandomFieldCell)));
219  // Load the map contents:
220  in >> n;
221  m_map.resize(n);
222 
223 // Read the note in writeToStream()
224 #if MRPT_IS_BIG_ENDIAN
225  for (uint32_t i = 0; i < n; i++)
226  in >> m_map[i].kf_mean >> m_map[i].dm_mean >>
227  m_map[i].dmv_var_mean;
228 #else
229  // Little endian: just read all at once:
230  in.ReadBuffer(&m_map[0], sizeof(m_map[0]) * m_map.size());
231 #endif
232 
233  {
234  uint8_t i;
235  in >> i;
236  m_mapType = TMapRepresentation(i);
237 
238  in >> m_cov >> m_stackedCov;
239 
240  in >> insertionOptions.sigma >> insertionOptions.cutoffRadius >>
241  insertionOptions.R_min >> insertionOptions.R_max >>
242  insertionOptions.KF_covSigma >>
243  insertionOptions.KF_initialCellStd >>
244  insertionOptions.KF_observationModelNoise >>
245  insertionOptions.KF_defaultCellMeanValue >>
246  insertionOptions.KF_W_size;
247  }
248 
249  {
250  uint64_t N;
251  in >> m_average_normreadings_mean >>
252  m_average_normreadings_var >> N;
253  m_average_normreadings_count = N;
254  }
255 
256  in >> genericMapParams;
257 
258  m_hasToRecoverMeanAndCov = true;
259  }
260  break;
261  default:
263  };
264 }
265 
266 /*---------------------------------------------------------------
267  TInsertionOptions
268  ---------------------------------------------------------------*/
271  std::ostream& out) const
272 {
273  out << "\n----------- [CHeightGridMap2D_MRF::TInsertionOptions] "
274  "------------ "
275  "\n\n";
276  out << "[TInsertionOptions.Common] ------------ \n\n";
277  internal_dumpToTextStream_common(
278  out); // Common params to all random fields maps:
279 
280  out << "\n";
281 }
282 
283 /*---------------------------------------------------------------
284  loadFromConfigFile
285  ---------------------------------------------------------------*/
287  const mrpt::config::CConfigFileBase& iniFile, const std::string& section)
288 {
289  // Common data fields for all random fields maps:
290  internal_loadFromConfigFile_common(iniFile, section);
291 
292  // Specific data fields for this class:
293  // ...
294 }
295 
296 /*---------------------------------------------------------------
297  getAs3DObject
298 ---------------------------------------------------------------*/
300  mrpt::opengl::CSetOfObjects::Ptr& outObj) const
301 {
302  MRPT_START
303  if (!genericMapParams.enableSaveAs3DObject) return;
305  MRPT_END
306 }
307 
308 /*---------------------------------------------------------------
309  getAs3DObject
310 ---------------------------------------------------------------*/
313  mrpt::opengl::CSetOfObjects::Ptr& varObj) const
314 {
315  MRPT_START
316  if (!genericMapParams.enableSaveAs3DObject) return;
317  CRandomFieldGridMap2D::getAs3DObject(meanObj, varObj);
318  MRPT_END
319 }
void clear()
Erase all the contents of the map.
Definition: CMetricMap.cpp:30
bool run_map_estimation_at_ctor
Runs map estimation at start up (Default:true)
Virtual base for specifying the kind and parameters of one map (normally, to be inserted into mrpt::m...
Extra params for insertIndividualPoint()
#define MRPT_START
Definition: exceptions.h:241
bool internal_insertObservation(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D *robotPose=nullptr) override
Internal method called by insertObservation()
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
MRPT_TODO("Improvement: Rewrite to avoid union -> variant")
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
mrpt::maps::CHeightGridMap2D_MRF::TMapRepresentation mapType
The kind of map representation (see CHeightGridMap2D_MRF::CHeightGridMap2D_MRF)
void loadFromConfigFile_map_specific(const mrpt::config::CConfigFileBase &source, const std::string &sectionNamePrefix) override
Load all map-specific params.
mrpt::vision::TStereoCalibParams params
TMapRepresentation
The type of map representation to be used, see CRandomFieldGridMap2D for a discussion.
STL namespace.
CHeightGridMap2D_MRF represents digital-elevation-model over a 2D area, with uncertainty, based on a Markov-Random-Field (MRF) estimator.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map (mean)
void dumpToTextStream_map_specific(std::ostream &out) const override
ENUMTYPE read_enum(const std::string &section, const std::string &name, const ENUMTYPE &defaultValue, bool failIfNotFound=false) const
Reads an "enum" value, where the value in the config file can be either a numerical value or the symb...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
This class allows loading and storing values and vectors of different types from a configuration text...
This base provides a set of functions for maths stuff.
double kf_mean
[KF-methods only] The mean value of this cell
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
The contents of each cell in a CRandomFieldGridMap2D map.
A helper class that can convert an enum value into its textual representation, and viceversa...
double min_x
See CHeightGridMap2D_MRF::CHeightGridMap2D_MRF.
This namespace contains representation of robot actions and observations.
string iniFile(myDataDir+string("benchmark-options.ini"))
void dem_update_map() override
Ensure that all observations are reflected in the map estimate.
bool insertIndividualPoint(const double x, const double y, const double z, const CHeightGridMap2D_Base::TPointInsertParams &params=CHeightGridMap2D_Base::TPointInsertParams()) override
Update the DEM with one new point.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
bool dem_get_z_by_cell(const size_t cx, const size_t cy, double &z_out) const override
Get cell &#39;z&#39; by (cx,cy) cell indices.
static mrpt::maps::CMetricMap * internal_CreateFromMapDefinition(const mrpt::maps::TMetricMapInitializer &def)
#define LOADABLEOPTS_DUMP_VAR(variableName, variableType)
Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types a...
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MAP_DEFINITION_REGISTER(_CLASSNAME_STRINGS, _CLASSNAME_WITH_NS)
Registers one map class into TMetricMapInitializer factory.
CRandomFieldGridMap2D represents a 2D grid map where each cell is associated one real-valued property...
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
void dumpToTextStream(std::ostream &out) const override
This method should clearly display all the contents of the structure in textual form, sending it to a std::ostream.
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:52
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
mrpt::maps::CHeightGridMap2D_MRF::TInsertionOptions insertionOpts
Observations insertion options.
void internal_clear() override
Erase all the contents of the map.
#define MRPT_END
Definition: exceptions.h:245
void internal_clear() override
Internal method called by clear()
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CArchive.cpp:25
bool dem_get_z(const double x, const double y, double &z_out) const override
Get cell &#39;z&#39; (x,y) by metric coordinates.
double dem_get_resolution() const override
void loadFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
double internal_computeObservationLikelihood(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D &takenFrom) override
Internal method called by computeObservationLikelihood()
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020