Apollo 10.0
自动驾驶开放平台
pyramid_map_matrix_handler.cc
浏览该文件的文档.
1
2/******************************************************************************
3 * Copyright 2019 The Apollo Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *****************************************************************************/
18
19#include <memory>
20#include "cyber/common/log.h"
21
22namespace apollo {
23namespace localization {
24namespace msf {
25namespace pyramid_map {
26// =================PyramidMapMatrixHandlerSelector=================
28
30
33 MapVersion version) {
34 switch (version) {
38 return new LosslessMapMatrixHandler();
44 default:
45 AINFO << "Unknown map version!";
46 }
47 return nullptr;
48}
49
50// =================LossyMapMatrixHandler=================
52
54
55unsigned char LossyMapMatrixHandler::EncodeIntensity(float intensity) const {
56 unsigned char encoded_intensity = 0;
57 if (intensity > 255) {
58 encoded_intensity = 255;
59 } else if (intensity < 0) {
60 encoded_intensity = 0;
61 } else {
62 encoded_intensity = static_cast<unsigned char>(intensity);
63 }
64 return encoded_intensity;
65}
66
68 float* intensity) const {
69 *intensity = static_cast<float>(data);
70}
71
73 var = std::sqrt(var);
74 unsigned int encoded_var =
75 static_cast<unsigned int>(static_cast<float>(var_range_) /
76 (var * static_cast<float>(var_ratio_) + 1.0));
77 if (encoded_var > var_range_) {
78 encoded_var = var_range_;
79 }
80 if (encoded_var < 1) {
81 encoded_var = 1;
82 }
83 return static_cast<uint16_t>(encoded_var);
84}
85
87 float* var) const {
88 *var = static_cast<float>(data);
89 *var = (static_cast<float>(var_range_) / (*var) - 1.0f) /
90 static_cast<float>(var_ratio_);
91 *var = (*var) * (*var);
92}
93
95 float min_altitude,
96 float altitude_interval) const {
97 float delta_alt = altitude - min_altitude;
98 delta_alt /= altitude_interval;
99 int encoded_altitude = static_cast<int>(delta_alt + 0.5f);
100 if (encoded_altitude > 0xffff) {
101 encoded_altitude = 0xffff;
102 }
103 if (encoded_altitude < 0) {
104 encoded_altitude = 0;
105 }
106 return static_cast<uint16_t>(encoded_altitude);
107}
108
109void LossyMapMatrixHandler::DecodeAltitude(uint16_t data, float min_altitude,
110 float altitude_interval,
111 float* altitude) const {
112 *altitude = min_altitude + data * altitude_interval;
113}
114
116 unsigned int count, unsigned int count_range) const {
117 unsigned int encoded_count = 0;
118 while (count > 0) {
119 ++encoded_count;
120 count /= 2;
121 }
122 if (encoded_count > count_range) {
123 encoded_count = count_range;
124 }
125 return static_cast<unsigned char>(encoded_count);
126}
127
129 unsigned int* count) const {
130 if (data == 0) {
131 *count = data;
132 } else {
133 *count = 1 << (data - 1);
134 }
135}
136
137// =================LossyMapFullAltMatrixHandler=================
139
141
143 const unsigned char* buf, std::shared_ptr<BaseMapMatrix> base_matrix) {
144 std::shared_ptr<PyramidMapMatrix> matrix =
145 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
146
147 size_t binary_size = sizeof(unsigned int) * 2;
148 const unsigned int* uint_p = reinterpret_cast<const unsigned int*>(buf);
149 unsigned int rows = *uint_p; // rows in first level
150 ++uint_p;
151 unsigned int cols = *uint_p; // cols in first level
152 ++uint_p;
153
154 // reset or init matrix
155 if ( // matrix->get_resolution_num() == 1 &&
156 // matrix->get_resolution_ratio() == 2 &&
157 rows == matrix->GetRows() && cols == matrix->GetCols() &&
158 matrix->HasIntensity() && matrix->HasIntensityVar() &&
159 matrix->HasAltitude() && matrix->HasGroundAltitude() &&
160 matrix->HasCount()) {
161 matrix->Reset();
162 } else {
163 matrix->Init(rows, cols, true, true, true, false, true, true, false);
164 }
165
166 // alt min max & ground alt min max
167 binary_size += sizeof(float) * 4;
168 const float* float_p =
169 reinterpret_cast<const float*>(reinterpret_cast<const void*>(uint_p));
170 alt_avg_min_ = *float_p;
171 ++float_p;
172 alt_avg_max_ = *float_p;
173 ++float_p;
174 ground_alt_min_ = *float_p;
175 ++float_p;
176 ground_alt_max_ = *float_p;
177 ++float_p;
178
179 // load matrix
180 const unsigned char* uc_p = reinterpret_cast<const unsigned char*>(float_p);
181 unsigned int matrix_size = rows * cols;
182
183 if (matrix->HasCount()) {
184 binary_size += sizeof(unsigned char) * matrix_size;
185 UIntMatrix* count_matrix = matrix->GetCountMatrixSafe();
186 for (unsigned int r = 0; r < rows; ++r) {
187 for (unsigned int c = 0; c < cols; ++c) {
188 DecodeCount(uc_p[r * cols + c], &(*count_matrix)[r][c]);
189 }
190 }
191 uc_p += matrix_size;
192 }
193
194 if (matrix->HasIntensity()) {
195 binary_size += sizeof(unsigned char) * matrix_size;
196 FloatMatrix* intensity_matrix = matrix->GetIntensityMatrixSafe();
197 for (unsigned int r = 0; r < rows; ++r) {
198 for (unsigned int c = 0; c < cols; ++c) {
199 DecodeIntensity(uc_p[r * cols + c], &(*intensity_matrix)[r][c]);
200 }
201 }
202 uc_p += matrix_size;
203 }
204
205 if (matrix->HasIntensityVar()) {
206 binary_size += sizeof(uint16_t) * matrix_size;
207 FloatMatrix* intensity_var_matrix = matrix->GetIntensityVarMatrixSafe();
208 const unsigned char* p_low = uc_p + matrix_size;
209 const unsigned char* p_high = uc_p;
210 for (unsigned int r = 0; r < rows; ++r) {
211 for (unsigned int c = 0; c < cols; ++c) {
212 unsigned int var = static_cast<unsigned int>(p_high[r * cols + c]);
213 var = var * 256u + static_cast<unsigned int>(p_low[r * cols + c]);
214 DecodeIntensityVar(static_cast<uint16_t>(var),
215 &(*intensity_var_matrix)[r][c]);
216 }
217 }
218 uc_p += 2 * matrix_size;
219 }
220
221 if (matrix->HasAltitude()) {
222 binary_size += sizeof(uint16_t) * matrix_size;
223 FloatMatrix* altitude_matrix = matrix->GetAltitudeMatrixSafe();
224 const unsigned char* p_low = uc_p + matrix_size;
225 const unsigned char* p_high = uc_p;
226 for (unsigned int r = 0; r < rows; ++r) {
227 for (unsigned int c = 0; c < cols; ++c) {
228 unsigned int alt = static_cast<unsigned int>(p_high[r * cols + c]);
229 alt = alt * 256u + static_cast<unsigned int>(p_low[r * cols + c]);
230 DecodeAltitude(static_cast<uint16_t>(alt), alt_avg_min_,
231 alt_avg_interval_, &(*altitude_matrix)[r][c]);
232 }
233 }
234 uc_p += 2 * matrix_size;
235 }
236
237 if (matrix->HasGroundAltitude()) {
238 binary_size += sizeof(uint16_t) * matrix_size;
239 FloatMatrix* ground_altitude_matrix = matrix->GetGroundAltitudeMatrixSafe();
240 const unsigned char* p_low = uc_p + matrix_size;
241 const unsigned char* p_high = uc_p;
242 for (unsigned int r = 0; r < rows; ++r) {
243 for (unsigned int c = 0; c < cols; ++c) {
244 unsigned int alt = static_cast<unsigned int>(p_high[r * cols + c]);
245 alt = alt * 256u + static_cast<unsigned int>(p_low[r * cols + c]);
246 DecodeAltitude(static_cast<uint16_t>(alt), ground_alt_min_,
247 ground_alt_interval_, &(*ground_altitude_matrix)[r][c]);
248 }
249 }
250 uc_p += 2 * matrix_size;
251 }
252
253 return binary_size;
254}
255
257 const std::shared_ptr<BaseMapMatrix> base_matrix, unsigned char* buf,
258 size_t buf_size) {
259 const std::shared_ptr<PyramidMapMatrix> matrix =
260 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
261
262 size_t target_size = GetBinarySize(matrix);
263 if (buf_size >= target_size) {
264 unsigned int rows = matrix->GetRowsSafe();
265 unsigned int cols = matrix->GetColsSafe();
266 unsigned int matrix_size = rows * cols;
267
268 unsigned int* uint_p = reinterpret_cast<unsigned int*>(buf);
269 *uint_p = rows;
270 ++uint_p;
271 *uint_p = cols;
272 ++uint_p;
273 // buf_size -= sizeof(unsigned int) * 2;
274
275 float* float_p = reinterpret_cast<float*>(reinterpret_cast<void*>(uint_p));
276 if (matrix->HasAltitude() && matrix->HasCount()) {
277 alt_avg_min_ = 1e8;
278 alt_avg_max_ = -1e8;
279 for (unsigned int y = 0; y < rows; ++y) {
280 for (unsigned int x = 0; x < cols; ++x) {
281 const float* altitude = matrix->GetAltitudeSafe(y, x);
282 const unsigned int* count = matrix->GetCountSafe(y, x);
283 if (*count == 0) {
284 continue;
285 }
286
287 if (*altitude < alt_avg_min_) {
288 alt_avg_min_ = *altitude;
289 }
290 if (*altitude > alt_avg_max_) {
291 alt_avg_max_ = *altitude;
292 }
293 }
294 }
295 } else {
296 alt_avg_min_ = 0.0;
297 alt_avg_max_ = 0.0;
298 }
299
300 if (matrix->HasGroundAltitude() && matrix->HasGroundCount()) {
301 ground_alt_min_ = 1e8;
302 ground_alt_max_ = -1e8;
303 for (unsigned int y = 0; y < rows; ++y) {
304 for (unsigned int x = 0; x < cols; ++x) {
305 const float* ground_altitude = matrix->GetGroundAltitudeSafe(y, x);
306 const unsigned int* ground_count = matrix->GetGroundCountSafe(y, x);
307 if (*ground_count == 0) {
308 continue;
309 }
310 if (*ground_altitude < ground_alt_min_) {
311 ground_alt_min_ = *ground_altitude;
312 }
313 if (*ground_altitude > ground_alt_max_) {
314 ground_alt_max_ = *ground_altitude;
315 }
316 }
317 }
318 } else {
319 ground_alt_min_ = 0.0;
320 ground_alt_max_ = 0.0;
321 }
322
323 *float_p = alt_avg_min_;
324 ++float_p;
325 *float_p = alt_avg_max_;
326 ++float_p;
327 *float_p = ground_alt_min_;
328 ++float_p;
329 *float_p = ground_alt_max_;
330 ++float_p;
331 // buf_size -= sizeof(float) * 4;
332
333 unsigned char* uc_p = reinterpret_cast<unsigned char*>(float_p);
334 // unsigned int processed_size = 0;
335
336 // count
337 for (unsigned int r = 0; r < rows; ++r) {
338 for (unsigned int c = 0; c < cols; ++c) {
339 const unsigned int* count = matrix->GetCountSafe(r, c);
340 unsigned int ct = (count != nullptr) ? *count : 0;
341 uc_p[r * cols + c] = EncodeCount(ct, count_range_);
342 }
343 }
344 uc_p += matrix_size;
345 // processed_size += matrix_size * sizeof(unsigned char);
346
347 // intensity
348 for (unsigned int r = 0; r < rows; ++r) {
349 for (unsigned int c = 0; c < cols; ++c) {
350 const float* intensity =
351 static_cast<const float*>(matrix->GetIntensitySafe(r, c));
352 float ity = (intensity != nullptr) ? *intensity : 0.0f;
353 uc_p[r * cols + c] = EncodeIntensity(ity);
354 }
355 }
356 uc_p += matrix_size;
357 // processed_size += matrix_size * sizeof(unsigned char);
358
359 // intensiy
360 unsigned char* p_low = uc_p + matrix_size;
361 unsigned char* p_high = uc_p;
362 for (unsigned int r = 0; r < rows; ++r) {
363 for (unsigned int c = 0; c < cols; ++c) {
364 const float* intensity_var =
365 static_cast<const float*>(matrix->GetIntensityVarSafe(r, c));
366 float iv = (intensity_var != nullptr) ? *intensity_var : 0.0f;
367 uint16_t var = EncodeIntensityVar(iv);
368 p_high[r * cols + c] = static_cast<uint8_t>(var / 256);
369 p_low[r * cols + c] = static_cast<uint8_t>(var % 256);
370 }
371 }
372 uc_p += 2 * matrix_size;
373 // processed_size += matrix_size * sizeof(uint16_t);
374
375 // altitude
376 p_low = uc_p + matrix_size;
377 p_high = uc_p;
378 for (unsigned int r = 0; r < rows; ++r) {
379 for (unsigned int c = 0; c < cols; ++c) {
380 const float* altitude = matrix->GetAltitudeSafe(r, c);
381 float a = (altitude != nullptr) ? *altitude : 0.0f;
383 p_high[r * cols + c] = static_cast<unsigned char>(alt / 256);
384 p_low[r * cols + c] = static_cast<unsigned char>(alt % 256);
385 }
386 }
387 uc_p += 2 * matrix_size;
388 // processed_size += matrix_size * sizeof(uint16_t);
389
390 // ground altitude
391 p_low = uc_p + matrix_size;
392 p_high = uc_p;
393 for (unsigned int r = 0; r < rows; ++r) {
394 for (unsigned int c = 0; c < cols; ++c) {
395 const float* ground_altitude = matrix->GetGroundAltitudeSafe(r, c);
396 float ga = (ground_altitude != nullptr) ? *ground_altitude : 0.0f;
397 uint16_t alt =
399 p_high[r * cols + c] = static_cast<unsigned char>(alt / 256);
400 p_low[r * cols + c] = static_cast<unsigned char>(alt % 256);
401 }
402 }
403 uc_p += 2 * matrix_size;
404 // processed_size += matrix_size * sizeof(uint16_t);
405
406 // assert(buf_size >= processed_size);
407 // buf_size -= processed_size;
408 return target_size;
409 }
410
411 return 0;
412}
413
415 const std::shared_ptr<BaseMapMatrix> base_matrix) {
416 const std::shared_ptr<PyramidMapMatrix> matrix =
417 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
418 // assert(matrix->get_resolution_num() > 0);
419 if (matrix->GetResolutionNum() == 0) {
420 throw "[LossyMapFullAltMatrixHandler::get_binary_size]"
421 "matrix->get_resolution_num() == 0";
422 }
423
424 // rows and cols
425 // altitude min max & ground altitude min max
426 size_t target_size = sizeof(unsigned int) * 2 + sizeof(float) * 4;
427 // count, intensity, intensity_var, altitude_avg, altitude_ground
428 target_size += matrix->GetRowsSafe() * matrix->GetColsSafe() *
429 (sizeof(unsigned char) + sizeof(unsigned char) +
430 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t));
431 return target_size;
432}
433
434// =================LosslessMapMatrixHandler====================
436
438
440 const unsigned char* buf, std::shared_ptr<BaseMapMatrix> base_matrix) {
441 std::shared_ptr<PyramidMapMatrix> matrix =
442 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
443
444 size_t binary_size = sizeof(unsigned int) * 2; // rows and cols
445
446 const unsigned int* uint_p = reinterpret_cast<const unsigned int*>(buf);
447 unsigned int rows = *uint_p;
448 ++uint_p;
449 unsigned int cols = *uint_p;
450 ++uint_p;
451
452 // reset or init matrix
453 if ( // matrix->get_resolution_num() == 1 &&
454 // matrix->get_resolution_ratio() == 2 &&
455 rows == matrix->GetRowsSafe() && cols == matrix->GetColsSafe() &&
456 matrix->HasIntensity() && matrix->HasIntensityVar() &&
457 matrix->HasAltitude() && matrix->HasAltitudeVar() &&
458 matrix->HasGroundAltitude() && matrix->HasCount() &&
459 matrix->HasGroundCount()) {
460 matrix->Reset();
461 } else {
462 matrix->Init(rows, cols);
463 }
464
465 // load matrix
466 for (unsigned int y = 0; y < rows; y++) {
467 for (unsigned int x = 0; x < cols; x++) {
468 binary_size += sizeof(unsigned int); // layer
469
470 unsigned int size = *uint_p;
471 ++uint_p;
472
473 for (unsigned int i = 0; i < size; i++) {
474 if (i == 0) { // all points layer
475 binary_size += sizeof(float) * 4 + sizeof(unsigned int);
476
477 const float* float_p = reinterpret_cast<const float*>(
478 reinterpret_cast<const void*>(uint_p));
479 matrix->SetIntensitySafe(*float_p, y, x);
480 ++float_p;
481 matrix->SetIntensityVarSafe(*float_p, y, x);
482 ++float_p;
483 matrix->SetAltitudeSafe(*float_p, y, x);
484 ++float_p;
485 matrix->SetAltitudeVarSafe(*float_p, y, x);
486 ++float_p;
487 uint_p = reinterpret_cast<const unsigned int*>(
488 reinterpret_cast<const void*>(float_p));
489 matrix->SetCountSafe(*uint_p, y, x);
490 ++uint_p;
491 } else if (i == 1) { // ground points layer
492 binary_size += sizeof(float) * 4 + sizeof(unsigned int);
493
494 const float* float_p = reinterpret_cast<const float*>(
495 reinterpret_cast<const void*>(uint_p));
496 ++float_p;
497 ++float_p;
498 matrix->SetGroundAltitudeSafe(*float_p, y, x);
499 ++float_p;
500 ++float_p;
501 uint_p = reinterpret_cast<const unsigned int*>(
502 reinterpret_cast<const void*>(float_p));
503 matrix->SetGroundCountSafe(*uint_p, y, x);
504 ++uint_p;
505 }
506 }
507 }
508 }
509
510 return binary_size;
511}
512
514 const std::shared_ptr<BaseMapMatrix> base_matrix, unsigned char* buf,
515 size_t buf_size) {
516 const std::shared_ptr<PyramidMapMatrix> matrix =
517 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
518
519 size_t target_size = GetBinarySize(matrix);
520 if (buf_size >= target_size) {
521 unsigned int rows = matrix->GetRowsSafe();
522 unsigned int cols = matrix->GetColsSafe();
523
524 unsigned int* uint_p = reinterpret_cast<unsigned int*>(buf);
525 *uint_p = rows;
526 ++uint_p;
527 *uint_p = cols;
528 ++uint_p;
529 // buf_size -= sizeof(unsigned int) * 2;
530 for (unsigned int y = 0; y < rows; y++) {
531 for (unsigned int x = 0; x < cols; x++) {
532 const unsigned int* ground_count = matrix->GetGroundCountSafe(y, x);
533 unsigned int layer_num = 0;
534 if (ground_count != NULL && *ground_count > 0) {
535 layer_num = 2;
536 *uint_p = 2;
537 ++uint_p;
538 } else {
539 layer_num = 1;
540 *uint_p = 1;
541 ++uint_p;
542 }
543
544 // buf_size -= sizeof(unsigned int);
545 for (unsigned int i = 0; i < layer_num; i++) {
546 if (i == 0) { // all points layer
547 const float* intensity = matrix->GetIntensitySafe(y, x);
548 const float* intensity_var = matrix->GetIntensityVarSafe(y, x);
549 const float* altitude = matrix->GetAltitudeSafe(y, x);
550 const float* altitude_var = matrix->GetAltitudeVarSafe(y, x);
551 const unsigned int* count = matrix->GetCountSafe(y, x);
552
553 float* float_p =
554 reinterpret_cast<float*>(reinterpret_cast<void*>(uint_p));
555 *float_p = (intensity != nullptr) ? *intensity : 0.0f;
556 ++float_p;
557 *float_p = (intensity_var != nullptr) ? *intensity_var : 0.0f;
558 ++float_p;
559 *float_p = (altitude != nullptr) ? *altitude : 0.0f;
560 ++float_p;
561 *float_p = (altitude_var != nullptr) ? *altitude_var : 0.0f;
562 ++float_p;
563 uint_p = reinterpret_cast<unsigned int*>(
564 reinterpret_cast<void*>(float_p));
565 *uint_p = (count != nullptr) ? *count : 0;
566 ++uint_p;
567 } else if (i == 1) { // ground points layer
568 const float* ground_altitude = matrix->GetGroundAltitudeSafe(y, x);
569 const unsigned int* ground_count = matrix->GetGroundCountSafe(y, x);
570
571 float* float_p =
572 reinterpret_cast<float*>(reinterpret_cast<void*>(uint_p));
573 *float_p = 0.0f;
574 ++float_p;
575 *float_p = 0.0f;
576 ++float_p;
577 *float_p = (ground_altitude != nullptr) ? *ground_altitude : 0.0f;
578 ++float_p;
579 *float_p = 0.0f;
580 ++float_p;
581 uint_p = reinterpret_cast<unsigned int*>(
582 reinterpret_cast<void*>(float_p));
583 *uint_p = (ground_count != nullptr) ? *ground_count : 0;
584 ++uint_p;
585 }
586
587 // unsigned int processed_size += sizeof(float) * 4 + sizeof(unsigned
588 // int); assert(buf_size >= processed_size); buf_size -=
589 // processed_size;
590 }
591 }
592 }
593
594 return target_size;
595 }
596
597 return 0;
598}
599
601 const std::shared_ptr<BaseMapMatrix> base_matrix) {
602 const std::shared_ptr<PyramidMapMatrix> matrix =
603 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
604 // assert(matrix->get_resolution_num() > 0);
605 if (matrix->GetResolutionNum() == 0) {
606 throw "[LosslessMapMatrixHandler::get_binary_size]"
607 "matrix->get_resolution_num() == 0";
608 }
609
610 size_t target_size = sizeof(unsigned int) * 2; // rows and cols
611 for (unsigned int y = 0; y < matrix->GetRowsSafe(); y++) {
612 for (unsigned int x = 0; x < matrix->GetColsSafe(); x++) {
613 target_size += sizeof(unsigned int); // layer
614
615 const unsigned int* ground_count = matrix->GetGroundCountSafe(y, x);
616 if (ground_count != NULL && *ground_count > 0) {
617 target_size += (sizeof(float) * 4 + sizeof(unsigned int)) * 2;
618 } else {
619 target_size += sizeof(float) * 4 + sizeof(unsigned int);
620 }
621 }
622 }
623
624 return target_size;
625}
626
627// =================PyramidLossyMapMatrixHandler====================
629
631
633 const unsigned char* buf, std::shared_ptr<BaseMapMatrix> base_matrix) {
634 std::shared_ptr<PyramidMapMatrix> matrix =
635 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
636
637 size_t binary_size = sizeof(unsigned int) * 4;
638 const unsigned int* uint_p =
639 reinterpret_cast<const unsigned int*>(buf); // resolution num
640 unsigned int resolution_num = *uint_p;
641 ++uint_p;
642 unsigned int ratio = *uint_p; // ratio
643 ++uint_p;
644 unsigned int rows = *uint_p; // rows in first level
645 ++uint_p;
646 unsigned int cols = *uint_p; // cols in first level
647 ++uint_p;
648
649 // set has_*
650 binary_size += sizeof(unsigned char) * 4;
651 const unsigned char* uc_p = reinterpret_cast<const unsigned char*>(uint_p);
652 ++uc_p;
653 ++uc_p;
654 ++uc_p;
655 unsigned char has_flag = *uc_p;
656 ++uc_p;
657 bool has_intensity = (has_flag & 1);
658 bool has_intensity_var = (has_flag & 2);
659 bool has_altitude = (has_flag & 4);
660 bool has_altitude_var = (has_flag & 8);
661 bool has_ground_altitude = (has_flag & 16);
662 bool has_count = (has_flag & 32);
663 bool has_ground_count = (has_flag & 64);
664
665 // reset or init matrix
666 if (resolution_num == matrix->GetResolutionNum() &&
667 ratio == matrix->GetResolutionRatio() && rows == matrix->GetRowsSafe() &&
668 cols == matrix->GetColsSafe() &&
669 has_intensity == matrix->HasIntensity() &&
670 has_intensity_var == matrix->HasIntensityVar() &&
671 has_altitude == matrix->HasAltitude() &&
672 has_altitude_var == matrix->HasAltitudeVar() &&
673 has_ground_altitude == matrix->HasGroundAltitude() &&
674 has_count == matrix->HasCount() &&
675 has_ground_count == matrix->HasGroundCount()) {
676 matrix->Reset();
677 } else {
678 matrix->Init(rows, cols, has_intensity, has_intensity_var, has_altitude,
679 has_altitude_var, has_ground_altitude, has_count,
680 has_ground_count, resolution_num, ratio);
681 }
682
683 // alt min & ground alt min
684 binary_size += sizeof(float) * 2;
685 const float* float_p =
686 reinterpret_cast<const float*>(reinterpret_cast<const void*>(uc_p));
687 alt_avg_min_ = *float_p;
688 ++float_p;
689 ground_alt_min_ = *float_p;
690 ++float_p;
691
692 // load matrix
693 uc_p = reinterpret_cast<const unsigned char*>(float_p);
694 for (unsigned int l = 0; l < resolution_num; ++l) {
695 unsigned int rows = matrix->GetRowsSafe(l);
696 unsigned int cols = matrix->GetColsSafe(l);
697 unsigned int matrix_size = matrix->GetRowsSafe(l) * matrix->GetColsSafe(l);
698
699 if (matrix->HasCount()) {
700 binary_size += sizeof(unsigned char) * matrix_size;
701 UIntMatrix* count_matrix = matrix->GetCountMatrixSafe(l);
702 for (unsigned int r = 0; r < rows; ++r) {
703 for (unsigned int c = 0; c < cols; ++c) {
704 DecodeCount(uc_p[r * cols + c], &(*count_matrix)[r][c]);
705 }
706 }
707 uc_p += matrix_size;
708 }
709 if (matrix->HasGroundCount()) {
710 binary_size += sizeof(unsigned char) * matrix_size;
711 UIntMatrix* ground_count_matrix = matrix->GetGroundCountMatrixSafe(l);
712 for (unsigned int r = 0; r < rows; ++r) {
713 for (unsigned int c = 0; c < cols; ++c) {
714 DecodeCount(uc_p[r * cols + c], &(*ground_count_matrix)[r][c]);
715 }
716 }
717 uc_p += matrix_size;
718 }
719
720 if (matrix->HasIntensity()) {
721 binary_size += sizeof(unsigned char) * matrix_size;
722 FloatMatrix* intensity_matrix = matrix->GetIntensityMatrixSafe(l);
723 for (unsigned int r = 0; r < rows; ++r) {
724 for (unsigned int c = 0; c < cols; ++c) {
725 DecodeIntensity(uc_p[r * cols + c], &(*intensity_matrix)[r][c]);
726 }
727 }
728 uc_p += matrix_size;
729 }
730
731 if (matrix->HasIntensityVar()) {
732 binary_size += sizeof(uint16_t) * matrix_size;
733 FloatMatrix* intensity_var_matrix = matrix->GetIntensityVarMatrixSafe(l);
734 const unsigned char* p_low = uc_p + matrix_size;
735 const unsigned char* p_high = uc_p;
736 for (unsigned int r = 0; r < rows; ++r) {
737 for (unsigned int c = 0; c < cols; ++c) {
738 uint16_t var = p_high[r * cols + c];
739 var = static_cast<uint16_t>(var * 256 + p_low[r * cols + c]);
740 DecodeIntensityVar(var, &(*intensity_var_matrix)[r][c]);
741 }
742 }
743 uc_p += 2 * matrix_size;
744 }
745
746 if (matrix->HasAltitude()) {
747 binary_size += sizeof(uint16_t) * matrix_size;
748 FloatMatrix* altitude_matrix = matrix->GetAltitudeMatrixSafe(l);
749 const unsigned char* p_low = uc_p + matrix_size;
750 const unsigned char* p_high = uc_p;
751 for (unsigned int r = 0; r < rows; ++r) {
752 for (unsigned int c = 0; c < cols; ++c) {
753 uint16_t alt = p_high[r * cols + c];
754 alt = static_cast<uint16_t>(alt * 256 + p_low[r * cols + c]);
756 &(*altitude_matrix)[r][c]);
757 }
758 }
759 uc_p += 2 * matrix_size;
760 }
761
762 if (matrix->HasAltitudeVar()) {
763 // skip
764 binary_size += sizeof(uint16_t) * matrix_size;
765 uc_p += 2 * matrix_size;
766 }
767
768 if (matrix->HasGroundAltitude()) {
769 binary_size += sizeof(uint16_t) * matrix_size;
770 FloatMatrix* ground_altitude_matrix =
771 matrix->GetGroundAltitudeMatrixSafe(l);
772 const unsigned char* p_low = uc_p + matrix_size;
773 const unsigned char* p_high = uc_p;
774 for (unsigned int r = 0; r < rows; ++r) {
775 for (unsigned int c = 0; c < cols; ++c) {
776 uint16_t alt = p_high[r * cols + c];
777 alt = static_cast<uint16_t>(alt * 256 + p_low[r * cols + c]);
779 &(*ground_altitude_matrix)[r][c]);
780 }
781 }
782 uc_p += 2 * matrix_size;
783 }
784 }
785
786 return binary_size;
787}
788
790 const std::shared_ptr<BaseMapMatrix> base_matrix, unsigned char* buf,
791 size_t buf_size) {
792 const std::shared_ptr<PyramidMapMatrix> matrix =
793 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
794
795 size_t target_size = GetBinarySize(matrix);
796 if (buf_size >= target_size) {
797 unsigned int resolution_num = matrix->GetResolutionNum();
798 unsigned int ratio = matrix->GetResolutionRatio();
799 unsigned int rows = matrix->GetRowsSafe();
800 unsigned int cols = matrix->GetColsSafe();
801
802 unsigned int* uint_p =
803 reinterpret_cast<unsigned int*>(buf); // resolution num
804 *uint_p = resolution_num;
805 ++uint_p;
806 *uint_p = ratio;
807 ++uint_p;
808 *uint_p = rows; // rows in first level
809 ++uint_p;
810 *uint_p = cols; // cols in first level
811 ++uint_p;
812 // buf_size -= sizeof(unsigned int) * 4;
813
814 // set has_*
815 unsigned char* uc_p = reinterpret_cast<unsigned char*>(uint_p);
816 *uc_p = 0;
817 ++uc_p;
818 *uc_p = 0;
819 ++uc_p;
820 *uc_p = 0;
821 ++uc_p;
822 *uc_p = 0;
823 if (matrix->HasIntensity()) {
824 *uc_p |= 1;
825 }
826 if (matrix->HasIntensityVar()) {
827 *uc_p |= 2;
828 }
829 if (matrix->HasAltitude()) {
830 *uc_p |= 4;
831 }
832 if (matrix->HasAltitudeVar()) {
833 *uc_p |= 8;
834 }
835 if (matrix->HasGroundAltitude()) {
836 *uc_p |= 16;
837 }
838 if (matrix->HasCount()) {
839 *uc_p |= 32;
840 }
841 if (matrix->HasGroundCount()) {
842 *uc_p |= 64;
843 }
844 ++uc_p;
845 // buf_size -= sizeof(unsigned char) * 4;
846
847 // altitude min
848 float* float_p = reinterpret_cast<float*>(reinterpret_cast<void*>(uc_p));
849 if (matrix->HasAltitude() && matrix->HasCount()) {
850 alt_avg_min_ = 1e8;
851 for (unsigned int y = 0; y < rows; ++y) {
852 for (unsigned int x = 0; x < cols; ++x) {
853 const float* altitude = matrix->GetAltitudeSafe(y, x);
854 const unsigned int* count = matrix->GetCountSafe(y, x);
855 if (*count == 0) {
856 continue;
857 }
858 if (*altitude < alt_avg_min_) {
859 alt_avg_min_ = *altitude;
860 }
861 }
862 }
863 } else {
864 alt_avg_min_ = 0.0;
865 }
866
867 if (matrix->HasGroundAltitude() && matrix->HasGroundCount()) {
868 ground_alt_min_ = 1e8;
869 for (unsigned int y = 0; y < rows; ++y) {
870 for (unsigned int x = 0; x < cols; ++x) {
871 const float* ground_altitude = matrix->GetGroundAltitudeSafe(y, x);
872 const unsigned int* ground_count = matrix->GetGroundCountSafe(y, x);
873 if (*ground_count == 0) {
874 continue;
875 }
876 if (*ground_altitude < ground_alt_min_) {
877 ground_alt_min_ = *ground_altitude;
878 }
879 }
880 }
881 } else {
882 ground_alt_min_ = 0.0;
883 }
884
885 *float_p = alt_avg_min_;
886 ++float_p;
887 *float_p = ground_alt_min_;
888 ++float_p;
889 // buf_size -= sizeof(float)*2;
890
891 uc_p = reinterpret_cast<unsigned char*>(float_p);
892 for (unsigned int l = 0; l < resolution_num; ++l) {
893 // unsigned int processed_size = 0;
894 unsigned int rows = matrix->GetRowsSafe(l);
895 unsigned int cols = matrix->GetColsSafe(l);
896 unsigned int matrix_size = rows * cols;
897
898 if (matrix->HasCount()) {
899 for (unsigned int r = 0; r < rows; ++r) {
900 for (unsigned int c = 0; c < cols; ++c) {
901 const unsigned int* count = matrix->GetCountSafe(r, c, l);
902 uc_p[r * cols + c] = EncodeCount(*count, count_range_);
903 }
904 }
905 uc_p += matrix_size;
906 // processed_size += matrix_size * sizeof(unsigned char);
907 }
908
909 if (matrix->HasGroundCount()) {
910 for (unsigned int r = 0; r < rows; ++r) {
911 for (unsigned int c = 0; c < cols; ++c) {
912 const unsigned int* ground_count =
913 matrix->GetGroundCountSafe(r, c, l);
914 uc_p[r * cols + c] = EncodeCount(*ground_count, count_range_);
915 }
916 }
917 uc_p += matrix_size;
918 // processed_size += matrix_size * sizeof(unsigned char);
919 }
920
921 if (matrix->HasIntensity()) {
922 for (unsigned int r = 0; r < rows; ++r) {
923 for (unsigned int c = 0; c < cols; ++c) {
924 const float* intensity = matrix->GetIntensitySafe(r, c, l);
925 uc_p[r * cols + c] = EncodeIntensity(*intensity);
926 }
927 }
928 uc_p += matrix_size;
929 // processed_size += matrix_size * sizeof(unsigned char);
930 }
931
932 if (matrix->HasIntensityVar()) {
933 unsigned char* p_low = uc_p + matrix_size;
934 unsigned char* p_high = uc_p;
935 for (unsigned int r = 0; r < rows; ++r) {
936 for (unsigned int c = 0; c < cols; ++c) {
937 const float* intensity_var = matrix->GetIntensityVarSafe(r, c, l);
938 uint16_t var = EncodeIntensityVar(*intensity_var);
939 p_high[r * cols + c] = static_cast<unsigned char>(var / 256);
940 p_low[r * cols + c] = static_cast<unsigned char>(var % 256);
941 }
942 }
943 uc_p += 2 * matrix_size;
944 // processed_size += matrix_size * sizeof(uint16_t);
945 }
946
947 if (matrix->HasAltitude()) {
948 unsigned char* p_low = uc_p + matrix_size;
949 unsigned char* p_high = uc_p;
950 for (unsigned int r = 0; r < rows; ++r) {
951 for (unsigned int c = 0; c < cols; ++c) {
952 const float* altitude = matrix->GetAltitudeSafe(r, c, l);
953 uint16_t alt =
955 p_high[r * cols + c] = static_cast<unsigned char>(alt / 256);
956 p_low[r * cols + c] = static_cast<unsigned char>(alt % 256);
957 }
958 }
959 uc_p += 2 * matrix_size;
960 // processed_size += matrix_size * sizeof(uint16_t);
961 }
962 if (matrix->HasAltitudeVar()) {
963 // default 0
964 memset(uc_p, 0, 2 * matrix_size * sizeof(unsigned char));
965 uc_p += 2 * matrix_size;
966 }
967
968 if (matrix->HasGroundAltitude()) {
969 unsigned char* p_low = uc_p + matrix_size;
970 unsigned char* p_high = uc_p;
971 for (unsigned int r = 0; r < rows; ++r) {
972 for (unsigned int c = 0; c < cols; ++c) {
973 const float* ground_altitude =
974 matrix->GetGroundAltitudeSafe(r, c, l);
975 uint16_t alt = EncodeAltitude(*ground_altitude, ground_alt_min_,
977 p_high[r * cols + c] = static_cast<unsigned char>(alt / 256);
978 p_low[r * cols + c] = static_cast<unsigned char>(alt % 256);
979 }
980 }
981 uc_p += 2 * matrix_size;
982 // processed_size += matrix_size * sizeof(uint16_t);
983 }
984
985 // assert(buf_size >= processed_size);
986 // buf_size -= processed_size;
987 }
988
989 return target_size;
990 }
991
992 return 0;
993}
994
996 const std::shared_ptr<BaseMapMatrix> base_matrix) {
997 const std::shared_ptr<PyramidMapMatrix> matrix =
998 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
999
1000 unsigned int resolution_num = matrix->GetResolutionNum();
1001 // assert(resolution_num > 0);
1002 if (resolution_num == 0) {
1003 throw "[PyramidLossyMapMatrixHandler::get_binary_size] resolution_num == 0";
1004 }
1005
1006 // resolution_num and ratio
1007 // rows and cols in first level
1008 // space for has_*
1009 // altitude min & ground altitude min
1010 size_t target_size = sizeof(unsigned int) * 2 + sizeof(unsigned int) * 2 +
1011 sizeof(unsigned char) * 4 + sizeof(float) * 2;
1012
1013 for (unsigned int l = 0; l < resolution_num; ++l) {
1014 unsigned int matrix_size = matrix->GetRowsSafe(l) * matrix->GetColsSafe(l);
1015 if (matrix->HasCount()) {
1016 target_size += sizeof(unsigned char) * matrix_size;
1017 }
1018 if (matrix->HasGroundCount()) {
1019 target_size += sizeof(unsigned char) * matrix_size;
1020 }
1021 if (matrix->HasIntensity()) {
1022 target_size += sizeof(unsigned char) * matrix_size;
1023 }
1024 if (matrix->HasIntensityVar()) {
1025 target_size += sizeof(uint16_t) * matrix_size;
1026 }
1027 if (matrix->HasAltitude()) {
1028 target_size += sizeof(uint16_t) * matrix_size;
1029 }
1030 if (matrix->HasAltitudeVar()) {
1031 target_size += sizeof(uint16_t) * matrix_size;
1032 }
1033 if (matrix->HasGroundAltitude()) {
1034 target_size += sizeof(uint16_t) * matrix_size;
1035 }
1036 }
1037
1038 return target_size;
1039}
1040
1041// =================PyramidLosslessMapMatrixHandler====================
1043
1045
1047 const unsigned char* buf, std::shared_ptr<BaseMapMatrix> base_matrix) {
1048 std::shared_ptr<PyramidMapMatrix> matrix =
1049 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
1050
1051 size_t binary_size = sizeof(unsigned int) * 4;
1052 const unsigned int* uint_p =
1053 reinterpret_cast<const unsigned int*>(buf); // resolution num
1054 unsigned int resolution_num = *uint_p;
1055 ++uint_p;
1056 unsigned int ratio = *uint_p; // ratio
1057 ++uint_p;
1058 unsigned int rows = *uint_p; // rows in first level
1059 ++uint_p;
1060 unsigned int cols = *uint_p; // cols in first level
1061 ++uint_p;
1062
1063 // set has_*
1064 binary_size += sizeof(unsigned char) * 4;
1065 const unsigned char* uc_p = reinterpret_cast<const unsigned char*>(uint_p);
1066 ++uc_p;
1067 ++uc_p;
1068 ++uc_p;
1069 unsigned char has_flag = *uc_p;
1070 ++uc_p;
1071 bool has_intensity = (has_flag & 1);
1072 bool has_intensity_var = (has_flag & 2);
1073 bool has_altitude = (has_flag & 4);
1074 bool has_altitude_var = (has_flag & 8);
1075 bool has_ground_altitude = (has_flag & 16);
1076 bool has_count = (has_flag & 32);
1077 bool has_ground_count = (has_flag & 64);
1078
1079 // reset or init matrix
1080 if (resolution_num == matrix->GetResolutionNum() &&
1081 ratio == matrix->GetResolutionRatio() && rows == matrix->GetRowsSafe() &&
1082 cols == matrix->GetColsSafe() &&
1083 has_intensity == matrix->HasIntensity() &&
1084 has_intensity_var == matrix->HasIntensityVar() &&
1085 has_altitude == matrix->HasAltitude() &&
1086 has_altitude_var == matrix->HasAltitudeVar() &&
1087 has_ground_altitude == matrix->HasGroundAltitude() &&
1088 has_count == matrix->HasCount() &&
1089 has_ground_count == matrix->HasGroundCount()) {
1090 matrix->Reset();
1091 } else {
1092 matrix->Init(rows, cols, has_intensity, has_intensity_var, has_altitude,
1093 has_altitude_var, has_ground_altitude, has_count,
1094 has_ground_count, resolution_num, ratio);
1095 }
1096
1097 // load matrix
1098 const float* float_p =
1099 reinterpret_cast<const float*>(reinterpret_cast<const void*>(uc_p));
1100 for (unsigned int l = 0; l < resolution_num; ++l) {
1101 unsigned int matrix_size = matrix->GetRowsSafe(l) * matrix->GetColsSafe(l);
1102 if (matrix->HasIntensity()) {
1103 binary_size += sizeof(float) * matrix_size;
1104 matrix->SetIntensityMatrix(float_p, matrix_size, 0, l);
1105 float_p += matrix_size;
1106 }
1107 if (matrix->HasIntensityVar()) {
1108 binary_size += sizeof(float) * matrix_size;
1109 matrix->SetIntensityVarMatrix(float_p, matrix_size, 0, l);
1110 float_p += matrix_size;
1111 }
1112 if (matrix->HasAltitude()) {
1113 binary_size += sizeof(float) * matrix_size;
1114 matrix->SetAltitudeMatrix(float_p, matrix_size, 0, l);
1115 float_p += matrix_size;
1116 }
1117 if (matrix->HasAltitudeVar()) {
1118 binary_size += sizeof(float) * matrix_size;
1119 matrix->SetAltitudeVarMatrix(float_p, matrix_size, 0, l);
1120 float_p += matrix_size;
1121 }
1122 if (matrix->HasGroundAltitude()) {
1123 binary_size += sizeof(float) * matrix_size;
1124 matrix->SetGroundAltitudeMatrix(float_p, matrix_size, 0, l);
1125 float_p += matrix_size;
1126 }
1127
1128 uint_p = reinterpret_cast<const unsigned int*>(
1129 reinterpret_cast<const void*>(float_p));
1130 if (matrix->HasCount()) {
1131 binary_size += sizeof(unsigned int) * matrix_size;
1132 matrix->SetCountMatrix(uint_p, matrix_size, 0, l);
1133 uint_p += matrix_size;
1134 }
1135 if (matrix->HasGroundCount()) {
1136 binary_size += sizeof(unsigned int) * matrix_size;
1137 matrix->SetGroundCountMatrix(uint_p, matrix_size, 0, l);
1138 uint_p += matrix_size;
1139 }
1140 float_p =
1141 reinterpret_cast<const float*>(reinterpret_cast<const void*>(uint_p));
1142 }
1143
1144 return binary_size;
1145}
1146
1148 const std::shared_ptr<BaseMapMatrix> base_matrix, unsigned char* buf,
1149 size_t buf_size) {
1150 const std::shared_ptr<PyramidMapMatrix> matrix =
1151 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
1152
1153 size_t target_size = GetBinarySize(matrix);
1154 if (buf_size >= target_size) {
1155 unsigned int resolution_num = matrix->GetResolutionNum();
1156 unsigned int ratio = matrix->GetResolutionRatio();
1157 unsigned int rows = matrix->GetRowsSafe();
1158 unsigned int cols = matrix->GetColsSafe();
1159
1160 unsigned int* uint_p =
1161 reinterpret_cast<unsigned int*>(buf); // resolution num
1162 *uint_p = resolution_num;
1163 ++uint_p;
1164 *uint_p = ratio;
1165 ++uint_p;
1166 *uint_p = rows; // rows in first level
1167 ++uint_p;
1168 *uint_p = cols; // cols in first level
1169 ++uint_p;
1170 // buf_size -= sizeof(unsigned int) * 4;
1171
1172 // set has_*
1173 unsigned char* uc_p = reinterpret_cast<unsigned char*>(uint_p);
1174 *uc_p = 0;
1175 ++uc_p;
1176 *uc_p = 0;
1177 ++uc_p;
1178 *uc_p = 0;
1179 ++uc_p;
1180 *uc_p = 0;
1181 if (matrix->HasIntensity()) {
1182 *uc_p |= 1;
1183 }
1184 if (matrix->HasIntensityVar()) {
1185 *uc_p |= 2;
1186 }
1187 if (matrix->HasAltitude()) {
1188 *uc_p |= 4;
1189 }
1190 if (matrix->HasAltitudeVar()) {
1191 *uc_p |= 8;
1192 }
1193 if (matrix->HasGroundAltitude()) {
1194 *uc_p |= 16;
1195 }
1196 if (matrix->HasCount()) {
1197 *uc_p |= 32;
1198 }
1199 if (matrix->HasGroundCount()) {
1200 *uc_p |= 64;
1201 }
1202 ++uc_p;
1203 // buf_size -= sizeof(unsigned char) * 4;
1204
1205 float* float_p = reinterpret_cast<float*>(reinterpret_cast<void*>(uc_p));
1206 for (unsigned int l = 0; l < resolution_num; ++l) {
1207 // unsigned int processed_size = 0;
1208 unsigned int matrix_size =
1209 matrix->GetRowsSafe(l) * matrix->GetColsSafe(l);
1210 if (matrix->HasIntensity()) {
1211 const FloatMatrix* intensity_matrix = matrix->GetIntensityMatrixSafe(l);
1212 memcpy(float_p, (*intensity_matrix)[0], matrix_size * sizeof(float));
1213 float_p += matrix_size;
1214 // processed_size += matrix_size * sizeof(float);
1215 }
1216 if (matrix->HasIntensityVar()) {
1217 const FloatMatrix* intensity_var_matrix =
1218 matrix->GetIntensityVarMatrixSafe(l);
1219 memcpy(float_p, (*intensity_var_matrix)[0],
1220 matrix_size * sizeof(float));
1221 float_p += matrix_size;
1222 // processed_size += matrix_size * sizeof(float);
1223 }
1224 if (matrix->HasAltitude()) {
1225 const FloatMatrix* altitude_matrix = matrix->GetAltitudeMatrixSafe(l);
1226 memcpy(float_p, (*altitude_matrix)[0], matrix_size * sizeof(float));
1227 float_p += matrix_size;
1228 // processed_size += matrix_size * sizeof(float);
1229 }
1230 if (matrix->HasAltitudeVar()) {
1231 const FloatMatrix* altitude_var_matrix =
1232 matrix->GetAltitudeVarMatrixSafe(l);
1233 memcpy(float_p, (*altitude_var_matrix)[0], matrix_size * sizeof(float));
1234 float_p += matrix_size;
1235 // processed_size += matrix_size * sizeof(float);
1236 }
1237 if (matrix->HasGroundAltitude()) {
1238 const FloatMatrix* ground_altitude_matrix =
1239 matrix->GetGroundAltitudeMatrixSafe(l);
1240 memcpy(float_p, (*ground_altitude_matrix)[0],
1241 matrix_size * sizeof(float));
1242 float_p += matrix_size;
1243 // processed_size += matrix_size * sizeof(float);
1244 }
1245
1246 uint_p =
1247 reinterpret_cast<unsigned int*>(reinterpret_cast<void*>(float_p));
1248 if (matrix->HasCount()) {
1249 const UIntMatrix* count_matrix = matrix->GetCountMatrixSafe(l);
1250 memcpy(uint_p, (*count_matrix)[0], matrix_size * sizeof(unsigned int));
1251 uint_p += matrix_size;
1252 // processed_size += matrix_size * sizeof(unsigned int);
1253 }
1254 if (matrix->HasGroundCount()) {
1255 const UIntMatrix* ground_count_matrix =
1256 matrix->GetGroundCountMatrixSafe(l);
1257 memcpy(uint_p, (*ground_count_matrix)[0],
1258 matrix_size * sizeof(unsigned int));
1259 uint_p += matrix_size;
1260 // processed_size += matrix_size * sizeof(unsigned int);
1261 }
1262 float_p = reinterpret_cast<float*>(reinterpret_cast<void*>(uint_p));
1263 // assert(buf_size >= processed_size);
1264 // buf_size -= processed_size;
1265 }
1266
1267 return target_size;
1268 }
1269
1270 return 0;
1271}
1272
1274 const std::shared_ptr<BaseMapMatrix> base_matrix) {
1275 const std::shared_ptr<PyramidMapMatrix> matrix =
1276 std::dynamic_pointer_cast<PyramidMapMatrix>(base_matrix);
1277
1278 unsigned int resolution_num = matrix->GetResolutionNum();
1279 // assert(resolution_num > 0);
1280 if (resolution_num == 0) {
1281 throw "[PyramidLosslessMapMatrixHandler::get_binary_size]"
1282 "resolution_num == 0";
1283 }
1284
1285 // resolution_num and ratio
1286 // rows and cols in first level
1287 // space for has_*
1288 size_t target_size = sizeof(unsigned int) * 2 + sizeof(unsigned int) * 2 +
1289 sizeof(unsigned char) * 4;
1290
1291 for (unsigned int l = 0; l < resolution_num; ++l) {
1292 unsigned int matrix_size = matrix->GetRowsSafe(l) * matrix->GetColsSafe(l);
1293 if (matrix->HasIntensity()) {
1294 target_size += sizeof(float) * matrix_size;
1295 }
1296 if (matrix->HasIntensityVar()) {
1297 target_size += sizeof(float) * matrix_size;
1298 }
1299 if (matrix->HasAltitude()) {
1300 target_size += sizeof(float) * matrix_size;
1301 }
1302 if (matrix->HasAltitudeVar()) {
1303 target_size += sizeof(float) * matrix_size;
1304 }
1305 if (matrix->HasGroundAltitude()) {
1306 target_size += sizeof(float) * matrix_size;
1307 }
1308 if (matrix->HasCount()) {
1309 target_size += sizeof(unsigned int) * matrix_size;
1310 }
1311 if (matrix->HasGroundCount()) {
1312 target_size += sizeof(unsigned int) * matrix_size;
1313 }
1314 }
1315
1316 return target_size;
1317}
1318
1319} // namespace pyramid_map
1320} // namespace msf
1321} // namespace localization
1322} // namespace apollo
virtual size_t LoadBinary(const unsigned char *buf, std::shared_ptr< BaseMapMatrix > matrix)
Load the map cell from a binary chunk.
virtual size_t GetBinarySize(const std::shared_ptr< BaseMapMatrix > matrix)
Get the binary size of the object.
virtual size_t CreateBinary(const std::shared_ptr< BaseMapMatrix > matrix, unsigned char *buf, size_t buf_size)
Create the binary.
virtual size_t LoadBinary(const unsigned char *buf, std::shared_ptr< BaseMapMatrix > matrix)
Load the map cell from a binary chunk.
virtual size_t CreateBinary(const std::shared_ptr< BaseMapMatrix > matrix, unsigned char *buf, size_t buf_size)
Create the binary.
virtual size_t GetBinarySize(const std::shared_ptr< BaseMapMatrix > matrix)
Get the binary size of the object.
virtual unsigned char EncodeCount(unsigned int count, unsigned int count_range) const
virtual uint16_t EncodeAltitude(float altitude, float min_altitude, float altitude_interval) const
virtual void DecodeIntensity(unsigned char data, float *intensity) const
virtual void DecodeAltitude(uint16_t data, float min_altitude, float altitude_interval, float *altitude) const
virtual void DecodeCount(unsigned char data, unsigned int *count) const
virtual size_t LoadBinary(const unsigned char *buf, std::shared_ptr< BaseMapMatrix > matrix)
Load the map cell from a binary chunk.
virtual size_t CreateBinary(const std::shared_ptr< BaseMapMatrix > matrix, unsigned char *buf, size_t buf_size)
Create the binary.
virtual size_t GetBinarySize(const std::shared_ptr< BaseMapMatrix > matrix)
Get the binary size of the object.
virtual size_t GetBinarySize(const std::shared_ptr< BaseMapMatrix > matrix)
Get the binary size of the object.
virtual size_t LoadBinary(const unsigned char *buf, std::shared_ptr< BaseMapMatrix > matrix)
Load the map cell from a binary chunk.
virtual size_t CreateBinary(const std::shared_ptr< BaseMapMatrix > matrix, unsigned char *buf, size_t buf_size)
Create the binary.
static BaseMapMatrixHandler * AllocPyramidMapMatrixHandler(MapVersion version)
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37