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>
62 template<
typename Po
intT>
70 f.
name = pcl::traits::name<PointT, U>::value;
71 f.
offset = pcl::traits::offset<PointT, U>::value;
72 f.
datatype = pcl::traits::datatype<PointT, U>::value;
73 f.
count = pcl::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_)
99 mapping.
struct_offset = pcl::traits::offset<PointT, Tag>::value;
100 mapping.
size =
sizeof (
typename pcl::traits::datatype<PointT, Tag>::type);
101 map_.push_back (mapping);
106 PCL_WARN (
"Failed to find match for field '%s'.\n", pcl::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);
162 template <
typename Po
intT>
void
164 const MsgFieldMap& field_map,
const std::uint8_t* msg_data)
174 std::uint8_t* cloud_data =
reinterpret_cast<std::uint8_t*
>(&cloud[0]);
179 if (field_map.size() == 1 &&
180 field_map[0].serialized_offset == 0 &&
181 field_map[0].struct_offset == 0 &&
183 field_map[0].size ==
sizeof(
PointT))
185 const auto cloud_row_step = (
sizeof (
PointT) * cloud.
width);
194 memcpy (cloud_data, msg_data, cloud_row_step);
201 for (std::size_t row = 0; row < msg.
height; ++row)
203 const std::uint8_t* row_data = msg_data + row * msg.
row_step;
204 for (std::size_t col = 0; col < msg.
width; ++col)
206 const std::uint8_t* msg_data = row_data + col * msg.
point_step;
209 std::copy(msg_data + mapping.serialized_offset, msg_data + mapping.serialized_offset + mapping.size,
210 cloud_data + mapping.struct_offset);
212 cloud_data +=
sizeof (
PointT);
231 template <
typename Po
intT>
void
242 template<
typename Po
intT>
void
246 createMapping<PointT> (msg.
fields, field_map);
254 template<
typename Po
intT>
void
271 std::size_t data_size =
sizeof (
PointT) * cloud.
size ();
272 msg.
data.resize (data_size);
275 memcpy(&msg.
data[0], &cloud[0], data_size);
295 template<
typename CloudT>
void
299 if (cloud.width == 0 && cloud.height == 0)
300 throw std::runtime_error(
"Needs to be a dense like cloud!!");
303 if (cloud.size () != cloud.width * cloud.height)
304 throw std::runtime_error(
"The width and height do not match the cloud size!");
305 msg.
height = cloud.height;
306 msg.
width = cloud.width;
310 msg.
header = cloud.header;
312 msg.
step = msg.
width *
sizeof (std::uint8_t) * 3;
314 for (std::size_t y = 0; y < cloud.height; y++)
316 for (std::size_t x = 0; x < cloud.width; x++)
318 std::uint8_t * pixel = &(msg.
data[y * msg.
step + x * 3]);
319 memcpy (pixel, &cloud (x, y).rgb, 3 *
sizeof(std::uint8_t));
332 const auto predicate = [](
const auto& field) {
return field.name ==
"rgb"; };
333 const auto result = std::find_if(cloud.
fields.cbegin (), cloud.
fields.cend (), predicate);
334 if (result == cloud.
fields.end ())
335 throw std::runtime_error (
"No rgb field!!");
339 throw std::runtime_error (
"Needs to be a dense like cloud!!");
345 auto rgb_offset = cloud.
fields[rgb_index].offset;
351 msg.
step = (msg.
width *
sizeof (std::uint8_t) * 3);
354 for (std::size_t y = 0; y < cloud.
height; y++)
356 for (std::size_t x = 0; x < cloud.
width; x++, rgb_offset += point_step)
358 std::uint8_t * pixel = &(msg.
data[y * msg.
step + x * 3]);
359 std::copy(&cloud.
data[rgb_offset], &cloud.
data[rgb_offset] + 3, pixel);
PointCloud represents the base class in PCL for storing collections of 3D points.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
void resize(std::size_t count)
Resizes the container to contain count elements.
std::uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
std::uint32_t height
The point cloud height (if organized as an image-structure).
bool fieldOrdering(const FieldMapping &a, const FieldMapping &b)
float distance(const PointT &p1, const PointT &p2)
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map, const std::uint8_t *msg_data)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
void createMapping(const std::vector< pcl::PCLPointField > &msg_fields, MsgFieldMap &field_map)
std::vector< detail::FieldMapping > MsgFieldMap
std::vector< std::uint8_t > data
std::vector<::pcl::PCLPointField > fields
std::vector< std::uint8_t > data
A point structure representing Euclidean xyz coordinates, and the RGB color.
FieldAdder(std::vector< pcl::PCLPointField > &fields)
std::vector< pcl::PCLPointField > & fields_
FieldMapper(const std::vector< pcl::PCLPointField > &fields, std::vector< FieldMapping > &map)
const std::vector< pcl::PCLPointField > & fields_
std::vector< FieldMapping > & map_
std::size_t struct_offset
std::size_t serialized_offset