Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef CLogOddsGridMap2D_H
00030 #define CLogOddsGridMap2D_H
00031
00032 #include <mrpt/utils/utils_defs.h>
00033
00034 namespace mrpt
00035 {
00036 namespace slam
00037 {
00038 namespace detail
00039 {
00040 template <typename TCELL> struct logoddscell_traits;
00041
00042 template <> struct logoddscell_traits<int8_t>
00043 {
00044 static const int8_t CELLTYPE_MIN = -127;
00045 static const int8_t CELLTYPE_MAX = 127;
00046 static const int8_t P2LTABLE_SIZE = CELLTYPE_MAX;
00047 static const size_t LOGODDS_LUT_ENTRIES = 1<<8;
00048 };
00049 template <> struct logoddscell_traits<int16_t>
00050 {
00051 static const int16_t CELLTYPE_MIN = -32767;
00052 static const int16_t CELLTYPE_MAX = 32767;
00053 static const int16_t P2LTABLE_SIZE = CELLTYPE_MAX;
00054 static const size_t LOGODDS_LUT_ENTRIES = 1<<16;
00055 };
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 template <typename TCELL>
00067 struct CLogOddsGridMap2D : public detail::logoddscell_traits<TCELL>
00068 {
00069 typedef TCELL cell_t;
00070 typedef detail::logoddscell_traits<TCELL> traits_t;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 inline static void updateCell_fast_occupied(
00081 const unsigned x,
00082 const unsigned y,
00083 const cell_t logodd_obs,
00084 const cell_t thres,
00085 cell_t *mapArray,
00086 const unsigned _size_x)
00087 {
00088 cell_t *theCell = mapArray + (x+y*_size_x);
00089 if (*theCell > thres )
00090 *theCell -= logodd_obs;
00091 else *theCell = traits_t::CELLTYPE_MIN;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101 inline static void updateCell_fast_occupied(
00102 cell_t *theCell,
00103 const cell_t logodd_obs,
00104 const cell_t thres )
00105 {
00106 if (*theCell > thres )
00107 *theCell -= logodd_obs;
00108 else *theCell = traits_t::CELLTYPE_MIN;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 inline static void updateCell_fast_free(
00120 const unsigned x,
00121 const unsigned y,
00122 const cell_t logodd_obs,
00123 const cell_t thres,
00124 cell_t *mapArray,
00125 const unsigned _size_x)
00126 {
00127 cell_t *theCell = mapArray + (x+y*_size_x);
00128 if (*theCell < thres )
00129 *theCell += logodd_obs;
00130 else *theCell = traits_t::CELLTYPE_MAX;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 inline static void updateCell_fast_free(
00142 cell_t *theCell,
00143 const cell_t logodd_obs,
00144 const cell_t thres)
00145 {
00146 if (*theCell < thres )
00147 *theCell += logodd_obs;
00148 else *theCell = traits_t::CELLTYPE_MAX;
00149 }
00150
00151 };
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 template <typename TCELL>
00162 struct CLogOddsGridMapLUT : public detail::logoddscell_traits<TCELL>
00163 {
00164 typedef TCELL cell_t;
00165 typedef detail::logoddscell_traits<TCELL> traits_t;
00166
00167
00168
00169 std::vector<float> logoddsTable;
00170
00171
00172
00173
00174 std::vector<uint8_t> logoddsTable_255;
00175
00176
00177 std::vector<cell_t> p2lTable;
00178
00179
00180 CLogOddsGridMapLUT()
00181 {
00182 MRPT_START
00183
00184
00185 static const double LOGODD_K = 16;
00186 static const double LOGODD_K_INV = 1.0/LOGODD_K;
00187
00188 logoddsTable.resize( traits_t::LOGODDS_LUT_ENTRIES );
00189 logoddsTable_255.resize( traits_t::LOGODDS_LUT_ENTRIES );
00190 for (int i=traits_t::CELLTYPE_MIN;i<=traits_t::CELLTYPE_MAX;i++)
00191 {
00192 float f = 1.0f / (1.0f + exp( - i * LOGODD_K_INV ) );
00193 unsigned int idx = -traits_t::CELLTYPE_MIN+i;
00194 logoddsTable[idx] = f;
00195 logoddsTable_255[idx] = (uint8_t)(f*255.0f);
00196 }
00197
00198
00199 p2lTable.resize( traits_t::P2LTABLE_SIZE+1 );
00200 double K = 1.0 / traits_t::P2LTABLE_SIZE;
00201 for (int j=0;j<=traits_t::P2LTABLE_SIZE;j++)
00202 {
00203 double p = j*K;
00204 if (p==0)
00205 p=1e-14;
00206 else if (p==1)
00207 p=1-1e-14;
00208
00209 double logodd = log(p)-log(1-p);
00210 int L = round(logodd * LOGODD_K);
00211 if (L>traits_t::CELLTYPE_MAX)
00212 L=traits_t::CELLTYPE_MAX;
00213 else if (L<traits_t::CELLTYPE_MIN)
00214 L=traits_t::CELLTYPE_MIN;
00215 p2lTable[j] = L;
00216 }
00217
00218 MRPT_END
00219 }
00220
00221
00222
00223 inline float l2p(const cell_t l)
00224 {
00225 if (l<traits_t::CELLTYPE_MIN)
00226 return logoddsTable[ 0 ];
00227 else return logoddsTable[ -traits_t::CELLTYPE_MIN+l ];
00228 }
00229
00230
00231
00232 inline uint8_t l2p_255(const cell_t l)
00233 {
00234 if (l<traits_t::CELLTYPE_MIN)
00235 return logoddsTable_255[ 0 ];
00236 else return logoddsTable_255[ -traits_t::CELLTYPE_MIN+l ];
00237 }
00238
00239
00240
00241 inline cell_t p2l(const float p)
00242 {
00243 return p2lTable[ static_cast<unsigned int>(p * traits_t::P2LTABLE_SIZE) ];
00244 }
00245
00246 };
00247
00248 }
00249 }
00250
00251 #endif