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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020