Apollo 10.0
自动驾驶开放平台
pyramid_map_matrix.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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 *****************************************************************************/
17
18#include <algorithm>
19#include <memory>
20#include "cyber/common/log.h"
21
22namespace apollo {
23namespace localization {
24namespace msf {
25namespace pyramid_map {
26
28
30
32 Clear();
33 resolution_num_ = map_matrix.resolution_num_;
34 ratio_ = map_matrix.ratio_;
35 rows_mr_ = map_matrix.rows_mr_;
36 cols_mr_ = map_matrix.cols_mr_;
37 ratio_multiplier_ = map_matrix.ratio_multiplier_;
38
39 intensity_matrixes_ = map_matrix.intensity_matrixes_;
40 intensity_var_matrixes_ = map_matrix.intensity_var_matrixes_;
41 altitude_matrixes_ = map_matrix.altitude_matrixes_;
42 altitude_var_matrixes_ = map_matrix.altitude_var_matrixes_;
43 ground_altitude_matrixes_ = map_matrix.ground_altitude_matrixes_;
44 count_matrixes_ = map_matrix.count_matrixes_;
45 ground_count_matrixes_ = map_matrix.ground_count_matrixes_;
46
47 has_intensity_ = map_matrix.has_intensity_;
48 has_intensity_var_ = map_matrix.has_intensity_var_;
49 has_altitude_ = map_matrix.has_altitude_;
50 has_altitude_var_ = map_matrix.has_altitude_var_;
51 has_ground_altitude_ = map_matrix.has_ground_altitude_;
52 has_count_ = map_matrix.has_count_;
53 has_ground_count_ = map_matrix.has_ground_count_;
54}
55
57 const PyramidMapConfig* pconfig =
58 dynamic_cast<const PyramidMapConfig*>(&config);
59 Init(pconfig->map_node_size_y_, pconfig->map_node_size_x_,
60 pconfig->has_intensity_, pconfig->has_intensity_var_,
61 pconfig->has_altitude_, pconfig->has_altitude_var_,
62 pconfig->has_ground_altitude_, pconfig->has_count_,
63 pconfig->has_ground_count_, pconfig->resolution_num_,
64 pconfig->resolution_ratio_);
65}
66
68 for (unsigned int i = 0; i < resolution_num_; i++) {
69 Reset(i);
70 }
71}
72
73void PyramidMapMatrix::Init(unsigned int rows, unsigned int cols,
74 bool has_intensity, bool has_intensity_var,
75 bool has_altitude, bool has_altitude_var,
76 bool has_ground_altitude, bool has_count,
77 bool has_ground_count, unsigned int resolution_num,
78 unsigned int ratio) {
79 Clear();
80
81 // resolution_num should greater than 0
82 if (resolution_num < 1) {
83 AERROR
84 << "PyramidMapMatrix: [init] The resolution_num should greater than 0.";
85 return;
86 }
87
88 // ratio should greater than 0
89 if (ratio < 1) {
90 AERROR << "PyramidMapMatrix: [init] The ratio should greater than 0.";
91 return;
92 }
93
94 // rows and cols in each level should be divisible by ratio
95 unsigned int rows_tem = rows;
96 unsigned int cols_tem = cols;
97 for (unsigned int i = 1; i < resolution_num_; ++i) {
98 unsigned int rows_remainder = rows_tem % ratio;
99 unsigned int cols_remainder = cols_tem % ratio;
100
101 if (rows_remainder != 0 || cols_remainder != 0) {
102 AERROR << "PyramidMapMatrix: [init] "
103 << "Rows and cols in each level should be divisible by ratio.";
104 return;
105 }
106
107 rows_tem /= ratio;
108 cols_tem /= ratio;
109 }
110
111 resolution_num_ = resolution_num;
112 ratio_ = ratio;
113
114 has_intensity_ = has_intensity;
115 has_intensity_var_ = has_intensity_var;
116 has_altitude_ = has_altitude;
117 has_altitude_var_ = has_altitude_var;
118 has_ground_altitude_ = has_ground_altitude;
119 has_count_ = has_count;
120 has_ground_count_ = has_ground_count;
121
122 // Init rows_mr_ and cols_mr_
123 rows_mr_.resize(resolution_num_, 0);
124 cols_mr_.resize(resolution_num_, 0);
125 rows_mr_[0] = rows;
126 cols_mr_[0] = cols;
127 for (unsigned int i = 1; i < resolution_num_; ++i) {
128 rows_mr_[i] = rows_mr_[i - 1] / ratio_;
129 cols_mr_[i] = cols_mr_[i - 1] / ratio_;
130 }
131
132 // Init data matrixes
133 if (has_intensity_) {
134 intensity_matrixes_.resize(resolution_num_);
135 for (unsigned int i = 0; i < resolution_num_; i++) {
136 intensity_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
137 }
138 }
139 if (has_intensity_var_) {
140 intensity_var_matrixes_.resize(resolution_num_);
141 for (unsigned int i = 0; i < resolution_num_; i++) {
142 intensity_var_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
143 }
144 }
145 if (has_altitude_) {
146 altitude_matrixes_.resize(resolution_num_);
147 for (unsigned int i = 0; i < resolution_num_; i++) {
148 altitude_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
149 }
150 }
151 if (has_altitude_var_) {
152 altitude_var_matrixes_.resize(resolution_num_);
153 for (unsigned int i = 0; i < resolution_num_; i++) {
154 altitude_var_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
155 }
156 }
157 if (has_ground_altitude_) {
158 ground_altitude_matrixes_.resize(resolution_num_);
159 for (unsigned int i = 0; i < resolution_num_; i++) {
160 ground_altitude_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
161 }
162 }
163 if (has_count_) {
164 count_matrixes_.resize(resolution_num_);
165 for (unsigned int i = 0; i < resolution_num_; i++) {
166 count_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
167 }
168 }
169 if (has_ground_count_) {
170 ground_count_matrixes_.resize(resolution_num_);
171 for (unsigned int i = 0; i < resolution_num_; i++) {
172 ground_count_matrixes_[i].Init(rows_mr_[i], cols_mr_[i]);
173 }
174 }
175
176 // Init ratio multiplier
177 unsigned int size = std::max(rows_mr_[0], cols_mr_[0]);
178 ratio_multiplier_.resize(size, 0);
179 for (unsigned int i = 0; i < size; ++i) {
180 ratio_multiplier_[i] = i * ratio_;
181 }
182}
183
184void PyramidMapMatrix::Reset(unsigned int level) {
185 if (level >= resolution_num_) {
186 AERROR << "PyramidMapMatrix: [reset] The level id is illegal.";
187 return;
188 }
189
190 if (has_intensity_) {
191 intensity_matrixes_[level].MakeEmpty();
192 }
193 if (has_intensity_var_) {
194 intensity_var_matrixes_[level].MakeEmpty();
195 }
196 if (has_altitude_) {
197 altitude_matrixes_[level].MakeEmpty();
198 }
199 if (has_altitude_var_) {
200 altitude_var_matrixes_[level].MakeEmpty();
201 }
202 if (has_ground_altitude_) {
203 ground_altitude_matrixes_[level].MakeEmpty();
204 }
205 if (has_count_) {
206 count_matrixes_[level].MakeEmpty();
207 }
208 if (has_ground_count_) {
209 ground_count_matrixes_[level].MakeEmpty();
210 }
211}
212
213void PyramidMapMatrix::ResetCells(unsigned int start_id, unsigned int end_id,
214 unsigned int level) {
215 if (level >= resolution_num_) {
216 AERROR << "PyramidMapMatrix: [ResetCells] The level id is illegal.";
217 return;
218 }
219
220 unsigned int length = rows_mr_[level] * cols_mr_[level];
221 if (start_id >= length || end_id >= length) {
222 AERROR
223 << "PyramidMapMatrix: [ResetCell] The start_id or end_id is illegal.";
224 return;
225 }
226
227 if (has_intensity_) {
228 intensity_matrixes_[level].MakeEmpty(start_id, end_id);
229 }
230 if (has_intensity_var_) {
231 intensity_var_matrixes_[level].MakeEmpty(start_id, end_id);
232 }
233 if (has_altitude_) {
234 altitude_matrixes_[level].MakeEmpty(start_id, end_id);
235 }
236 if (has_altitude_var_) {
237 altitude_var_matrixes_[level].MakeEmpty(start_id, end_id);
238 }
239 if (has_ground_altitude_) {
240 ground_altitude_matrixes_[level].MakeEmpty(start_id, end_id);
241 }
242 if (has_count_) {
243 count_matrixes_[level].MakeEmpty(start_id, end_id);
244 }
245 if (has_ground_count_) {
246 ground_count_matrixes_[level].MakeEmpty(start_id, end_id);
247 }
248}
249
250void PyramidMapMatrix::ResetCell(unsigned int id, unsigned int level) {
251 ResetCells(id, id, level);
252}
253
255 resolution_num_ = 1;
256 ratio_ = 2;
257
258 has_intensity_ = false;
259 has_intensity_var_ = false;
260 has_altitude_ = false;
261 has_altitude_var_ = false;
262 has_ground_altitude_ = false;
263 has_count_ = false;
264 has_ground_count_ = false;
265
266 rows_mr_.clear();
267 cols_mr_.clear();
268 ratio_multiplier_.clear();
269 intensity_matrixes_.clear();
270 intensity_var_matrixes_.clear();
271 altitude_matrixes_.clear();
272 altitude_var_matrixes_.clear();
273 ground_altitude_matrixes_.clear();
274 count_matrixes_.clear();
275 ground_count_matrixes_.clear();
276}
277
278bool PyramidMapMatrix::GetIntensityImg(cv::Mat* intensity_img) const {
279 return GetIntensityImg(0, intensity_img);
280}
281
282bool PyramidMapMatrix::GetIntensityImg(unsigned int level,
283 cv::Mat* intensity_img) const {
284 if (!has_intensity_ || resolution_num_ < 1) {
285 AERROR << "PyramidMapMatrix: [GetIntensityImg] No intensity data.";
286 return false;
287 }
288
289 if (level >= resolution_num_) {
290 AERROR << "PyramidMapMatrix: [GetIntensityImg] The level id is illegal.";
291 return false;
292 }
293
294 *intensity_img = cv::Mat(cv::Size(cols_mr_[level], rows_mr_[level]), CV_8UC1);
295
296 for (unsigned int y = 0; y < rows_mr_[level]; ++y) {
297 for (unsigned int x = 0; x < cols_mr_[level]; ++x) {
298 if (intensity_matrixes_[level][y][x] < 0) {
299 intensity_img->at<unsigned char>(y, x) = 0;
300 } else if (intensity_matrixes_[level][y][x] > 255) {
301 intensity_img->at<unsigned char>(y, x) = 255;
302 } else {
303 intensity_img->at<unsigned char>(y, x) =
304 static_cast<unsigned char>(intensity_matrixes_[level][y][x]);
305 }
306 }
307 }
308
309 return true;
310}
311
312bool PyramidMapMatrix::GetAltitudeImg(cv::Mat* altitude_img) const {
313 return GetAltitudeImg(0, altitude_img);
314}
315
316bool PyramidMapMatrix::GetAltitudeImg(unsigned int level,
317 cv::Mat* altitude_img) const {
318 if (!has_altitude_ || resolution_num_ < 1) {
319 AERROR << "PyramidMapMatrix: [GetAltitudeImg] No altitude data.";
320 return false;
321 }
322
323 if (level >= resolution_num_) {
324 AERROR << "PyramidMapMatrix: [GetAltitudeImg] The level id is illegal.";
325 return false;
326 }
327
328 float min_alt = 1e8;
329 float max_alt = -1e8;
330
331 for (unsigned int y = 0; y < rows_mr_[level]; y++) {
332 for (unsigned int x = 0; x < cols_mr_[level]; x++) {
333 if (count_matrixes_[level][y][x] > 0) {
334 if (altitude_matrixes_[level][y][x] > max_alt) {
335 max_alt = altitude_matrixes_[level][y][x];
336 }
337 if (altitude_matrixes_[level][y][x] < min_alt) {
338 min_alt = altitude_matrixes_[level][y][x];
339 }
340 }
341 }
342 }
343
344 *altitude_img = cv::Mat(cv::Size(cols_mr_[level], rows_mr_[level]), CV_8UC1);
345 for (unsigned int y = 0; y < rows_mr_[level]; y++) {
346 for (unsigned int x = 0; x < cols_mr_[level]; x++) {
347 if (count_matrixes_[level][y][x] > 0) {
348 if (altitude_matrixes_[level][y][x] >= max_alt) {
349 altitude_img->at<unsigned char>(y, x) = 255;
350 } else if (altitude_matrixes_[level][y][x] <= min_alt) {
351 altitude_img->at<unsigned char>(y, x) = 0;
352 } else {
353 altitude_img->at<unsigned char>(y, x) = static_cast<unsigned char>(
354 (altitude_matrixes_[level][y][x] - min_alt) /
355 (max_alt - min_alt) * 255);
356 }
357 } else {
358 altitude_img->at<unsigned char>(y, x) = 0;
359 }
360 }
361 }
362
363 return true;
364}
365
367 if (!has_count_) {
368 AERROR << "PyramidMapMatrix: [bottom_up] Has no count.";
369 return;
370 }
371
372 for (unsigned int i = 1; i < resolution_num_; ++i) {
373 const unsigned int& row = rows_mr_[i];
374 const unsigned int& col = rows_mr_[i];
375 for (unsigned int r = 0; r < row; ++r) {
376 for (unsigned int c = 0; c < col; ++c) {
377 unsigned int& r0 = ratio_multiplier_[r];
378 unsigned int& c0 = ratio_multiplier_[c];
379 for (unsigned int rl = r0; rl < r0 + ratio_; ++rl) {
380 for (unsigned int cl = c0; cl < c0 + ratio_; ++cl) {
381 const unsigned int& count = count_matrixes_[i - 1][rl][cl];
382 if (count > 0) {
383 float* intensity = nullptr;
384 float* intensity_var = nullptr;
385 float* altitude = nullptr;
386 float* altitude_var = nullptr;
387 float* ground_altitude = nullptr;
388 unsigned int* count = nullptr;
389 unsigned int* ground_count = nullptr;
390
391 GetMapCellSafe(&intensity, &intensity_var, &altitude,
392 &altitude_var, &ground_altitude, &count,
393 &ground_count, rl, cl, i - 1);
394 MergeCellSafe(intensity, intensity_var, altitude, altitude_var,
395 ground_altitude, count, ground_count, r, c, i);
396 }
397 }
398 }
399 }
400 }
401 }
402}
403
405 for (unsigned int i = 1; i < resolution_num_; ++i) {
406 const unsigned int& row = rows_mr_[i];
407 const unsigned int& col = rows_mr_[i];
408 for (unsigned int r = 0; r < row; ++r) {
409 for (unsigned int c = 0; c < col; ++c) {
410 unsigned int& r0 = ratio_multiplier_[r];
411 unsigned int& c0 = ratio_multiplier_[c];
412 for (unsigned int rl = r0; rl < r0 + ratio_; ++rl) {
413 for (unsigned int cl = c0; cl < c0 + ratio_; ++cl) {
414 const unsigned int& count = count_matrixes_[i - 1][rl][cl];
415 if (count > 0) {
416 float* intensity = nullptr;
417 float* intensity_var = nullptr;
418 float* altitude = nullptr;
419 unsigned int* count = nullptr;
420
421 GetMapCellBase(&intensity, &intensity_var, &altitude, &count, rl,
422 cl, i - 1);
423 MergeCellBase(intensity, intensity_var, altitude, count, r, c, i);
424 }
425 }
426 }
427 }
428 }
429 }
430}
431
433 const PyramidMapMatrix& map_matrix) {
434 Clear();
435 resolution_num_ = map_matrix.resolution_num_;
436 ratio_ = map_matrix.ratio_;
437 rows_mr_ = map_matrix.rows_mr_;
438 cols_mr_ = map_matrix.cols_mr_;
439 ratio_multiplier_ = map_matrix.ratio_multiplier_;
440
441 intensity_matrixes_ = map_matrix.intensity_matrixes_;
442 intensity_var_matrixes_ = map_matrix.intensity_var_matrixes_;
443 altitude_matrixes_ = map_matrix.altitude_matrixes_;
444 altitude_var_matrixes_ = map_matrix.altitude_var_matrixes_;
445 ground_altitude_matrixes_ = map_matrix.ground_altitude_matrixes_;
446 count_matrixes_ = map_matrix.count_matrixes_;
447 ground_count_matrixes_ = map_matrix.ground_count_matrixes_;
448
449 has_intensity_ = map_matrix.has_intensity_;
450 has_intensity_var_ = map_matrix.has_intensity_var_;
451 has_altitude_ = map_matrix.has_altitude_;
452 has_altitude_var_ = map_matrix.has_altitude_var_;
453 has_ground_altitude_ = map_matrix.has_ground_altitude_;
454 has_count_ = map_matrix.has_count_;
455 has_ground_count_ = map_matrix.has_ground_count_;
456 return *this;
457}
458
459const float* PyramidMapMatrix::GetIntensitySafe(unsigned int row,
460 unsigned int col,
461 unsigned int level) const {
462 if (!has_intensity_) {
463 AERROR << "PyramidMapMatrix: [GetIntensitySafe] Has no intensity.";
464 return nullptr;
465 }
466
467 if (!CheckLegalityForGetData(row, col, level)) {
468 AERROR << "PyramidMapMatrix: [GetIntensitySafe] Params is illegal.";
469 return nullptr;
470 }
471
472 return &intensity_matrixes_[level][row][col];
473}
474
475const float* PyramidMapMatrix::GetIntensityVarSafe(unsigned int row,
476 unsigned int col,
477 unsigned int level) const {
478 if (!has_intensity_var_) {
479 AERROR << "PyramidMapMatrix: [GetIntensityVarSafe] Has no intensity_var.";
480 return nullptr;
481 }
482
483 if (!CheckLegalityForGetData(row, col, level)) {
484 AERROR << "PyramidMapMatrix: [GetIntensityVarSafe] Params is illegal.";
485 return nullptr;
486 }
487
488 return &intensity_var_matrixes_[level][row][col];
489}
490
491const float* PyramidMapMatrix::GetAltitudeSafe(unsigned int row,
492 unsigned int col,
493 unsigned int level) const {
494 if (!has_altitude_) {
495 AERROR << "PyramidMapMatrix: [GetAltitudeSafe] Has no altitude.";
496 return nullptr;
497 }
498
499 if (!CheckLegalityForGetData(row, col, level)) {
500 AERROR << "PyramidMapMatrix: [GetAltitudeSafe] Params is illegal.";
501 return nullptr;
502 }
503
504 return &altitude_matrixes_[level][row][col];
505}
506
507const float* PyramidMapMatrix::GetAltitudeVarSafe(unsigned int row,
508 unsigned int col,
509 unsigned int level) const {
510 if (!has_altitude_var_) {
511 AERROR << "PyramidMapMatrix: [GetAltitudeVarSafe] Has no altitude_var.";
512 return nullptr;
513 }
514
515 if (!CheckLegalityForGetData(row, col, level)) {
516 AERROR << "PyramidMapMatrix: [get_altitude_var] Params is illegal.";
517 return nullptr;
518 }
519
520 return &altitude_var_matrixes_[level][row][col];
521}
522
523const float* PyramidMapMatrix::GetGroundAltitudeSafe(unsigned int row,
524 unsigned int col,
525 unsigned int level) const {
526 if (!has_ground_altitude_) {
527 AERROR
528 << "PyramidMapMatrix: [GetGroundAltitudeSafe] Has no ground_altitude.";
529 return nullptr;
530 }
531
532 if (!CheckLegalityForGetData(row, col, level)) {
533 AERROR << "PyramidMapMatrix: [GetGroundAltitudeSafe] Params is illegal.";
534 return nullptr;
535 }
536
537 return &ground_altitude_matrixes_[level][row][col];
538}
539
540const unsigned int* PyramidMapMatrix::GetCountSafe(unsigned int row,
541 unsigned int col,
542 unsigned int level) const {
543 if (!has_count_) {
544 AERROR << "PyramidMapMatrix: [GetCountSafe] Has no count.";
545 return nullptr;
546 }
547
548 if (!CheckLegalityForGetData(row, col, level)) {
549 AERROR << "PyramidMapMatrix: [GetCountSafe] Params is illegal.";
550 return nullptr;
551 }
552
553 return &count_matrixes_[level][row][col];
554}
555
557 unsigned int row, unsigned int col, unsigned int level) const {
558 if (!has_ground_count_) {
559 AERROR << "PyramidMapMatrix: [GetGroundCountSafe] Has no ground_count.";
560 return nullptr;
561 }
562
563 if (!CheckLegalityForGetData(row, col, level)) {
564 AERROR << "PyramidMapMatrix: [GetGroundCountSafe] Params is illegal.";
565 return nullptr;
566 }
567
568 return &ground_count_matrixes_[level][row][col];
569}
570
571void PyramidMapMatrix::GetMapCellSafe(float** intensity, float** intensity_var,
572 float** altitude, float** altitude_var,
573 float** ground_altitude,
574 unsigned int** count,
575 unsigned int** ground_count,
576 unsigned int row, unsigned int col,
577 unsigned int level) {
578 if (!CheckLegalityForGetData(row, col, level)) {
579 AERROR << "PyramidMapMatrix: [GetMapCellSafe] Params is illegal.";
580 return;
581 }
582
583 if (has_intensity_) {
584 *intensity = &intensity_matrixes_[level][row][col];
585 }
586
587 if (has_intensity_var_) {
588 *intensity_var = &intensity_var_matrixes_[level][row][col];
589 }
590
591 if (has_altitude_) {
592 *altitude = &altitude_matrixes_[level][row][col];
593 }
594
595 if (has_altitude_var_) {
596 *altitude_var = &altitude_var_matrixes_[level][row][col];
597 }
598
599 if (has_ground_altitude_) {
600 *ground_altitude = &ground_altitude_matrixes_[level][row][col];
601 }
602
603 if (has_count_) {
604 *count = &count_matrixes_[level][row][col];
605 }
606
607 if (has_ground_count_) {
608 *ground_count = &ground_count_matrixes_[level][row][col];
609 }
610}
611
613 if (!has_intensity_) {
614 AERROR << "PyramidMapMatrix: [GetIntensityMatrixSafe] Has no intensity.";
615 return nullptr;
616 }
617
618 if (level >= resolution_num_) {
619 AERROR << "PyramidMapMatrix: [GetIntensityMatrixSafe] The level id is "
620 "illegal.";
621 return nullptr;
622 }
623
624 return &intensity_matrixes_[level];
625}
626
628 if (!has_intensity_var_) {
629 AERROR << "PyramidMapMatrix: [GetIntensityVarMatrixSafe] Has no "
630 "intensity_var.";
631 return nullptr;
632 }
633
634 if (level >= resolution_num_) {
635 AERROR << "PyramidMapMatrix: [GetIntensityVarMatrixSafe] The level id "
636 "is illegal.";
637 return nullptr;
638 }
639
640 return &intensity_var_matrixes_[level];
641}
642
644 if (!has_altitude_) {
645 AERROR << "PyramidMapMatrix: [GetAltitudeMatrixSafe] Has no altitude.";
646 return nullptr;
647 }
648
649 if (level >= resolution_num_) {
650 AERROR
651 << "PyramidMapMatrix: [GetAltitudeMatrixSafe] The level id is illegal.";
652 return nullptr;
653 }
654
655 return &altitude_matrixes_[level];
656}
657
659 if (!has_altitude_var_) {
660 AERROR
661 << "PyramidMapMatrix: [GetAltitudeVarMatrixSafe] Has no altitude_var.";
662 return nullptr;
663 }
664
665 if (level >= resolution_num_) {
666 AERROR << "PyramidMapMatrix: [GetAltitudeVarMatrixSafe] The level id is "
667 "illegal.";
668 return nullptr;
669 }
670
671 return &altitude_var_matrixes_[level];
672}
673
675 if (!has_ground_altitude_) {
676 AERROR << "PyramidMapMatrix: [GetGroundAltitudeMatrixSafe] Has no "
677 "ground_altitude.";
678 return nullptr;
679 }
680
681 if (level >= resolution_num_) {
682 AERROR << "PyramidMapMatrix: [GetGroundAltitudeMatrixSafe] The level id "
683 "is illegal.";
684 return nullptr;
685 }
686
687 return &ground_altitude_matrixes_[level];
688}
689
691 if (!has_count_) {
692 AERROR << "PyramidMapMatrix: [GetCountMatrixSafe] Has no count.";
693 return nullptr;
694 }
695
696 if (level >= resolution_num_) {
697 AERROR << "PyramidMapMatrix: [GetCountMatrixSafe] The level id is illegal.";
698 return nullptr;
699 }
700
701 return &count_matrixes_[level];
702}
703
705 if (!has_ground_count_) {
706 AERROR
707 << "PyramidMapMatrix: [GetGroundCountMatrixSafe] Has no ground_count.";
708 return nullptr;
709 }
710
711 if (level >= resolution_num_) {
712 AERROR << "PyramidMapMatrix: [GetGroundCountMatrixSafe] The level id is "
713 "illegal.";
714 return nullptr;
715 }
716
717 return &ground_count_matrixes_[level];
718}
719
721 unsigned int level) const {
722 if (!has_intensity_) {
723 AERROR << "PyramidMapMatrix: [GetIntensityMatrixSafe] Has no intensity.";
724 return nullptr;
725 }
726
727 if (level >= resolution_num_) {
728 AERROR << "PyramidMapMatrix: [GetIntensityMatrixSafe] The level id is "
729 "illegal.";
730 return nullptr;
731 }
732
733 return &intensity_matrixes_[level];
734}
735
737 unsigned int level) const {
738 if (!has_intensity_var_) {
739 AERROR << "PyramidMapMatrix: [GetIntensityVarMatrixSafe] Has no "
740 "intensity_var.";
741 return nullptr;
742 }
743
744 if (level >= resolution_num_) {
745 AERROR << "PyramidMapMatrix: [GetIntensityVarMatrixSafe] The level id "
746 "is illegal.";
747 return nullptr;
748 }
749
750 return &intensity_var_matrixes_[level];
751}
752
754 unsigned int level) const {
755 if (!has_altitude_) {
756 AERROR << "PyramidMapMatrix: [GetAltitudeMatrixSafe] Has no altitude.";
757 return nullptr;
758 }
759
760 if (level >= resolution_num_) {
761 AERROR
762 << "PyramidMapMatrix: [GetAltitudeMatrixSafe] The level id is illegal.";
763 return nullptr;
764 }
765
766 return &altitude_matrixes_[level];
767}
768
770 unsigned int level) const {
771 if (!has_altitude_var_) {
772 AERROR
773 << "PyramidMapMatrix: [GetAltitudeVarMatrixSafe] Has no altitude_var.";
774 return nullptr;
775 }
776
777 if (level >= resolution_num_) {
778 AERROR << "PyramidMapMatrix: [GetAltitudeVarMatrixSafe] The level id is "
779 "illegal.";
780 return nullptr;
781 }
782
783 return &altitude_var_matrixes_[level];
784}
785
787 unsigned int level) const {
788 if (!has_ground_altitude_) {
789 AERROR << "PyramidMapMatrix: [GetGroundAltitudeMatrixSafe] Has no "
790 "ground_altitude.";
791 return nullptr;
792 }
793
794 if (level >= resolution_num_) {
795 AERROR << "PyramidMapMatrix: [GetGroundAltitudeMatrixSafe] The level id "
796 "is illegal.";
797 return nullptr;
798 }
799
800 return &ground_altitude_matrixes_[level];
801}
802
804 unsigned int level) const {
805 if (!has_count_) {
806 AERROR << "PyramidMapMatrix: [GetCountMatrixSafe] Has no count.";
807 return nullptr;
808 }
809
810 if (level >= resolution_num_) {
811 AERROR << "PyramidMapMatrix: [GetCountMatrixSafe] The level id is illegal.";
812 return nullptr;
813 }
814
815 return &count_matrixes_[level];
816}
817
819 unsigned int level) const {
820 if (!has_ground_count_) {
821 AERROR
822 << "PyramidMapMatrix: [GetGroundCountMatrixSafe] Has no ground_count.";
823 return nullptr;
824 }
825
826 if (level >= resolution_num_) {
827 AERROR << "PyramidMapMatrix: [GetGroundCountMatrixSafe] The level id is "
828 "illegal.";
829 return nullptr;
830 }
831
832 return &ground_count_matrixes_[level];
833}
834
835void PyramidMapMatrix::SetIntensityMatrix(const float* input, unsigned int size,
836 unsigned int start_index,
837 unsigned int level) {
838 if (!has_intensity_) {
839 AERROR << "PyramidMapMatrix: [SetIntensityMatrix] Has no intensity.";
840 return;
841 }
842
843 if (!CheckLegalityForSetData(level, start_index, size)) {
844 AERROR << "PyramidMapMatrix: [SetIntensityMatrix] Params is illegal.";
845 return;
846 }
847
848 intensity_matrixes_[level].SetData(input, size, start_index);
849}
850
852 unsigned int size,
853 unsigned int start_index,
854 unsigned int level) {
855 if (!has_intensity_var_) {
856 AERROR
857 << "PyramidMapMatrix: [set_intensity_var_matrix] Has no intensity_var.";
858 return;
859 }
860
861 if (!CheckLegalityForSetData(level, start_index, size)) {
862 AERROR << "PyramidMapMatrix: [set_intensity_var_matrix] Params is illegal.";
863 return;
864 }
865
866 intensity_var_matrixes_[level].SetData(input, size, start_index);
867}
868
869void PyramidMapMatrix::SetAltitudeMatrix(const float* input, unsigned int size,
870 unsigned int start_index,
871 unsigned int level) {
872 if (!has_altitude_) {
873 AERROR << "PyramidMapMatrix: [SetAltitudeMatrix] Has no altitude.";
874 return;
875 }
876
877 if (!CheckLegalityForSetData(level, start_index, size)) {
878 AERROR << "PyramidMapMatrix: [SetAltitudeMatrix] Params is illegal.";
879 return;
880 }
881
882 altitude_matrixes_[level].SetData(input, size, start_index);
883}
884
886 unsigned int size,
887 unsigned int start_index,
888 unsigned int level) {
889 if (!has_altitude_var_) {
890 AERROR << "PyramidMapMatrix: [SetAltitudeVarMatrix] Has no altitude_var.";
891 return;
892 }
893
894 if (!CheckLegalityForSetData(level, start_index, size)) {
895 AERROR << "PyramidMapMatrix: [SetAltitudeVarMatrix] Params is illegal.";
896 return;
897 }
898
899 altitude_var_matrixes_[level].SetData(input, size, start_index);
900}
901
903 unsigned int size,
904 unsigned int start_index,
905 unsigned int level) {
906 if (!has_ground_altitude_) {
907 AERROR << "PyramidMapMatrix: [SetGroundAltitudeMatrix] Has no "
908 "ground_altitude.";
909 return;
910 }
911
912 if (!CheckLegalityForSetData(level, start_index, size)) {
913 AERROR << "PyramidMapMatrix: [SetGroundAltitudeMatrix] Params is illegal.";
914 return;
915 }
916
917 ground_altitude_matrixes_[level].SetData(input, size, start_index);
918}
919
920void PyramidMapMatrix::SetCountMatrix(const unsigned int* input,
921 unsigned int size,
922 unsigned int start_index,
923 unsigned int level) {
924 if (!has_count_) {
925 AERROR << "PyramidMapMatrix: [SetCountMatrix] Has no count.";
926 return;
927 }
928
929 if (!CheckLegalityForSetData(level, start_index, size)) {
930 AERROR << "PyramidMapMatrix: [SetCountMatrix] Params is illegal.";
931 return;
932 }
933
934 count_matrixes_[level].SetData(input, size, start_index);
935}
936
937void PyramidMapMatrix::SetGroundCountMatrix(const unsigned int* input,
938 unsigned int size,
939 unsigned int start_index,
940 unsigned int level) {
941 if (!has_ground_count_) {
942 AERROR << "PyramidMapMatrix: [SetGroundCountMatrix] Has no ground count.";
943 return;
944 }
945
946 if (!CheckLegalityForSetData(level, start_index, size)) {
947 AERROR << "PyramidMapMatrix: [SetGroundCountMatrix] Params is illegal.";
948 return;
949 }
950
951 ground_count_matrixes_[level].SetData(input, size, start_index);
952}
953
955 const Rect2D<unsigned int>& source_roi,
956 const Rect2D<unsigned int>& target_roi,
957 unsigned int type,
958 unsigned int level) {
959 if (source_matrix == nullptr) {
960 AERROR << "PyramidMapMatrix: [SetFloatMatrixRoi] Source matrix is nullptr.";
961 return;
962 }
963
964 switch (type) {
965 case 0:
966 if (!has_intensity_) {
967 AERROR << "PyramidMapMatrix: [SetFloatMatrixRoi] Has no intensity.";
968 return;
969 }
970 break;
971 case 1:
972 if (!has_intensity_var_) {
973 AERROR << "PyramidMapMatrix: [SetFloatMatrixRoi] Has no intensity var.";
974 return;
975 }
976 break;
977 case 2:
978 if (!has_altitude_) {
979 AERROR << "PyramidMapMatrix: [SetFloatMatrixRoi] Has no altitude.";
980 return;
981 }
982 break;
983 case 3:
984 if (!has_altitude_var_) {
985 AERROR << "PyramidMapMatrix: [SetFloatMatrixRoi] Has no altitude var.";
986 return;
987 }
988 break;
989 case 4:
990 if (!has_ground_altitude_) {
991 AERROR
992 << "PyramidMapMatrix: [SetFloatMatrixRoi] Has no ground altitude.";
993 return;
994 }
995 break;
996 }
997
998 if (!CheckLegalityForSetDataRoi(
999 level, static_cast<unsigned int>(source_matrix->GetRow()),
1000 static_cast<unsigned int>(source_matrix->GetCol()), source_roi,
1001 target_roi)) {
1002 AERROR << "PyramidMapMatrix: [SetFloatMatrixRoi] Params is illegal.";
1003 return;
1004 }
1005
1006 const unsigned int& source_roi_min_x = source_roi.GetMinX();
1007 const unsigned int& source_roi_min_y = source_roi.GetMinY();
1008
1009 const unsigned int& target_roi_min_x = target_roi.GetMinX();
1010 const unsigned int& target_roi_min_y = target_roi.GetMinY();
1011 const unsigned int& target_roi_max_x = target_roi.GetMaxX();
1012 const unsigned int& target_roi_max_y = target_roi.GetMaxY();
1013
1014 // unsigned int roi_rows = target_roi_max_y - target_roi_min_y + 1;
1015 unsigned int roi_cols = target_roi_max_x - target_roi_min_x + 1;
1016
1017 unsigned int inc = 0;
1018 for (unsigned int y = target_roi_min_y; y <= target_roi_max_y; ++y) {
1019 unsigned int target_start_index = y * cols_mr_[level] + target_roi_min_x;
1020 unsigned int source_start_index =
1021 (source_roi_min_y + inc) * source_matrix->GetCol() + source_roi_min_x;
1022 const float* input = (*source_matrix)[0] + source_start_index;
1023
1024 switch (type) {
1025 case 0:
1026 SetIntensityMatrix(input, roi_cols, target_start_index, level);
1027 break;
1028 case 1:
1029 SetIntensityVarMatrix(input, roi_cols, target_start_index, level);
1030 break;
1031 case 2:
1032 SetAltitudeMatrix(input, roi_cols, target_start_index, level);
1033 break;
1034 case 3:
1035 SetAltitudeVarMatrix(input, roi_cols, target_start_index, level);
1036 break;
1037 case 4:
1038 SetGroundAltitudeMatrix(input, roi_cols, target_start_index, level);
1039 break;
1040 }
1041 ++inc;
1042 }
1043}
1044
1046 const Rect2D<unsigned int>& source_roi,
1047 const Rect2D<unsigned int>& target_roi,
1048 unsigned int type, unsigned int level) {
1049 if (source_matrix == nullptr) {
1050 AERROR << "PyramidMapMatrix: [SetUintMatrixRoi] Source matrix is nullptr.";
1051 return;
1052 }
1053
1054 switch (type) {
1055 case 0:
1056 if (!has_count_) {
1057 AERROR << "PyramidMapMatrix: [SetUintMatrixRoi] Has no count.";
1058 return;
1059 }
1060 break;
1061 case 1:
1062 if (!has_ground_count_) {
1063 AERROR << "PyramidMapMatrix: [SetUintMatrixRoi] Has no ground count.";
1064 return;
1065 }
1066 break;
1067 }
1068
1069 if (!CheckLegalityForSetDataRoi(
1070 level, static_cast<unsigned int>(source_matrix->GetRow()),
1071 static_cast<unsigned int>(source_matrix->GetCol()), source_roi,
1072 target_roi)) {
1073 AERROR << "PyramidMapMatrix: [SetUintMatrixRoi] Params is illegal.";
1074 return;
1075 }
1076
1077 const unsigned int& source_roi_min_x = source_roi.GetMinX();
1078 const unsigned int& source_roi_min_y = source_roi.GetMinY();
1079
1080 const unsigned int& target_roi_min_x = target_roi.GetMinX();
1081 const unsigned int& target_roi_min_y = target_roi.GetMinY();
1082 const unsigned int& target_roi_max_x = target_roi.GetMaxX();
1083 const unsigned int& target_roi_max_y = target_roi.GetMaxY();
1084
1085 unsigned int roi_cols = target_roi_max_x - target_roi_min_x + 1;
1086
1087 unsigned int inc = 0;
1088 for (unsigned int y = target_roi_min_y; y <= target_roi_max_y; ++y) {
1089 unsigned int target_start_index = y * cols_mr_[level] + target_roi_min_x;
1090 unsigned int source_start_index =
1091 (source_roi_min_y + inc) * source_matrix->GetCol() + source_roi_min_x;
1092 const unsigned int* input = (*source_matrix)[0] + source_start_index;
1093
1094 switch (type) {
1095 case 0:
1096 SetCountMatrix(input, roi_cols, target_start_index, level);
1097 break;
1098 case 1:
1099 SetGroundCountMatrix(input, roi_cols, target_start_index, level);
1100 break;
1101 }
1102 ++inc;
1103 }
1104}
1105
1106void PyramidMapMatrix::SetIntensitySafe(float intensity, unsigned int row,
1107 unsigned int col, unsigned int level) {
1108 if (!has_intensity_) {
1109 AERROR << "PyramidMapMatrix: [SetIntensitySafe] Has no intensity.";
1110 return;
1111 }
1112
1113 if (!CheckLegalityForGetData(row, col, level)) {
1114 AERROR << "PyramidMapMatrix: [SetIntensitySafe] Params is illegal.";
1115 return;
1116 }
1117
1118 intensity_matrixes_[level][row][col] = intensity;
1119}
1120
1122 unsigned int row, unsigned int col,
1123 unsigned int level) {
1124 if (!has_intensity_var_) {
1125 AERROR << "PyramidMapMatrix: [SetIntensityVarSafe] Has no intensity_var.";
1126 return;
1127 }
1128
1129 if (!CheckLegalityForGetData(row, col, level)) {
1130 AERROR << "PyramidMapMatrix: [SetIntensityVarSafe] Params is illegal.";
1131 return;
1132 }
1133
1134 intensity_var_matrixes_[level][row][col] = intensity_var;
1135}
1136
1137void PyramidMapMatrix::SetAltitudeSafe(float altitude, unsigned int row,
1138 unsigned int col, unsigned int level) {
1139 if (!has_altitude_) {
1140 AERROR << "PyramidMapMatrix: [SetAltitudeSafe] Has no altitude.";
1141 return;
1142 }
1143
1144 if (!CheckLegalityForGetData(row, col, level)) {
1145 AERROR << "PyramidMapMatrix: [SetAltitudeSafe] Params is illegal.";
1146 return;
1147 }
1148
1149 altitude_matrixes_[level][row][col] = altitude;
1150}
1151
1152void PyramidMapMatrix::SetAltitudeVarSafe(float altitude_var, unsigned int row,
1153 unsigned int col,
1154 unsigned int level) {
1155 if (!has_altitude_var_) {
1156 AERROR << "PyramidMapMatrix: [SetAltitudeVarSafe] Has no altitude var.";
1157 return;
1158 }
1159
1160 if (!CheckLegalityForGetData(row, col, level)) {
1161 AERROR << "PyramidMapMatrix: [SetAltitudeVarSafe] Params is illegal.";
1162 return;
1163 }
1164
1165 altitude_var_matrixes_[level][row][col] = altitude_var;
1166}
1167
1169 unsigned int row, unsigned int col,
1170 unsigned int level) {
1171 if (!has_ground_altitude_) {
1172 AERROR
1173 << "PyramidMapMatrix: [SetGroundAltitudeSafe] Has no ground altitude.";
1174 return;
1175 }
1176
1177 if (!CheckLegalityForGetData(row, col, level)) {
1178 AERROR << "PyramidMapMatrix: [SetGroundAltitudeSafe] Params is illegal.";
1179 return;
1180 }
1181
1182 ground_altitude_matrixes_[level][row][col] = ground_altitude;
1183}
1184
1185void PyramidMapMatrix::SetCountSafe(unsigned int count, unsigned int row,
1186 unsigned int col, unsigned int level) {
1187 if (!has_count_) {
1188 AERROR << "PyramidMapMatrix: [SetCountSafe] Has no count.";
1189 return;
1190 }
1191
1192 if (!CheckLegalityForGetData(row, col, level)) {
1193 AERROR << "PyramidMapMatrix: [SetCountSafe] Params is illegal.";
1194 return;
1195 }
1196
1197 count_matrixes_[level][row][col] = count;
1198}
1199
1200void PyramidMapMatrix::SetGroundCountSafe(unsigned int ground_count,
1201 unsigned int row, unsigned int col,
1202 unsigned int level) {
1203 if (!has_ground_count_) {
1204 AERROR << "PyramidMapMatrix: [SetGroundCountSafe] Has no ground count.";
1205 return;
1206 }
1207
1208 if (!CheckLegalityForGetData(row, col, level)) {
1209 AERROR << "PyramidMapMatrix: [SetGroundCountSafe] Params is illegal.";
1210 return;
1211 }
1212
1213 ground_count_matrixes_[level][row][col] = ground_count;
1214}
1215
1216void PyramidMapMatrix::SetValueSafe(unsigned char intensity, float altitude,
1217 unsigned int row, unsigned int col,
1218 unsigned int level) {
1219 if (!has_intensity_) {
1220 AERROR << "PyramidMapMatrix: [SetValueSafe] Has no intensity.";
1221 return;
1222 }
1223
1224 if (!has_altitude_) {
1225 AERROR << "PyramidMapMatrix: [SetValueSafe] Has no altitude.";
1226 return;
1227 }
1228
1229 if (!CheckLegalityForGetData(row, col, level)) {
1230 AERROR << "PyramidMapMatrix: [SetValueSafe] Params is illegal.";
1231 return;
1232 }
1233
1234 intensity_matrixes_[level][row][col] = intensity;
1235 altitude_matrixes_[level][row][col] = altitude;
1236}
1237
1239 const float* intensity, const float* intensity_var, const float* altitude,
1240 const float* altitude_var, const float* ground_altitude,
1241 const unsigned int* count, const unsigned int* ground_count,
1242 unsigned int row, unsigned int col, unsigned int level) {
1243 if (!CheckLegalityForGetData(row, col, level)) {
1244 AERROR << "PyramidMapMatrix: [MergeCellSafe] Params is illegal.";
1245 return;
1246 }
1247
1248 if (count == nullptr || !has_count_) {
1249 return;
1250 }
1251
1252 unsigned int new_count = count_matrixes_[level][row][col] + *count;
1253 float p0 = static_cast<float>(count_matrixes_[level][row][col]) /
1254 static_cast<float>(new_count);
1255 float p1 = static_cast<float>(*count) / static_cast<float>(new_count);
1256
1257 float intensity_diff = 0.0f;
1258 if (intensity != nullptr && has_intensity_) {
1259 intensity_diff = intensity_matrixes_[level][row][col] - *intensity;
1260 intensity_matrixes_[level][row][col] =
1261 intensity_matrixes_[level][row][col] * p0 + *intensity * p1;
1262 }
1263
1264 if (intensity != nullptr && has_intensity_ && intensity_var != nullptr &&
1265 has_intensity_var_) {
1266 intensity_var_matrixes_[level][row][col] =
1267 intensity_var_matrixes_[level][row][col] * p0 + *intensity_var * p1 +
1268 intensity_diff * intensity_diff * p0 * p1;
1269 }
1270
1271 float altitude_diff = 0.0f;
1272 if (altitude != nullptr && has_altitude_) {
1273 altitude_diff = altitude_matrixes_[level][row][col] - *altitude;
1274 altitude_matrixes_[level][row][col] =
1275 altitude_matrixes_[level][row][col] * p0 + *altitude * p1;
1276 }
1277
1278 if (altitude != nullptr && has_altitude_ && altitude_var != nullptr &&
1279 has_altitude_var_) {
1280 altitude_var_matrixes_[level][row][col] =
1281 altitude_var_matrixes_[level][row][col] * p0 + *altitude_var * p1 +
1282 altitude_diff * altitude_diff * p0 * p1;
1283 }
1284
1285 count_matrixes_[level][row][col] = new_count;
1286
1287 // for points on ground
1288 if (ground_count == nullptr || !has_ground_count_) {
1289 return;
1290 }
1291
1292 unsigned int new_ground_count =
1293 ground_count_matrixes_[level][row][col] + *ground_count;
1294 p0 = static_cast<float>(ground_count_matrixes_[level][row][col]) /
1295 static_cast<float>(new_ground_count);
1296 p1 = static_cast<float>(*ground_count) / static_cast<float>(new_ground_count);
1297
1298 if (ground_altitude != nullptr && has_ground_altitude_) {
1299 ground_altitude_matrixes_[level][row][col] =
1300 ground_altitude_matrixes_[level][row][col] * p0 + *ground_altitude * p1;
1301 }
1302
1303 ground_count_matrixes_[level][row][col] = new_ground_count;
1304}
1305
1306bool PyramidMapMatrix::CheckLegalityForGetData(unsigned int row,
1307 unsigned int col,
1308 unsigned int level) const {
1309 if (level >= resolution_num_) {
1310 AERROR << "PyramidMapMatrix: [CheckLegalityForGetData] The level id is "
1311 "illegal.";
1312 return false;
1313 }
1314
1315 if (row >= rows_mr_[level]) {
1316 AERROR << "PyramidMapMatrix: [CheckLegalityForGetData] The row is illegal.";
1317 return false;
1318 }
1319
1320 if (col >= cols_mr_[level]) {
1321 AERROR << "PyramidMapMatrix: [CheckLegalityForGetData] The col is illegal.";
1322 return false;
1323 }
1324
1325 return true;
1326}
1327
1328bool PyramidMapMatrix::CheckLegalityForSetData(unsigned int level,
1329 unsigned int start_id,
1330 unsigned int size) const {
1331 if (level >= resolution_num_) {
1332 AERROR << "PyramidMapMatrix: [CheckLegalityForSetData] The level id is "
1333 "illegal.";
1334 return false;
1335 }
1336
1337 if (start_id + size > rows_mr_[level] * cols_mr_[level]) {
1338 AERROR << "PyramidMapMatrix: [CheckLegalityForSetData] The start_id or "
1339 "size is illegal.";
1340 return false;
1341 }
1342
1343 return true;
1344}
1345
1346bool PyramidMapMatrix::CheckLegalityForSetDataRoi(
1347 unsigned int level, unsigned int source_matrix_rows,
1348 unsigned int source_matrix_cols, const Rect2D<unsigned int>& source_roi,
1349 const Rect2D<unsigned int>& target_roi) const {
1350 if (level >= resolution_num_) {
1351 AERROR << "PyramidMapMatrix: [CheckLegalityForSetDataRoi] The level id "
1352 "is illegal.";
1353 return false;
1354 }
1355
1356 const unsigned int& source_roi_min_x = source_roi.GetMinX();
1357 const unsigned int& source_roi_min_y = source_roi.GetMinY();
1358 const unsigned int& source_roi_max_x = source_roi.GetMaxX();
1359 const unsigned int& source_roi_max_y = source_roi.GetMaxY();
1360
1361 const unsigned int& target_roi_min_x = target_roi.GetMinX();
1362 const unsigned int& target_roi_min_y = target_roi.GetMinY();
1363 const unsigned int& target_roi_max_x = target_roi.GetMaxX();
1364 const unsigned int& target_roi_max_y = target_roi.GetMaxY();
1365
1366 if (source_roi_min_x > source_roi_max_x ||
1367 source_roi_min_y > source_roi_max_y ||
1368 target_roi_min_x > target_roi_max_x ||
1369 target_roi_min_y > target_roi_max_y ||
1370 source_roi_max_x >= source_matrix_cols ||
1371 source_roi_max_y >= source_matrix_rows ||
1372 target_roi_max_x >= cols_mr_[level] ||
1373 target_roi_max_y >= rows_mr_[level] ||
1374 source_roi_max_x - source_roi_min_x !=
1375 target_roi_max_x - target_roi_min_x ||
1376 source_roi_max_y - source_roi_min_y !=
1377 target_roi_max_y - target_roi_min_y) {
1378 AERROR << "PyramidMapMatrix: [CheckLegalityForSetDataRoi]"
1379 " The source_roi or target_roi is illegal.";
1380 return false;
1381 }
1382
1383 return true;
1384}
1385
1386void PyramidMapMatrix::AddSampleSafe(float intensity, float altitude,
1387 unsigned int row, unsigned int col,
1388 unsigned int level) {
1389 if (!CheckLegalityForGetData(row, col, level)) {
1390 AERROR << "PyramidMapMatrix: [AddSampleSafe] Params is illegal.";
1391 return;
1392 }
1393
1394 if (has_count_) {
1395 ++count_matrixes_[level][row][col];
1396 }
1397
1398 float v1 = 0.0;
1399 float v2 = 0.0;
1400 float value = 0.0;
1401 if (has_count_ && has_intensity_) {
1402 v1 = intensity - intensity_matrixes_[level][row][col];
1403 value = v1 / static_cast<float>(count_matrixes_[level][row][col]);
1404 intensity_matrixes_[level][row][col] += value;
1405 }
1406
1407 if (has_count_ && has_intensity_ && has_intensity_var_) {
1408 v2 = intensity - intensity_matrixes_[level][row][col];
1409 intensity_var_matrixes_[level][row][col] =
1410 (static_cast<float>(count_matrixes_[level][row][col] - 1) *
1411 intensity_var_matrixes_[level][row][col] +
1412 v1 * v2) /
1413 static_cast<float>(count_matrixes_[level][row][col]);
1414 }
1415
1416 if (has_count_ && has_altitude_) {
1417 v1 = altitude - altitude_matrixes_[level][row][col];
1418 value = v1 / static_cast<float>(count_matrixes_[level][row][col]);
1419 altitude_matrixes_[level][row][col] += value;
1420 }
1421
1422 if (has_count_ && has_altitude_ && has_altitude_var_) {
1423 v2 = altitude - altitude_matrixes_[level][row][col];
1424 altitude_var_matrixes_[level][row][col] =
1425 (static_cast<float>(count_matrixes_[level][row][col] - 1) *
1426 altitude_var_matrixes_[level][row][col] +
1427 v1 * v2) /
1428 static_cast<float>(count_matrixes_[level][row][col]);
1429 }
1430}
1431
1432void PyramidMapMatrix::AddGroundSample(float ground_altitude, unsigned int row,
1433 unsigned int col, unsigned int level) {
1434 if (!CheckLegalityForGetData(row, col, level)) {
1435 AERROR << "PyramidMapMatrix: [AddGroundSample] Params is illegal.";
1436 return;
1437 }
1438
1439 if (has_ground_count_) {
1440 ++ground_count_matrixes_[level][row][col];
1441 }
1442
1443 float v1 = 0.0;
1444 float value = 0.0;
1445 if (has_ground_count_ && has_ground_altitude_) {
1446 v1 = ground_altitude - ground_altitude_matrixes_[level][row][col];
1447 value = v1 / static_cast<float>(ground_count_matrixes_[level][row][col]);
1448 ground_altitude_matrixes_[level][row][col] += value;
1449 }
1450}
1451
1452double PyramidMapMatrix::ComputeMeanIntensity(unsigned int level) {
1453 if (!has_count_) {
1454 AERROR << "PyramidMapMatrix: [ComputeMeanIntensity] Has no count.";
1455 return 0.0;
1456 }
1457
1458 if (!has_intensity_ || resolution_num_ < 1) {
1459 AERROR << "PyramidMapMatrix: [ComputeMeanIntensity] No intensity data.";
1460 return 0.0;
1461 }
1462
1463 if (level >= resolution_num_) {
1464 AERROR
1465 << "PyramidMapMatrix: [ComputeMeanIntensity] The level id is illegal.";
1466 return 0.0;
1467 }
1468
1469 double avg = 0.0;
1470 int count = 0;
1471 for (unsigned int y = 0; y < rows_mr_[level]; ++y) {
1472 for (unsigned int x = 0; x < cols_mr_[level]; ++x) {
1473 if (count_matrixes_[level][y][x] > 0) {
1474 ++count;
1475 double delta = intensity_matrixes_[level][y][x] - avg;
1476 avg += (delta / count);
1477 }
1478 }
1479 }
1480
1481 return avg;
1482}
1483
1484void PyramidMapMatrix::Reduce(std::shared_ptr<PyramidMapMatrix> cells,
1485 const PyramidMapMatrix& new_cells,
1486 unsigned int level, unsigned int new_level) {
1487 if (level >= cells->resolution_num_) {
1488 AERROR << "PyramidMapMatrix: [Reduce] The level id is illegal.";
1489 return;
1490 }
1491
1492 if (new_level >= new_cells.resolution_num_) {
1493 AERROR << "PyramidMapMatrix: [Reduce] The new level id is illegal.";
1494 return;
1495 }
1496
1497 if (cells->rows_mr_[level] != new_cells.rows_mr_[new_level] ||
1498 cells->cols_mr_[level] != new_cells.cols_mr_[new_level]) {
1499 return;
1500 }
1501
1502 for (unsigned int r = 0; r < cells->rows_mr_[level]; r++) {
1503 for (unsigned int c = 0; c < cells->cols_mr_[level]; c++) {
1504 const float* intensity = new_cells.GetIntensitySafe(r, c, new_level);
1505 const float* intensity_var =
1506 new_cells.GetIntensityVarSafe(r, c, new_level);
1507 const float* altitude = new_cells.GetAltitudeSafe(r, c, new_level);
1508 const float* altitude_var = new_cells.GetAltitudeVarSafe(r, c, new_level);
1509 const float* ground_altitude =
1510 new_cells.GetGroundAltitudeSafe(r, c, new_level);
1511 const unsigned int* count = new_cells.GetCountSafe(r, c, new_level);
1512 const unsigned int* ground_count =
1513 new_cells.GetGroundCountSafe(r, c, new_level);
1514
1515 cells->MergeCellSafe(intensity, intensity_var, altitude, altitude_var,
1516 ground_altitude, count, ground_count, r, c, level);
1517 }
1518 }
1519}
1520
1521} // namespace pyramid_map
1522} // namespace msf
1523} // namespace localization
1524} // namespace apollo
T GetMaxY() const
Get the max y of the rectangle.
Definition rect2d.h:88
T GetMaxX() const
Get the max x of the rectangle.
Definition rect2d.h:83
T GetMinX() const
Get the min x of the rectangle.
Definition rect2d.h:73
T GetMinY() const
Get the min y of the rectangle.
Definition rect2d.h:78
unsigned int map_node_size_y_
The map node size in pixels.
unsigned int map_node_size_x_
The map node size in pixels.
const unsigned int * GetGroundCountSafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get a ground count value by check.
void SetValueSafe(unsigned char intensity, float altitude, unsigned int row, unsigned int col, unsigned int level=0)
Set the several values by check.
virtual bool GetAltitudeImg(cv::Mat *altitude_img) const
get altitude image of node.
void SetUintMatrixRoi(const UIntMatrix *source_matrix, const Rect2D< unsigned int > &source_roi, const Rect2D< unsigned int > &target_roi, unsigned int type, unsigned int level=0)
set unsigned int matrix with a ROI.
PyramidMapMatrix & operator=(const PyramidMapMatrix &map_matrix)
const float * GetIntensityVarSafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get an intensity variance value by check.
const float * GetIntensitySafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get an intensity value by check.
void SetGroundAltitudeSafe(float ground_altitude, unsigned int row, unsigned int col, unsigned int level=0)
Set an altitude ground value by check.
virtual bool GetIntensityImg(cv::Mat *intensity_img) const
get intensity image of node.
void BottomUpSafe()
Propagate the data from fine level to the coarse resolution by check.
void BottomUpBase()
Propagate the data from fine level to the coarse resolution by check.
const float * GetAltitudeSafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get an altitude value by check.
void SetAltitudeVarMatrix(const float *input, unsigned int size, unsigned int start_index, unsigned int level=0)
void SetIntensityMatrix(const float *input, unsigned int size, unsigned int start_index, unsigned int level=0)
void GetMapCellBase(float **intensity, float **intensity_var, float **altitude, unsigned int **count, unsigned int row, unsigned int col, unsigned int level=0)
Get cell values.
double ComputeMeanIntensity(unsigned int level=0)
Compute mean intensity.
void SetAltitudeSafe(float altitude, unsigned int row, unsigned int col, unsigned int level=0)
Set an altitude value by check.
void SetCountSafe(unsigned int count, unsigned int row, unsigned int col, unsigned int level=0)
Set a count value by check.
void SetGroundCountMatrix(const unsigned int *input, unsigned int size, unsigned int start_index, unsigned int level=0)
void Clear()
Release all memory in PyramidMapMatrix.
void MergeCellBase(const float *intensity, const float *intensity_var, const float *altitude, const unsigned int *count, unsigned int row, unsigned int col, unsigned int level)
Merge the data from another map cell.
const unsigned int * GetCountSafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get a count value by check.
void SetIntensityVarSafe(float intensity_var, unsigned int row, unsigned int col, unsigned int level=0)
Set an intensity variance value by check.
const float * GetAltitudeVarSafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get an altitude variance value by check.
void SetFloatMatrixRoi(const FloatMatrix *source_matrix, const Rect2D< unsigned int > &source_roi, const Rect2D< unsigned int > &target_roi, unsigned int type, unsigned int level=0)
set float matrix with a ROI.
FloatMatrix * GetIntensityVarMatrixSafe(unsigned int level=0)
void SetAltitudeMatrix(const float *input, unsigned int size, unsigned int start_index, unsigned int level=0)
void ResetCell(unsigned int id, unsigned int level=0)
Reset a map cell in a specific resolution level.
static void Reduce(std::shared_ptr< PyramidMapMatrix > cells, const PyramidMapMatrix &new_cells, unsigned int level=0, unsigned int new_level=0)
Combine two PyramidMapMatrix instances (Reduce).
void SetIntensityVarMatrix(const float *input, unsigned int size, unsigned int start_index, unsigned int level=0)
void SetGroundAltitudeMatrix(const float *input, unsigned int size, unsigned int start_index, unsigned int level=0)
FloatMatrix * GetGroundAltitudeMatrixSafe(unsigned int level=0)
const float * GetGroundAltitudeSafe(unsigned int row, unsigned int col, unsigned int level=0) const
Get an altitude ground value by check.
void AddGroundSample(float ground_altitude, unsigned int row, unsigned int col, unsigned int level=0)
Add ground sample to the map cell.
void AddSampleSafe(float intensity, float altitude, unsigned int row, unsigned int col, unsigned int level)
Add sample to the map cell with check.
void SetGroundCountSafe(unsigned int ground_count, unsigned int row, unsigned int col, unsigned int level=0)
Set a ground count value by check.
void SetIntensitySafe(float intensity, unsigned int row, unsigned int col, unsigned int level=0)
Set an intensity value by check.
void SetCountMatrix(const unsigned int *input, unsigned int size, unsigned int start_index, unsigned int level=0)
virtual void Init(const BaseMapConfig &config)
Initialize the map matrix.
void ResetCells(unsigned int start_id, unsigned int end_id, unsigned int level=0)
Reset map cells data from start_id to end_id in a specific resolution level
void SetAltitudeVarSafe(float altitude_var, unsigned int row, unsigned int col, unsigned int level=0)
Set an altitude variance value by check.
void GetMapCellSafe(float **intensity, float **intensity_var, float **altitude, float **altitude_var, float **ground_altitude, unsigned int **count, unsigned int **ground_count, unsigned int row, unsigned int col, unsigned int level=0)
Get cell values by check.
void MergeCellSafe(const float *intensity, const float *intensity_var, const float *altitude, const float *altitude_var, const float *ground_altitude, const unsigned int *count, const unsigned int *ground_count, unsigned int row, unsigned int col, unsigned int level)
Merge the data from another map cell by check.
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37