Writing Point Cloud data to PCD files — Point Cloud Library 0.0 documentation (original) (raw)
In this tutorial we will learn how to write point cloud data to a PCD file.
The code
First, create a file called, let’s say, pcd_write.cpp
in your favorite editor, and place the following code inside it:
1#include 2#include <pcl/io/pcd_io.h> 3#include <pcl/point_types.h> 4 5int 6 main () 7{ 8 pcl::PointCloudpcl::PointXYZ cloud; 9 10 // Fill in the cloud data 11 cloud.width = 5; 12 cloud.height = 1; 13 cloud.is_dense = false; 14 cloud.resize (cloud.width * cloud.height); 15 16 for (auto& point: cloud) 17 { 18 point.x = 1024 * rand () / (RAND_MAX + 1.0f); 19 point.y = 1024 * rand () / (RAND_MAX + 1.0f); 20 point.z = 1024 * rand () / (RAND_MAX + 1.0f); 21 } 22 23 pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud); 24 std::cerr << "Saved " << cloud.size () << " data points to test_pcd.pcd." << std::endl; 25 26 for (const auto& point: cloud) 27 std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl; 28 29 return (0); 30}
The explanation
Now, let’s break down the code piece by piece.
#include <pcl/io/pcd_io.h> #include <pcl/point_types.h>
The first file is the header that contains the definitions for PCD I/O operations, and second one contains definitions for several point type structures, including pcl::PointXYZ
that we will use.
pcl::PointCloudpcl::PointXYZ cloud;
describes the templated PointCloud structure that we will create. The type of each point is set to pcl::PointXYZ
, which is a structure that has x
,y
, and z
fields.
The lines:
// Fill in the cloud data cloud.width = 5; cloud.height = 1; cloud.is_dense = false; cloud.resize (cloud.width * cloud.height);
for (auto& point: cloud) { point.x = 1024 * rand () / (RAND_MAX + 1.0f); point.y = 1024 * rand () / (RAND_MAX + 1.0f); point.z = 1024 * rand () / (RAND_MAX + 1.0f); }
fill in the PointCloud structure with random point values, and set the appropriate parameters (width, height, is_dense).
Then:
pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);
saves the PointCloud data to disk into a file called test_pcd.pcd
Finally:
std::cerr << "Saved " << cloud.size () << " data points to test_pcd.pcd." << std::endl;
for (const auto& point: cloud) std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl;
is used to show the data that was generated.
Compiling and running the program
Add the following lines to your CMakeLists.txt file:
1cmake_minimum_required(VERSION 3.5 FATAL_ERROR) 2 3project(pcd_write) 4 5find_package(PCL 1.2 REQUIRED) 6 7add_executable (pcd_write pcd_write.cpp) 8target_link_libraries (pcd_write ${PCL_LIBRARIES})
After you have made the executable, you can run it. Simply do:
You will see something similar to:
Saved 5 data points to test_pcd.pcd. 0.352222 -0.151883 -0.106395 -0.397406 -0.473106 0.292602 -0.731898 0.667105 0.441304 -0.734766 0.854581 -0.0361733 -0.4607 -0.277468 -0.916762
You can check the content of the file test_pcd.pcd, using:
$ cat test_pcd.pcd
.PCD v.5 - Point Cloud Data file format
FIELDS x y z SIZE 4 4 4 TYPE F F F WIDTH 5 HEIGHT 1 POINTS 5 DATA ascii 0.35222 -0.15188 -0.1064 -0.39741 -0.47311 0.2926 -0.7319 0.6671 0.4413 -0.73477 0.85458 -0.036173 -0.4607 -0.27747 -0.91676