Main MRPT website > C++ reference for MRPT 1.9.9
CReflectivityGridMap2D.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 
14 #include <mrpt/poses/CPose2D.h>
15 #include <mrpt/utils/round.h> // round()
16 #include <mrpt/system/os.h>
19 #include <mrpt/utils/CStream.h>
20 
21 using namespace mrpt;
22 using namespace mrpt::maps;
23 using namespace mrpt::obs;
24 using namespace mrpt::poses;
25 using namespace mrpt::math;
26 using namespace mrpt::utils;
27 using namespace mrpt::system;
28 using namespace std;
29 
30 // =========== Begin of Map definition ============
32  "CReflectivityGridMap2D,reflectivityMap",
34 
36  : min_x(-10.0f),
37  max_x(10.0f),
38  min_y(-10.0f),
39  max_y(10.0f),
40  resolution(0.10f)
41 {
42 }
43 
46  const std::string& sectionNamePrefix)
47 {
48  // [<sectionNamePrefix>+"_creationOpts"]
49  const std::string sSectCreation =
50  sectionNamePrefix + string("_creationOpts");
51  MRPT_LOAD_CONFIG_VAR(min_x, double, source, sSectCreation);
52  MRPT_LOAD_CONFIG_VAR(max_x, double, source, sSectCreation);
53  MRPT_LOAD_CONFIG_VAR(min_y, double, source, sSectCreation);
54  MRPT_LOAD_CONFIG_VAR(max_y, double, source, sSectCreation);
55  MRPT_LOAD_CONFIG_VAR(resolution, double, source, sSectCreation);
56 
57  insertionOpts.loadFromConfigFile(
58  source, sectionNamePrefix + string("_insertOpts"));
59 }
60 
62  mrpt::utils::CStream& out) const
63 {
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 
76 {
78  *dynamic_cast<const CReflectivityGridMap2D::TMapDefinition*>(&_def);
80  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 // Lookup tables for log-odds
91 
92 /*---------------------------------------------------------------
93  Constructor
94  ---------------------------------------------------------------*/
96  double x_min, double x_max, double y_min, double y_max, double resolution)
97  : CDynamicGrid<int8_t>(x_min, x_max, y_min, y_max, resolution),
98  insertionOptions()
99 {
100  internal_clear();
101 }
102 
103 /*---------------------------------------------------------------
104  clear
105  ---------------------------------------------------------------*/
107 /*---------------------------------------------------------------
108  isEmpty
109  ---------------------------------------------------------------*/
110 bool CReflectivityGridMap2D::isEmpty() const { return false; }
111 /*---------------------------------------------------------------
112  insertObservation
113  ---------------------------------------------------------------*/
115  const CObservation* obs, const CPose3D* robotPose)
116 {
117  MRPT_START
118 
119  CPose2D robotPose2D;
120  CPose3D robotPose3D;
121 
122  if (robotPose)
123  {
124  robotPose2D = CPose2D(*robotPose);
125  robotPose3D = (*robotPose);
126  }
127  else
128  {
129  // Default values are (0,0,0)
130  }
131 
133  {
134  /********************************************************************
135  OBSERVATION TYPE: CObservationReflectivity
136  ********************************************************************/
137  const CObservationReflectivity* o =
138  static_cast<const CObservationReflectivity*>(obs);
139 
140  if (o->channel != -1 && insertionOptions.channel != -1 &&
142  {
143  return false; // Incorrect channel
144  }
145 
146  CPose3D sensor_pose;
147  sensor_pose.composeFrom(robotPose3D, o->sensorPose);
148 
149  // log-odd increment due to the observation:
150  const cell_t logodd_observation =
152 
153  // Update cell, with saturation:
154  cell_t* cell = cellByPos(sensor_pose.x(), sensor_pose.y());
155  if (!cell)
156  {
157  // We need to resize the grid!
158  const double new_x_min = std::min(m_x_min, sensor_pose.x());
159  const double new_y_min = std::min(m_y_min, sensor_pose.y());
160  const double new_x_max = std::min(m_x_max, sensor_pose.x());
161  const double new_y_max = std::min(m_y_max, sensor_pose.y());
162 
163  const int8_t default_cell = m_logodd_lut.p2l(0.5);
164  resize(
165  new_x_min, new_x_max, new_y_min, new_y_max, default_cell,
166  2.0 /* addit. margin */);
167 
168  // Now we should get the cell:
169  cell = cellByPos(sensor_pose.x(), sensor_pose.y());
170 
171  ASSERTMSG_(
172  cell != nullptr, "cell==nullptr even after resizing grid!?")
173  }
174 
175  const int cell_old = static_cast<int>(*cell);
176  int cell_new = cell_old + static_cast<int>(logodd_observation);
177  keep_min(cell_new, static_cast<int>(CELLTYPE_MAX) - 1);
178  keep_max(cell_new, static_cast<int>(CELLTYPE_MIN) + 1);
179 
180  *cell = static_cast<cell_t>(cell_new);
181 
182  return true; // Done!
183  } // end if "CObservationGasSensors"
184 
185  return false;
186 
187  MRPT_END
188 }
189 
190 /*---------------------------------------------------------------
191  computeObservationLikelihood
192  ---------------------------------------------------------------*/
194  const CObservation* obs, const CPose3D& takenFrom)
195 {
196  MRPT_START
197 
199  {
200  /********************************************************************
201  OBSERVATION TYPE: CObservationReflectivity
202  ********************************************************************/
203  const CObservationReflectivity* o =
204  static_cast<const CObservationReflectivity*>(obs);
205 
206  if (o->channel != -1 && insertionOptions.channel != -1 &&
208  {
209  return 0; // Incorrect channel
210  }
211 
212  CPose3D sensor_pose;
213  sensor_pose.composeFrom(takenFrom, o->sensorPose);
214 
215  cell_t* cell = cellByPos(sensor_pose.x(), sensor_pose.y());
216  if (!cell)
217  return 0; // out of the map..
218  else
219  {
222  return -0.5 * square(
223  (m_logodd_lut.l2p(*cell) - o->reflectivityLevel) /
224  o->sensorStdNoise);
225  }
226  }
227  else
228  return 0;
229 
230  MRPT_END
231 }
232 
233 /*---------------------------------------------------------------
234  Implements the writing to a CStream capability of CSerializable objects
235  ---------------------------------------------------------------*/
237  mrpt::utils::CStream& out, int* version) const
238 {
239  if (version)
240  *version = 3;
241  else
242  {
244 
245  // Map cells:
246  const uint32_t n = static_cast<uint32_t>(m_map.size());
247  out << n;
248  if (n) out.WriteBuffer(&m_map[0], n);
249 
250  // Save the insertion options
251  out << insertionOptions.channel; // v3
252 
253  out << genericMapParams; // v1
254  }
255 }
256 
257 /*---------------------------------------------------------------
258  Implements the reading from a CStream capability of CSerializable objects
259  ---------------------------------------------------------------*/
261  mrpt::utils::CStream& in, int version)
262 {
263  switch (version)
264  {
265  case 0:
266  case 1:
267  case 2:
268  case 3:
269  {
270  dyngridcommon_readFromStream(in, version < 2);
271 
272  // Map cells:
273  uint32_t n;
274  in >> n;
275  m_map.resize(n);
276  if (n) in.ReadBuffer(&m_map[0], n);
277 
278  // Load the insertion options:
279  if (version >= 3) in >> insertionOptions.channel;
280 
281  if (version >= 1) in >> genericMapParams;
282  }
283  break;
284  default:
286  };
287 }
288 
289 /*---------------------------------------------------------------
290  TInsertionOptions
291  ---------------------------------------------------------------*/
293 /*---------------------------------------------------------------
294  dumpToTextStream
295  ---------------------------------------------------------------*/
297  mrpt::utils::CStream& out) const
298 {
299  out.printf(
300  "\n----------- [CReflectivityGridMap2D::TInsertionOptions] "
301  "------------ \n\n");
302 
303  LOADABLEOPTS_DUMP_VAR(channel, int);
304 
305  out.printf("\n");
306 }
307 
308 /*---------------------------------------------------------------
309  loadFromConfigFile
310  ---------------------------------------------------------------*/
312  const mrpt::utils::CConfigFileBase& iniFile, const std::string& section)
313 {
314  MRPT_UNUSED_PARAM(iniFile);
315  MRPT_UNUSED_PARAM(section);
316  MRPT_LOAD_CONFIG_VAR(channel, int, iniFile, section);
317 }
318 
319 /*---------------------------------------------------------------
320  saveMetricMapRepresentationToFile
321  ---------------------------------------------------------------*/
323  const std::string& filNamePrefix) const
324 {
325  // Text matrix:
326  saveToTextFile(filNamePrefix + std::string("_probability.txt"));
327 }
328 
329 /*---------------------------------------------------------------
330  getAsImage
331  ---------------------------------------------------------------*/
333  utils::CImage& img, bool verticalFlip, bool forceRGB) const
334 {
335  if (!forceRGB)
336  { // 8bit gray-scale
337  img.resize(m_size_x, m_size_y, 1, true);
338  const cell_t* srcPtr = &m_map[0];
339  unsigned char* destPtr;
340  for (unsigned int y = 0; y < m_size_y; y++)
341  {
342  if (!verticalFlip)
343  destPtr = img(0, m_size_y - 1 - y);
344  else
345  destPtr = img(0, y);
346  for (unsigned int x = 0; x < m_size_x; x++)
347  {
348  *destPtr++ = m_logodd_lut.l2p_255(*srcPtr++);
349  }
350  }
351  }
352  else
353  { // 24bit RGB:
354  img.resize(m_size_x, m_size_y, 3, true);
355  const cell_t* srcPtr = &m_map[0];
356  unsigned char* destPtr;
357  for (unsigned int y = 0; y < m_size_y; y++)
358  {
359  if (!verticalFlip)
360  destPtr = img(0, m_size_y - 1 - y);
361  else
362  destPtr = img(0, y);
363  for (unsigned int x = 0; x < m_size_x; x++)
364  {
365  uint8_t c = m_logodd_lut.l2p_255(*srcPtr++);
366  *destPtr++ = c;
367  *destPtr++ = c;
368  *destPtr++ = c;
369  }
370  }
371  }
372 }
373 
374 /*---------------------------------------------------------------
375  getAs3DObject
376 ---------------------------------------------------------------*/
378  mrpt::opengl::CSetOfObjects::Ptr& outSetOfObj) const
379 {
381 
382  MRPT_START
383 
385  mrpt::make_aligned_shared<opengl::CTexturedPlane>();
386 
387  outObj->setPlaneCorners(m_x_min, m_x_max, m_y_min, m_y_max);
388 
389  // Create the color & transparecy (alpha) images:
390  CImage imgColor(m_size_x, m_size_y, 1);
391  CImage imgTrans(m_size_x, m_size_y, 1);
392 
393  const cell_t* srcPtr = &m_map[0];
394  unsigned char* destPtr_color;
395  unsigned char* destPtr_trans;
396 
397  for (unsigned int y = 0; y < m_size_y; y++)
398  {
399  destPtr_color = imgColor(0, y);
400  destPtr_trans = imgTrans(0, y);
401  for (unsigned int x = 0; x < m_size_x; x++)
402  {
403  uint8_t cell255 = m_logodd_lut.l2p_255(*srcPtr++);
404  *destPtr_color++ = cell255;
405 
406  int8_t auxC = (int8_t)((signed short)cell255) - 128;
407  *destPtr_trans++ = auxC > 0 ? (auxC << 1) : ((-auxC) << 1);
408  }
409  }
410 
411  outObj->assignImage_fast(imgColor, imgTrans);
412  outSetOfObj->insert(outObj);
413 
414  MRPT_END
415 }
416 
418  const mrpt::maps::CMetricMap* otherMap,
419  const mrpt::poses::CPose3D& otherMapPose,
420  const TMatchingRatioParams& params) const
421 {
422  MRPT_UNUSED_PARAM(otherMap);
423  MRPT_UNUSED_PARAM(otherMapPose);
425  return 0;
426 }
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
Virtual base for specifying the kind and parameters of one map (normally, to be inserted into mrpt::m...
Parameters for CMetricMap::compute3DMatchingRatio()
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 this always returns 0.
#define ASSERT_BELOWEQ_(__A, __B)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
#define min(a, b)
CReflectivityGridMap2D(double x_min=-2, double x_max=2, double y_min=-2, double y_max=2, double resolution=0.1)
Constructor.
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 ...
std::shared_ptr< CTexturedPlane > Ptr
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLenum GLsizei n
Definition: glext.h:5074
#define LOADABLEOPTS_DUMP_VAR(variableName, variableType)
Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types a...
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:64
float reflectivityLevel
The read reflectivity level, in the range [0,1] (0=black, 1=white).
virtual void resize(double new_x_min, double new_x_max, double new_y_min, double new_y_max, const int8_t &defaultValueNewCells, double additionalMarginMeters=2.0)
Changes the size of the grid, maintaining previous contents.
Definition: CDynamicGrid.h:129
signed char int8_t
Definition: rptypes.h:40
Declares a class derived from "CObservation" that encapsules a single short-range reflectivity measur...
STL namespace.
TMapGenericParams genericMapParams
Common params to all maps.
Definition: CMetricMap.h:281
void fill(const int8_t &value)
Fills all the cells with the same value.
Definition: CDynamicGrid.h:119
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void saveMetricMapRepresentationToFile(const std::string &filNamePrefix) const override
This virtual method saves the map to a file "filNamePrefix"+< some_file_extension >...
float sensorStdNoise
1-sigma of the sensor Gaussian noise (in the same normalized units than reflectivityLevel) ...
A 2D grid map representing the reflectivity of the environment (for example, measured with an IR prox...
This class allows loading and storing values and vectors of different types from a configuration text...
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
mrpt::poses::CPose3D sensorPose
The pose of this sensor in robot&#39;s local coordinates.
unsigned char uint8_t
Definition: rptypes.h:41
int8_t * 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
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
A 2D grid of dynamic size which stores any kind of data at each cell.
Definition: CDynamicGrid.h:40
cell_t p2l(const float p)
Scales a real valued probability in [0,1] to an integer representation of: log(p)-log(1-p) in the val...
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map.
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void composeFrom(const CPose3D &A, const CPose3D &B)
Makes "this = A (+) B"; this method is slightly more efficient than "this= A + B;" since it avoids th...
Definition: CPose3D.cpp:639
bool internal_insertObservation(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D *robotPose=nullptr) override
Internal method called by insertObservation()
const GLubyte * c
Definition: glext.h:6313
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLint GLvoid * img
Definition: glext.h:3763
std::vector< int8_t > m_map
The cells.
Definition: CDynamicGrid.h:44
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
This namespace contains representation of robot actions and observations.
bool isEmpty() const override
Returns true if the map is empty/no observation has been inserted.
GLsizei const GLchar ** string
Definition: glext.h:4101
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
Definition: bits.h:227
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
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.
mrpt::maps::CReflectivityGridMap2D::TInsertionOptions insertionOptions
#define MRPT_START
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.
#define ASSERT_ABOVEEQ_(__A, __B)
uint8_t l2p_255(const cell_t l)
Scales an integer representation of the log-odd into a linear scale [0,255], using p=exp(l)/(1+exp(l)...
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.
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
Definition: bits.h:220
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:55
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
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
void internal_clear() override
Internal method called by clear()
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
void dumpToTextStream_map_specific(mrpt::utils::CStream &out) const override
bool saveToTextFile(const std::string &fileName) const
Saves a float representation of the grid (via "cell2float()") to a text file.
Definition: CDynamicGrid.h:329
GLuint in
Definition: glext.h:7274
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
int16_t channel
The reflectivity channel for this map.
void dyngridcommon_writeToStream(mrpt::utils::CStream &out) const
Definition: CDynamicGrid.h:348
int16_t channel
The channel for this observation.
GLenum GLint GLint y
Definition: glext.h:3538
void getAsImage(utils::CImage &img, bool verticalFlip=false, bool forceRGB=false) const
Returns the grid as a 8-bit graylevel image, where each pixel is a cell (output image is RGB only if ...
void loadFromConfigFile_map_specific(const mrpt::utils::CConfigFileBase &source, const std::string &sectionNamePrefix) override
Load all map-specific params.
#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
static CLogOddsGridMapLUT< cell_t > m_logodd_lut
Lookup tables for log-odds.
mrpt::maps::CReflectivityGridMap2D::TInsertionOptions insertionOpts
double internal_computeObservationLikelihood(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D &takenFrom) override
Internal method called by computeObservationLikelihood()
float l2p(const cell_t l)
Scales an integer representation of the log-odd into a real valued probability in [0...
unsigned __int32 uint32_t
Definition: rptypes.h:47
void dyngridcommon_readFromStream(mrpt::utils::CStream &in, bool cast_from_float=false)
Definition: CDynamicGrid.h:355
#define ASSERTMSG_(f, __ERROR_MSG)
static mrpt::maps::CMetricMap * internal_CreateFromMapDefinition(const mrpt::maps::TMetricMapInitializer &def)
GLenum const GLfloat * params
Definition: glext.h:3534
double min_x
See CReflectivityGridMap2DOptions::CReflectivityGridMap2DOptions.
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
One static instance of this struct should exist in any class implementing CLogOddsGridMap2D to hold t...



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