Apollo 11.0
自动驾驶开放平台
ndt_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
19
20namespace apollo {
21namespace localization {
22namespace msf {
23
25 intensity_ = 0.0;
26 intensity_var_ = 0.0;
28 count_ = 0;
29 centroid_ = Eigen::Vector3f::Zero();
30 centroid_average_cov_ = Eigen::Matrix3f::Zero();
31 centroid_icov_ = Eigen::Matrix3f::Identity();
33}
34
35unsigned int NdtMapSingleCell::LoadBinary(unsigned char* buf) {
36 float* f_buf = reinterpret_cast<float*>(buf);
37 intensity_ = *f_buf;
38 ++f_buf;
39 intensity_var_ = *f_buf;
40 ++f_buf;
41 unsigned int* ui_buf =
42 reinterpret_cast<unsigned int*>(reinterpret_cast<void*>(f_buf));
43 road_pt_count_ = *ui_buf;
44 ++ui_buf;
45 count_ = *ui_buf;
46 ++ui_buf;
47 f_buf = reinterpret_cast<float*>(reinterpret_cast<void*>(ui_buf));
48 centroid_[0] = *f_buf;
49 ++f_buf;
50 centroid_[1] = *f_buf;
51 ++f_buf;
52 centroid_[2] = *f_buf;
53
54 for (unsigned int i = 0; i < 3; ++i) {
55 for (unsigned int j = 0; j < 3; ++j) {
56 ++f_buf;
57 centroid_average_cov_(i, j) = *f_buf;
58 }
59 }
61 for (unsigned int i = 0; i < 3; ++i) {
62 for (unsigned int j = 0; j < 3; ++j) {
63 ++f_buf;
64 centroid_icov_(i, j) = *f_buf;
65 }
66 }
67 ++f_buf;
68 unsigned char* uc_buf = reinterpret_cast<unsigned char*>(f_buf);
69 is_icov_available_ = *uc_buf;
70 } else {
71 centroid_icov_ = Eigen::Matrix3f::Identity();
73 }
74 return GetBinarySize();
75}
76
77unsigned int NdtMapSingleCell::CreateBinary(unsigned char* buf,
78 unsigned int buf_size) const {
79 unsigned int target_size = GetBinarySize();
80 if (buf_size >= target_size) {
81 float* p = reinterpret_cast<float*>(buf);
82 *p = intensity_;
83 ++p;
84 *p = intensity_var_;
85 ++p;
86 unsigned int* pp =
87 reinterpret_cast<unsigned int*>(reinterpret_cast<void*>(p));
88 *pp = road_pt_count_;
89 ++pp;
90 *pp = count_;
91 ++pp;
92 float* ppp = reinterpret_cast<float*>(reinterpret_cast<void*>(pp));
93 *ppp = centroid_[0];
94 ++ppp;
95 *ppp = centroid_[1];
96 ++ppp;
97 *ppp = centroid_[2];
98
99 for (unsigned int i = 0; i < 3; ++i) {
100 for (unsigned int j = 0; j < 3; ++j) {
101 ++ppp;
102 *ppp = centroid_average_cov_(i, j);
103 }
104 }
106 for (unsigned int i = 0; i < 3; ++i) {
107 for (unsigned int j = 0; j < 3; ++j) {
108 ++ppp;
109 *ppp = centroid_icov_(i, j);
110 }
111 }
112 ++ppp;
113 unsigned char* pppp = reinterpret_cast<unsigned char*>(ppp);
114 *pppp = is_icov_available_;
115 }
116 }
117 return target_size;
118}
119
121 unsigned int sz =
122 static_cast<unsigned int>(sizeof(float) * 2 + sizeof(unsigned int) * 2 +
123 sizeof(float) * 3 + sizeof(float) * 9);
125 sz += static_cast<unsigned int>(sizeof(float) * 9 +
126 sizeof(unsigned char) * 1);
127 }
128 return sz;
129}
130
142
144 const NdtMapSingleCell& cell_new) {
145 cell->MergeCell(cell_new);
146}
147
149 max_altitude_index_ = static_cast<int>(-1e10);
150 min_altitude_index_ = static_cast<int>(1e10);
151}
152
154 max_altitude_index_ = static_cast<int>(-1e10);
155 min_altitude_index_ = static_cast<int>(1e10);
156 cells_.clear();
157 road_cell_indices_.clear();
158}
159
160int NdtMapCells::AddSample(const float intensity, const float altitude,
161 const float resolution,
162 const Eigen::Vector3f centroid, bool is_road) {
163 int altitude_index = CalAltitudeIndex(resolution, altitude);
164 NdtMapSingleCell& cell = cells_[altitude_index];
165 cell.AddSample(intensity, altitude, centroid, is_road);
166 if (altitude_index > max_altitude_index_) {
167 max_altitude_index_ = altitude_index;
168 }
169 if (altitude_index < min_altitude_index_) {
170 min_altitude_index_ = altitude_index;
171 }
172 if (is_road) {
173 auto got = std::find(road_cell_indices_.begin(), road_cell_indices_.end(),
174 altitude_index);
175 if (got == road_cell_indices_.end()) {
176 road_cell_indices_.push_back(altitude_index);
177 }
178 }
179
180 return altitude_index;
181}
182
183unsigned int NdtMapCells::LoadBinary(unsigned char* buf) {
184 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
185 unsigned int size = *p;
186 ++p;
187
188 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
189 for (unsigned int i = 0; i < size; ++i) {
190 int* ppp = reinterpret_cast<int*>(pp);
191 int altitude_index = *ppp;
192 ++ppp;
193 pp = reinterpret_cast<unsigned char*>(ppp);
194 NdtMapSingleCell cell;
195 unsigned int processed_size = cell.LoadBinary(pp);
196 cells_[altitude_index] = cell;
197 pp += processed_size;
198 }
199
200 int* ppp = reinterpret_cast<int*>(pp);
201 max_altitude_index_ = *ppp;
202 ++ppp;
203 min_altitude_index_ = *ppp;
204 ++ppp;
205
206 p = reinterpret_cast<unsigned int*>(ppp);
207 size = *p;
208 ++p;
209 ppp = reinterpret_cast<int*>(p);
210 for (unsigned int i = 0; i < size; ++i) {
211 int index = *ppp;
212 ++ppp;
213 road_cell_indices_.push_back(index);
214 }
215
216 return GetBinarySize();
217}
218
219unsigned int NdtMapCells::CreateBinary(unsigned char* buf,
220 unsigned int buf_size) const {
221 unsigned int target_size = GetBinarySize();
222 if (buf_size >= target_size) {
223 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
224 *p = static_cast<unsigned int>(cells_.size());
225 ++p;
226 buf_size -= static_cast<unsigned int>(sizeof(unsigned int));
227 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
228 for (auto it = cells_.begin(); it != cells_.end(); ++it) {
229 int altitude_index = it->first;
230 const NdtMapSingleCell& cell = it->second;
231 int* ppp = reinterpret_cast<int*>(pp);
232 *ppp = altitude_index;
233 ++ppp;
234 pp = reinterpret_cast<unsigned char*>(ppp);
235 unsigned int processed_size = cell.CreateBinary(pp, buf_size);
236 assert(buf_size >= processed_size);
237 buf_size -= processed_size;
238 pp += processed_size;
239 }
240
241 int* ppp = reinterpret_cast<int*>(pp);
242 *ppp = max_altitude_index_;
243 ++ppp;
244 *ppp = min_altitude_index_;
245 ++ppp;
246
247 unsigned int size = static_cast<unsigned int>(road_cell_indices_.size());
248 p = reinterpret_cast<unsigned int*>(ppp);
249 *p = size;
250 ++p;
251 ppp = reinterpret_cast<int*>(p);
252 for (unsigned int i = 0; i < size; ++i) {
253 *ppp = road_cell_indices_[i];
254 ++ppp;
255 }
256 }
257 return target_size;
258}
259
260unsigned int NdtMapCells::GetBinarySize() const {
261 unsigned int target_size = sizeof(unsigned int);
262 for (auto it = cells_.begin(); it != cells_.end(); ++it) {
263 target_size += static_cast<unsigned int>(sizeof(int));
264 const NdtMapSingleCell& cell = it->second;
265 target_size += cell.GetBinarySize();
266 }
267
268 target_size += static_cast<unsigned int>(sizeof(int) * 2);
269 target_size += static_cast<unsigned int>(sizeof(unsigned int));
270 target_size +=
271 static_cast<unsigned int>(sizeof(int) * road_cell_indices_.size());
272
273 return target_size;
274}
275
276int NdtMapCells::CalAltitudeIndex(const float resolution,
277 const float altitude) {
278 return static_cast<int>(altitude / resolution);
279}
280
281float NdtMapCells::CalAltitude(const float resolution,
282 const int altitude_index) {
283 return static_cast<float>(resolution *
284 (static_cast<float>(altitude_index) + 0.5));
285}
286
287void NdtMapCells::Reduce(NdtMapCells* cell, const NdtMapCells& cell_new) {
288 // Reduce cells
289 for (auto it = cell_new.cells_.begin(); it != cell_new.cells_.end(); ++it) {
290 int altitude_index = it->first;
291 auto got = cell->cells_.find(altitude_index);
292 if (got != cell->cells_.end()) {
293 cell->cells_[altitude_index].MergeCell(it->second);
294 } else {
295 cell->cells_[altitude_index] = NdtMapSingleCell(it->second);
296 }
297 }
298
299 if (cell_new.max_altitude_index_ > cell->max_altitude_index_) {
301 }
302
303 if (cell_new.min_altitude_index_ < cell->min_altitude_index_) {
305 }
306
307 for (auto it_new = cell_new.road_cell_indices_.begin();
308 it_new != cell_new.road_cell_indices_.end(); ++it_new) {
309 auto got_it = std::find(cell->road_cell_indices_.begin(),
310 cell->road_cell_indices_.end(), *it_new);
311 if (got_it != cell->road_cell_indices_.end()) {
312 *got_it += *it_new;
313 } else {
314 cell->road_cell_indices_.push_back(*it_new);
315 }
316 }
317}
318
320 rows_ = 0;
321 cols_ = 0;
322 map3d_cells_ = nullptr;
323}
324
326
328 Init(cells.rows_, cells.cols_);
329 for (unsigned int y = 0; y < rows_; ++y) {
330 for (unsigned int x = 0; x < cols_; ++x) {
331 NdtMapCells& cell = GetMapCell(y, x);
332 const NdtMapCells& src_cell = cells.GetMapCell(y, x);
333 cell = NdtMapCells(src_cell);
334 }
335 }
336}
337
339 Init(config->map_node_size_y_, config->map_node_size_x_);
340}
341
343 Reset(config->map_node_size_y_, config->map_node_size_x_);
344}
345
346void NdtMapMatrix::Init(unsigned int rows, unsigned int cols) {
347 map3d_cells_.reset(new NdtMapCells[rows * cols]);
348 rows_ = rows;
349 cols_ = cols;
350}
351
352void NdtMapMatrix::Reset(unsigned int rows, unsigned int cols) {
353 unsigned int length = rows * cols;
354 for (unsigned int i = 0; i < length; ++i) {
355 map3d_cells_[i].Reset();
356 }
357}
358
359unsigned int NdtMapMatrix::LoadBinary(unsigned char* buf) {
360 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
361 rows_ = *p;
362 ++p;
363 cols_ = *p;
364 ++p;
365 Init(rows_, cols_);
366 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
367 for (unsigned int y = 0; y < rows_; ++y) {
368 for (unsigned int x = 0; x < cols_; ++x) {
369 NdtMapCells& cell = GetMapCell(y, x);
370 unsigned int processed_size = cell.LoadBinary(pp);
371 pp += processed_size;
372 }
373 }
374 return GetBinarySize();
375}
376
377unsigned int NdtMapMatrix::CreateBinary(unsigned char* buf,
378 unsigned int buf_size) const {
379 unsigned int target_size = GetBinarySize();
380 if (buf_size >= target_size) {
381 unsigned int* p = reinterpret_cast<unsigned int*>(buf);
382 *p = rows_;
383 ++p;
384 *p = cols_;
385 ++p;
386 buf_size -= static_cast<unsigned int>(sizeof(unsigned int) * 2);
387 unsigned char* pp = reinterpret_cast<unsigned char*>(p);
388 for (unsigned int y = 0; y < rows_; ++y) {
389 for (unsigned int x = 0; x < cols_; ++x) {
390 const NdtMapCells& cell = GetMapCell(y, x);
391 unsigned int processed_size = cell.CreateBinary(pp, buf_size);
392 assert(buf_size >= processed_size);
393 buf_size -= processed_size;
394 pp += processed_size;
395 }
396 }
397 }
398 return target_size;
399}
400
401unsigned int NdtMapMatrix::GetBinarySize() const {
402 unsigned int target_size =
403 static_cast<unsigned int>(sizeof(unsigned int) * 2);
404 for (unsigned int y = 0; y < rows_; ++y) {
405 for (unsigned int x = 0; x < cols_; ++x) {
406 const NdtMapCells& cell = GetMapCell(y, x);
407 target_size += cell.GetBinarySize();
408 }
409 }
410 return target_size;
411}
412
413void NdtMapMatrix::Reduce(NdtMapMatrix* cells, const NdtMapMatrix& cells_new) {
414 for (unsigned int y = 0; y < cells->GetRows(); ++y) {
415 for (unsigned int x = 0; x < cells->GetCols(); ++x) {
416 NdtMapCells& cell = cells->GetMapCell(y, x);
417 const NdtMapCells& cell_new = cells_new.GetMapCell(y, x);
418 NdtMapCells::Reduce(&cell, cell_new);
419 }
420 }
421}
422
423void NdtMapMatrix::GetIntensityImg(cv::Mat* intensity_img) const {
424 *intensity_img = cv::Mat(cv::Size(cols_, rows_), CV_8UC1);
425 for (unsigned int y = 0; y < rows_; ++y) {
426 for (unsigned int x = 0; x < cols_; ++x) {
427 unsigned int id = y * cols_ + x;
428 int min_altitude_index = map3d_cells_[id].min_altitude_index_;
429 intensity_img->at<unsigned char>(y, x) =
430 (unsigned char)(map3d_cells_[id]
431 .cells_[min_altitude_index]
432 .intensity_);
433 }
434 }
435}
436
437} // namespace msf
438} // namespace localization
439} // 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 ndt Map cell.
void Reset()
Reset to default value.
int AddSample(const float intensity, const float altitude, const float resolution, const Eigen::Vector3f centroid, bool is_road=false)
Add an sample.
unsigned int GetBinarySize() const
Get the binary size of the object.
std::vector< int > road_cell_indices_
The indices of road surface.
static float CalAltitude(const float resolution, const int altitude_index)
Calculate altitude from altitude index.
unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary.
int max_altitude_index_
The index of biggest altitude.
unsigned int LoadBinary(unsigned char *buf)
Load the map cell from a binary chunk.
static int CalAltitudeIndex(const float resolution, const float altitude)
Calculate altitude index from altitude.
static void Reduce(NdtMapCells *cell, const NdtMapCells &cell_new)
Combine two MapCell instances (Reduce).
std::unordered_map< int, NdtMapSingleCell > cells_
The multiple altitudes of the cell.
NdtMapCells()
The default constructor.
int min_altitude_index_
The index of smallest altitude.
The data structure of ndt Map matrix.
const NdtMapCells & GetMapCell(unsigned int row, unsigned int col) const
Get a const map cell.
unsigned int GetRows() const
Get the size of row.
virtual void Reset(const BaseMapConfig *config)
Reset the matrix item to default value.
virtual void GetIntensityImg(cv::Mat *intensity_img) const
get intensity image of node.
static void Reduce(NdtMapMatrix *cells, const NdtMapMatrix &cells_new)
Combine two NdtMapMatrix instances (Reduce).
virtual unsigned int LoadBinary(unsigned char *buf)
Load the map cell from a binary chunk.
virtual unsigned int GetBinarySize() const
Get the binary size of the object.
virtual void Init(const BaseMapConfig *config)
Initialize the matrix with the config.
unsigned int GetCols() const
Get the size of cols.
virtual unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary.
The data structure of a single ndt map cell.
float intensity_var_
The variance intensity value.
NdtMapSingleCell & operator=(const NdtMapSingleCell &ref)
Overloading the assign operator.
unsigned int GetBinarySize() const
Get the binary size of the object.
void AddSample(const float intensity, const float altitude, const Eigen::Vector3f centroid, bool is_road=false)
Add an sample to the single 3d map cell.
unsigned char is_icov_available_
the inverse covariance available flag.
const unsigned int minimum_points_threshold_
minimum number of points needed.
Eigen::Vector3f centroid_
the centroid of the cell.
Eigen::Matrix3f centroid_icov_
the pose inverse covariance of the cell.
Eigen::Matrix3f centroid_average_cov_
the pose covariance of the cell.
unsigned int road_pt_count_
The number of samples belonging to road surface.
unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary.
unsigned int LoadBinary(unsigned char *buf)
Load the map cell from a binary chunk.
unsigned int count_
The number of samples in the cell.
float intensity_
The average intensity value.
static void Reduce(NdtMapSingleCell *cell, const NdtMapSingleCell &cell_new)
Combine two NdtMapSingleCell instances (Reduce).
void MergeCell(const float intensity, const float intensity_var, const unsigned int road_pt_count, const unsigned int count, const Eigen::Vector3f &centroid, const Eigen::Matrix3f &centroid_cov)
Merge two cells.
class register implement
Definition arena_queue.h:37