MRPT  1.9.9
CLogOddsGridMapLUT.h
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 #pragma once
11 
13 
14 namespace mrpt::maps
15 {
16 /** One static instance of this struct should exist in any class implementing
17  * CLogOddsGridMap2D to hold the Look-up-tables (LUTs) for log-odss Bayesian
18  *update. Map cells must be type TCELL, which can be only:
19  * - int8_t or
20  * - int16_t
21  *
22  * \sa CLogOddsGridMap2D, see derived classes for usage examples.
23  * \ingroup mrpt_maps_grp
24  */
25 template <typename TCELL>
27 {
28  /** The type of */
29  using cell_t = TCELL;
31 
32  /** A lookup table to compute occupancy probabilities in [0,1] from integer
33  * log-odds values in the cells, using \f$ p(m_{xy}) =
34  * \frac{1}{1+exp(-log_odd)} \f$.
35  */
36  std::vector<float> logoddsTable;
37 
38  /** A lookup table to compute occupancy probabilities in the range [0,255]
39  * from integer log-odds values in the cells, using \f$ p(m_{xy}) =
40  * \frac{1}{1+exp(-log_odd)} \f$.
41  * This is used to speed-up conversions to grayscale images.
42  */
43  std::vector<uint8_t> logoddsTable_255;
44 
45  /** A lookup table for passing from float to log-odds as cell_t. */
46  std::vector<cell_t> p2lTable;
47 
48  /** Constructor: computes all the required stuff. */
50  {
51  // The factor for converting log2-odds into integers:
52  static const double LOGODD_K = 16;
53  static const double LOGODD_K_INV = 1.0 / LOGODD_K;
54 
55  logoddsTable.resize(traits_t::LOGODDS_LUT_ENTRIES);
56  logoddsTable_255.resize(traits_t::LOGODDS_LUT_ENTRIES);
57  for (int i = traits_t::CELLTYPE_MIN; i <= traits_t::CELLTYPE_MAX; i++)
58  {
59  float f = 1.0f / (1.0f + exp(-i * LOGODD_K_INV));
60  unsigned int idx = -traits_t::CELLTYPE_MIN + i;
61  logoddsTable[idx] = f;
62  logoddsTable_255[idx] = (uint8_t)(f * 255.0f);
63  }
64 
65  // Build the p2lTable as well:
66  p2lTable.resize(traits_t::P2LTABLE_SIZE + 1);
67  double K = 1.0 / traits_t::P2LTABLE_SIZE;
68  for (int j = 0; j <= traits_t::P2LTABLE_SIZE; j++)
69  {
70  double p = j * K;
71  if (p == 0)
72  p = 1e-14;
73  else if (p == 1)
74  p = 1 - 1e-14;
75 
76  double logodd = log(p) - log(1 - p);
77  int L = round(logodd * LOGODD_K);
78  if (L > traits_t::CELLTYPE_MAX)
79  L = traits_t::CELLTYPE_MAX;
80  else if (L < traits_t::CELLTYPE_MIN)
81  L = traits_t::CELLTYPE_MIN;
82  p2lTable[j] = L;
83  }
84  }
85 
86  /** Scales an integer representation of the log-odd into a real valued
87  * probability in [0,1], using p=exp(l)/(1+exp(l))
88  */
89  inline float l2p(const cell_t l)
90  {
91  if (l < traits_t::CELLTYPE_MIN)
92  // This is needed since min can be -127 and int8_t can be -128.
93  return logoddsTable[0];
94  else
95  return logoddsTable[-traits_t::CELLTYPE_MIN + l];
96  }
97 
98  /** Scales an integer representation of the log-odd into a linear scale
99  * [0,255], using p=exp(l)/(1+exp(l))
100  */
101  inline uint8_t l2p_255(const cell_t l)
102  {
103  if (l < traits_t::CELLTYPE_MIN)
104  // This is needed since min can be -127 and int8_t can be -128.
105  return logoddsTable_255[0];
106  else
107  return logoddsTable_255[-traits_t::CELLTYPE_MIN + l];
108  }
109 
110  /** Scales a real valued probability in [0,1] to an integer representation
111  * of: log(p)-log(1-p) in the valid range of cell_t.
112  */
113  inline cell_t p2l(const float p)
114  {
115  return p2lTable[static_cast<unsigned int>(p * traits_t::P2LTABLE_SIZE)];
116  }
117 };
118 
119 } // namespace mrpt::maps
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:44
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...
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.
std::vector< float > logoddsTable
A lookup table to compute occupancy probabilities in [0,1] from integer log-odds values in the cells...
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)...
float l2p(const cell_t l)
Scales an integer representation of the log-odd into a real valued probability in [0...
GLfloat GLfloat p
Definition: glext.h:6398
One static instance of this struct should exist in any class implementing CLogOddsGridMap2D to hold t...
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019