Build a common output layout and per-input copy plan for a cloud batch.
Invalid inputs are retained in the result with a rejection reason. Depending on the requested fields and input compatibility, the result either preserves a homogeneous input layout or builds a packed layout for compatible fields.
295 {},
296 const sensor_msgs::msg::PointField* calibration_field = nullptr) {
297 BatchLayout result;
298 result.inputs.resize(clouds.size());
299
300 std::vector<std::size_t> valid_indices;
301 for (std::size_t index = 0; index < clouds.size(); ++index) {
302 const auto& cloud = clouds[index];
303 auto& input = result.inputs[index];
304 if (!cloud) {
305 input.rejection_reason = "null cloud";
306 continue;
307 }
308 const auto* x = findField(*cloud, "x");
309 const auto* y = findField(*cloud, "y");
310 const auto* z = findField(*cloud, "z");
311 if (!validXyzField(x, cloud->point_step) || !validXyzField(y, cloud->point_step) || !validXyzField(z, cloud->point_step)) {
312 input.rejection_reason = "missing or invalid FLOAT32 x/y/z field";
313 continue;
314 }
315 input.valid = true;
316 result.is_dense = result.is_dense && cloud->is_dense;
317 input.x_offset = x->offset;
318 input.y_offset = y->offset;
319 input.z_offset = z->offset;
320 if (
const auto* time =
findField(*cloud, time_field_name); time !=
nullptr &&
321 time->datatype == sensor_msgs::msg::PointField::UINT32 &&
322 time->count == 1 &&
validField(*time, cloud->point_step)) {
323 input.time_offset = static_cast<int>(time->offset);
324 }
325 valid_indices.push_back(index);
326 }
327 if (valid_indices.empty()) return result;
328
329 const auto& first = *clouds[valid_indices.front()];
330 result.is_bigendian = first.is_bigendian;
331 result.homogeneous_fast_path = std::all_of(valid_indices.begin(), valid_indices.end(), [&](std::size_t index) {
332 const auto& cloud = *clouds[index];
333 return cloud.point_step == first.point_step && cloud.fields == first.fields && cloud.is_bigendian == first.is_bigendian;
334 });
335
336 std::vector<sensor_msgs::msg::PointField> selected;
337 if (!requested_fields.empty()) {
338 std::unordered_set<std::string> emitted;
339 for (const auto& name : requested_fields) {
340 if (!emitted.insert(name).second) continue;
341 const sensor_msgs::msg::PointField* descriptor = nullptr;
342 for (const auto index : valid_indices) {
343 const auto* candidate =
findField(*clouds[index], name);
345 descriptor = candidate;
346 break;
347 }
348 }
349 if (descriptor != nullptr) {
350 selected.push_back(calibration_field != nullptr && name == calibration_field->name ? *calibration_field : *descriptor);
351 }
352 }
353 } else if (result.homogeneous_fast_path) {
354 selected = first.fields;
355 } else {
356 std::unordered_map<std::string, sensor_msgs::msg::PointField> descriptors;
357 std::unordered_set<std::string> conflicts;
358 std::vector<std::string> order;
359 for (const auto index : valid_indices) {
360 for (
const auto& field : clouds[index]->
fields) {
362 const auto [it, inserted] = descriptors.emplace(field.name, field);
363 if (inserted) {
364 order.push_back(field.name);
365 }
else if (!
sameType(it->second, field)) {
366 conflicts.insert(field.name);
367 }
368 }
369 }
370 for (const auto& name : order) {
371 if (calibration_field != nullptr && name == calibration_field->name) {
372 selected.push_back(*calibration_field);
373 } else if (conflicts.count(name) != 0U) {
374 result.conflicting_fields.push_back(name);
375 } else {
376 selected.push_back(descriptors.at(name));
377 }
378 }
379 }
380
381
382
383
384 for (const char* name : {"x", "y", "z"}) {
385 if (std::none_of(selected.begin(), selected.end(), [name](const auto& field) { return field.name == name; })) {
386 selected.push_back(*
findField(first, name));
387 }
388 }
389
390 if (requested_fields.empty() && result.homogeneous_fast_path) {
391 result.fields = selected;
392 result.point_step = first.point_step;
393 } else {
394 for (auto field : selected) {
395 field.offset = result.point_step;
396 result.point_step +=
static_cast<uint32_t
>(
fieldDatatypeSize(field.datatype) * field.count);
397 result.fields.push_back(std::move(field));
398 }
399 }
400 for (const auto& field : result.fields) {
401 if (field.name == "x") result.x_offset = field.offset;
402 if (field.name == "y") result.y_offset = field.offset;
403 if (field.name == "z") result.z_offset = field.offset;
404 }
405
406 std::unordered_set<std::string> used_fill_fields;
407 for (const auto index : valid_indices) {
408 const auto& cloud = *clouds[index];
409 auto& input = result.inputs[index];
410 input.whole_point_copy = requested_fields.empty() && result.homogeneous_fast_path;
411 if (input.whole_point_copy) continue;
412 for (const auto& destination : result.fields) {
413 const auto* source =
findField(cloud, destination.name);
414 if (source !=
nullptr &&
sameType(*source, destination) &&
validField(*source, cloud.point_step)) {
415 input.copies.push_back({source->offset, destination.offset,
fieldDatatypeSize(source->datatype) * source->count});
416 } else if (calibration_field != nullptr && destination.name == calibration_field->name && source != nullptr &&
417 source->count == 1 &&
validField(*source, cloud.point_step)) {
418 input.conversions.push_back({source->offset, destination.offset, source->datatype, destination.datatype});
419 } else if (const auto fill = fill_values.find(destination.name);
420 fill != fill_values.end() || destination.datatype == sensor_msgs::msg::PointField::FLOAT32 ||
421 destination.datatype == sensor_msgs::msg::PointField::FLOAT64) {
422 const double value = fill != fill_values.end() ? fill->second : std::numeric_limits<double>::quiet_NaN();
423 try {
424 input.field_fills.push_back(
makeFieldFill(destination, value, result.is_bigendian));
425 } catch (const std::invalid_argument& error) {
426 result.fill_error = "Cannot fill missing field '" + destination.name + "': " + error.what();
427 return result;
428 }
429 if (fill != fill_values.end()) used_fill_fields.insert(destination.name);
430 if (std::isnan(value)) result.is_dense = false;
431 } else {
432 input.zero_filled_fields.push_back(destination.name);
433 }
434 }
435 }
436 for (const auto& fill : fill_values) {
437 if (used_fill_fields.count(fill.first) == 0U) result.unused_fill_fields.push_back(fill.first);
438 }
439 std::sort(result.unused_fill_fields.begin(), result.unused_fill_fields.end());
440 return result;
441}
bool sameType(const sensor_msgs::msg::PointField &lhs, const sensor_msgs::msg::PointField &rhs)
Check whether two fields have the same name and element type.
std::size_t fieldDatatypeSize(uint8_t datatype)
Return the size in bytes of one PointField datatype element.
FieldFill makeFieldFill(const sensor_msgs::msg::PointField &field, double value, bool is_bigendian)
bool validField(const sensor_msgs::msg::PointField &field, uint32_t point_step)
Check whether a field descriptor fits within a point record.
const sensor_msgs::msg::PointField * findField(const sensor_msgs::msg::PointCloud2 &cloud, const std::string &name)
Find a field descriptor by name in a point cloud.
std::vector< int > fields