Hi Community!
I'm trying to import and manipulate a PCD file in C++ code then visualize the results in Rviz. I feel like this is an easy problem to solve with a couple lines of code but I can't seem to figure them out. I have successfully connected Rviz to my Kinect and Lidar devices and then use PCL etc. to manipulate the live point messages. But I can't seem to import anything and visualize successfully. Any suggestions??
EDIT: Figured it out! heres the code. it might be a bit inefficient but it works.
# include <iostream>
# include <string>
#include <ros/ros.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl_ros/io/pcd_io.h>
typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
using namespace std;
int main(int argc, char** argv)
{
double x_cloud; double y_cloud; double z_cloud;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
ros::init (argc, argv, "my_pcl_tutorial");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<PointCloud> ("points2", 1);
pcl::io::loadPCDFile<pcl::PointXYZ> ("yourfile.pcd", *cloud);
PointCloud::Ptr msg (new PointCloud);
msg->header.frame_id = "frame1";
msg->height = cloud->height;
msg->width = cloud->width;
for (size_t i = 0; i < cloud->size (); i++) {
x_cloud = cloud->points[i].x;
y_cloud = cloud->points[i].y;
z_cloud = cloud->points[i].z;
msg->points.push_back (pcl::PointXYZ (x_cloud, y_cloud, z_cloud));
}
ros::Rate loop_rate(4);
while (nh.ok())
{
//msg->header.stamp = ros::Time::now().toNSec();
pub.publish (msg);
ros::spinOnce ();
loop_rate.sleep ();
}
}
Originally posted by MT on ROS Answers with karma: 31 on 2017-10-14
Post score: 0
Original comments
Comment by gvdhoorn on 2017-10-16:
If you feel your code is the correct answer to your question (but please take a look at pcd_to_pointcloud
), please post it as an answer yourself and accept your own answer.
Comment by MT on 2017-10-16:
The goal was to perform PCL tutorials without the kinect but the PCD files given. This was the fist step of many.