Main MRPT website > C++ reference for MRPT 1.9.9
CHeightGridMap2D.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 "maps-precomp.h" // Precomp header
11 
13 #include <mrpt/utils/CConfigFileBase.h> // MRPT_LOAD_CONFIG_VAR()
14 #include <mrpt/poses/CPose3D.h>
16 #include <mrpt/opengl/CMesh.h>
18 
19 //#include <mrpt/system/os.h>
20 //#include <mrpt/utils/color_maps.h>
21 #include <mrpt/utils/CStream.h>
22 
23 using namespace mrpt::maps;
24 using namespace mrpt::obs;
25 using namespace mrpt::poses;
26 using namespace mrpt::math;
27 using namespace mrpt::utils;
28 using namespace mrpt::system;
29 using namespace std;
30 
31 // =========== Begin of Map definition ============
33  "CHeightGridMap2D,heightMap,dem", mrpt::maps::CHeightGridMap2D)
34 
36  : min_x(-2),
37  max_x(2),
38  min_y(-2),
39  max_y(2),
40  resolution(0.10f),
41  mapType(CHeightGridMap2D::mrSimpleAverage)
42 {
43 }
44 
47  const std::string& sectionNamePrefix)
48 {
49  // [<sectionNamePrefix>+"_creationOpts"]
50  const std::string sSectCreation =
51  sectionNamePrefix + string("_creationOpts");
52  MRPT_LOAD_CONFIG_VAR(min_x, double, source, sSectCreation);
53  MRPT_LOAD_CONFIG_VAR(max_x, double, source, sSectCreation);
54  MRPT_LOAD_CONFIG_VAR(min_y, double, source, sSectCreation);
55  MRPT_LOAD_CONFIG_VAR(max_y, double, source, sSectCreation);
56  MRPT_LOAD_CONFIG_VAR(resolution, double, source, sSectCreation);
57  mapType = source.read_enum<CHeightGridMap2D::TMapRepresentation>(
58  sSectCreation, "mapType", mapType);
59 
60  insertionOpts.loadFromConfigFile(
61  source, sectionNamePrefix + string("_insertOpts"));
62 }
63 
65  mrpt::utils::CStream& out) const
66 {
67  LOADABLEOPTS_DUMP_VAR(min_x, double);
68  LOADABLEOPTS_DUMP_VAR(max_x, double);
69  LOADABLEOPTS_DUMP_VAR(min_y, double);
70  LOADABLEOPTS_DUMP_VAR(max_y, double);
71  LOADABLEOPTS_DUMP_VAR(resolution, double);
72  out.printf(
73  "MAP TYPE = %s\n",
75  CHeightGridMap2D::TMapRepresentation>::value2name(mapType)
76  .c_str());
77 
78  this->insertionOpts.dumpToTextStream(out);
79 }
80 
83 {
85  *dynamic_cast<const CHeightGridMap2D::TMapDefinition*>(&_def);
87  def.mapType, def.min_x, def.max_x, def.min_y, def.max_y,
88  def.resolution);
89  obj->insertionOptions = def.insertionOpts;
90  return obj;
91 }
92 // =========== End of Map definition Block =========
93 
95 
97 
98 bool mrpt::global_settings::HEIGHTGRIDMAP_EXPORT3D_AS_MESH()
99 {
101 }
103 {
105 }
106 
107 /*---------------------------------------------------------------
108  Constructor
109  ---------------------------------------------------------------*/
111  TMapRepresentation mapType, double x_min, double x_max, double y_min,
112  double y_max, double resolution)
113  : CDynamicGrid<THeightGridmapCell>(x_min, x_max, y_min, y_max, resolution),
114  insertionOptions(),
115  m_mapType(mapType)
116 {
117 }
118 
119 /*---------------------------------------------------------------
120  clear
121  ---------------------------------------------------------------*/
123 /*---------------------------------------------------------------
124  isEmpty
125  ---------------------------------------------------------------*/
126 bool CHeightGridMap2D::isEmpty() const { return false; }
128  const double x, const double y, const double z,
130 {
131  THeightGridmapCell* cell = cellByPos(x, y);
132  if (!cell)
133  return false; // Out of the map: Ignore if we've not resized before.
134 
137  {
138  cell->u += z;
139  cell->v += z * z;
140  if (!cell->w)
141  {
142  cell->h = z; // First observation
143  cell->w = 1;
144  }
145  else
146  {
147  float W = cell->w++; // W = N-1
148  cell->h = (cell->h * W + z) / cell->w;
149  if (W > 0)
150  cell->var = 1 / (W) * (cell->v - pow(cell->u, 2) / cell->w);
151  }
152  } // end if really inserted
153  return true;
154 }
155 
157  const CObservation* obs, const CPose3D* robotPose)
158 {
159  return dem_internal_insertObservation(obs, robotPose);
160 }
161 
162 /*---------------------------------------------------------------
163  computeObservationLikelihood
164  ---------------------------------------------------------------*/
166  const CObservation* obs, const CPose3D& takenFrom)
167 {
168  MRPT_UNUSED_PARAM(obs);
169  MRPT_UNUSED_PARAM(takenFrom);
170 
171  THROW_EXCEPTION("Not implemented yet!");
172 }
173 
174 /*---------------------------------------------------------------
175  Implements the writing to a CStream capability of CSerializable objects
176  ---------------------------------------------------------------*/
178  mrpt::utils::CStream& out, int* version) const
179 {
180  if (version)
181  *version = 3;
182  else
183  {
185 
186  // To assure compatibility: The size of each cell:
187  uint32_t n = static_cast<uint32_t>(sizeof(THeightGridmapCell));
188  out << n;
189 
190  // Save the map contents:
191  n = static_cast<uint32_t>(m_map.size());
192  out << n;
194  it != m_map.end(); ++it)
195  out << it->h
196  << it->w; // This was removed in version 1: << it->history_Zs;
197 
198  // Save the insertion options:
199  out << uint8_t(m_mapType);
200 
203 
204  out << genericMapParams; // v2
205  }
206 }
207 
208 /*---------------------------------------------------------------
209  Implements the reading from a CStream capability of CSerializable objects
210  ---------------------------------------------------------------*/
212 {
213  switch (version)
214  {
215  case 0:
216  case 1:
217  case 2:
218  case 3:
219  {
220  dyngridcommon_readFromStream(in, version < 3);
221  // To ensure compatibility: The size of each cell:
222  uint32_t n;
223  in >> n;
224  ASSERT_(n == static_cast<uint32_t>(sizeof(THeightGridmapCell)));
225 
226  // Save the map contents:
227  in >> n;
228  m_map.resize(n);
230  it != m_map.end(); ++it)
231  {
232  in >> it->h >> it->w;
233  // Data member in version 0:
234  if (version == 0)
235  {
236  std::multimap<mrpt::system::TTimeStamp, float> history_Zs;
237  in >> history_Zs; // Discarded now...
238  }
239  }
240 
241  // Insertion options:
242  uint8_t ty;
243  in >> ty;
245 
248 
249  if (version >= 2) in >> genericMapParams;
250  }
251  break;
252  default:
254  };
255 }
256 
257 /*---------------------------------------------------------------
258  TInsertionOptions
259  ---------------------------------------------------------------*/
261  : filterByHeight(false), z_min(-0.5), z_max(0.5), colorMap(cmJET)
262 {
263 }
264 
265 /*---------------------------------------------------------------
266  dumpToTextStream
267  ---------------------------------------------------------------*/
269  mrpt::utils::CStream& out) const
270 {
271  out.printf(
272  "\n----------- [CHeightGridMap2D::TInsertionOptions] ------------ "
273  "\n\n");
274  out.printf(
275  "filterByHeight = %c\n",
276  filterByHeight ? 'y' : 'n');
277  out.printf("z_min = %f\n", z_min);
278  out.printf("z_max = %f\n", z_max);
279  out.printf(
280  "colormap = %s\n",
281  colorMap == cmJET ? "jet" : "grayscale");
282  out.printf("\n");
283 }
284 
285 /*---------------------------------------------------------------
286  loadFromConfigFile
287  ---------------------------------------------------------------*/
289  const mrpt::utils::CConfigFileBase& iniFile, const std::string& section)
290 {
291  MRPT_LOAD_CONFIG_VAR(filterByHeight, bool, iniFile, section)
292  MRPT_LOAD_CONFIG_VAR(z_min, float, iniFile, section)
293  MRPT_LOAD_CONFIG_VAR(z_max, float, iniFile, section)
294  string aux = iniFile.read_string(section, "colorMap", "jet");
295 
296  if (strCmp(aux, "jet"))
297  colorMap = cmJET;
298  else if (strCmp(aux, "grayscale"))
299  colorMap = cmGRAYSCALE;
300 }
301 
302 /*---------------------------------------------------------------
303  saveMetricMapRepresentationToFile
304  ---------------------------------------------------------------*/
306  const std::string& filNamePrefix) const
307 {
308  // Text matrix:
309  saveToTextFile(filNamePrefix + std::string("_mean.txt"));
310 }
311 
312 /*---------------------------------------------------------------
313  getAs3DObject
314 ---------------------------------------------------------------*/
316  mrpt::opengl::CSetOfObjects::Ptr& outObj) const
317 {
319 
321  {
322  opengl::CMesh::Ptr mesh = mrpt::make_aligned_shared<opengl::CMesh>();
323 
324  mesh->setGridLimits(m_x_min, m_x_max, m_y_min, m_y_max);
325 
326  mesh->setColor(0.4, 0.4, 0.4);
327 
328  mesh->enableWireFrame(true);
329  mesh->enableColorFromZ(true, insertionOptions.colorMap /*cmJET*/);
330 
331  CMatrixFloat Z, mask;
332  /*mesh->getZ(Z);
333 
334  mesh->getMask(mask);*/
335 
336  Z.setSize(m_size_x, m_size_y);
337  mask.setSize(m_size_x, m_size_y);
338 
339  for (size_t x = 0; x < m_size_x; x++)
340  {
341  for (size_t y = 0; y < m_size_y; y++)
342  {
343  const THeightGridmapCell* c = cellByIndex(x, y);
344  ASSERTDEB_(c);
345  Z.set_unsafe(x, y, c->h);
346  mask.set_unsafe(x, y, c->w ? 1 : 0);
347  }
348  }
349  mesh->setZ(Z);
350  mesh->setMask(mask);
351 
352  outObj->insert(mesh);
353  }
354  else
355  {
356  // As points:
358  mrpt::make_aligned_shared<mrpt::opengl::CPointCloudColoured>();
359  obj->setPointSize(2);
360 
361  // Find min/max:
362  float z_min, z_max;
363  float K;
364  if (this->getMinMaxHeight(z_min, z_max))
365  K = 1.0f / (z_max - z_min);
366  else
367  K = 1.0f;
368 
369  obj->reserve(m_size_x * m_size_y);
370  for (size_t x = 0; x < m_size_x; x++)
371  for (size_t y = 0; y < m_size_y; y++)
372  {
373  const THeightGridmapCell* c = cellByIndex(x, y);
374  ASSERTDEB_(c)
375  if (c->w)
376  {
377  float r, g, b;
378  const float col_idx = (c->h - z_min) * K;
379  colormap(
380  insertionOptions.colorMap, // cmJET, //cmGRAYSCALE,
381  col_idx, r, g, b);
382  obj->push_back(idx2x(x), idx2y(y), c->h, r, g, b);
383  }
384  }
385 
386  outObj->insert(obj);
387  }
388 }
389 
390 /** Return the number of cells with at least one height data inserted. */
392 {
393  switch (m_mapType)
394  {
395  case mrSimpleAverage:
396  {
397  size_t obsCells = 0;
398  const size_t N = m_map.size();
399  for (size_t i = 0; i < N; i++)
400  if (m_map[i].w) obsCells++;
401  return obsCells;
402  }
403  break;
404  default:
406  "countObservedCells() not implemented for this mapType (!?)")
407  };
408 }
409 
411 size_t CHeightGridMap2D::dem_get_size_x() const { return m_size_x; }
412 size_t CHeightGridMap2D::dem_get_size_y() const { return m_size_y; }
414  const size_t cx, const size_t cy, double& z_out) const
415 {
416  const THeightGridmapCell* cell = cellByIndex(cx, cy);
417  if (cell && cell->w)
418  {
419  z_out = cell->h;
420  return true;
421  }
422  else
423  return false;
424 }
426  const double x, const double y, double& z_out) const
427 {
428  const THeightGridmapCell* cell = cellByPos(x, y);
429  if (cell && cell->w)
430  {
431  z_out = cell->h;
432  return true;
433  }
434  else
435  {
436  return false;
437  }
438 }
440 {
441  // Nothing to do in this class: estimate is always up-to-date
442 }
443 
445  const mrpt::maps::CMetricMap* otherMap,
446  const mrpt::poses::CPose3D& otherMapPose,
447  const TMatchingRatioParams& params) const
448 {
449  MRPT_UNUSED_PARAM(otherMap);
450  MRPT_UNUSED_PARAM(otherMapPose);
452  return 0;
453 }
Digital Elevation Model (DEM), a mesh or grid representation of a surface which keeps the estimated h...
void dumpToTextStream_map_specific(mrpt::utils::CStream &out) const override
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()
GLenum GLint GLuint mask
Definition: glext.h:4050
Extra params for insertIndividualPoint()
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble z
Definition: glext.h:3872
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.
virtual void dem_update_map() override
Ensure that all observations are reflected in the map estimate.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
bool enableSaveAs3DObject
(Default=true) If false, calling CMetricMap::getAs3DObject() will have no effects ...
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
TMapRepresentation
The type of map representation to be used.
#define THROW_EXCEPTION(msg)
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.
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:5074
Scalar * iterator
Definition: eigen_plugins.h:26
#define LOADABLEOPTS_DUMP_VAR(variableName, variableType)
Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types a...
The contents of each cell in a CHeightGridMap2D map.
virtual 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.
virtual 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)
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
TMapGenericParams genericMapParams
Common params to all maps.
Definition: CMetricMap.h:281
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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:115
uint32_t w
[For mrSimpleAverage model] The accumulated weight: initially zero if un-observed, increased by one for each observation
void loadFromConfigFile_map_specific(const mrpt::utils::CConfigFileBase &source, const std::string &sectionNamePrefix) override
Load all map-specific params.
float v
Auxiliary (in meters)
void fill(const THeightGridmapCell &value)
Fills all the cells with the same value.
Definition: CDynamicGrid.h:119
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
This class allows loading and storing values and vectors of different types from a configuration text...
unsigned char uint8_t
Definition: rptypes.h:41
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:213
mrpt::maps::CHeightGridMap2D::TInsertionOptions insertionOptions
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
virtual size_t dem_get_size_x() const override
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
virtual 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 2D grid of dynamic size which stores any kind of data at each cell.
Definition: CDynamicGrid.h:40
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
A helper class that can convert an enum value into its textual representation, and viceversa...
Definition: TEnumType.h:38
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
const GLubyte * c
Definition: glext.h:6313
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
std::vector< THeightGridmapCell > m_map
The cells.
Definition: CDynamicGrid.h:44
GLbyte ty
Definition: glext.h:6092
bool dem_internal_insertObservation(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D *robotPose=nullptr)
Internal method called by internal_insertObservation()
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::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:32
GLubyte g
Definition: glext.h:6279
This namespace contains representation of robot actions and observations.
GLubyte GLubyte b
Definition: glext.h:6279
virtual double dem_get_resolution() const override
float u
Auxiliary variable for storing the incremental mean value (in meters).
GLsizei const GLchar ** string
Definition: glext.h:4101
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:234
double idx2x(int cx) const
Transform a cell index into a coordinate value of the cell central point.
Definition: CDynamicGrid.h:291
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
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.
std::shared_ptr< CMesh > Ptr
Definition: CMesh.h:41
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
virtual 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:55
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:88
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:41
bool saveToTextFile(const std::string &fileName) const
Saves a float representation of the grid (via "cell2float()") to a text file.
Definition: CDynamicGrid.h:329
void internal_clear() override
Internal method called by clear()
GLuint in
Definition: glext.h:7274
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
#define ASSERT_(f)
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...
void dyngridcommon_writeToStream(mrpt::utils::CStream &out) const
Definition: CDynamicGrid.h:348
TMapRepresentation m_mapType
The map representation type of this map.
GLenum GLint GLint y
Definition: glext.h:3538
double internal_computeObservationLikelihood(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D &takenFrom) override
Internal method called by computeObservationLikelihood()
GLsizei const GLfloat * value
Definition: glext.h:4117
void dumpToTextStream(mrpt::utils::CStream &out) const override
This method should clearly display all the contents of the structure in textual form, sending it to a CStream.
void saveMetricMapRepresentationToFile(const std::string &filNamePrefix) const override
This virtual method saves the map to a file "filNamePrefix"+< some_file_extension >...
mrpt::maps::CHeightGridMap2D::TInsertionOptions insertionOpts
#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...
GLenum GLint x
Definition: glext.h:3538
double min_x
See CHeightGridMap2D::CHeightGridMap2D.
bool isEmpty() const override
Returns true if the map is empty/no observation has been inserted.
unsigned __int32 uint32_t
Definition: rptypes.h:47
size_t countObservedCells() const
Return the number of cells with at least one height data inserted.
void dyngridcommon_readFromStream(mrpt::utils::CStream &in, bool cast_from_float=false)
Definition: CDynamicGrid.h:355
bool getMinMaxHeight(float &z_min, float &z_max) const
Computes the minimum and maximum height in the grid.
std::shared_ptr< CPointCloudColoured > Ptr
GLenum const GLfloat * params
Definition: glext.h:3534
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:597
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



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