@@ -306,8 +306,8 @@ void log_point_cloud2(
306
306
// TODO(leo) if not specified, check if 2D points or 3D points
307
307
// TODO(leo) allow arbitrary color mapping
308
308
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 } ;
311
311
312
312
for (const auto & field : msg->fields ) {
313
313
if (field.name == " x" ) {
@@ -331,6 +331,14 @@ void log_point_cloud2(
331
331
return ;
332
332
}
333
333
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 ;
334
342
}
335
343
}
336
344
@@ -345,6 +353,10 @@ void log_point_cloud2(
345
353
std::vector<rerun::Position3D> points (msg->width * msg->height );
346
354
std::vector<rerun::Color> colors;
347
355
356
+ if (has_rgb) {
357
+ colors.reserve (msg->width * msg->height );
358
+ }
359
+
348
360
for (size_t i = 0 ; i < msg->height ; ++i) {
349
361
for (size_t j = 0 ; j < msg->width ; ++j) {
350
362
auto point_offset = i * msg->row_step + j * msg->point_step ;
@@ -353,7 +365,12 @@ void log_point_cloud2(
353
365
std::memcpy (&position.xyz .xyz [0 ], &msg->data [point_offset + x_offset], sizeof (float ));
354
366
std::memcpy (&position.xyz .xyz [1 ], &msg->data [point_offset + y_offset], sizeof (float ));
355
367
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;
357
374
}
358
375
}
359
376
@@ -371,10 +388,14 @@ void log_point_cloud2(
371
388
&msg->data [i * msg->row_step + j * msg->point_step + colormap_field.offset ],
372
389
sizeof (float )
373
390
);
374
- values. emplace_back ( value) ;
391
+ values[i * msg-> width + j] = value;
375
392
}
376
393
}
377
394
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
+ }
378
399
} else if (options.colormap ) {
379
400
rec.log (" /" , rerun::TextLog (" Unsupported colormap specified: " + options.colormap .value ()));
380
401
}
0 commit comments