43 #pragma GCC system_header
46 #include <pcl/PCLPointField.h>
47 #include <pcl/PCLPointCloud2.h>
48 #include <pcl/PCLImage.h>
49 #include <pcl/point_cloud.h>
50 #include <pcl/type_traits.h>
51 #include <pcl/for_each_type.h>
52 #include <pcl/console/print.h>
54 #include <boost/foreach.hpp>
62 template<
typename Po
intT>
70 f.
name = traits::name<PointT, U>::value;
71 f.
offset = traits::offset<PointT, U>::value;
72 f.
datatype = traits::datatype<PointT, U>::value;
73 f.
count = traits::datatype<PointT, U>::size;
81 template<
typename Po
intT>
85 std::vector<FieldMapping>& map)
90 template<
typename Tag>
void
93 for (
const auto& field :
fields_)
100 mapping.
size =
sizeof (
typename traits::datatype<PointT, Tag>::type);
101 map_.push_back (mapping);
106 PCL_WARN (
"Failed to find match for field '%s'.\n", traits::name<PointT, Tag>::value);
110 const std::vector<pcl::PCLPointField>&
fields_;
111 std::vector<FieldMapping>&
map_;
122 template<
typename Po
intT>
void
127 for_each_type< typename traits::fieldList<PointT>::type > (mapper);
130 if (field_map.size() > 1)
133 MsgFieldMap::iterator i = field_map.begin(), j = i + 1;
134 while (j != field_map.end())
139 if (j->serialized_offset - i->serialized_offset == j->struct_offset - i->struct_offset)
141 i->size += (j->struct_offset + j->size) - (i->struct_offset + i->size);
142 j = field_map.erase(j);
166 template <
typename Po
intT>
void
178 cloud.
resize (num_points);
179 std::uint8_t* cloud_data =
reinterpret_cast<std::uint8_t*
>(&cloud[0]);
184 if (field_map.size() == 1 &&
185 field_map[0].serialized_offset == 0 &&
186 field_map[0].struct_offset == 0 &&
188 field_map[0].size ==
sizeof(
PointT))
190 std::uint32_t cloud_row_step =
static_cast<std::uint32_t
> (
sizeof (
PointT) * cloud.
width);
191 const std::uint8_t* msg_data = &msg.
data[0];
195 memcpy (cloud_data, msg_data, msg.
data.size ());
199 for (std::uint32_t i = 0; i < msg.
height; ++i, cloud_data += cloud_row_step, msg_data += msg.
row_step)
200 memcpy (cloud_data, msg_data, cloud_row_step);
209 const std::uint8_t* row_data = &msg.
data[row * msg.
row_step];
212 const std::uint8_t* msg_data = row_data + col * msg.
point_step;
215 memcpy (cloud_data + mapping.struct_offset, msg_data + mapping.serialized_offset, mapping.size);
217 cloud_data +=
sizeof (
PointT);
227 template<
typename Po
intT>
void
231 createMapping<PointT> (msg.
fields, field_map);
239 template<
typename Po
intT>
void
256 std::size_t data_size =
sizeof (
PointT) * cloud.
size ();
257 msg.
data.resize (data_size);
260 memcpy(&msg.
data[0], &cloud[0], data_size);
280 template<
typename CloudT>
void
284 if (cloud.width == 0 && cloud.height == 0)
285 throw std::runtime_error(
"Needs to be a dense like cloud!!");
288 if (cloud.size () != cloud.width * cloud.height)
289 throw std::runtime_error(
"The width and height do not match the cloud size!");
290 msg.
height = cloud.height;
291 msg.
width = cloud.width;
295 msg.
header = cloud.header;
297 msg.
step = msg.
width *
sizeof (std::uint8_t) * 3;
299 for (std::size_t y = 0; y < cloud.height; y++)
301 for (std::size_t x = 0; x < cloud.width; x++)
303 std::uint8_t * pixel = &(msg.
data[y * msg.
step + x * 3]);
304 memcpy (pixel, &cloud (x, y).rgb, 3 *
sizeof(std::uint8_t));
317 const auto predicate = [](
const auto& field) {
return field.name ==
"rgb"; };
318 const auto result = std::find_if(cloud.
fields.cbegin (), cloud.
fields.cend (), predicate);
319 if (result == cloud.
fields.end ())
320 throw std::runtime_error (
"No rgb field!!");
324 throw std::runtime_error (
"Needs to be a dense like cloud!!");
330 int rgb_offset = cloud.
fields[rgb_index].offset;
336 msg.
step =
static_cast<std::uint32_t
>(msg.
width *
sizeof (std::uint8_t) * 3);
339 for (std::size_t y = 0; y < cloud.
height; y++)
341 for (std::size_t x = 0; x < cloud.
width; x++, rgb_offset += point_step)
343 std::uint8_t * pixel = &(msg.
data[y * msg.
step + x * 3]);
344 memcpy (pixel, &(cloud.
data[rgb_offset]), 3 * sizeof (std::uint8_t));