PCL读取TXT文件点云并显示

PCL读取TXT文件点云并显示

#include 
#include 
#include 

//---------------读取txt文件-------------------
void CreateCloudFromTxt(const std::string& file_path, pcl::PointCloud::Ptr cloud)
{
	std::ifstream file(file_path.c_str());//c_str():生成一个const char*指针,指向以空字符终止的数组。
	std::string line;
	pcl::PointXYZ point;
	//float nx, ny, nz;
	while (getline(file, line)) {
		std::stringstream ss(line);
		ss >> point.x;
		ss >> point.y;
		ss >> point.z;
		//ss >> nx;
		//ss >> ny;
		//ss >> nz;
		cloud->push_back(point);
	}
	file.close();
}


void visualization(pcl::PointCloud::Ptr cloud)
{
	boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));

	// 添加需要显示的点云数据

	pcl::visualization::PointCloudColorHandlerCustom single_color(cloud, 255, 0, 0);
	viewer->addPointCloud(cloud, single_color, "example");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

	while (!viewer->wasStopped()) {
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
}

int main(int argc, char** argv) {

	//程序运行时间*******************************************************//
	clock_t start, end;
	start = clock();

	// -------------------加载点云----------------------
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	CreateCloudFromTxt("ccc5.asc", cloud);

	end = clock();
	cout << "程序运行时间: " << (double)(end - start) << " ms" << endl;
	// -----------------可视化点云---------------------
	visualization(cloud);


	return 0;
}
PCL读取TXT文件点云并显示_第1张图片

你可能感兴趣的:(三维点云学习过程,c++)