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-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" // 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  "CHeightGridMap2D_MRF,dem_mrf", mrpt::maps::CHeightGridMap2D_MRF)
26 
27 MRPT_TODO("Improvement: Rewrite to avoid union -> variant");
28 
30 
31  = default;
32 
35  const std::string& sectionNamePrefix)
36 {
37  // [<sectionNamePrefix>+"_creationOpts"]
38  const std::string sSectCreation =
39  sectionNamePrefix + string("_creationOpts");
41  run_map_estimation_at_ctor, bool, source, sSectCreation);
42  MRPT_LOAD_CONFIG_VAR(min_x, double, source, sSectCreation);
43  MRPT_LOAD_CONFIG_VAR(max_x, double, source, sSectCreation);
44  MRPT_LOAD_CONFIG_VAR(min_y, double, source, sSectCreation);
45  MRPT_LOAD_CONFIG_VAR(max_y, double, source, sSectCreation);
46  MRPT_LOAD_CONFIG_VAR(resolution, double, source, sSectCreation);
48  sSectCreation, "mapType", mapType);
49 
50  insertionOpts.loadFromConfigFile(
51  source, sectionNamePrefix + string("_insertOpts"));
52 }
53 
55  std::ostream& out) const
56 {
57  out << mrpt::format(
58  "MAP TYPE = %s\n",
60  CHeightGridMap2D_MRF::TMapRepresentation>::value2name(mapType)
61  .c_str());
62  LOADABLEOPTS_DUMP_VAR(run_map_estimation_at_ctor, bool);
63  LOADABLEOPTS_DUMP_VAR(min_x, double);
64  LOADABLEOPTS_DUMP_VAR(max_x, double);
65  LOADABLEOPTS_DUMP_VAR(min_y, double);
66  LOADABLEOPTS_DUMP_VAR(max_y, double);
67  LOADABLEOPTS_DUMP_VAR(resolution, double);
68 
69  this->insertionOpts.dumpToTextStream(out);
70 }
71 
74 {
76  *dynamic_cast<const CHeightGridMap2D_MRF::TMapDefinition*>(&_def);
77  auto* obj = new CHeightGridMap2D_MRF(
78  def.mapType, def.min_x, def.max_x, def.min_y, def.max_y, def.resolution,
80  obj->insertionOptions = def.insertionOpts;
81  return obj;
82 }
83 // =========== End of Map definition Block =========
84 
86 
87 // Constructor
89  TMapRepresentation mapType, double x_min, double x_max, double y_min,
90  double y_max, double resolution, bool run_first_map_estimation_now)
91  : CRandomFieldGridMap2D(mapType, x_min, x_max, y_min, y_max, resolution),
92  insertionOptions()
93 {
94  m_rfgm_run_update_upon_clear = run_first_map_estimation_now;
95  // Set the grid to initial values (and adjusts the KF covariance matrix!)
96  // Also, calling clear() is mandatory to end initialization of our base
97  // class (read note in CRandomFieldGridMap2D::CRandomFieldGridMap2D)
99 }
100 
102  const double x, const double y, const double z,
104 {
105  const TRandomFieldCell* cell = cellByPos(x, y);
106  if (!cell) return false;
107  this->insertIndividualReading(
108  z, mrpt::math::TPoint2D(x, y), params.update_map_after_insertion,
109  true /*time invariant*/, params.pt_z_std);
110  return true;
111 }
112 double CHeightGridMap2D_MRF::dem_get_resolution() const { return m_resolution; }
113 size_t CHeightGridMap2D_MRF::dem_get_size_x() const { return m_size_x; }
114 size_t CHeightGridMap2D_MRF::dem_get_size_y() const { return m_size_y; }
116  const size_t cx, const size_t cy, double& z_out) const
117 {
118  const TRandomFieldCell* cell = cellByIndex(cx, cy);
119  if (cell && cell->kf_mean)
120  {
121  z_out = cell->kf_mean;
122  return true;
123  }
124  else
125  return false;
126 }
128  const double x, const double y, double& z_out) const
129 {
130  const TRandomFieldCell* cell = cellByPos(x, y);
131  if (cell && cell->kf_mean)
132  {
133  z_out = cell->kf_mean;
134  return true;
135  }
136  else
137  return false;
138 }
139 void CHeightGridMap2D_MRF::dem_update_map() { this->updateMapEstimation(); }
141 {
142  // Just do the generic clear:
144  // Anything else special for this derived class?
145 }
146 
148  const CObservation& obs, const CPose3D* robotPose)
149 {
150  return dem_internal_insertObservation(obs, robotPose);
151 }
152 
153 /*---------------------------------------------------------------
154  computeObservationLikelihood
155  ---------------------------------------------------------------*/
157  const CObservation& obs, const CPose3D& takenFrom)
158 {
159  MRPT_UNUSED_PARAM(obs);
160  MRPT_UNUSED_PARAM(takenFrom);
161  THROW_EXCEPTION("Not implemented yet!");
162 }
163 
166 {
167  dyngridcommon_writeToStream(out);
168 
169  // To assure compatibility: The size of each cell:
170  auto n = static_cast<uint32_t>(sizeof(TRandomFieldCell));
171  out << n;
172 
173  // Save the map contents:
174  n = static_cast<uint32_t>(m_map.size());
175  out << n;
176 
177 // Save the "m_map": This requires special handling for big endian systems:
178 #if MRPT_IS_BIG_ENDIAN
179  for (uint32_t i = 0; i < n; i++)
180  {
181  out << m_map[i].kf_mean << m_map[i].dm_mean << m_map[i].dmv_var_mean;
182  }
183 #else
184  // Little endian: just write all at once:
185  out.WriteBuffer(&m_map[0], sizeof(m_map[0]) * m_map.size());
186 #endif
187 
188  // Save the insertion options:
189  out << uint8_t(m_mapType) << m_cov << m_stackedCov;
190 
191  out << insertionOptions.sigma << insertionOptions.cutoffRadius
192  << insertionOptions.R_min << insertionOptions.R_max
193  << insertionOptions.KF_covSigma << insertionOptions.KF_initialCellStd
194  << insertionOptions.KF_observationModelNoise
195  << insertionOptions.KF_defaultCellMeanValue
196  << insertionOptions.KF_W_size;
197 
198  out << m_average_normreadings_mean << m_average_normreadings_var
199  << uint64_t(m_average_normreadings_count);
200 
201  out << genericMapParams;
202 }
203 
206 {
207  switch (version)
208  {
209  case 0:
210  {
211  dyngridcommon_readFromStream(in);
212 
213  // To assure compatibility: The size of each cell:
214  uint32_t n;
215  in >> n;
216 
217  ASSERT_EQUAL_(n, static_cast<uint32_t>(sizeof(TRandomFieldCell)));
218  // Load the map contents:
219  in >> n;
220  m_map.resize(n);
221 
222 // Read the note in writeToStream()
223 #if MRPT_IS_BIG_ENDIAN
224  for (uint32_t i = 0; i < n; i++)
225  in >> m_map[i].kf_mean >> m_map[i].dm_mean >>
226  m_map[i].dmv_var_mean;
227 #else
228  // Little endian: just read all at once:
229  in.ReadBuffer(&m_map[0], sizeof(m_map[0]) * m_map.size());
230 #endif
231 
232  {
233  uint8_t i;
234  in >> i;
235  m_mapType = TMapRepresentation(i);
236 
237  in >> m_cov >> m_stackedCov;
238 
239  in >> insertionOptions.sigma >> insertionOptions.cutoffRadius >>
240  insertionOptions.R_min >> insertionOptions.R_max >>
241  insertionOptions.KF_covSigma >>
242  insertionOptions.KF_initialCellStd >>
243  insertionOptions.KF_observationModelNoise >>
244  insertionOptions.KF_defaultCellMeanValue >>
245  insertionOptions.KF_W_size;
246  }
247 
248  {
249  uint64_t N;
250  in >> m_average_normreadings_mean >>
251  m_average_normreadings_var >> N;
252  m_average_normreadings_count = N;
253  }
254 
255  in >> genericMapParams;
256 
257  m_hasToRecoverMeanAndCov = true;
258  }
259  break;
260  default:
262  };
263 }
264 
265 /*---------------------------------------------------------------
266  TInsertionOptions
267  ---------------------------------------------------------------*/
270  std::ostream& out) const
271 {
272  out << mrpt::format(
273  "\n----------- [CHeightGridMap2D_MRF::TInsertionOptions] ------------ "
274  "\n\n");
275  out << mrpt::format("[TInsertionOptions.Common] ------------ \n\n");
276  internal_dumpToTextStream_common(
277  out); // Common params to all random fields maps:
278 
279  out << mrpt::format("\n");
280 }
281 
282 /*---------------------------------------------------------------
283  loadFromConfigFile
284  ---------------------------------------------------------------*/
286  const mrpt::config::CConfigFileBase& iniFile, const std::string& section)
287 {
288  // Common data fields for all random fields maps:
289  internal_loadFromConfigFile_common(iniFile, section);
290 
291  // Specific data fields for this class:
292  // ...
293 }
294 
295 /*---------------------------------------------------------------
296  getAs3DObject
297 ---------------------------------------------------------------*/
299  mrpt::opengl::CSetOfObjects::Ptr& outObj) const
300 {
301  MRPT_START
302  if (!genericMapParams.enableSaveAs3DObject) return;
304  MRPT_END
305 }
306 
307 /*---------------------------------------------------------------
308  getAs3DObject
309 ---------------------------------------------------------------*/
312  mrpt::opengl::CSetOfObjects::Ptr& varObj) const
313 {
314  MRPT_START
315  if (!genericMapParams.enableSaveAs3DObject) return;
316  CRandomFieldGridMap2D::getAs3DObject(meanObj, varObj);
317  MRPT_END
318 }
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
GLdouble GLdouble z
Definition: glext.h:3879
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
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.
GLenum GLsizei n
Definition: glext.h:5136
TMapRepresentation
The type of map representation to be used, see CRandomFieldGridMap2D for a discussion.
STL namespace.
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CArchive.cpp:49
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.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
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
unsigned char uint8_t
Definition: rptypes.h:44
#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"))
GLsizei const GLchar ** string
Definition: glext.h:4116
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.
unsigned __int64 uint64_t
Definition: rptypes.h:53
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:84
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
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()
GLuint in
Definition: glext.h:7391
GLsizei GLsizei GLchar * source
Definition: glext.h:4097
GLenum GLint GLint y
Definition: glext.h:3542
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.
GLenum GLint x
Definition: glext.h:3542
unsigned __int32 uint32_t
Definition: rptypes.h:50
Lightweight 2D point.
Definition: TPoint2D.h:31
GLenum const GLfloat * params
Definition: glext.h:3538
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: 1de0e027c Sat Sep 14 16:15:22 2019 +0200 at sáb sep 14 16:20:14 CEST 2019