def array_to_pointcloud2(cloud_arr, stamp=None, frame_id=None):
'''Converts a numpy record array to a sensor_msgs.msg.PointCloud2.
'''
# make it 2d (even if height will be 1)
cloud_arr = np.atleast_2d(cloud_arr)
cloud_msg = PointCloud2()
if stamp is not None:
cloud_msg.header.stamp = stamp
if frame_id is not None:
cloud_msg.header.frame_id = frame_id
cloud_msg.height = cloud_arr.shape[0]
cloud_msg.width = cloud_arr.shape[1]
cloud_msg.fields = dtype_to_fields(cloud_arr.dtype)
cloud_msg.is_bigendian = False # assumption
cloud_msg.point_step = cloud_arr.dtype.itemsize
cloud_msg.row_step = cloud_msg.point_step*cloud_arr.shape[1]
cloud_msg.is_dense = all([np.isfinite(cloud_arr[fname]).all() for fname in cloud_arr.dtype.names])
cloud_msg.data = cloud_arr.tostring()
return cloud_msg
评论列表
文章目录