22#include <boost/filesystem.hpp>
28namespace localization {
30namespace pyramid_map {
33 : map_config_(config),
34 map_node_cache_lvl1_(nullptr),
35 map_node_cache_lvl2_(nullptr),
36 map_node_pool_(nullptr) {}
43 std::placeholders::_1);
46 std::placeholders::_1);
86 std::cerr <<
"GetMapNodeSafe: This node don't exist in cache! " << std::endl
87 <<
"load this node from disk now!" << index << std::endl;
125 std::cerr <<
"map_ids's size is bigger than cache's capacity" << std::endl;
130 std::set<MapNodeIndex>::iterator itr = map_ids->begin();
131 while (itr != map_ids->end()) {
137 itr = map_ids->erase(itr);
146 std::vector<std::future<void>> load_futures_;
147 itr = map_ids->begin();
148 while (itr != map_ids->end()) {
149 AERROR <<
"Preload map node failed!";
150 load_futures_.emplace_back(
155 for (
auto& future : load_futures_) {
156 if (future.valid()) {
165 std::set<MapNodeIndex>::iterator itr = map_ids->begin();
167 while (itr != map_ids->end()) {
173 itr = map_ids->erase(itr);
183 AERROR <<
"map_ids's size is bigger than cache's capacity";
187 std::set<MapNodeIndex>::iterator itr = map_ids->begin();
188 bool is_exist =
false;
189 while (itr != map_ids->end()) {
194 itr = map_ids->erase(itr);
200 itr = map_ids->begin();
202 while (itr != map_ids->end()) {
208 itr = map_ids->erase(itr);
214 std::vector<std::future<void>> preload_futures;
215 itr = map_ids->begin();
216 AINFO <<
"Preload map node size: " << map_ids->size();
217 while (itr != map_ids->end()) {
218 AINFO <<
"Preload map node: " << *itr << std::endl;
222 preload_futures.emplace_back(
231 while (map_node ==
nullptr) {
233 if (map_node ==
nullptr) {
244 if (!map_node->
Load()) {
245 AINFO <<
"Created map node: " << index;
247 AINFO <<
"Loaded map node: " << index;
264 const Eigen::Vector3d& trans_diff,
265 unsigned int resolution_id,
unsigned int zone_id) {
267 std::cerr <<
"Map node pool is nullptr!" << std::endl;
270 int x_direction = trans_diff[0] > 0 ? 1 : -1;
271 int y_direction = trans_diff[1] > 0 ? 1 : -1;
272 std::set<MapNodeIndex> map_ids;
273 float map_pixel_resolution =
276 Eigen::Vector3d pt_top_left;
279 map_pixel_resolution / 2.0f);
282 map_pixel_resolution / 2.0f);
285 resolution_id, zone_id);
286 map_ids.insert(map_id);
288 Eigen::Vector3d pt_top_center;
289 pt_top_center[0] = location[0];
290 pt_top_center[1] = pt_top_left[1];
291 pt_top_center[2] = 0;
293 resolution_id, zone_id);
294 map_ids.insert(map_id);
296 Eigen::Vector3d pt_top_right;
299 map_pixel_resolution / 2.0f);
300 pt_top_right[1] = pt_top_left[1];
303 resolution_id, zone_id);
304 map_ids.insert(map_id);
306 Eigen::Vector3d pt_middle_left;
307 pt_middle_left[0] = pt_top_left[0];
308 pt_middle_left[1] = location[1];
309 pt_middle_left[2] = 0;
311 resolution_id, zone_id);
312 map_ids.insert(map_id);
316 map_ids.insert(map_id);
318 Eigen::Vector3d pt_middle_right;
319 pt_middle_right[0] = pt_top_right[0];
320 pt_middle_right[1] = pt_middle_left[1];
321 pt_middle_right[2] = 0;
323 resolution_id, zone_id);
324 map_ids.insert(map_id);
326 Eigen::Vector3d pt_bottom_left;
327 pt_bottom_left[0] = pt_top_left[0];
330 map_pixel_resolution / 2.0);
331 pt_bottom_left[2] = 0;
333 resolution_id, zone_id);
334 map_ids.insert(map_id);
336 Eigen::Vector3d pt_bottom_center;
337 pt_bottom_center[0] = pt_top_center[0];
338 pt_bottom_center[1] = pt_bottom_left[1];
339 pt_bottom_center[2] = 0;
341 resolution_id, zone_id);
342 map_ids.insert(map_id);
344 Eigen::Vector3d pt_bottom_right;
345 pt_bottom_right[0] = pt_top_right[0];
346 pt_bottom_right[1] = pt_bottom_left[1];
347 pt_bottom_right[2] = 0;
349 resolution_id, zone_id);
350 map_ids.insert(map_id);
351 for (
int i = -1; i < 2; ++i) {
353 pt[0] = location[0] + x_direction * 1.5 *
355 map_pixel_resolution;
356 pt[1] = location[1] +
static_cast<double>(i) *
358 map_pixel_resolution;
362 map_ids.insert(map_id);
364 for (
int i = -1; i < 2; ++i) {
366 pt[0] = location[0] +
static_cast<double>(i) *
368 map_pixel_resolution;
369 pt[1] = location[1] + y_direction * 1.5 *
371 map_pixel_resolution;
375 map_ids.insert(map_id);
379 pt[0] = location[0] + x_direction * 1.5 *
381 map_pixel_resolution;
382 pt[1] = location[1] + y_direction * 1.5 *
384 map_pixel_resolution;
388 map_ids.insert(map_id);
395 unsigned int resolution_id,
unsigned int zone_id,
396 int filter_size_x,
int filter_size_y) {
398 std::cerr <<
"Map node pool is nullptr!" << std::endl;
401 std::set<MapNodeIndex> map_ids;
402 float map_pixel_resolution =
405 Eigen::Vector3d pt_top_left;
406 pt_top_left[0] = seed_pt3d[0] -
408 map_pixel_resolution / 2.0) -
409 static_cast<float>(filter_size_x / 2) * map_pixel_resolution;
410 pt_top_left[1] = seed_pt3d[1] -
412 map_pixel_resolution / 2.0) -
413 static_cast<float>(filter_size_y / 2) * map_pixel_resolution;
416 resolution_id, zone_id);
417 map_ids.insert(map_id);
419 Eigen::Vector3d pt_top_center;
420 pt_top_center[0] = seed_pt3d[0];
421 pt_top_center[1] = pt_top_left[1];
422 pt_top_center[2] = 0;
424 resolution_id, zone_id);
425 map_ids.insert(map_id);
427 Eigen::Vector3d pt_top_right;
431 map_pixel_resolution / 2.0) +
432 static_cast<float>(filter_size_x / 2) * map_pixel_resolution;
433 pt_top_right[1] = pt_top_left[1];
436 resolution_id, zone_id);
437 map_ids.insert(map_id);
439 Eigen::Vector3d pt_middle_left;
440 pt_middle_left[0] = pt_top_left[0];
441 pt_middle_left[1] = seed_pt3d[1];
442 pt_middle_left[2] = 0;
444 resolution_id, zone_id);
445 map_ids.insert(map_id);
449 map_ids.insert(map_id);
451 Eigen::Vector3d pt_middle_right;
452 pt_middle_right[0] = pt_top_right[0];
453 pt_middle_right[1] = seed_pt3d[1];
454 pt_middle_right[2] = 0;
456 resolution_id, zone_id);
457 map_ids.insert(map_id);
459 Eigen::Vector3d pt_bottom_left;
460 pt_bottom_left[0] = pt_top_left[0];
464 map_pixel_resolution / 2.0) +
465 static_cast<float>(filter_size_y / 2) * map_pixel_resolution;
466 pt_bottom_left[2] = 0;
468 resolution_id, zone_id);
469 map_ids.insert(map_id);
471 Eigen::Vector3d pt_bottom_center;
472 pt_bottom_center[0] = seed_pt3d[0];
473 pt_bottom_center[1] = pt_bottom_left[1];
474 pt_bottom_center[2] = 0;
476 resolution_id, zone_id);
477 map_ids.insert(map_id);
479 Eigen::Vector3d pt_bottom_right;
480 pt_bottom_right[0] = pt_top_right[0];
481 pt_bottom_right[1] = pt_bottom_left[1];
482 pt_bottom_right[2] = 0;
484 resolution_id, zone_id);
485 map_ids.insert(map_id);
493 sscanf(map_path.c_str(),
"/%03u/%05s/%02d/%08u/%08u", &index.
resolution_id_,
495 std::string zone = buf;
496 if (zone ==
"south") {
504 boost::filesystem::path map_folder_path_boost(map_folder_path);
507 boost::filesystem::recursive_directory_iterator end_iter;
508 boost::filesystem::recursive_directory_iterator iter(map_folder_path_boost);
509 for (; iter != end_iter; ++iter) {
510 if (!boost::filesystem::is_directory(*iter)) {
511 if (iter->path().extension() ==
"") {
512 std::string path = iter->path().string();
513 path = path.substr(map_folder_path.length(), path.length());
538 std::map<std::string, std::string>::iterator it =
static void ComputeFileMd5(const std::string &file_path, unsigned char res[kUcharMd5Length])
Compute file md5 given a file path.
static const size_t kCharMd5Lenth
The data structure of the LRUCache.
The options of the reflectance map.
std::vector< std::string > map_datasets_
The datasets that contributed to the map.
bool Save(const std::string &file_path)
Save the map option to a XML file.
std::string map_folder_path_
The map folder path.
unsigned int map_node_size_y_
The map node size in pixels.
std::vector< float > map_resolutions_
The pixel resolutions in the map in meters.
bool Load(const std::string &file_path)
Load the map option from a XML file.
unsigned int map_node_size_x_
The map node size in pixels.
std::map< std::string, std::string > node_md5_map_
The map structure to store map node file name and its md5.
The memory pool for the data structure of BaseMapNode.
BaseMapNode * AllocMapNode()
Get a MapNode object from memory pool.
void FreeMapNode(BaseMapNode *map_node)
Release MapNode object to memory pool.
The data structure of a Node in the map.
void SetIsReserved(bool is_reserved)
Set if the map node is reserved.
bool Load()
Load the map node from the disk.
void SetMapNodeIndex(const MapNodeIndex &index)
Set the map node index.
std::vector< std::string > all_map_node_md5s_
All the map nodes' md5 in the Map (in the disk).
bool SetMapFolderPath(const std::string folder_path)
Set the directory of the map.
void PreloadMapNodes(std::set< MapNodeIndex > *map_ids)
Load map node by index.
BaseMapNode * GetMapNodeSafe(const MapNodeIndex &index)
Return the map node, if it's not in the cache, safely load it.
void AddDataset(const std::string dataset_path)
Add a dataset path to the map config.
void GetAllMapIndexAndPath()
bool CheckMapStrictly()
Check if map is normal(with map node checking).
BaseMapConfig * map_config_
The map settings.
MapNodeCache< MapNodeIndex, BaseMapNode >::DestroyFunc destroy_func_lvl1_
void LoadMapNodeThreadSafety(const MapNodeIndex &index, bool is_reserved=false)
Load map node by index, thread_safety.
boost::recursive_mutex map_load_mutex_
The mutex for preload map node.
std::vector< MapNodeIndex > all_map_node_indices_
All the map nodes in the Map (in the disk).
std::unique_ptr< MapNodeCache< MapNodeIndex, BaseMapNode > > map_node_cache_lvl2_
brief The dynamic map node preloading thread pool pointer.
BaseMapNode * GetMapNode(const MapNodeIndex &index)
Return the map node, if it's not in the cache, return false.
virtual void InitMapNodeCaches(int cacheL1_size, int cahceL2_size)
std::unique_ptr< MapNodeCache< MapNodeIndex, BaseMapNode > > map_node_cache_lvl1_
The cache for map node preload.
MapNodeCache< MapNodeIndex, BaseMapNode >::DestroyFunc destroy_func_lvl2_
virtual bool LoadMapArea(const Eigen::Vector3d &seed_pt3d, unsigned int resolution_id, unsigned int zone_id, int filter_size_x, int filter_size_y)
Load map nodes for the location calculate of this frame.
virtual ~BaseMap()
The destructor.
BaseMapNodePool * map_node_pool_
The map node memory pool pointer.
bool CheckMap()
Check if map is normal.
virtual void PreloadMapArea(const Eigen::Vector3d &location, const Eigen::Vector3d &trans_diff, unsigned int resolution_id, unsigned int zone_id)
Preload map nodes for the next frame location calculation.
bool IsMapNodeExist(const MapNodeIndex &index)
Check if the map node in the cache.
MapNodeIndex GetMapIndexFromMapPath(const std::string &map_path)
BaseMap(BaseMapConfig *config)
The constructor.
void CheckAndUpdateCache(std::set< MapNodeIndex > *map_ids)
Check map node in L2 Cache.
void AttachMapNodePool(BaseMapNodePool *p_map_node_pool)
Attach map node pointer.
std::vector< std::string > all_map_node_paths_
void LoadMapNodes(std::set< MapNodeIndex > *map_ids)
Load map node by index.
void ComputeMd5ForAllMapNodes()
Compute md5 for all map node file in map.
std::set< MapNodeIndex > map_preloading_task_index_
@bried Keep the index of preloading nodes.
unsigned int m_
The map node ID at the northing direction.
unsigned int resolution_id_
The ID of the resolution.
unsigned int n_
The map node ID at the easting direction.
static MapNodeIndex GetMapNodeIndex(const BaseMapConfig &option, const Eigen::Vector3d &coordinate, unsigned int resolution_id, int zone_id)
Construct a map node index, given a global coordinate, eigen version.
bool PathExists(const std::string &path)
Check if the path exists.