PCL智能指針疑雲 <二> 使用同一智能指針做爲PCL預處理API的輸入和輸出

問題介紹app

slam構建地圖,先進行降採樣,再進行可視化或存儲。然而通過降採樣後,代碼沒有報錯的狀況下,點雲數據散成一團。將代碼和點雲數據展現以下,spa

pcl::VoxelGrid<Lidar::PointType> voxel_filter;
voxel_filter.setLeafSize(0.02, 0.02, 0.02);

Lidar::PointCloudPtr mapPointCloud(new Lidar::PointCloudType); //濾波器輸入變量
//...
//往輸入變量中填充數據
//...

voxel_filter.setInputCloud(mapPointCloud);
voxel_filter.filter(*mapPointCloud);
std::cout << "after voxel_filter, mapPointCloud size : " << mapPointCloud->points.size() << std::endl;
std::string index = std::to_string(ndtCount);
pcl::io::savePCDFileASCII ("/home/gordon/fase_ws/src/ddd_wall_mapping/filter_map_"+index+".pcd",
                               *mapPointCloud);

 

問題分析指針

猜測是因爲降採樣濾波器的輸入和輸出是同一個指針變量,在處理過程當中內存混亂,致使點雲數據出錯。code

 

解決方案blog

使用兩個不一樣的變量做爲降採樣濾波器的輸入和輸出,而且做爲輸出的變量每次都要進行清空操做。內存

問題解決後的代碼和點雲數據展現以下,string

pcl::VoxelGrid<Lidar::PointType> voxel_filter;
voxel_filter.setLeafSize(0.02, 0.02, 0.02);

Lidar::PointCloudPtr mapPointCloud(new Lidar::PointCloudType); //濾波器輸入變量
//...
//往輸入變量中填充數據
//...

Lidar::PointCloudPtr filter_mapPointCloud(new Lidar::PointCloudType); // 濾波器輸出變量
voxel_filter.setInputCloud(mapPointCloud);
voxel_filter.filter(*filter_mapPointCloud);
mapPointCloud->clear();
*mapPointCloud += *filter_mapPointCloud;
std::cout << "after voxel_filter, mapPointCloud size : " << mapPointCloud->points.size() << std::endl;
std::string index = std::to_string(ndtCount);
pcl::io::savePCDFileASCII ("/home/gordon/fase_ws/src/ddd_wall_mapping/filter_map_"+index+".pcd", *mapPointCloud);

 

 

至於具體緣由,至今不詳,忘高手或前輩指點迷津。 io

相關文章
相關標籤/搜索