如何在 C++ 和 ROS 中正确读取点云文件
How to properly read a Point Cloud File in C++ and ROS
我刚开始使用 Point Cloud Library
,一开始我想从文件中读取点云。我关注了与此相关的tutorial。这只是我正在构建的一个主要 CMake
项目的一个小例子。只是和教程略有不同,我把项目分了,让它更CMake
合适。 CMake
运行 很好,项目似乎是有组织的。但是,当我尝试 运行 项目时,我得到以下 /home/emanuele/catkin_ws/src/map_ros/src/pointcloud_reader_node.cpp:6:10: fatal error: ../map_ros/include/cloud.h: No such file or directory #include "../map_ros/include/cloud.h"
error::Cloud::readPCloud(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
并且我不知道如何解释这个错误。
在我使用的代码片段下方:
cloud.h
#ifndef CLOUD_H
#define CLOUD_H
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <string>
class Cloud
{
public:
void readPCloud(std::string filename);
private:
std::string path;
};
#endif// CLOUD_H
cloud.cpp
#include "cloud.h"
void Cloud::readPCloud(std::string filename)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud) == -1) // load point cloud file
{
PCL_ERROR("Could not read the file");
return;
}
std::cout<<"Loaded"<<cloud->width * cloud->height
<<"data points from filename 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;
}
pointcloud_reader_node.cpp
#include <ros/ros.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include "../map_ros/include/cloud.h"
using namespace std;
int main()
{
std::string fstring = "/home/to/Desktop/file.pcd";
Cloud p;
p.readPCloud(fstring); // <-- Error Here
return 0;
}
另外为了完整起见,我在下面添加 CMake
文件:
cmake_minimum_required(VERSION 2.8.3)
project(map_ros)
add_compile_options(-std=c++11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(catkin REQUIRED COMPONENTS
// ....
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS
// ......
)
###########
## Build ##
###########
include_directories(${catkin_INCLUDE_DIRS})
add_executable(pointcloud_reader_node src/pointcloud_reader_node.cpp ${SRCS})
target_link_libraries(pointcloud_reader_node ${catkin_LIBRARIES})
我前一段时间已经解决了我的问题,但想分享它以防有人遇到我同样的问题。
所以有两个问题同时出现,让我认为这只是一个 CMake
问题:
1) catkin_make
没有正确编译不是因为 CMake
我想了很久,而是因为缓存文件 catkin_ws.workspace
导致 [=12= 出现问题] 本身。所以这个问题的第一个解决方案是删除缓存文件 catkin_ws.workspace
并重新编译。所有 CMake
个问题都消失了。
2) 第二个问题:读取 point-cloud
的正确伪代码如下:
main()
{
init node
create pointcloud publisher
create rate object with 1 second duration
load point cloud from file
while(ros::ok())
{
rate.sleep
publish point cloud message
}
}
我意识到输入中没有发布任何内容,并且执行了回调。
下面是从文件读取 point-cloud
并将所有点输出到 .txt 文件的完整代码。我希望这对任何可能遇到这个问题的人都有帮助:
test.cpp
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/io/pcd_io.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
void loadFromFile(std::string filename)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud) == -1) // load point cloud file
{
PCL_ERROR("Could not read the file");
return;
}
std::cout<<"Loaded"<<cloud->width * cloud->height
<<"data points from /home/to/Desktop/point_cloud/yourFile.pcd with the following fields: "
<<std::endl;
// Write entire point clouds to a .txt file
std::ofstream myfile;
myfile.open ("/home/to/Desktop/exampleCloud.txt");
if (myfile.is_open()) {
for(size_t i = 0; i < cloud->points.size(); ++i)
myfile << " " << cloud->points[i].x
<< " " << cloud->points[i].y
<< " " << cloud->points[i].z << std::endl;
myfile.close();
}
}
int main (int argc, char** argv)
{
// Initialize ROS
ros::init (argc, argv, "pcl_tutorial_cloud");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud2>("output", 1000);
ros::Rate loop_rate(1);
loadFromFile("/home/to/Desktop/yourFile.pcd");
int count = 0;
while(ros::ok())
{
sensor_msgs::PointCloud2 pcloud2;
pub.publish(pcloud2);
ros::spinOnce();
loop_rate.sleep();
count ++;
}
return 0;
}
这是 运行 之后的结果:
1) catkin_make
2) rosrun yourProject test
我刚开始使用 Point Cloud Library
,一开始我想从文件中读取点云。我关注了与此相关的tutorial。这只是我正在构建的一个主要 CMake
项目的一个小例子。只是和教程略有不同,我把项目分了,让它更CMake
合适。 CMake
运行 很好,项目似乎是有组织的。但是,当我尝试 运行 项目时,我得到以下 /home/emanuele/catkin_ws/src/map_ros/src/pointcloud_reader_node.cpp:6:10: fatal error: ../map_ros/include/cloud.h: No such file or directory #include "../map_ros/include/cloud.h"
error::Cloud::readPCloud(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
并且我不知道如何解释这个错误。
在我使用的代码片段下方:
cloud.h
#ifndef CLOUD_H
#define CLOUD_H
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <string>
class Cloud
{
public:
void readPCloud(std::string filename);
private:
std::string path;
};
#endif// CLOUD_H
cloud.cpp
#include "cloud.h"
void Cloud::readPCloud(std::string filename)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud) == -1) // load point cloud file
{
PCL_ERROR("Could not read the file");
return;
}
std::cout<<"Loaded"<<cloud->width * cloud->height
<<"data points from filename 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;
}
pointcloud_reader_node.cpp
#include <ros/ros.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include "../map_ros/include/cloud.h"
using namespace std;
int main()
{
std::string fstring = "/home/to/Desktop/file.pcd";
Cloud p;
p.readPCloud(fstring); // <-- Error Here
return 0;
}
另外为了完整起见,我在下面添加 CMake
文件:
cmake_minimum_required(VERSION 2.8.3)
project(map_ros)
add_compile_options(-std=c++11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(catkin REQUIRED COMPONENTS
// ....
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS
// ......
)
###########
## Build ##
###########
include_directories(${catkin_INCLUDE_DIRS})
add_executable(pointcloud_reader_node src/pointcloud_reader_node.cpp ${SRCS})
target_link_libraries(pointcloud_reader_node ${catkin_LIBRARIES})
我前一段时间已经解决了我的问题,但想分享它以防有人遇到我同样的问题。
所以有两个问题同时出现,让我认为这只是一个 CMake
问题:
1) catkin_make
没有正确编译不是因为 CMake
我想了很久,而是因为缓存文件 catkin_ws.workspace
导致 [=12= 出现问题] 本身。所以这个问题的第一个解决方案是删除缓存文件 catkin_ws.workspace
并重新编译。所有 CMake
个问题都消失了。
2) 第二个问题:读取 point-cloud
的正确伪代码如下:
main()
{
init node
create pointcloud publisher
create rate object with 1 second duration
load point cloud from file
while(ros::ok())
{
rate.sleep
publish point cloud message
}
}
我意识到输入中没有发布任何内容,并且执行了回调。
下面是从文件读取 point-cloud
并将所有点输出到 .txt 文件的完整代码。我希望这对任何可能遇到这个问题的人都有帮助:
test.cpp
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/io/pcd_io.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
void loadFromFile(std::string filename)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud) == -1) // load point cloud file
{
PCL_ERROR("Could not read the file");
return;
}
std::cout<<"Loaded"<<cloud->width * cloud->height
<<"data points from /home/to/Desktop/point_cloud/yourFile.pcd with the following fields: "
<<std::endl;
// Write entire point clouds to a .txt file
std::ofstream myfile;
myfile.open ("/home/to/Desktop/exampleCloud.txt");
if (myfile.is_open()) {
for(size_t i = 0; i < cloud->points.size(); ++i)
myfile << " " << cloud->points[i].x
<< " " << cloud->points[i].y
<< " " << cloud->points[i].z << std::endl;
myfile.close();
}
}
int main (int argc, char** argv)
{
// Initialize ROS
ros::init (argc, argv, "pcl_tutorial_cloud");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud2>("output", 1000);
ros::Rate loop_rate(1);
loadFromFile("/home/to/Desktop/yourFile.pcd");
int count = 0;
while(ros::ok())
{
sensor_msgs::PointCloud2 pcloud2;
pub.publish(pcloud2);
ros::spinOnce();
loop_rate.sleep();
count ++;
}
return 0;
}
这是 运行 之后的结果:
1) catkin_make
2) rosrun yourProject test