@@ -306,8 +306,8 @@ void log_point_cloud2(
306306 // TODO(leo) if not specified, check if 2D points or 3D points
307307 // TODO(leo) allow arbitrary color mapping
308308
309- size_t x_offset, y_offset, z_offset;
310- bool has_x{false }, has_y{false }, has_z{false };
309+ size_t x_offset, y_offset, z_offset, rgb_offset ;
310+ bool has_x{false }, has_y{false }, has_z{false }, has_rgb{ false } ;
311311
312312 for (const auto & field : msg->fields ) {
313313 if (field.name == " x" ) {
@@ -331,6 +331,14 @@ void log_point_cloud2(
331331 return ;
332332 }
333333 has_z = true ;
334+ } else if (field.name == options.colormap_field .value_or (" rgb" )) {
335+ rgb_offset = field.offset ;
336+ if (field.datatype != sensor_msgs::msg::PointField::UINT32 &&
337+ field.datatype != sensor_msgs::msg::PointField::FLOAT32 ) {
338+ rec.log (entity_path, rerun::TextLog (" Only UINT32 and FLOAT32 rgb field supported" ));
339+ return ;
340+ }
341+ has_rgb = true ;
334342 }
335343 }
336344
@@ -345,6 +353,10 @@ void log_point_cloud2(
345353 std::vector<rerun::Position3D> points (msg->width * msg->height );
346354 std::vector<rerun::Color> colors;
347355
356+ if (has_rgb) {
357+ colors.reserve (msg->width * msg->height );
358+ }
359+
348360 for (size_t i = 0 ; i < msg->height ; ++i) {
349361 for (size_t j = 0 ; j < msg->width ; ++j) {
350362 auto point_offset = i * msg->row_step + j * msg->point_step ;
@@ -353,7 +365,12 @@ void log_point_cloud2(
353365 std::memcpy (&position.xyz .xyz [0 ], &msg->data [point_offset + x_offset], sizeof (float ));
354366 std::memcpy (&position.xyz .xyz [1 ], &msg->data [point_offset + y_offset], sizeof (float ));
355367 std::memcpy (&position.xyz .xyz [2 ], &msg->data [point_offset + z_offset], sizeof (float ));
356- points.emplace_back (std::move (position));
368+ if (has_rgb) {
369+ uint8_t rgba[4 ];
370+ std::memcpy (&rgba, &msg->data [point_offset + rgb_offset], sizeof (uint32_t ));
371+ colors.emplace_back (rerun::Color (rgba[2 ], rgba[1 ], rgba[0 ]));
372+ }
373+ points[i * msg->width + j] = position;
357374 }
358375 }
359376
@@ -371,10 +388,14 @@ void log_point_cloud2(
371388 &msg->data [i * msg->row_step + j * msg->point_step + colormap_field.offset ],
372389 sizeof (float )
373390 );
374- values. emplace_back ( value) ;
391+ values[i * msg-> width + j] = value;
375392 }
376393 }
377394 colors = colormap (values, options.colormap_min , options.colormap_max );
395+ } else if (options.colormap == " rgb" ) {
396+ if (!has_rgb) {
397+ rec.log (entity_path, rerun::TextLog (" RGB colormap specified but no RGB field present" ));
398+ }
378399 } else if (options.colormap ) {
379400 rec.log (" /" , rerun::TextLog (" Unsupported colormap specified: " + options.colormap .value ()));
380401 }
0 commit comments