ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to clone a PointCloud in a subscriber callback

asked 2014-09-18 11:30:18 -0500

madmage gravatar image

updated 2014-09-18 11:44:27 -0500

Dear all, I am using Indigo, coming from Groovy and Hydro, I found it difficult to understand the internals of pcl_ros and pcl_conversions packages.

My problem is the following: I have a subscription callback like this: void RosHandler::xtionCloudCallback(const sensor_msgs::PointCloud2::ConstPtr& msg); or, for me it's the same, like this: void RosHandler::xtionCloudCallback(const pcl::PointCloud<pcl::pointxyzrgb>::ConstPtr& msg);

the first question is: is this pcl::PointCloud<pcl::pointxyzrgb> the same as in pcl? if it is the same, how can I clone the data, in such a way that I can use them without bothering of what ROS is doing meanwhile (i.e., in another thread)?

What I did in Groovy (and it worked) was to manually copy the message field by field (and cloning "data"), but it does not work anymore (and, indeed, I was not sure it was safe to do that in that way).

EDIT: when I say "it does not work" I mean that it segfault when I delete the new (cloned) cloud, so this means the ownership of the cloud was not (entirely?) mine EDIT2: the problem is not only I cannot delete the cloud, of course, but also that I cannot read it (it has been deleted by someone else)

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-09-19 08:10:13 -0500

Wolf gravatar image

just to expand on @paulbovbel 's answer

If you want to continue with processing shared_ptrs maybe the makeShared() method helps you. Creates a deep copy of the cloud and returns you a shared_ptr to the copied cloud, see:

edit flag offensive delete link more

answered 2014-09-18 14:37:33 -0500

paulbovbel gravatar image

Yes it is the same object as in PCL, so the same APIs will work. pcl::Poincloud has a copy constructor that you can use to make the copy (pcl::Poincloud copy = pcl::PointCloud(*cloud_msg_const_ptr))

Addendum, the message you receive is const, so you can feel safe using the received cloud directly without copying, as long as you don't need/try to modify it.

edit flag offensive delete link more


Actually, I temporary solved the problem,by cloning the vector<pointxyzrgb> (that I know how to deep-clone), and it works without segfaults. It means that during the copy (I tested with the copy constructor and with the copy field by field) there's something in the header that is not copied properly

madmage gravatar image madmage  ( 2014-09-19 05:21:36 -0500 )edit

Either copy constructor or @Wolf's answer (makeShared()) should work to make a deep copy the original object.

paulbovbel gravatar image paulbovbel  ( 2014-09-19 10:59:29 -0500 )edit

I'm not really sure what you mean by cloning, but for modern C++ with std and boost libraries and RAII, there's not many occasions when you should be calling delete on anything, ever. If you want to post a gist of your code, I could tell you more.

paulbovbel gravatar image paulbovbel  ( 2014-09-19 11:00:34 -0500 )edit

Question Tools

1 follower


Asked: 2014-09-18 11:30:18 -0500

Seen: 5,607 times

Last updated: Sep 19 '14