point_cloud_utilities.h
Go to the documentation of this file.
1 
34 #ifndef MULTISENSE_ROS_POINT_CLOUD_UTILITY_H
35 #define MULTISENSE_ROS_POINT_CLOUD_UTILITY_H
36 
37 #include <type_traits>
38 
39 #include <arpa/inet.h>
40 #include <Eigen/Geometry>
41 
42 #include <sensor_msgs/PointCloud2.h>
43 
44 namespace multisense_ros {
45 
46 template <typename T>
47 uint8_t messageFormat();
48 
49 template <typename ColorT>
50 typename std::enable_if<!std::is_same<ColorT, void>::value, void>::type
51  initPointcloudColorChannel(sensor_msgs::PointCloud2& point_cloud,
52  const size_t offset,
53  const std::string& color_channel)
54 {
55  assert(point_cloud.fields.size() >= 4);
56 
57  const auto color_datatype = messageFormat<ColorT>();
58 
59  point_cloud.fields[3].name = color_channel;
60  point_cloud.fields[3].offset = offset;
61  point_cloud.fields[3].count = 1;
62  point_cloud.fields[3].datatype = color_datatype;
63 
64  point_cloud.point_step += sizeof(ColorT);
65 }
66 
67 template <typename ColorT>
68 typename std::enable_if<std::is_same<ColorT, void>::value, void>::type
69 initPointcloudColorChannel(sensor_msgs::PointCloud2& point_cloud,
70  const size_t offset,
71  const std::string& color_channel)
72 {
73  (void) point_cloud;
74  (void) offset;
75  (void) color_channel;
76  return;
77 }
78 
79 template <typename PointT, typename ColorT>
80 void initializePointcloud (sensor_msgs::PointCloud2& point_cloud,
81  const ros::Time& stamp,
82  const size_t width,
83  const size_t height,
84  const bool dense,
85  const std::string& frame_id,
86  const std::string& color_channel)
87 {
88  const auto point_datatype = messageFormat<PointT>();
89 
90  const bool has_color = not std::is_same<ColorT, void>::value;
91 
92  point_cloud.is_bigendian = (htonl(1) == 1);
93  point_cloud.is_dense = dense;
94  point_cloud.point_step = 0;
95  point_cloud.header.frame_id = frame_id;
96  point_cloud.header.stamp = stamp;
97  point_cloud.width = width;
98  point_cloud.height = height;
99 
100  if (has_color)
101  {
102  point_cloud.fields.resize(4);
103  }
104  else
105  {
106  point_cloud.fields.resize(3);
107  }
108 
109  point_cloud.fields[0].name = "x";
110  point_cloud.fields[0].offset = point_cloud.point_step;
111  point_cloud.fields[0].count = 1;
112  point_cloud.fields[0].datatype = point_datatype;
113  point_cloud.point_step += sizeof(PointT);
114 
115  point_cloud.fields[1].name = "y";
116  point_cloud.fields[1].offset = point_cloud.point_step;
117  point_cloud.fields[1].count = 1;
118  point_cloud.fields[1].datatype = point_datatype;
119  point_cloud.point_step += sizeof(PointT);
120 
121  point_cloud.fields[2].name = "z";
122  point_cloud.fields[2].offset = point_cloud.point_step;
123  point_cloud.fields[2].count = 1;
124  point_cloud.fields[2].datatype = point_datatype;
125  point_cloud.point_step += sizeof(PointT);
126 
127  if (has_color)
128  {
129  initPointcloudColorChannel<ColorT>(point_cloud,
130  point_cloud.point_step,
131  color_channel);
132  }
133 
134  point_cloud.row_step = width * point_cloud.point_step;
135  point_cloud.data.resize(width * height * point_cloud.point_step);
136 }
137 
138 template <typename PointT, typename ColorT>
139 sensor_msgs::PointCloud2 initializePointcloud(const ros::Time& stamp,
140  const size_t width,
141  const size_t height,
142  const bool dense,
143  const std::string& frame_id,
144  const std::string& color_channel)
145 {
146  sensor_msgs::PointCloud2 point_cloud;
147 
148  initializePointcloud<PointT, ColorT>(point_cloud,
149  stamp,
150  width,
151  height,
152  dense,
153  frame_id,
154  color_channel);
155 
156  return point_cloud;
157 }
158 
159 void writePoint(sensor_msgs::PointCloud2 &pointcloud, const size_t index, const Eigen::Vector3f &point);
160 
161 template <typename ColorT>
162 void writePoint(sensor_msgs::PointCloud2 &pointcloud, const size_t index, const Eigen::Vector3f &point, const ColorT &color)
163 {
164  writePoint(pointcloud, index, point);
165 
166  assert(pointcloud.fields.size() == 4);
167  assert(pointcloud.fields[3].datatype == messageFormat<ColorT>());
168 
169  ColorT* colorP = reinterpret_cast<ColorT*>(&(pointcloud.data[index * pointcloud.point_step + pointcloud.fields[3].offset]));
170  colorP[0] = color;
171 }
172 
173 void writePoint(sensor_msgs::PointCloud2 &pointcloud,
174  const size_t pointcloud_index,
175  const Eigen::Vector3f &point,
176  const size_t image_index,
177  const uint32_t bitsPerPixel,
178  const void* imageDataP);
179 
180 }// namespace
181 
182 #endif
multisense_ros::initPointcloudColorChannel
std::enable_if<!std::is_same< ColorT, void >::value, void >::type initPointcloudColorChannel(sensor_msgs::PointCloud2 &point_cloud, const size_t offset, const std::string &color_channel)
Definition: point_cloud_utilities.h:51
multisense_ros
Definition: camera.h:58
multisense_ros::initializePointcloud
void initializePointcloud(sensor_msgs::PointCloud2 &point_cloud, const ros::Time &stamp, const size_t width, const size_t height, const bool dense, const std::string &frame_id, const std::string &color_channel)
Definition: point_cloud_utilities.h:80
multisense_ros::messageFormat
uint8_t messageFormat()
multisense_ros::writePoint
void writePoint(sensor_msgs::PointCloud2 &pointcloud, const size_t index, const Eigen::Vector3f &point)
Definition: point_cloud_utilities.cpp:92
ros::Time


multisense_ros
Author(s):
autogenerated on Thu Feb 20 2025 03:51:03