MRPT  1.9.9
CHeightGridMap2D.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 
12 #include <mrpt/config/CConfigFileBase.h> // MRPT_LOAD_CONFIG_VAR()
13 #include <mrpt/img/color_maps.h>
15 #include <mrpt/opengl/CMesh.h>
17 #include <mrpt/poses/CPose3D.h>
20 
21 using namespace mrpt::maps;
22 using namespace mrpt::obs;
23 using namespace mrpt::poses;
24 using namespace mrpt::img;
25 using namespace mrpt::math;
26 using namespace mrpt::system;
27 using namespace std;
28 
29 // =========== Begin of Map definition ============
31  "mrpt::maps::CHeightGridMap2D,heightMap,dem", mrpt::maps::CHeightGridMap2D)
32 
34 
35  = default;
36 
39  const std::string& sectionNamePrefix)
40 {
41  // [<sectionNamePrefix>+"_creationOpts"]
42  const std::string sSectCreation =
43  sectionNamePrefix + string("_creationOpts");
44  MRPT_LOAD_CONFIG_VAR(min_x, double, source, sSectCreation);
45  MRPT_LOAD_CONFIG_VAR(max_x, double, source, sSectCreation);
46  MRPT_LOAD_CONFIG_VAR(min_y, double, source, sSectCreation);
47  MRPT_LOAD_CONFIG_VAR(max_y, double, source, sSectCreation);
48  MRPT_LOAD_CONFIG_VAR(resolution, double, source, sSectCreation);
49  mapType = source.read_enum<CHeightGridMap2D::TMapRepresentation>(
50  sSectCreation, "mapType", mapType);
51 
52  insertionOpts.loadFromConfigFile(
53  source, sectionNamePrefix + string("_insertOpts"));
54 }
55 
57  std::ostream& out) const
58 {
59  LOADABLEOPTS_DUMP_VAR(min_x, double);
60  LOADABLEOPTS_DUMP_VAR(max_x, double);
61  LOADABLEOPTS_DUMP_VAR(min_y, double);
62  LOADABLEOPTS_DUMP_VAR(max_y, double);
63  LOADABLEOPTS_DUMP_VAR(resolution, double);
64  out << mrpt::format(
65  "MAP TYPE = %s\n",
67  CHeightGridMap2D::TMapRepresentation>::value2name(mapType)
68  .c_str());
69 
70  this->insertionOpts.dumpToTextStream(out);
71 }
72 
75 {
77  *dynamic_cast<const CHeightGridMap2D::TMapDefinition*>(&_def);
78  auto* obj = new CHeightGridMap2D(
79  def.mapType, def.min_x, def.max_x, def.min_y, def.max_y,
80  def.resolution);
81  obj->insertionOptions = def.insertionOpts;
82  return obj;
83 }
84 // =========== End of Map definition Block =========
85 
87 
89 
90 bool mrpt::global_settings::HEIGHTGRIDMAP_EXPORT3D_AS_MESH()
91 {
93 }
95 {
97 }
98 
99 /*---------------------------------------------------------------
100  Constructor
101  ---------------------------------------------------------------*/
103  TMapRepresentation mapType, double x_min, double x_max, double y_min,
104  double y_max, double resolution)
105  : CDynamicGrid<THeightGridmapCell>(x_min, x_max, y_min, y_max, resolution),
106  insertionOptions(),
107  m_mapType(mapType)
108 {
109 }
110 
111 /*---------------------------------------------------------------
112  clear
113  ---------------------------------------------------------------*/
115 /*---------------------------------------------------------------
116  isEmpty
117  ---------------------------------------------------------------*/
118 bool CHeightGridMap2D::isEmpty() const { return false; }
120  const double x, const double y, const double z,
122 {
123  THeightGridmapCell* cell = cellByPos(x, y);
124  if (!cell)
125  return false; // Out of the map: Ignore if we've not resized before.
126 
129  {
130  cell->u += z;
131  cell->v += z * z;
132  if (!cell->w)
133  {
134  cell->h = z; // First observation
135  cell->w = 1;
136  }
137  else
138  {
139  float W = cell->w++; // W = N-1
140  cell->h = (cell->h * W + z) / cell->w;
141  if (W > 0)
142  cell->var = 1 / (W) * (cell->v - pow(cell->u, 2) / cell->w);
143  }
144  } // end if really inserted
145  return true;
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 
163  THROW_EXCEPTION("Not implemented yet!");
164 }
165 
166 uint8_t CHeightGridMap2D::serializeGetVersion() const { return 3; }
168 {
170 
171  // To assure compatibility: The size of each cell:
172  auto n = static_cast<uint32_t>(sizeof(THeightGridmapCell));
173  out << n;
174 
175  // Save the map contents:
176  n = static_cast<uint32_t>(m_map.size());
177  out << n;
178  for (const auto& it : m_map)
179  out << it.h
180  << it.w; // This was removed in version 1: << it->history_Zs;
181 
182  // Save the insertion options:
183  out << uint8_t(m_mapType);
184 
187 
188  out << genericMapParams; // v2
189 }
190 
192  mrpt::serialization::CArchive& in, uint8_t version)
193 {
194  switch (version)
195  {
196  case 0:
197  case 1:
198  case 2:
199  case 3:
200  {
201  dyngridcommon_readFromStream(in, version < 3);
202  // To ensure compatibility: The size of each cell:
203  uint32_t n;
204  in >> n;
205  ASSERT_(n == static_cast<uint32_t>(sizeof(THeightGridmapCell)));
206 
207  // Save the map contents:
208  in >> n;
209  m_map.resize(n);
210  for (auto& it : m_map)
211  {
212  in >> it.h >> it.w;
213  // Data member in version 0:
214  if (version == 0)
215  {
216  std::multimap<mrpt::system::TTimeStamp, float> history_Zs;
217  in >> history_Zs; // Discarded now...
218  }
219  }
220 
221  // Insertion options:
222  uint8_t ty;
223  in >> ty;
225 
228 
229  if (version >= 2) in >> genericMapParams;
230  }
231  break;
232  default:
234  };
235 }
236 
238 
239  = default;
240 
242  std::ostream& out) const
243 {
244  out << "\n----------- [CHeightGridMap2D::TInsertionOptions] ------------ "
245  "\n\n";
246  out << mrpt::format(
247  "filterByHeight = %c\n",
248  filterByHeight ? 'y' : 'n');
249  out << mrpt::format(
250  "z_min = %f\n", z_min);
251  out << mrpt::format(
252  "z_max = %f\n", z_max);
253  out << mrpt::format(
254  "colormap = %s\n",
255  colorMap == cmJET ? "jet" : "grayscale");
256  out << "\n";
257 }
258 
259 /*---------------------------------------------------------------
260  loadFromConfigFile
261  ---------------------------------------------------------------*/
263  const mrpt::config::CConfigFileBase& iniFile, const std::string& section)
264 {
265  MRPT_LOAD_CONFIG_VAR(filterByHeight, bool, iniFile, section)
266  MRPT_LOAD_CONFIG_VAR(z_min, float, iniFile, section)
267  MRPT_LOAD_CONFIG_VAR(z_max, float, iniFile, section)
268  string aux = iniFile.read_string(section, "colorMap", "jet");
269 
270  if (strCmp(aux, "jet"))
271  colorMap = cmJET;
272  else if (strCmp(aux, "grayscale"))
273  colorMap = cmGRAYSCALE;
274 }
275 
276 /*---------------------------------------------------------------
277  saveMetricMapRepresentationToFile
278  ---------------------------------------------------------------*/
280  const std::string& filNamePrefix) const
281 {
282  // Text matrix:
283  saveToTextFile(filNamePrefix + std::string("_mean.txt"));
284 }
285 
286 /*---------------------------------------------------------------
287  getAs3DObject
288 ---------------------------------------------------------------*/
290  mrpt::opengl::CSetOfObjects::Ptr& outObj) const
291 {
293 
295  {
296  opengl::CMesh::Ptr mesh = std::make_shared<opengl::CMesh>();
297 
298  mesh->setGridLimits(m_x_min, m_x_max, m_y_min, m_y_max);
299 
300  mesh->setColor(0.4, 0.4, 0.4);
301 
302  mesh->enableWireFrame(true);
303  mesh->enableColorFromZ(true, insertionOptions.colorMap /*cmJET*/);
304 
305  CMatrixFloat Z, mask;
306  /*mesh->getZ(Z);
307 
308  mesh->getMask(mask);*/
309 
311  mask.setSize(m_size_x, m_size_y);
312 
313  for (size_t x = 0; x < m_size_x; x++)
314  {
315  for (size_t y = 0; y < m_size_y; y++)
316  {
317  const THeightGridmapCell* c = cellByIndex(x, y);
318  ASSERTDEB_(c);
319  Z(x, y) = c->h;
320  mask(x, y) = c->w ? 1 : 0;
321  }
322  }
323  mesh->setZ(Z);
324  mesh->setMask(mask);
325 
326  outObj->insert(mesh);
327  }
328  else
329  {
330  // As points:
333  obj->setPointSize(2);
334 
335  // Find min/max:
336  float z_min, z_max;
337  float K;
338  if (this->getMinMaxHeight(z_min, z_max))
339  K = 1.0f / (z_max - z_min);
340  else
341  K = 1.0f;
342 
343  obj->reserve(m_size_x * m_size_y);
344  for (size_t x = 0; x < m_size_x; x++)
345  for (size_t y = 0; y < m_size_y; y++)
346  {
347  const THeightGridmapCell* c = cellByIndex(x, y);
348  ASSERTDEB_(c);
349  if (c->w)
350  {
351  float r, g, b;
352  const float col_idx = (c->h - z_min) * K;
353  colormap(
354  insertionOptions.colorMap, // cmJET, //cmGRAYSCALE,
355  col_idx, r, g, b);
356  obj->push_back(idx2x(x), idx2y(y), c->h, r, g, b);
357  }
358  }
359 
360  outObj->insert(obj);
361  }
362 }
363 
364 /** Return the number of cells with at least one height data inserted. */
366 {
367  switch (m_mapType)
368  {
369  case mrSimpleAverage:
370  {
371  size_t obsCells = 0;
372  const size_t N = m_map.size();
373  for (size_t i = 0; i < N; i++)
374  if (m_map[i].w) obsCells++;
375  return obsCells;
376  }
377  break;
378  default:
380  "countObservedCells() not implemented for this mapType (!?)");
381  };
382 }
383 
385 size_t CHeightGridMap2D::dem_get_size_x() const { return m_size_x; }
386 size_t CHeightGridMap2D::dem_get_size_y() const { return m_size_y; }
388  const size_t cx, const size_t cy, double& z_out) const
389 {
390  const THeightGridmapCell* cell = cellByIndex(cx, cy);
391  if (cell && cell->w)
392  {
393  z_out = cell->h;
394  return true;
395  }
396  else
397  return false;
398 }
400  const double x, const double y, double& z_out) const
401 {
402  const THeightGridmapCell* cell = cellByPos(x, y);
403  if (cell && cell->w)
404  {
405  z_out = cell->h;
406  return true;
407  }
408  else
409  {
410  return false;
411  }
412 }
414 {
415  // Nothing to do in this class: estimate is always up-to-date
416 }
417 
419  const mrpt::maps::CMetricMap* otherMap,
420  const mrpt::poses::CPose3D& otherMapPose,
421  const TMatchingRatioParams& params) const
422 {
423  MRPT_UNUSED_PARAM(otherMap);
424  MRPT_UNUSED_PARAM(otherMapPose);
426  return 0;
427 }
Digital Elevation Model (DEM), a mesh or grid representation of a surface which keeps the estimated h...
float var
The current standard deviation of the height (in meters)
float h
The current average height (in meters)
Virtual base for specifying the kind and parameters of one map (normally, to be inserted into mrpt::m...
Parameters for CMetricMap::compute3DMatchingRatio()
std::vector< THeightGridmapCell > m_map
The cells.
Definition: CDynamicGrid.h:42
void colormap(const TColormap &color_map, const float color_index, float &r, float &g, float &b)
Transform a float number in the range [0,1] into RGB components.
Definition: color_maps.cpp:114
GLenum GLint GLuint mask
Definition: glext.h:4062
Extra params for insertIndividualPoint()
GLdouble GLdouble z
Definition: glext.h:3879
static Ptr Create(Args &&... args)
void fill(const THeightGridmapCell &value)
Fills all the cells with the same value.
Definition: CDynamicGrid.h:109
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.
CHeightGridMap2D(TMapRepresentation mapType=mrSimpleAverage, double x_min=-2, double x_max=2, double y_min=-2, double y_max=2, double resolution=0.1)
Constructor.
void dem_update_map() override
Ensure that all observations are reflected in the map estimate.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
bool enableSaveAs3DObject
(Default=true) If false, calling CMetricMap::getAs3DObject() will have no effects ...
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
TMapRepresentation
The type of map representation to be used.
float compute3DMatchingRatio(const mrpt::maps::CMetricMap *otherMap, const mrpt::poses::CPose3D &otherMapPose, const TMatchingRatioParams &params) const override
See docs in base class: in this class it always returns 0.
GLenum GLsizei n
Definition: glext.h:5136
The contents of each cell in a CHeightGridMap2D map.
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.
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.
bool strCmp(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case sensitive)
STL namespace.
TMapGenericParams genericMapParams
Common params to all maps.
Definition: CMetricMap.h:275
double idx2x(int cx) const
Transform a cell index into a coordinate value of the cell central point.
Definition: CDynamicGrid.h:279
uint32_t w
[For mrSimpleAverage model] The accumulated weight: initially zero if un-observed, increased by one for each observation
float v
Auxiliary (in meters)
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4199
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.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::maps::CHeightGridMap2D::TInsertionOptions insertionOptions
size_t dem_get_size_x() const override
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.
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.
float z_min
Only when filterByHeight is true: coordinates are always RELATIVE to the robot for this filter...
A helper class that can convert an enum value into its textual representation, and viceversa...
const GLubyte * c
Definition: glext.h:6406
THeightGridmapCell * cellByPos(double x, double y)
Returns a pointer to the contents of a cell given by its coordinates, or nullptr if it is out of the ...
Definition: CDynamicGrid.h:201
GLbyte ty
Definition: glext.h:6179
GLubyte g
Definition: glext.h:6372
This namespace contains representation of robot actions and observations.
string iniFile(myDataDir+string("benchmark-options.ini"))
Undefined colormap [New in MRPT 2.0].
Definition: color_maps.h:32
GLubyte GLubyte b
Definition: glext.h:6372
double dem_get_resolution() const override
THeightGridmapCell * cellByIndex(unsigned int cx, unsigned int cy)
Returns a pointer to the contents of a cell given by its cell indexes, or nullptr if it is out of the...
Definition: CDynamicGrid.h:222
double internal_computeObservationLikelihood(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D &takenFrom) override
Internal method called by computeObservationLikelihood()
float u
Auxiliary variable for storing the incremental mean value (in meters).
void dyngridcommon_readFromStream(STREAM &in, bool cast_from_float=false)
Definition: CDynamicGrid.h:342
GLsizei const GLchar ** string
Definition: glext.h:4116
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
#define LOADABLEOPTS_DUMP_VAR(variableName, variableType)
Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types a...
void dumpToTextStream_map_specific(std::ostream &out) const override
#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...
mrpt::maps::CHeightGridMap2D::TMapRepresentation mapType
The kind of map representation (see CHeightGridMap2D::CHeightGridMap2D)
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.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
size_t dem_get_size_y() const override
static mrpt::maps::CMetricMap * internal_CreateFromMapDefinition(const mrpt::maps::TMetricMapInitializer &def)
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:52
bool filterByHeight
Whether to perform filtering by z-coordinate (default=false): coordinates are always RELATIVE to the ...
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
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: exceptions.h:190
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
void internal_clear() override
Internal method called by clear()
GLuint in
Definition: glext.h:7391
bool dem_internal_insertObservation(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D *robotPose=nullptr)
Internal method called by internal_insertObservation()
bool saveToTextFile(const std::string &fileName) const
Saves a float representation of the grid (via "cell2float()") to a text file.
Definition: CDynamicGrid.h:313
GLsizei GLsizei GLchar * source
Definition: glext.h:4097
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map: by default, it will be a mrpt::opengl::CMesh object...
TMapRepresentation m_mapType
The map representation type of this map.
GLenum GLint GLint y
Definition: glext.h:3542
bool internal_insertObservation(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D *robotPose=nullptr) override
Internal method called by insertObservation()
GLsizei const GLfloat * value
Definition: glext.h:4134
void saveMetricMapRepresentationToFile(const std::string &filNamePrefix) const override
This virtual method saves the map to a file "filNamePrefix"+< some_file_extension >...
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
mrpt::maps::CHeightGridMap2D::TInsertionOptions insertionOpts
GLenum GLint x
Definition: glext.h:3542
double min_x
See CHeightGridMap2D::CHeightGridMap2D.
bool isEmpty() const override
Returns true if the map is empty/no observation has been inserted.
size_t countObservedCells() const
Return the number of cells with at least one height data inserted.
bool getMinMaxHeight(float &z_min, float &z_max) const
Computes the minimum and maximum height in the grid.
This template class provides the basic functionality for a general 2D any-size, resizable container o...
void loadFromConfigFile_map_specific(const mrpt::config::CConfigFileBase &source, const std::string &sectionNamePrefix) override
Load all map-specific params.
GLenum const GLfloat * params
Definition: glext.h:3538
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
void HEIGHTGRIDMAP_EXPORT3D_AS_MESH(bool value)
If set to true (default), mrpt::maps::CHeightGridMap2D will be exported as a opengl::CMesh, otherwise, as a opengl::CPointCloudColoured Affects to:
bool HEIGHTGRIDMAP_EXPORT3D_AS_MESH_value
#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: cfb2df9d3 Fri Nov 15 06:57:14 2019 +0100 at vie nov 15 07:00:11 CET 2019