Accessing values while iterating points in sensor_msgs::PointCloud2 from sensor_msgs::PointCloud2ConstIterator
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?
I am new to point cloud as well. Usually i do
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...