Apollo 11.0
自动驾驶开放平台
lossless_map_matrix.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19namespace apollo {
20namespace localization {
21namespace msf {
22
23// ======================LosslessMapSingleCell===========================
25 : intensity(0.0),
26 intensity_var(0.0),
27 altitude(0.0),
28 altitude_var(0.0),
29 count(0) {}
30
32 intensity = 0.0;
33 intensity_var = 0.0;
34 altitude = 0.0;
35 count = 0;
36}
37
46
47void LosslessMapSingleCell::AddSample(const float new_altitude,
48 const float new_intensity) {
49 ++count;
50 float fcount = static_cast<float>(count);
51 float v1 = new_intensity - intensity;
52 float value = v1 / fcount;
53 intensity += value;
54 float v2 = new_intensity - intensity;
55 intensity_var = (fcount - 1.0f) * intensity_var + v1 * v2 / fcount;
56
57 v1 = new_altitude - altitude;
58 value = v1 / fcount;
59 altitude += value;
60 v2 = new_altitude - altitude;
61 altitude_var = ((fcount - 1) * altitude_var + v1 * v2) / fcount;
62}
63
64unsigned int LosslessMapSingleCell::LoadBinary(unsigned char* buf) {
65 float* p = reinterpret_cast<float*>(buf);
66 intensity = *p;
67 ++p;
68 intensity_var = *p;
69 ++p;
70 altitude = *p;
71 ++p;
72 altitude_var = *p;
73 ++p;
74 unsigned int* pp =
75 reinterpret_cast<unsigned int*>(reinterpret_cast<void*>(p));
76 count = *pp;
77 return GetBinarySize();
78}
79
80unsigned int LosslessMapSingleCell::CreateBinary(unsigned char* buf,
81 unsigned int buf_size) const {
82 unsigned int target_size = GetBinarySize();
83 if (buf_size >= target_size) {
84 float* p = reinterpret_cast<float*>(buf);
85 *p = intensity;
86 ++p;
87 *p = intensity_var;
88 ++p;
89 *p = altitude;
90 ++p;
91 *p = altitude_var;
92 ++p;
93 unsigned int* pp =
94 reinterpret_cast<unsigned int*>(reinterpret_cast<void*>(p));
95 *pp = count;
96 }
97 return target_size;
98}
99
101 return static_cast<unsigned int>(sizeof(float) * 4 + sizeof(unsigned int));
102}
103
104// ======================LosslessMapCell===========================
106
108 for (unsigned int i = 0; i < IDL_CAR_NUM_RESERVED_MAP_LAYER; ++i) {
109 map_cells[i].Reset();
110 }
111 layer_num = 1;
112}
113
114void LosslessMapCell::SetValueLayer(double altitude, unsigned char intensity,
115 double altitude_thres) {
117
118 unsigned int best_layer_id = GetLayerId(altitude);
119 DCHECK_LT(best_layer_id, layer_num);
120 if (best_layer_id == 0) {
122 // No layer yet, create a new one
124 cell.AddSample(static_cast<float>(altitude),
125 static_cast<float>(intensity));
126 } else {
127 // No enough reserved map layers.
128 std::cerr << "[Warning] There are no enough reserved map cell layers. "
129 "Please increase IDL_CAR_NUM_RESERVED_MAP_LAYER."
130 << std::endl;
131 }
132 } else {
133 // There is a best layer
134 double layer_alt_dif = fabs(map_cells[best_layer_id].altitude - altitude);
135 if (layer_alt_dif < altitude_thres) {
136 // Still a good one
137 LosslessMapSingleCell& cell = map_cells[best_layer_id];
138 cell.AddSample(static_cast<float>(altitude),
139 static_cast<float>(intensity));
140 } else {
141 // Should create a new one
144 cell.AddSample(static_cast<float>(altitude),
145 static_cast<float>(intensity));
146 } else {
147 // No enough reserved map layers.
148 std::cerr << "[Warning] There are no enough reserved map cell layers. "
149 "Please increase IDL_CAR_NUM_RESERVED_MAP_LAYER."
150 << std::endl;
151 }
152 }
153 }
154}
155
156void LosslessMapCell::SetValue(double altitude, unsigned char intensity) {
159 cell.AddSample(static_cast<float>(altitude), static_cast<float>(intensity));
160}
161
162unsigned int LosslessMapCell::LoadBinary(unsigned char* buf) {
163 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
164 unsigned int size = *p;
165 ++p;
166 layer_num = size;
167 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
168 for (unsigned int i = 0; i < size; ++i) {
170 unsigned int processed_size = cell.LoadBinary(pp);
171 pp += processed_size;
172 }
173 return GetBinarySize();
174}
175
176unsigned int LosslessMapCell::CreateBinary(unsigned char* buf,
177 unsigned int buf_size) const {
178 unsigned int target_size = GetBinarySize();
179 if (buf_size >= target_size) {
180 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
181 *p = layer_num;
182 ++p;
183 buf_size -= static_cast<unsigned int>(sizeof(unsigned int));
184 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
185 for (size_t i = 0; i < layer_num; ++i) {
186 const LosslessMapSingleCell& cell = map_cells[i];
187 unsigned int processed_size = cell.CreateBinary(pp, buf_size);
188 DCHECK_GE(buf_size, processed_size);
189 buf_size -= processed_size;
190 pp += processed_size;
191 }
192 }
193 return target_size;
194}
195
196unsigned int LosslessMapCell::GetBinarySize() const {
197 unsigned int target_size = sizeof(
198 unsigned int); // The size of the variable for the number of layers.
199 for (size_t i = 0; i < layer_num; ++i) {
200 const LosslessMapSingleCell& cell = map_cells[i];
201 target_size += cell.GetBinarySize();
202 }
203 return target_size;
204}
205
206unsigned int LosslessMapCell::GetLayerId(double altitude) const {
207 unsigned int best_layer_id = 0;
208 double best_layer_alt_dif = 1e10;
209 for (unsigned int i = 1; i < layer_num; ++i) {
210 const LosslessMapSingleCell& cell = map_cells[i];
211 double layer_alt_dif = fabs(cell.altitude - altitude);
212 if (layer_alt_dif < best_layer_alt_dif) {
213 best_layer_alt_dif = layer_alt_dif;
214 best_layer_id = i;
215 }
216 }
217 return best_layer_id;
218}
219
220void LosslessMapCell::GetValue(std::vector<unsigned char>* values) const {
221 values->clear();
222 for (unsigned int i = 1; i < layer_num; ++i) {
223 const LosslessMapSingleCell& cell = map_cells[i];
224 values->push_back(static_cast<unsigned char>(cell.intensity));
225 }
226}
227
228void LosslessMapCell::GetVar(std::vector<float>* vars) const {
229 vars->clear();
230 for (unsigned int i = 1; i < layer_num; ++i) {
231 const LosslessMapSingleCell& cell = map_cells[i];
232 vars->push_back(cell.intensity_var);
233 }
234}
235
236void LosslessMapCell::GetAlt(std::vector<float>* alts) const {
237 alts->clear();
238 for (unsigned int i = 1; i < layer_num; ++i) {
239 const LosslessMapSingleCell& cell = map_cells[i];
240 alts->push_back(cell.altitude);
241 }
242}
243
244void LosslessMapCell::GetAltVar(std::vector<float>* alt_vars) const {
245 alt_vars->clear();
246 for (unsigned int i = 1; i < layer_num; ++i) {
247 const LosslessMapSingleCell& cell = map_cells[i];
248 alt_vars->push_back(cell.altitude_var);
249 }
250}
251
252void LosslessMapCell::GetCount(std::vector<unsigned int>* counts) const {
253 counts->clear();
254 for (unsigned int i = 1; i < layer_num; ++i) {
255 const LosslessMapSingleCell& cell = map_cells[i];
256 counts->push_back(cell.count);
257 }
258}
259
260// ======================LosslessMapMatrix===========================
262 rows_ = 0;
263 cols_ = 0;
264 map_cells_ = nullptr;
265}
266
268 if (map_cells_) {
269 delete[] map_cells_;
270 }
271 rows_ = 0;
272 cols_ = 0;
273}
274
276 : BaseMapMatrix(matrix) {
277 Init(matrix.rows_, matrix.cols_);
278 for (unsigned int y = 0; y < rows_; ++y) {
279 for (unsigned int x = 0; x < cols_; ++x) {
280 map_cells_[y * cols_ + x] = matrix[y][x];
281 }
282 }
283}
284
286 unsigned int rows = config->map_node_size_y_;
287 unsigned int cols = config->map_node_size_x_;
288 if (rows_ == rows && cols_ == cols) {
289 return;
290 }
291 Init(rows, cols);
292}
293
295 Reset(config->map_node_size_y_, config->map_node_size_x_);
296}
297
298void LosslessMapMatrix::Init(unsigned int rows, unsigned int cols) {
299 if (map_cells_) {
300 delete[] map_cells_;
301 map_cells_ = nullptr;
302 }
303 map_cells_ = new LosslessMapCell[rows * cols];
304 rows_ = rows;
305 cols_ = cols;
306}
307
308void LosslessMapMatrix::Reset(unsigned int rows, unsigned int cols) {
309 unsigned int length = rows * cols;
310 for (unsigned int i = 0; i < length; ++i) {
311 map_cells_[i].Reset();
312 }
313}
314
315unsigned int LosslessMapMatrix::LoadBinary(unsigned char* buf) {
316 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
317 rows_ = *p;
318 ++p;
319 cols_ = *p;
320 ++p;
321 Init(rows_, cols_);
322
323 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
324 for (unsigned int y = 0; y < rows_; ++y) {
325 for (unsigned int x = 0; x < cols_; ++x) {
326 LosslessMapCell& cell = GetMapCell(y, x);
327 unsigned int processed_size = cell.LoadBinary(pp);
328 pp += processed_size;
329 }
330 }
331 return GetBinarySize();
332}
333
334unsigned int LosslessMapMatrix::CreateBinary(unsigned char* buf,
335 unsigned int buf_size) const {
336 unsigned int target_size = GetBinarySize();
337 if (buf_size >= target_size) {
338 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
339 *p = rows_;
340 ++p;
341 *p = cols_;
342 ++p;
343 buf_size -= static_cast<unsigned int>(sizeof(unsigned int) * 2);
344 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
345 for (unsigned int y = 0; y < rows_; ++y) {
346 for (unsigned int x = 0; x < cols_; ++x) {
347 const LosslessMapCell& cell = GetMapCell(y, x);
348 unsigned int processed_size = cell.CreateBinary(pp, buf_size);
349 DCHECK_GE(buf_size, processed_size);
350 buf_size -= processed_size;
351 pp += processed_size;
352 }
353 }
354 }
355 return target_size;
356}
357
359 // default binary size
360 unsigned int target_size =
361 static_cast<unsigned int>(sizeof(unsigned int) * 2); // rows and cols
362 for (unsigned int y = 0; y < rows_; ++y) {
363 for (unsigned int x = 0; x < cols_; ++x) {
364 const LosslessMapCell& cell = GetMapCell(y, x);
365 target_size += cell.GetBinarySize();
366 }
367 }
368 return target_size;
369}
370
371void LosslessMapMatrix::GetIntensityImg(cv::Mat* intensity_img) const {
372 *intensity_img = cv::Mat(cv::Size(cols_, rows_), CV_8UC1);
373
374 for (uint32_t y = 0; y < rows_; ++y) {
375 for (uint32_t x = 0; x < cols_; ++x) {
376 intensity_img->at<unsigned char>(y, x) = GetMapCell(y, x).GetValue();
377 }
378 }
379}
380
381} // namespace msf
382} // namespace localization
383} // namespace apollo
The options of the reflectance map.
unsigned int map_node_size_y_
The map node size in pixels.
unsigned int map_node_size_x_
The map node size in pixels.
The data structure of the map cells in a map node.
virtual unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary.
const LosslessMapCell & GetMapCell(unsigned int row, unsigned int col) const
Get a map cell.
virtual void Reset(const BaseMapConfig *config)
Reset map cells data.
virtual unsigned int GetBinarySize() const
Get the binary size of the object.
unsigned int cols_
The number of columns.
virtual void Init(const BaseMapConfig *config)
Initialize the map matrix.
virtual unsigned int LoadBinary(unsigned char *buf)
Load the map cell from a binary chunk.
virtual void GetIntensityImg(cv::Mat *intensity_img) const
get intensity image of node.
LosslessMapCell * map_cells_
The matrix data structure.
#define IDL_CAR_NUM_RESERVED_MAP_LAYER
The first layer (layer 0) includes all the intensities from any layers.
class register implement
Definition arena_queue.h:37
float GetAlt() const
Get the average altitude of the map cell.
float GetVar() const
Get the variance of the intensity of the map cell.
unsigned int GetCount() const
Get the count of the samples in the map cell.
LosslessMapSingleCell map_cells[IDL_CAR_NUM_RESERVED_MAP_LAYER]
The multiple layers of the cell.
unsigned char GetValue() const
Get the average intensity of the map cell.
unsigned int GetBinarySize() const
Get the binary size of the object.
unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary.
void SetValueLayer(double altitude, unsigned char intensity, double altitude_thres=10.0)
Set the value of a layer that layer_id > 0.
unsigned int layer_num
The layers of the cell.
float GetAltVar() const
Get the variance of the altitude of the map cell.
unsigned int GetLayerId(double altitude) const
Match a layer in the map cell given an altitude.
unsigned int LoadBinary(unsigned char *buf)
Load the map cell from a binary chunk.
void SetValue(double altitude, unsigned char intensity)
Set the value.
void GetValue(std::vector< unsigned char > *values) const
Load the map cell from a binary chunk.
float altitude_var
The variance altitude value of the cell.
float altitude
The average altitude of the cell.
unsigned int count
The number of samples in the cell.
unsigned int LoadBinary(unsigned char *buf)
Load the map cell from a binary chunk.
void AddSample(const float new_altitude, const float new_intensity)
Add a sample.
float intensity_var
The variance intensity value.
unsigned int GetBinarySize() const
Get the binary size of the object.
LosslessMapSingleCell & operator=(const LosslessMapSingleCell &ref)
Overloading the assign operator.
unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary.