目录

  • ​​保存txt文件为.pcd​​
  • ​​读取txt到点云​​
  • ​​pcl可视化​​
  • ​​分割字符串​​
  • ​​获取目录下所有文件名​​

保存txt文件为.pcd

// 保存txt文件为.pcd
/*
* filename: 文件名,全路径
* parent_dir: 保存的pcd路径
*/
bool savetxt2pcd(const std::string filename, const std::string parent_dir)
{
pcl::PointCloud<pcl::PointXYZIL>::Ptr cloudil(new pcl::PointCloud<pcl::PointXYZIL>);
txt2pcd::read_txt_pcd(filename, cloudil);

auto vec = txt2pcd::split(filename, std::string(".txt"));
auto filename_vec = txt2pcd::split(vec[0], "/");
auto sub_dir_name = *(filename_vec.end()-2);
auto pcd_name = filename_vec.back();
// for(const auto& ele:vec)
// {
// std::cout<< ele<< ", ";
// }
std::cout<<std::endl;

std::cout<< sub_dir_name<<", " <<pcd_name <<std::endl;

std::string output_dir = std::string(parent_dir) +"/" +sub_dir_name;
std::string output_file = pcd_name+std::string(".pcd");

// 创建文件夹
// make output directory
if (!boost::filesystem::exists(boost::filesystem::path(output_dir))) {
if(boost::filesystem::create_directory(boost::filesystem::path(output_dir)))
std::cout<<" --- mkdir -p "<<output_dir<<std::endl;
}
auto final_name = output_dir+"/"+output_file;
// std::cout<<" --- final_name " << final_name << std::endl;

// save
if(pcl::io::savePCDFile(final_name, *cloudil, true)!=-1)
{
std::cout<<" --- save " << final_name << std::endl;
}



}

读取txt到点云

// 读取txt
void read_txt_pcd(const std::string filename, pcl::PointCloud<pcl::PointXYZIL>::Ptr cloudil)
{
std::ifstream file(filename);
std::string line;
pcl::PointXYZIL tmp_point;
while(std::getline(file, line))
{
std::stringstream ss(line);
ss >> tmp_point.x;
ss >> tmp_point.y;
ss >> tmp_point.z;
ss >> tmp_point.intensity;
ss >> tmp_point.label;
cloudil->push_back(tmp_point);

}
file.close();
std::cout<<" --- num: "<< cloudil->size()<<std::endl;
}

pcl可视化

// 可视化 xyz i l的数据
void visualize_xyz_i_l(const pcl::PointCloud<pcl::PointXYZIL>::Ptr cloud_xyzil)
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr ccloud(new pcl::PointCloud<pcl::PointXYZRGB>);
for (const auto& upt : *cloud_xyzil)
{
pcl::PointXYZRGB cpt;
cpt.x = upt.x;
cpt.y = upt.y;
cpt.z = upt.z;
cpt.r = upt.intensity;
cpt.g = upt.intensity;
cpt.b = upt.intensity;
ccloud->points.push_back(cpt);
}
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
viewer->setBackgroundColor(1., 1., 1.);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgbs(ccloud);
viewer->addPointCloud<pcl::PointXYZRGB>(ccloud, rgbs, "pointcloud");
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
// boost::this_thread::sleep(boost::posix_time::microseconds(1000));
}
viewer->close();
}

void visualize_xyz_l(const pcl::PointCloud<pcl::PointXYZL>::Ptr cloud_xyzl)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
viewer->setBackgroundColor(1., 1., 1.);
pcl::visualization::PointCloudColorHandlerLabelField<pcl::PointXYZL> labels(cloud_xyzl);
viewer->addPointCloud<pcl::PointXYZL>(cloud_xyzl, labels, "pointcloud");


viewer->registerPointPickingCallback(txt2pcd::pp_callback_PointsSelect, (void*)&txt2pcd::out_cloud_ptr);
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
}
viewer->close();
}

分割字符串

std::vector<std::string> split(const std::string &str, const std::string &pattern)
{
std::vector<std::string> res;
if(str == "")
return res;
//在字符串末尾也加入分隔符,方便截取最后一段
std::string strs = str + pattern;
size_t pos = strs.find(pattern);

while(pos != strs.npos)
{
std::string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos+1, strs.size());
pos = strs.find(pattern);
}

return res;
}

获取目录下所有文件名

// 遍历文件夹下所有文件
/*
* dir 存放文件的目录
* suffix 文件名后缀
* filenames output 文件名vector
*/
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>

bool get_filenames(const std::string &dir, const std::string &suffix, std::vector<std::string> &filenames)
{
boost::filesystem::path path(dir);
if(!boost::filesystem::exists(path))
{
return false;
}

boost::filesystem::directory_iterator end_iter;
int i = 0;
for(boost::filesystem::directory_iterator iter(path); iter!=end_iter; ++iter)
{
// std::cout<<i++<<std::endl;
// std::cout<<"path: "<<iter->path()<<std::endl;

if(boost::filesystem::extension(iter->path())==suffix)
{
if( std::string::npos != std::string(iter->path().c_str()).find("frames"))
{
continue;
}

std::cout<<"path: "<<iter->path()<<std::endl;
filenames.push_back(std::string(iter->path().c_str()));
}
}
return true;
}