Main MRPT website > C++ reference for MRPT 1.9.9
CLogOddsGridMap2D.h
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 #ifndef CLogOddsGridMap2D_H
11 #define CLogOddsGridMap2D_H
12 
13 #include <mrpt/utils/core_defs.h>
14 #include <cstdint>
15 #include <vector>
16 
17 namespace mrpt
18 {
19 namespace maps
20 {
21 namespace detail
22 {
23 template <typename TCELL>
25 // Specializations:
26 template <>
28 {
29  static const int8_t CELLTYPE_MIN = -127; // In mrpt <0.9.4 was -128 (!) -
30  // This is to make it compatible
31  // with all architectures.
32  static const int8_t CELLTYPE_MAX = 127;
33  static const int8_t P2LTABLE_SIZE = CELLTYPE_MAX;
34  static const size_t LOGODDS_LUT_ENTRIES = 1 << 8;
35 };
36 template <>
38 {
39  static const int16_t CELLTYPE_MIN =
40  -32767; // In mrpt <0.9.4 was -32768 (!).
41  static const int16_t CELLTYPE_MAX = 32767;
42  static const int16_t P2LTABLE_SIZE = CELLTYPE_MAX;
43  static const size_t LOGODDS_LUT_ENTRIES = 1 << 16;
44 };
45 }
46 
47 /** A generic provider of log-odds grid-map maintainance functions.
48  * Map cells must be type TCELL, which can be only:
49  * - int8_t or
50  * - int16_t
51  *
52  * \sa CLogOddsGridMapLUT, See derived classes for usage examples.
53  * \ingroup mrpt_maps_grp
54  */
55 template <typename TCELL>
57 {
58  /** The type of cells */
59  typedef TCELL cell_t;
61 
62  /** Performs the Bayesian fusion of a new observation of a cell, without
63  * checking for grid limits nor updateInfoChangeOnly.
64  * This method increases the "occupancy-ness" of a cell, managing possible
65  * saturation.
66  * \param x Cell index in X axis.
67  * \param y Cell index in Y axis.
68  * \param logodd_obs Observation of the cell, in log-odd form as
69  * transformed by p2l.
70  * \param thres This must be CELLTYPE_MIN+logodd_obs
71  * \sa updateCell, updateCell_fast_free
72  */
73  inline static void updateCell_fast_occupied(
74  const unsigned x, const unsigned y, const cell_t logodd_obs,
75  const cell_t thres, cell_t* mapArray, const unsigned _size_x)
76  {
77  cell_t* theCell = mapArray + (x + y * _size_x);
78  if (*theCell > thres)
79  *theCell -= logodd_obs;
80  else
81  *theCell = traits_t::CELLTYPE_MIN;
82  }
83 
84  /** Performs the Bayesian fusion of a new observation of a cell, without
85  * checking for grid limits nor updateInfoChangeOnly.
86  * This method increases the "occupancy-ness" of a cell, managing possible
87  * saturation.
88  * \param theCell The cell to modify
89  * \param logodd_obs Observation of the cell, in log-odd form as
90  * transformed by p2l.
91  * \param thres This must be CELLTYPE_MIN+logodd_obs
92  * \sa updateCell, updateCell_fast_free
93  */
94  inline static void updateCell_fast_occupied(
95  cell_t* theCell, const cell_t logodd_obs, const cell_t thres)
96  {
97  if (*theCell > thres)
98  *theCell -= logodd_obs;
99  else
100  *theCell = traits_t::CELLTYPE_MIN;
101  }
102 
103  /** Performs the Bayesian fusion of a new observation of a cell, without
104  * checking for grid limits nor updateInfoChangeOnly.
105  * This method increases the "free-ness" of a cell, managing possible
106  * saturation.
107  * \param x Cell index in X axis.
108  * \param y Cell index in Y axis.
109  * \param logodd_obs Observation of the cell, in log-odd form as
110  * transformed by p2l.
111  * \param thres This must be CELLTYPE_MAX-logodd_obs
112  * \sa updateCell_fast_occupied
113  */
114  inline static void updateCell_fast_free(
115  const unsigned x, const unsigned y, const cell_t logodd_obs,
116  const cell_t thres, cell_t* mapArray, const unsigned _size_x)
117  {
118  cell_t* theCell = mapArray + (x + y * _size_x);
119  if (*theCell < thres)
120  *theCell += logodd_obs;
121  else
122  *theCell = traits_t::CELLTYPE_MAX;
123  }
124 
125  /** Performs the Bayesian fusion of a new observation of a cell, without
126  * checking for grid limits nor updateInfoChangeOnly.
127  * This method increases the "free-ness" of a cell, managing possible
128  * saturation.
129  * \param x Cell index in X axis.
130  * \param y Cell index in Y axis.
131  * \param logodd_obs Observation of the cell, in log-odd form as
132  * transformed by p2l.
133  * \param thres This must be CELLTYPE_MAX-logodd_obs
134  * \sa updateCell_fast_occupied
135  */
136  inline static void updateCell_fast_free(
137  cell_t* theCell, const cell_t logodd_obs, const cell_t thres)
138  {
139  if (*theCell < thres)
140  *theCell += logodd_obs;
141  else
142  *theCell = traits_t::CELLTYPE_MAX;
143  }
144 
145 }; // end of CLogOddsGridMap2D
146 
147 /** One static instance of this struct should exist in any class implementing
148  *CLogOddsGridMap2D to hold the Look-up-tables (LUTs) for log-odss Bayesian
149  *update.
150  * Map cells must be type TCELL, which can be only:
151  * - int8_t or
152  * - int16_t
153  *
154  * \sa CLogOddsGridMap2D, see derived classes for usage examples.
155  * \ingroup mrpt_maps_grp
156  */
157 template <typename TCELL>
159 {
160  /** The type of */
161  typedef TCELL cell_t;
163 
164  /** A lookup table to compute occupancy probabilities in [0,1] from integer
165  * log-odds values in the cells, using \f$ p(m_{xy}) =
166  * \frac{1}{1+exp(-log_odd)} \f$.
167  */
168  std::vector<float> logoddsTable;
169 
170  /** A lookup table to compute occupancy probabilities in the range [0,255]
171  * from integer log-odds values in the cells, using \f$ p(m_{xy}) =
172  * \frac{1}{1+exp(-log_odd)} \f$.
173  * This is used to speed-up conversions to grayscale images.
174  */
175  std::vector<uint8_t> logoddsTable_255;
176 
177  /** A lookup table for passing from float to log-odds as cell_t. */
178  std::vector<cell_t> p2lTable;
179 
180  /** Constructor: computes all the required stuff. */
182  {
183  // The factor for converting log2-odds into integers:
184  static const double LOGODD_K = 16;
185  static const double LOGODD_K_INV = 1.0 / LOGODD_K;
186 
187  logoddsTable.resize(traits_t::LOGODDS_LUT_ENTRIES);
188  logoddsTable_255.resize(traits_t::LOGODDS_LUT_ENTRIES);
189  for (int i = traits_t::CELLTYPE_MIN; i <= traits_t::CELLTYPE_MAX; i++)
190  {
191  float f = 1.0f / (1.0f + exp(-i * LOGODD_K_INV));
192  unsigned int idx = -traits_t::CELLTYPE_MIN + i;
193  logoddsTable[idx] = f;
194  logoddsTable_255[idx] = (uint8_t)(f * 255.0f);
195  }
196 
197  // Build the p2lTable as well:
198  p2lTable.resize(traits_t::P2LTABLE_SIZE + 1);
199  double K = 1.0 / traits_t::P2LTABLE_SIZE;
200  for (int j = 0; j <= traits_t::P2LTABLE_SIZE; j++)
201  {
202  double p = j * K;
203  if (p == 0)
204  p = 1e-14;
205  else if (p == 1)
206  p = 1 - 1e-14;
207 
208  double logodd = log(p) - log(1 - p);
209  int L = round(logodd * LOGODD_K);
210  if (L > traits_t::CELLTYPE_MAX)
211  L = traits_t::CELLTYPE_MAX;
212  else if (L < traits_t::CELLTYPE_MIN)
213  L = traits_t::CELLTYPE_MIN;
214  p2lTable[j] = L;
215  }
216  }
217 
218  /** Scales an integer representation of the log-odd into a real valued
219  * probability in [0,1], using p=exp(l)/(1+exp(l))
220  */
221  inline float l2p(const cell_t l)
222  {
223  if (l < traits_t::CELLTYPE_MIN)
224  return logoddsTable[0]; // This is needed since min can be -127 and
225  // int8_t can be -128.
226  else
227  return logoddsTable[-traits_t::CELLTYPE_MIN + l];
228  }
229 
230  /** Scales an integer representation of the log-odd into a linear scale
231  * [0,255], using p=exp(l)/(1+exp(l))
232  */
233  inline uint8_t l2p_255(const cell_t l)
234  {
235  if (l < traits_t::CELLTYPE_MIN)
236  return logoddsTable_255[0]; // This is needed since min can be -127
237  // and int8_t can be -128.
238  else
239  return logoddsTable_255[-traits_t::CELLTYPE_MIN + l];
240  }
241 
242  /** Scales a real valued probability in [0,1] to an integer representation
243  * of: log(p)-log(1-p) in the valid range of cell_t.
244  */
245  inline cell_t p2l(const float p)
246  {
247  return p2lTable[static_cast<unsigned int>(p * traits_t::P2LTABLE_SIZE)];
248  }
249 
250 }; // end of CLogOddsGridMap2D
251 
252 } // End of namespace
253 } // End of namespace
254 
255 #endif
TCELL cell_t
The type of cells.
signed char int8_t
Definition: rptypes.h:40
std::vector< uint8_t > logoddsTable_255
A lookup table to compute occupancy probabilities in the range [0,255] from integer log-odds values i...
unsigned char uint8_t
Definition: rptypes.h:41
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...
__int16 int16_t
Definition: rptypes.h:43
static void updateCell_fast_free(const unsigned x, const unsigned y, const cell_t logodd_obs, const cell_t thres, cell_t *mapArray, const unsigned _size_x)
Performs the Bayesian fusion of a new observation of a cell, without checking for grid limits nor upd...
detail::logoddscell_traits< TCELL > traits_t
CLogOddsGridMapLUT()
Constructor: computes all the required stuff.
std::vector< cell_t > p2lTable
A lookup table for passing from float to log-odds as cell_t.
detail::logoddscell_traits< TCELL > traits_t
std::vector< float > logoddsTable
A lookup table to compute occupancy probabilities in [0,1] from integer log-odds values in the cells...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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)...
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLenum GLint GLint y
Definition: glext.h:3538
A generic provider of log-odds grid-map maintainance functions.
GLenum GLint x
Definition: glext.h:3538
float l2p(const cell_t l)
Scales an integer representation of the log-odd into a real valued probability in [0...
static void updateCell_fast_free(cell_t *theCell, const cell_t logodd_obs, const cell_t thres)
Performs the Bayesian fusion of a new observation of a cell, without checking for grid limits nor upd...
GLfloat GLfloat p
Definition: glext.h:6305
One static instance of this struct should exist in any class implementing CLogOddsGridMap2D to hold t...
static void updateCell_fast_occupied(cell_t *theCell, const cell_t logodd_obs, const cell_t thres)
Performs the Bayesian fusion of a new observation of a cell, without checking for grid limits nor upd...
static void updateCell_fast_occupied(const unsigned x, const unsigned y, const cell_t logodd_obs, const cell_t thres, cell_t *mapArray, const unsigned _size_x)
Performs the Bayesian fusion of a new observation of a cell, without checking for grid limits nor upd...



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