使用C++常规的读写方式,即std::ifstream和std::ofstream
pcl::PointCloud<pcl::PointXYZRGB>::Ptr PointCloudGUI::readASCPoint(std::string path)
{
std::ifstream ifs(path, std::ios::in);
if (!ifs.is_open())
{
std::cout << "文件不存在" << std::endl;
ifs.close();
return NULL;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
std::cout << "文件为空" << std::endl;
ifs.close();
return NULL;
}
ifs.putback(ch);
pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);
std::string data_;
int i = 0;
int num = 0;
std::vector<pcl::PointXYZ> v;
v.push_back(pcl::PointXYZ(0, 0, 0));
while (ifs >> data_)
{
i++;
if (i % 3 == 1)
v[num].x = std::atof(data_.c_str());
if (i % 3 == 2)
v[num].y = std::atof(data_.c_str());
if (i % 3 == 0)
{
v[num].z = std::atof(data_.c_str());
v.push_back(pcl::PointXYZ(0, 0, 0));
basic_cloud_ptr->points.push_back(v[num]);
num++;
}
}
v.pop_back();
ifs.close();
pcl::PointCloud<pcl::PointXYZRGB>::Ptr m_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::copyPointCloud(*basic_cloud_ptr, *m_cloud); // PointXYZ转化为PointXYZRGB
return m_cloud;
}```