LASlib库将PCL库点云类型数据转换为las格式保存

       pcl 是一个命名空间,跟std类似,PointCloud是类模板,是模板类实例化的类型。

       在使用pcl::PointCloud::Ptr时需要使用new进行初始化,如下:

pcl::PointCloud::Ptr cloudxyzi(new pcl::PointCloud);

       在使用 pcl::PointCloud cloudxyzi时作为一个对象。

       两者可以相互转换,如下:

//PointCloud::Ptr—>PointCloud
pcl::PointCloud cloud;
pcl::PointCloud::Ptr cloud_ptr(new pcl::PointCloud);
cloud=*cloud_ptr;

//PointCloud->PointCloud::Ptr
pcl::PointCloud::Ptr cloud_ptr(new pcl::PointCloud);
pcl::PointCloud cloud;
cloud_ptr=cloud.makeShared();

        在实际的使用中,我们常常需要将pcl类型的数据保存为las格式。具体的代码如下所示:

	int pcl2las(const std::string &output_lasfile,
		const pcl::PointCloud::Ptr &cloud)
	{
		if (output_lasfile.empty() || output_lasfile.find_last_of(".") < 0)
		{
			//printf("   Error in lasread: input file is valid.");
			return 0;
		}

		int pos = output_lasfile.find_last_of(".");
		std::string ext = output_lasfile.substr(pos);
		if (ext != ".las")
		{
			//printf("   Error in laswrite: %s is not *.las file.\n", output_lasfile.c_str());
			return 0;
		}

		LASwriteOpener laswriteopener;
		laswriteopener.set_file_name(output_lasfile.c_str());
		if (!laswriteopener.active())
		{
			//printf("   Error in \"laswrite\": output lasfile is not specified.\n");
			return 0;
		}
		LASheader lasheader;
		lasheader.x_scale_factor = 1.0;
		lasheader.y_scale_factor = 1.0;
		lasheader.z_scale_factor = 1.0;
		lasheader.x_offset = 0.0;
		lasheader.y_offset = 0.0;
		lasheader.z_offset = 0.0;
		lasheader.point_data_format = 1;
		lasheader.point_data_record_length = 28;

		LASpoint laspoint;
		laspoint.init(&lasheader, lasheader.point_data_format, lasheader.point_data_record_length, 0);

		LASwriter* laswriter = NULL;
		laswriter = laswriteopener.open(&lasheader);
		if (laswriter == 0)
		{
			printf("   Error in \"laswrite\": could not open laswriter.\n");
			return 0;
		}
		
		// write points
		if (cloud->points.size() > 0)
		{
			for (size_t i = 0; i < cloud->points.size(); ++i)
			{
				double x, y, z;
				unsigned short intensity;
				x = (cloud->points[i].x - lasheader.x_offset) * lasheader.x_scale_factor;
				y = (cloud->points[i].y - lasheader.y_offset) * lasheader.y_scale_factor;
				z = (cloud->points[i].z - lasheader.z_offset) * lasheader.z_scale_factor;
				intensity = cloud->points[i].intensity;
				// populate the point
				laspoint.set_x(x);
				laspoint.set_y(y);
				laspoint.set_z(z);
				laspoint.set_intensity(intensity);
				/*laspoint.set_intensity((U16)i);
				laspoint.set_gps_time(0.0006*i);*/

				// write the point
				laswriter->write_point(&laspoint);

				// add it to the inventory
				laswriter->update_inventory(&laspoint);
			}
			// update the header
			laswriter->update_header(&lasheader, TRUE);

			laswriter->close();
			delete laswriter;
			laswriter = NULL;

			return 1;
		}
		else
		{
			printf("   Error in \"laswrite\": output pointset is empty.\n");
			return 0;
		}
	}

 

你可能感兴趣的:(点云读写,C++,PCL)