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

Revision history [back]

click to hide/show revision 1
initial version

I found some relevant information and example code snippets here:

  • http://docs.ros.org/kinetic/api/sensor_msgs/html/classsensor__msgs_1_1PointCloud2Iterator.html

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 of 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> iter(*msg, "x"); iter != iter.end(); ++iter) {
        // TODO: do something with the values of x, y, z
        std::cout << iter[0] << ", " << iter[1] << ", " << iter[2] << '\n';
    }
}

I found some relevant information and example code snippets here:

  • http://docs.ros.org/kinetic/api/sensor_msgs/html/classsensor__msgs_1_1PointCloud2Iterator.html

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 of 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> iter(*msg, it(*msg, "x"); iter it != iter.end(); ++iter) it.end(); ++it) {
        // TODO: do something with the values of x, y, z
        std::cout << iter[0] it[0] << ", " << iter[1] it[1] << ", " << iter[2] it[2] << '\n';
    }
}

I found some relevant information and example code snippets here:

  • http://docs.ros.org/kinetic/api/sensor_msgs/html/classsensor__msgs_1_1PointCloud2Iterator.html

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 of 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';
    }
}