Skip to content

Instantly share code, notes, and snippets.

@bricerebsamen
Created May 7, 2015 17:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricerebsamen/a74fc627390d4c685012 to your computer and use it in GitHub Desktop.
Save bricerebsamen/a74fc627390d4c685012 to your computer and use it in GitHub Desktop.
void
RingColors::convertPoints(const sensor_msgs::PointCloud2ConstPtr &inMsg)
{
if (output_.getNumSubscribers() == 0) // no one listening?
return; // do nothing
// allocate an PointXYZRGB message with same time and frame ID as
// input data
sensor_msgs::PointCloud2::Ptr outMsg(new sensor_msgs::PointCloud2());
sensor_msgs::PointCloud2Modifier modifier(*outMsg);
modifier.setPointCloud2FieldsByString(2, "xyz", "rgb");
modifier.resize(inMsg->height * inMsg->width);
outMsg->header.stamp = inMsg->header.stamp;
outMsg->header.frame_id = inMsg->header.frame_id;
outMsg->height = 1;
sensor_msgs::PointCloud2Iterator<float> out_x(*outMsg, "x");
sensor_msgs::PointCloud2Iterator<float> out_y(*outMsg, "y");
sensor_msgs::PointCloud2Iterator<float> out_z(*outMsg, "z");
sensor_msgs::PointCloud2Iterator<uint8_t> out_r(*outMsg, "r");
sensor_msgs::PointCloud2Iterator<uint8_t> out_g(*outMsg, "g");
sensor_msgs::PointCloud2Iterator<uint8_t> out_b(*outMsg, "b");
sensor_msgs::PointCloud2ConstIterator<float> in_x(*inMsg, "x");
sensor_msgs::PointCloud2ConstIterator<float> in_y(*inMsg, "y");
sensor_msgs::PointCloud2ConstIterator<float> in_z(*inMsg, "z");
sensor_msgs::PointCloud2ConstIterator<uint16_t> in_ring(*inMsg, "ring");
for (size_t i = 0; i < inMsg->height * inMsg->width; ++i, ++out_x, ++out_y, ++out_z, ++out_r, ++out_g, ++out_b,
++in_x, ++in_y, ++in_z, ++in_ring)
{
*out_x = *in_x;
*out_y = *in_y;
*out_z = *in_z;
// color lasers with the rainbow array
int color = *in_ring % N_COLORS;
*out_r = (rainbow[color] >> 16) & 0x0000ff;
*out_g = (rainbow[color] >> 8) & 0x0000ff;
*out_b = rainbow[color] & 0x0000ff;
}
output_.publish(outMsg);
}
@bricerebsamen
Copy link
Author

how to work with a PointCloud2 message without using PCL. I got it from ros-drivers/velodyne#49 from Vincent Rabaud.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment