Apollo 11.0
自动驾驶开放平台
pyramid_map_node.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 <memory>
19#include <vector>
23
24namespace apollo {
25namespace localization {
26namespace msf {
27namespace pyramid_map {
28
30
32
33void PyramidMapNode::Init(const BaseMapConfig* map_config) {
34 map_config_ = map_config;
35
40 map_node_config_->has_map_version_ = false;
41 map_node_config_->has_body_md5_ = false;
42 } else {
43 map_node_config_->has_map_version_ = true;
44 map_node_config_->has_body_md5_ = true;
45 }
46
47 is_reserved_ = false;
48 data_is_ready_ = false;
49 is_changed_ = false;
50
51 map_matrix_.reset(new PyramidMapMatrix());
54 map_node_config_->map_version_));
56
57 const PyramidMapConfig* pm_map_config =
58 dynamic_cast<const PyramidMapConfig*>(map_config_);
59 resolutions_mr_.resize(pm_map_config->resolution_num_, 0);
60 for (unsigned int i = 0; i < resolutions_mr_.size(); ++i) {
61 resolutions_mr_[i] =
62 static_cast<float>(pm_map_config->map_resolutions_[0] *
63 std::pow(pm_map_config->resolution_ratio_, i));
64 }
65
67}
68
69void PyramidMapNode::Init(const BaseMapConfig* map_config,
70 const MapNodeIndex& index, bool create_map_cells) {
71 map_config_ = map_config;
72
74 map_node_config_->node_index_ = index;
78 map_node_config_->has_map_version_ = false;
79 map_node_config_->has_body_md5_ = false;
80 } else {
81 map_node_config_->has_map_version_ = true;
82 map_node_config_->has_body_md5_ = true;
83 }
84
87 is_reserved_ = false;
88 data_is_ready_ = false;
89 is_changed_ = false;
90
91 map_matrix_.reset(new PyramidMapMatrix());
94 map_node_config_->map_version_));
96
97 if (create_map_cells) {
99 }
100}
101
103 std::shared_ptr<PyramidMapMatrix> map_matrix =
104 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
105 map_matrix->BottomUpBase();
106}
107
109 std::shared_ptr<PyramidMapMatrix> map_matrix =
110 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
111 map_matrix->BottomUpSafe();
112}
113
114bool PyramidMapNode::AddValueIfInBound(const Eigen::Vector3d& coordinate,
115 unsigned char intensity,
116 unsigned int level) {
117 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
118 // Eigen::Map<Eigen::Vector2d>(coordinate.data(), 2);
119 unsigned int x = 0;
120 unsigned int y = 0;
121 bool is_success = GetCoordinate(coord2d, level, &x, &y);
122 if (is_success) {
123 std::shared_ptr<PyramidMapMatrix> map_matrix =
124 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
125 map_matrix->AddSampleBase(static_cast<float>(intensity),
126 static_cast<float>(coordinate[2]), y, x, level);
127 return true;
128 } else {
129 return false;
130 }
131}
132
134 const std::vector<Eigen::Vector3d>& coordinates,
135 const std::vector<unsigned char>& intensity, unsigned int level) {
136 if (coordinates.size() != intensity.size()) {
137 return;
138 }
139
140 for (unsigned int i = 0; i < coordinates.size(); i++) {
141 AddValueIfInBound(coordinates[i], intensity[i], level);
142 }
143}
144
145bool PyramidMapNode::GetCoordinate(const Eigen::Vector2d& coordinate,
146 unsigned int level, unsigned int* x,
147 unsigned int* y) const {
148 std::shared_ptr<PyramidMapMatrix> map_matrix =
149 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
150
151 const float& current_resolution = resolutions_mr_[level];
152
153 unsigned int off_x = static_cast<unsigned int>(
154 (coordinate[0] - left_top_corner_[0]) / current_resolution);
155 unsigned int off_y = static_cast<unsigned int>(
156 (coordinate[1] - left_top_corner_[1]) / current_resolution);
157 if (off_x < map_matrix->GetCols(level) &&
158 off_y < map_matrix->GetRows(level)) {
159 *x = off_x;
160 *y = off_y;
161 return true;
162 }
163 return false;
164}
165
166Eigen::Vector2d PyramidMapNode::GetCoordinate(unsigned int level,
167 unsigned int x,
168 unsigned int y) const {
169 const PyramidMapConfig* map_config =
170 dynamic_cast<const PyramidMapConfig*>(map_config_);
171
172 float current_resolution =
173 static_cast<float>(map_config->map_resolutions_[0] *
174 std::pow(map_config->resolution_ratio_, level));
175
176 Eigen::Vector2d coord;
177 coord[0] = left_top_corner_[0] + current_resolution * static_cast<float>(x);
178 coord[1] = left_top_corner_[1] + current_resolution * static_cast<float>(y);
179 return coord;
180}
181
182bool PyramidMapNode::GetCoordinate(const Eigen::Vector2d& coordinate,
183 unsigned int* x, unsigned int* y) const {
184 return BaseMapNode::GetCoordinate(coordinate, x, y);
185}
186bool PyramidMapNode::GetCoordinate(const Eigen::Vector3d& coordinate,
187 unsigned int* x, unsigned int* y) const {
188 return BaseMapNode::GetCoordinate(coordinate, x, y);
189}
190
191Eigen::Vector2d PyramidMapNode::GetCoordinate(unsigned int x,
192 unsigned int y) const {
193 return BaseMapNode::GetCoordinate(x, y);
194}
195
196float PyramidMapNode::GetIntensitySafe(const Eigen::Vector3d& coordinate,
197 unsigned int level) const {
198 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
199 unsigned int x = 0;
200 unsigned int y = 0;
201 bool is_success = GetCoordinate(coord2d, level, &x, &y);
202
203 float intensity = 0.0;
204 if (is_success) {
205 std::shared_ptr<PyramidMapMatrix> map_matrix =
206 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
207 const float* intensity_ptr = map_matrix->GetIntensitySafe(y, x, level);
208
209 if (intensity_ptr != nullptr) {
210 intensity = *intensity_ptr;
211 }
212 }
213
214 return intensity;
215}
216
217float PyramidMapNode::GetIntensityVarSafe(const Eigen::Vector3d& coordinate,
218 unsigned int level) const {
219 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
220 unsigned int x = 0;
221 unsigned int y = 0;
222 bool is_success = GetCoordinate(coord2d, level, &x, &y);
223
224 float intensity_var = 0.0;
225 if (is_success) {
226 std::shared_ptr<PyramidMapMatrix> map_matrix =
227 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
228 const float* intensity_var_ptr =
229 map_matrix->GetIntensityVarSafe(y, x, level);
230
231 if (intensity_var_ptr != nullptr) {
232 intensity_var = *intensity_var_ptr;
233 }
234 }
235
236 return intensity_var;
237}
238
239float PyramidMapNode::GetAltitudeSafe(const Eigen::Vector3d& coordinate,
240 unsigned int level) const {
241 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
242 unsigned int x = 0;
243 unsigned int y = 0;
244 bool is_success = GetCoordinate(coord2d, level, &x, &y);
245
246 float altitude = 0.0;
247 if (is_success) {
248 std::shared_ptr<PyramidMapMatrix> map_matrix =
249 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
250 const float* altitude_ptr = map_matrix->GetAltitudeSafe(y, x, level);
251
252 if (altitude_ptr != nullptr) {
253 altitude = *altitude_ptr;
254 }
255 }
256
257 return altitude;
258}
259
260float PyramidMapNode::GetAltitudeVarSafe(const Eigen::Vector3d& coordinate,
261 unsigned int level) const {
262 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
263 unsigned int x = 0;
264 unsigned int y = 0;
265 bool is_success = GetCoordinate(coord2d, level, &x, &y);
266
267 float altitude_var = 0.0;
268 if (is_success) {
269 std::shared_ptr<PyramidMapMatrix> map_matrix =
270 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
271 const float* altitude_var_ptr = map_matrix->GetAltitudeVarSafe(y, x, level);
272
273 if (altitude_var_ptr != nullptr) {
274 altitude_var = *altitude_var_ptr;
275 }
276 }
277
278 return altitude_var;
279}
280
281float PyramidMapNode::GetGroundAltitudeSafe(const Eigen::Vector3d& coordinate,
282 unsigned int level) const {
283 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
284 unsigned int x = 0;
285 unsigned int y = 0;
286 bool is_success = GetCoordinate(coord2d, level, &x, &y);
287
288 float ground_altitude = 0.0;
289 if (is_success) {
290 std::shared_ptr<PyramidMapMatrix> map_matrix =
291 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
292 const float* ground_altitude_ptr =
293 map_matrix->GetGroundAltitudeSafe(y, x, level);
294
295 if (ground_altitude_ptr != nullptr) {
296 ground_altitude = *ground_altitude_ptr;
297 }
298 }
299
300 return ground_altitude;
301}
302
303unsigned int PyramidMapNode::GetCountSafe(const Eigen::Vector3d& coordinate,
304 unsigned int level) const {
305 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
306 unsigned int x = 0;
307 unsigned int y = 0;
308 bool is_success = GetCoordinate(coord2d, level, &x, &y);
309
310 unsigned int count = 0;
311 if (is_success) {
312 std::shared_ptr<PyramidMapMatrix> map_matrix =
313 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
314 const unsigned int* count_ptr = map_matrix->GetCountSafe(y, x, level);
315
316 if (count_ptr != nullptr) {
317 count = *count_ptr;
318 }
319 }
320
321 return count;
322}
323
325 const Eigen::Vector3d& coordinate, unsigned int level) const {
326 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
327 unsigned int x = 0;
328 unsigned int y = 0;
329 bool is_success = GetCoordinate(coord2d, level, &x, &y);
330
331 unsigned int ground_count = 0;
332 if (is_success) {
333 std::shared_ptr<PyramidMapMatrix> map_matrix =
334 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
335 const unsigned int* ground_count_ptr =
336 map_matrix->GetGroundCountSafe(y, x, level);
337
338 if (ground_count_ptr != nullptr) {
339 ground_count = *ground_count_ptr;
340 }
341 }
342
343 return ground_count;
344}
345
346float PyramidMapNode::GetIntensity(const Eigen::Vector3d& coordinate,
347 unsigned int level) const {
348 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
349 unsigned int x = 0;
350 unsigned int y = 0;
351 bool is_success = GetCoordinate(coord2d, level, &x, &y);
352
353 float intensity = 0.0;
354 if (is_success) {
355 std::shared_ptr<PyramidMapMatrix> map_matrix =
356 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
357 const float* intensity_ptr = map_matrix->GetIntensity(y, x, level);
358
359 if (intensity_ptr != nullptr) {
360 intensity = *intensity_ptr;
361 }
362 }
363
364 return intensity;
365}
366
367float PyramidMapNode::GetIntensityVar(const Eigen::Vector3d& coordinate,
368 unsigned int level) const {
369 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
370 unsigned int x = 0;
371 unsigned int y = 0;
372 bool is_success = GetCoordinate(coord2d, level, &x, &y);
373
374 float intensity_var = 0.0;
375 if (is_success) {
376 std::shared_ptr<PyramidMapMatrix> map_matrix =
377 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
378 const float* intensity_var_ptr = map_matrix->GetIntensityVar(y, x, level);
379
380 if (intensity_var_ptr != nullptr) {
381 intensity_var = *intensity_var_ptr;
382 }
383 }
384
385 return intensity_var;
386}
387
388float PyramidMapNode::GetAltitude(const Eigen::Vector3d& coordinate,
389 unsigned int level) const {
390 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
391 unsigned int x = 0;
392 unsigned int y = 0;
393 bool is_success = GetCoordinate(coord2d, level, &x, &y);
394
395 float altitude = 0.0;
396 if (is_success) {
397 std::shared_ptr<PyramidMapMatrix> map_matrix =
398 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
399 const float* altitude_ptr = map_matrix->GetAltitude(y, x, level);
400
401 if (altitude_ptr != nullptr) {
402 altitude = *altitude_ptr;
403 }
404 }
405
406 return altitude;
407}
408
409float PyramidMapNode::GetAltitudeVar(const Eigen::Vector3d& coordinate,
410 unsigned int level) const {
411 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
412 unsigned int x = 0;
413 unsigned int y = 0;
414 bool is_success = GetCoordinate(coord2d, level, &x, &y);
415
416 float altitude_var = 0.0;
417 if (is_success) {
418 std::shared_ptr<PyramidMapMatrix> map_matrix =
419 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
420 const float* altitude_var_ptr = map_matrix->GetAltitudeVar(y, x, level);
421
422 if (altitude_var_ptr != nullptr) {
423 altitude_var = *altitude_var_ptr;
424 }
425 }
426
427 return altitude_var;
428}
429
430float PyramidMapNode::GetGroundAltitude(const Eigen::Vector3d& coordinate,
431 unsigned int level) const {
432 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
433 unsigned int x = 0;
434 unsigned int y = 0;
435 bool is_success = GetCoordinate(coord2d, level, &x, &y);
436
437 float ground_altitude = 0.0;
438 if (is_success) {
439 std::shared_ptr<PyramidMapMatrix> map_matrix =
440 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
441 const float* ground_altitude_ptr =
442 map_matrix->GetGroundAltitude(y, x, level);
443
444 if (ground_altitude_ptr != nullptr) {
445 ground_altitude = *ground_altitude_ptr;
446 }
447 }
448
449 return ground_altitude;
450}
451
452unsigned int PyramidMapNode::GetCount(const Eigen::Vector3d& coordinate,
453 unsigned int level) const {
454 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
455 unsigned int x = 0;
456 unsigned int y = 0;
457 bool is_success = GetCoordinate(coord2d, level, &x, &y);
458
459 unsigned int count = 0;
460 if (is_success) {
461 std::shared_ptr<PyramidMapMatrix> map_matrix =
462 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
463 const unsigned int* count_ptr = map_matrix->GetCount(y, x, level);
464
465 if (count_ptr != nullptr) {
466 count = *count_ptr;
467 }
468 }
469
470 return count;
471}
472
473unsigned int PyramidMapNode::GetGroundCount(const Eigen::Vector3d& coordinate,
474 unsigned int level) const {
475 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
476 unsigned int x = 0;
477 unsigned int y = 0;
478 bool is_success = GetCoordinate(coord2d, level, &x, &y);
479
480 unsigned int ground_count = 0;
481 if (is_success) {
482 std::shared_ptr<PyramidMapMatrix> map_matrix =
483 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
484 const unsigned int* ground_count_ptr =
485 map_matrix->GetGroundCount(y, x, level);
486
487 if (ground_count_ptr != nullptr) {
488 ground_count = *ground_count_ptr;
489 }
490 }
491
492 return ground_count;
493}
494
495double PyramidMapNode::ComputeMeanIntensity(unsigned int level) {
496 std::shared_ptr<PyramidMapMatrix> map_matrix =
497 std::dynamic_pointer_cast<PyramidMapMatrix>(map_matrix_);
498 return map_matrix->ComputeMeanIntensity(level);
499}
500
501} // namespace pyramid_map
502} // namespace msf
503} // namespace localization
504} // namespace apollo
std::vector< float > map_resolutions_
The pixel resolutions in the map in meters.
std::shared_ptr< CompressionStrategy > compression_strategy_
@bried The compression strategy.
virtual bool GetCoordinate(const Eigen::Vector2d &coordinate, unsigned int *x, unsigned int *y) const
Given the global coordinate, get the local 2D coordinate of the map cell matrix.
const BaseMapConfig * map_config_
The map settings.
bool is_changed_
Has the map node been changed.
bool is_reserved_
If the node is reserved in map.
static Eigen::Vector2d ComputeLeftTopCorner(const BaseMapConfig &config, const MapNodeIndex &index)
std::shared_ptr< BaseMapMatrixHandler > map_matrix_handler_
The class to load and create map matrix binary.
std::shared_ptr< BaseMapMatrix > map_matrix_
The data structure of the map datas, which is a matrix.
Eigen::Vector2d left_top_corner_
The left top corner of the map node in the global coordinate system.
std::shared_ptr< BaseMapNodeConfig > map_node_config_
The map node config.
virtual void InitMapMatrix(const BaseMapConfig *map_config)
Initialize the map matrix.
static BaseMapMatrixHandler * AllocPyramidMapMatrixHandler(MapVersion version)
void BottomUpSafe()
Propagate the data to the coarse resolution by check.
float GetAltitudeVar(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's variance of the altitude without check.
float GetIntensityVar(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell variance of the intensity without check.
bool GetCoordinate(const Eigen::Vector2d &coordinate, unsigned int level, unsigned int *x, unsigned int *y) const
Given the global coordinate, get the local 2D coordinate of the map cell matrix.
float GetAltitudeVarSafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's variance of the altitude with check.
float GetIntensity(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell intensity without check.
void BottomUpBase()
Propagate the data to the coarse resolution.
unsigned int GetGroundCount(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's count of the ground samples without check.
unsigned int GetCount(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's count of the samples without check.
double ComputeMeanIntensity(unsigned int level=0)
Compute mean intensity.
float GetAltitudeSafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's average altitude with check.
unsigned int GetGroundCountSafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's count of the ground samples with check.
unsigned int GetCountSafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's count of the samples with check.
float GetGroundAltitude(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's average ground altitude without check.
float GetIntensitySafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell intensity with check.
float GetIntensityVarSafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell variance of the intensity with check.
bool AddValueIfInBound(const Eigen::Vector3d &coordinate, unsigned char intensity, unsigned int level=0)
Add the value of a pixel in the map node if the pixel in the node.
virtual void Init(const BaseMapConfig *map_config)
Initialize the map node.
float GetAltitude(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's average altitude without check.
float GetGroundAltitudeSafe(const Eigen::Vector3d &coordinate, unsigned int level=0) const
Given the 3D global coordinate, get the map cell's average ground altitude with check.
class register implement
Definition arena_queue.h:37