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

Accessing values while iterating points in sensor_msgs::PointCloud2 from sensor_msgs::PointCloud2ConstIterator

asked 2018-03-16 03:32:28 -0600

moooeeeep gravatar image

updated 2018-03-16 03:35:46 -0600

I am writing a node that subscribes to a topic of type sensor_msgs::PointCloud2.

In the callback I want to iterate the points in the point cloud using sensor_msgs::PointCloud2ConstIterator.

This is how I think it should be implemented (which seems to work, no serious testing so far, though):

void callback(sensor_msgs::PointCloud2ConstPtr const& msg) {
    sensor_msgs::PointCloud2ConstIterator<float> iter_x(*msg, "x"),
                                                 iter_y(*msg, "y"),
                                                 iter_z(*msg, "z");
    while (iter_x != iter_x.end()) {
        // TODO: do something with the values of x, y, z
        std::cout << *iter_x << ", " << *iter_y << ", " << *iter_z << "\n";
        ++iter_x; ++iter_y; ++iter_z;
    }
}

Is this about the recommended way of iterating the points in a point cloud?

I've seen there was also an overload of the element access operator[](...). While I was trying to figure out how element access exactly works, I wrote this little callback to get an impression:

void callback(sensor_msgs::PointCloud2ConstPtr const& msg) {
    sensor_msgs::PointCloud2ConstIterator<float> iter_x(*msg, "x");
    std::cout << *iter_x << ", " 
        << iter_x[0] << ", " 
        << iter_x[1] << ", "
        << *(iter_x+1) << "\n";
}

I got an output like this:

1.04852, 1.04852, -1.78216, 1.54717

I wondered, shouldn't the last two values be identical?

edit retag flag offensive close merge delete

Comments

I am new to point cloud as well. Usually i do

  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  pcl::fromROSMsg(*input, *cloud);
 for(auto& point : cloud->points)
 {
    point.x
 }
BhanuKiran.Chaluvadi gravatar image BhanuKiran.Chaluvadi  ( 2018-03-16 09:44:46 -0600 )edit

I assumed that this would create a copy of the entire point cloud, and I'm still not sure if I can use pcl for the next step of this task...

moooeeeep gravatar image moooeeeep  ( 2018-03-16 10:11:28 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-03-20 04:11:14 -0600

moooeeeep gravatar image

updated 2018-03-20 04:31:17 -0600

I found some relevant information and example code snippets here:

Essentially the element access operator is used for accessing the different components of each point.

That is, the iterator iterates the points, while providing access to the coordinates and additional data members for each point via the operator[](). The field given by name in the constructor of the iterator is then found at position zero. The remaining fields are available as they are stored the PointCloud2 message. You somehow have to know the order and number and the type of the remaining fields to be safe. (Or just setup separate iterators for each data member of the points...)

That is, one can iterate the points like this:

void callback(sensor_msgs::PointCloud2ConstPtr const& msg) {
    for (sensor_msgs::PointCloud2ConstIterator<float> it(*msg, "x"); it != it.end(); ++it) {
        // TODO: do something with the values of x, y, z
        std::cout << it[0] << ", " << it[1] << ", " << it[2] << '\n';
    }
}
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-03-16 03:32:28 -0600

Seen: 6,812 times

Last updated: Mar 20 '18