1.1 写入数据
新建项目(文件夹)MYP,在其中新建ma.cpp,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h>
int main (int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZ> cloud;
cloud.width = 5; cloud.height = 1; cloud.is_dense = false; cloud.points.resize (cloud.width * cloud.height); for (size_t i = 0; i < cloud.points.size (); ++i) { cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f); cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f); cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f); } pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);
std::cerr << "Saved " << cloud.points.size () << " data points to test_pcd.pcd." << std::endl;
for (size_t i = 0; i < cloud.points.size (); ++i) std::cerr << " " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;
return (0); }
|
在新一个CMakeLists.txt,添加如下:
主要干这么几件事:
- 指定cmake版本,
- 添加用到的库
- 生成可执行文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| cmake_minimum_required ( VERSION 2.6 FATAL_ERROR) project(Towrite)
find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARIES_DIRS}) add_definitions(${PCL_DEFINITIONS})
add_executable(mmmy_writer main.cpp)
target_link_libraries(mmmy_writer ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})
|
在当前路径下打开终端:
1 2 3 4 5
| mkdir build cd build cmake .. make ./mmmy_writer
|
如果正常的话,build文件夹会生成可执行程序mmmy_writer,并运行,终端会输出结果如下:
1.2 读取数据
在ma.cpp中添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h>
int main (int argc, char** argv) { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); std::cout<<"read pcl"<<std::endl; if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) { PCL_ERROR ("Couldn't read file test_pcd.pcd \n"); return (-1); }
std::cout << "Loaded " << cloud->width * cloud->height << " data points from test_pcd.pcd with the following fields: " << std::endl; for (size_t i = 0; i < cloud->points.size (); ++i) std::cout << " " << cloud->points[i].x << " " << cloud->points[i].y << " " << cloud->points[i].z << std::endl;
return (0); }return (-1);
|