template <typename PointT> void fromPCLPointCloud2 (const pcl::PCLPointCloud2& msg, pcl::PointCloud<PointT>& cloud, const MsgFieldMap& field_map) { // Copy info fields cloud.header = msg.header; cloud.width = msg.width; cloud.height = msg.height; cloud.is_dense = msg.is_dense == 1; // Copy point data cloud.resize (msg.width * msg.height); std::uint8_t* cloud_data = reinterpret_cast<std::uint8_t*>(&cloud[0]); // Check if we can copy adjacent points in a single memcpy. We can do so if there // is exactly one field to copy and it is the same size as the source and destination // point types. if (field_map.size() == 1 && field_map[0].serialized_offset == 0 && field_map[0].struct_offset == 0 && field_map[0].size == msg.point_step && field_map[0].size == sizeof(PointT)) { const auto cloud_row_step = (sizeof (PointT) * cloud.width); const std::uint8_t* msg_data = &msg.data[0]; // Should usually be able to copy all rows at once if (msg.row_step == cloud_row_step) { memcpy (cloud_data, msg_data, msg.data.size ()); } else { for (uindex_t i = 0; i < msg.height; ++i, cloud_data += cloud_row_step, msg_data += msg.row_step) memcpy (cloud_data, msg_data, cloud_row_step); } } else { // If not, memcpy each group of contiguous fields separately for (uindex_t row = 0; row < msg.height; ++row) { const std::uint8_t* row_data = &msg.data[row * msg.row_step]; for (uindex_t col = 0; col < msg.width; ++col) { const std::uint8_t* msg_data = row_data + col * msg.point_step; for (const detail::FieldMapping& mapping : field_map) { memcpy (cloud_data + mapping.struct_offset, msg_data + mapping.serialized_offset, mapping.size); } cloud_data += sizeof (PointT); } } } }
时间: 2023-06-20 12:06:11 浏览: 190
cpp代码-模板 泛型 template<typename> template<class>
这是一个模板函数,用于将PCLPointCloud2类型的数据转换为PointCloud<PointT>类型的点云数据。其中,PointT是点云中点的数据类型。该函数会将PCLPointCloud2中的点云数据复制到PointCloud<PointT>中。
在函数中,首先将PCLPointCloud2中的header、width、height、is_dense等信息复制到PointCloud<PointT>中。然后根据点云的大小调整PointCloud<PointT>的大小,并将数据复制到PointCloud<PointT>中。在复制数据时,先判断是否可以一次性复制整个点云,如果可以则使用memcpy函数将数据一次性复制到PointCloud<PointT>中。如果不能一次性复制,则需要分别复制每个点的数据。在复制每个点的数据时,需要遍历field_map中的每个字段,将每个字段的数据从PCLPointCloud2中复制到PointCloud<PointT>中。最后返回转换后的PointCloud<PointT>类型的点云数据。
阅读全文