0
$\begingroup$

Rosanswers logo

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.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

But I can't seem to import anything and visualize successfully.

RViz visualises data streams, not files, so in order for you to be able to have it visualise the output of your pointcloud processing, you'll have to publish those clouds on a topic.


Edit (after posting your own code): if all you wanted was to publish a .pcd file (instead of the results of your 'manipulation' directly (ie: without going through the FS)), then the pcd_to_pointcloud node in the pcl_ros package would probably have been better.


Originally posted by gvdhoorn with karma: 86574 on 2017-10-15

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2017-10-15:
And a bit of a nitpick: you can't really 'import' anything into RViz. It's not a visualiser like the pcd_viewer from PCL.

Comment by MT on 2017-10-16:
What exactly is FS? Just for future reference.

Comment by gvdhoorn on 2017-10-16:
The file system.

$\endgroup$
0
$\begingroup$

Rosanswers logo

Answer 1:

# 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 ();
  }
}

Answer 2: pcd_to_pointcloud


Originally posted by MT with karma: 31 on 2017-10-16

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Ajay on 2018-05-12:
Thank you for the code , It worked for me

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.