下面是一个简单的读取PCD文件并显示的代码:
#include
#include
#include
#include
#include
void main()
{
/* Create Point Cloud */
pcl::PointCloud<:pointxyzrgba>::Ptr cloud(new pcl::PointCloud<:pointxyzrgba>);
/* Read PCD File */
/* Read Wrong */
if (- == pcl::io::loadPCDFile<:pointxyzrgba>("table_scene_lms400.pcd", *cloud))
{
return;
}
/* Then show the point cloud */
boost::shared_ptr<:visualization::pclvisualizer> viewer(new pcl::visualization::PCLVisualizer("office chair model"));
viewer->setBackgroundColor(, , );
pcl::visualization::PointCloudColorHandlerRGBField<:pointxyzrgba> rgba(cloud); //Maybe set the cloud to he handler rgba?
viewer->addPointCloud<:pointxyzrgba>(cloud, rgba, "sample cloud"); //Add a Point Cloud (templated) to screen. Q:Some questions here
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, , "sample cloud"); //Set the rendering properties
//viewer->addCoordinateSystem(1.0); //Adds 3D axes describing a coordinate system to screen at 0,0,0
viewer->initCameraParameters (); //Initialize camera parameters with some default values.
/* Show the point cloud */
while (!viewer->wasStopped ())
{
viewer->spinOnce (); //updates the screen loop
boost::this_thread::sleep (boost::posix_time::microseconds ());
}
}
pcl::io::loadPCDFile用于读取一个PCD文件至一个PointCloud类型,这里就是将table_scene_lms400.pcd文件里的数据读取至cloud里。
在PCL文档里关于pcl::io::loadPCDFile的实现有3个,我目前只看了第一种。
下面看看loadPCDFile在namespace io里的实现:
template inline int
loadPCDFile (const std::string &file_name, pcl::PointCloud &cloud)
{
pcl::PCDReader p;
return (p.read (file_name, cloud));
}
可以看到loadPCDFile 这个内联函数,就是调用了一下pcl::PCDReader里的read函数。
继续看PCDReader函数:
template int
read (const std::string &file_name, pcl::PointCloud &cloud, const int offset = 0)
{
pcl::PCLPointCloud2 blob;
int pcd_version;
int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_, pcd_version, offset);
// If no error, convert the data
if (res == 0)
pcl::fromPCLPointCloud2 (blob, cloud);
return (res);
}
最后在pdc_io.cpp里找到代码:
int
pcl::PCDReader::read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,
const int offset)
{
pcl::console::TicToc tt;
tt.tic ();
int data_type;
unsigned int data_idx;
int res = readHeader (file_name, cloud, origin, orientation, pcd_version, data_type, data_idx, offset);
if (res < )
return (res);
unsigned int idx = ;
// Get the number of points the cloud should have
unsigned int nr_points = cloud.width * cloud.height;
// Setting the is_dense property to true by default
cloud.is_dense = true;
if (file_name == "" || !boost::filesystem::exists (file_name))
{
PCL_ERROR ("[pcl::PCDReader::read] Could not find file '%s'.\n", file_name.c_str ());
return (-);
}
// if ascii
if (data_type == )
{
// Re-open the file (readHeader closes it)
std::ifstream fs;
fs.open (file_name.c_str ());
if (!fs.is_open () || fs.fail ())
{
PCL_ERROR ("[pcl