point_cloud_fusion v1.6.0
Loading...
Searching...
No Matches
point_cloud_fusion::detail Namespace Reference

Classes

struct  BatchLayout
 
struct  FieldConversion
 
struct  FieldCopy
 
struct  FieldFill
 
struct  InputLayout
 
struct  SyncPolicyTraits
 
struct  SyncPolicyTraits< 2 >
 
struct  SyncPolicyTraits< 3 >
 
struct  SyncPolicyTraits< 4 >
 
struct  SyncPolicyTraits< 5 >
 
struct  SyncPolicyTraits< 6 >
 
struct  SyncPolicyTraits< 7 >
 
struct  SyncPolicyTraits< 8 >
 
struct  SyncPolicyTraits< 9 >
 

Typedefs

using MissingFieldFillValues = std::unordered_map<std::string, double>
 
template<std::size_t N>
using SyncPolicy = typename SyncPolicyTraits<N>::Policy
 
template<std::size_t N>
using SyncType = message_filters::Synchronizer<SyncPolicy<N>>
 

Functions

std::size_t fieldDatatypeSize (uint8_t datatype)
 Return the size in bytes of one PointField datatype element.
 
MissingFieldFillValues parseMissingFieldFillValues (const std::vector< std::string > &entries)
 
template<typename T >
void encodeFillValue (double value, uint8_t *destination)
 
FieldFill makeFieldFill (const sensor_msgs::msg::PointField &field, double value, bool is_bigendian)
 
void fillMissingFields (uint8_t *destination, const InputLayout &input)
 
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.
 
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.
 
bool fieldIsPublished (const std::vector< std::string > &requested_fields, const std::string &name)
 
std::vector< std::string > processingFields (const std::vector< std::string > &requested_fields, const std::string &time_field_name, bool motion_compensation)
 
bool validXyzField (const sensor_msgs::msg::PointField *field, uint32_t point_step)
 Check whether a field is a valid scalar FLOAT32 XYZ component.
 
BatchLayout buildBatchLayout (const std::vector< sensor_msgs::msg::PointCloud2::ConstSharedPtr > &clouds, const std::vector< std::string > &requested_fields, const std::string &time_field_name, const MissingFieldFillValues &fill_values={}, const sensor_msgs::msg::PointField *calibration_field=nullptr)
 Build a common output layout and per-input copy plan for a cloud batch.
 
template<std::size_t N, std::size_t... Is>
void connectInputsImpl (SyncType< N > &sync, const std::vector< std::shared_ptr< point_cloud_transport::SubscriberFilter > > &subs, std::index_sequence< Is... >)
 Connect subscriber filters to the synchronizer through an index sequence.
 
template<std::size_t N>
void connectInputs (SyncType< N > &sync, const std::vector< std::shared_ptr< point_cloud_transport::SubscriberFilter > > &subs)
 Connect all subscriber filters to the synchronizer for N inputs.
 

Typedef Documentation

◆ MissingFieldFillValues

using point_cloud_fusion::detail::MissingFieldFillValues = std::unordered_map<std::string, double>

Definition at line 98 of file point_cloud_layout.hpp.

◆ SyncPolicy

template<std::size_t N>
using point_cloud_fusion::detail::SyncPolicy = typename SyncPolicyTraits<N>::Policy

Definition at line 861 of file point_cloud_fusion.cpp.

◆ SyncType

template<std::size_t N>
using point_cloud_fusion::detail::SyncType = message_filters::Synchronizer<SyncPolicy<N>>

Definition at line 864 of file point_cloud_fusion.cpp.

Function Documentation

◆ buildBatchLayout()

BatchLayout point_cloud_fusion::detail::buildBatchLayout ( const std::vector< sensor_msgs::msg::PointCloud2::ConstSharedPtr > & clouds,
const std::vector< std::string > & requested_fields,
const std::string & time_field_name,
const MissingFieldFillValues & fill_values = {},
const sensor_msgs::msg::PointField * calibration_field = nullptr )
inline

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.

Parameters
cloudsPoint clouds to validate and combine.
requested_fieldsOptional ordered list of fields to include.
time_field_nameName of the per-point time-offset field.
fill_valuesOverrides; missing floats default to NaN, integers to zero.
calibration_fieldOptional leader descriptor retained across scalar datatype differences.
Returns
Output layout and copy plan for every input cloud.

Definition at line 292 of file point_cloud_layout.hpp.

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);
344 if (candidate != nullptr && validField(*candidate, clouds[index]->point_step)) {
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) {
361 if (!validField(field, clouds[index]->point_step)) continue;
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 // XYZ is part of every valid output even if a configured optional-field list
382 // omitted it. This keeps the PointCloud2 usable and guarantees transformed
383 // XYZ.
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
std::size_t point_step

◆ connectInputs()

template<std::size_t N>
void point_cloud_fusion::detail::connectInputs ( SyncType< N > & sync,
const std::vector< std::shared_ptr< point_cloud_transport::SubscriberFilter > > & subs )

Connect all subscriber filters to the synchronizer for N inputs.

Template Parameters
NNumber of synchronizer inputs.
Parameters
syncSynchronizer receiving the subscriber filters.
subsSubscriber filters to connect.

Definition at line 891 of file point_cloud_fusion.cpp.

891 {
892 connectInputsImpl<N>(sync, subs, std::make_index_sequence<N>{});
893}

◆ connectInputsImpl()

template<std::size_t N, std::size_t... Is>
void point_cloud_fusion::detail::connectInputsImpl ( SyncType< N > & sync,
const std::vector< std::shared_ptr< point_cloud_transport::SubscriberFilter > > & subs,
std::index_sequence< Is... >  )

Connect subscriber filters to the synchronizer through an index sequence.

Template Parameters
NNumber of synchronizer inputs.
IsSubscriber indices expanded into connectInput.
Parameters
syncSynchronizer receiving the subscriber filters.
subsSubscriber filters to connect.

Definition at line 876 of file point_cloud_fusion.cpp.

878 {
879 // Fan the subscriber filters into the synchronizer inputs.
880 sync.connectInput(*subs[Is]...);
881}

◆ encodeFillValue()

template<typename T >
void point_cloud_fusion::detail::encodeFillValue ( double value,
uint8_t * destination )
inline

Validate and encode one element of a field fill in host byte order.

Definition at line 132 of file point_cloud_layout.hpp.

132 {
133 T typed{};
134 if constexpr (std::is_integral_v<T>) {
135 if (!std::isfinite(value) || std::trunc(value) != value || value < static_cast<double>(std::numeric_limits<T>::lowest()) ||
136 value > static_cast<double>(std::numeric_limits<T>::max())) {
137 throw std::invalid_argument("fill must be an in-range integer (NaN is only supported for FLOAT32/FLOAT64)");
138 }
139 typed = static_cast<T>(value);
140 } else {
141 if (std::isinf(value) || (std::isfinite(value) && std::abs(value) > std::numeric_limits<T>::max())) {
142 throw std::invalid_argument("fill exceeds the floating-point datatype range");
143 }
144 typed = std::isnan(value) ? std::numeric_limits<T>::quiet_NaN() : static_cast<T>(value);
145 }
146 std::memcpy(destination, &typed, sizeof(typed));
147}

◆ fieldDatatypeSize()

std::size_t point_cloud_fusion::detail::fieldDatatypeSize ( uint8_t datatype)
inline

Return the size in bytes of one PointField datatype element.

Parameters
datatypePointField datatype identifier.
Returns
Element size in bytes, or zero if the datatype is unsupported.

Definition at line 31 of file point_cloud_layout.hpp.

31 {
32 using sensor_msgs::msg::PointField;
33 switch (datatype) {
34 case PointField::INT8:
35 case PointField::UINT8:
36 return 1;
37 case PointField::INT16:
38 case PointField::UINT16:
39 return 2;
40 case PointField::INT32:
41 case PointField::UINT32:
42 case PointField::FLOAT32:
43 return 4;
44 case PointField::FLOAT64:
45 return 8;
46 default:
47 return 0;
48 }
49}

◆ fieldIsPublished()

bool point_cloud_fusion::detail::fieldIsPublished ( const std::vector< std::string > & requested_fields,
const std::string & name )
inline

Return whether a configured output-field list publishes a field.

Definition at line 244 of file point_cloud_layout.hpp.

244 {
245 return requested_fields.empty() || std::find(requested_fields.begin(), requested_fields.end(), name) != requested_fields.end();
246}

◆ fillMissingFields()

void point_cloud_fusion::detail::fillMissingFields ( uint8_t * destination,
const InputLayout & input )
inline

Apply configured fills after zero-initializing a destination point.

Definition at line 200 of file point_cloud_layout.hpp.

200 {
201 for (const auto& fill : input.field_fills) {
202 std::copy(fill.bytes.begin(), fill.bytes.end(), std::next(destination, fill.destination_offset));
203 }
204}

◆ findField()

const sensor_msgs::msg::PointField * point_cloud_fusion::detail::findField ( const sensor_msgs::msg::PointCloud2 & cloud,
const std::string & name )
inline

Find a field descriptor by name in a point cloud.

Parameters
cloudPoint cloud whose fields are searched.
nameField name to find.
Returns
Pointer to the matching descriptor, or nullptr when it is absent.

Definition at line 237 of file point_cloud_layout.hpp.

237 {
238 const auto it =
239 std::find_if(cloud.fields.begin(), cloud.fields.end(), [&name](const auto& field) { return field.name == name; });
240 return it == cloud.fields.end() ? nullptr : &*it;
241}

◆ makeFieldFill()

FieldFill point_cloud_fusion::detail::makeFieldFill ( const sensor_msgs::msg::PointField & field,
double value,
bool is_bigendian )
inline

Build an endian-correct fill, broadcasting the value to every array element.

Definition at line 150 of file point_cloud_layout.hpp.

150 {
151 FieldFill fill;
152 fill.destination_offset = field.offset;
153 const auto element_size = fieldDatatypeSize(field.datatype);
154 if (element_size == 0 || field.count == 0) throw std::invalid_argument("invalid fill datatype or element count");
155 fill.bytes.resize(element_size * field.count);
156 using F = sensor_msgs::msg::PointField;
157 switch (field.datatype) {
158 case F::INT8:
159 encodeFillValue<int8_t>(value, fill.bytes.data());
160 break;
161 case F::UINT8:
162 encodeFillValue<uint8_t>(value, fill.bytes.data());
163 break;
164 case F::INT16:
165 encodeFillValue<int16_t>(value, fill.bytes.data());
166 break;
167 case F::UINT16:
168 encodeFillValue<uint16_t>(value, fill.bytes.data());
169 break;
170 case F::INT32:
171 encodeFillValue<int32_t>(value, fill.bytes.data());
172 break;
173 case F::UINT32:
174 encodeFillValue<uint32_t>(value, fill.bytes.data());
175 break;
176 case F::FLOAT32:
177 encodeFillValue<float>(value, fill.bytes.data());
178 break;
179 case F::FLOAT64:
180 encodeFillValue<double>(value, fill.bytes.data());
181 break;
182 default:
183 throw std::invalid_argument("unsupported fill datatype");
184 }
185 const uint16_t endian_check = 1;
186 uint8_t first_byte{};
187 std::memcpy(&first_byte, &endian_check, sizeof(first_byte));
188 const bool host_bigendian = first_byte == 0;
189 if (is_bigendian != host_bigendian) {
190 std::reverse(fill.bytes.begin(), std::next(fill.bytes.begin(), static_cast<std::ptrdiff_t>(element_size)));
191 }
192 for (uint32_t element = 1; element < field.count; ++element) {
193 std::copy_n(fill.bytes.cbegin(), element_size,
194 std::next(fill.bytes.begin(), static_cast<std::ptrdiff_t>(element * element_size)));
195 }
196 return fill;
197}

◆ parseMissingFieldFillValues()

MissingFieldFillValues point_cloud_fusion::detail::parseMissingFieldFillValues ( const std::vector< std::string > & entries)
inline

Parse field=value entries; values may be finite numbers or NaN.

Definition at line 101 of file point_cloud_layout.hpp.

101 {
102 const auto trim = [](const std::string& value) {
103 const auto first = value.find_first_not_of(" \t\r\n");
104 return first == std::string::npos ? std::string{} : value.substr(first, value.find_last_not_of(" \t\r\n") - first + 1);
105 };
106 MissingFieldFillValues result;
107 for (const auto& entry : entries) {
108 const auto separator = entry.find('=');
109 if (separator == std::string::npos) {
110 throw std::invalid_argument("Expected field=value in missing_field_fill_values: " + entry);
111 }
112 const auto name = trim(entry.substr(0, separator));
113 const auto text = trim(entry.substr(separator + 1));
114 if (name.empty() || text.empty()) throw std::invalid_argument("Empty field or value in missing_field_fill_values: " + entry);
115 double value{};
116 try {
117 std::size_t consumed = 0;
118 value = std::stod(text, &consumed);
119 if (consumed != text.size() || std::isinf(value)) throw std::invalid_argument("invalid value");
120 } catch (const std::exception&) {
121 throw std::invalid_argument("Fill value for '" + name + "' must be a finite number or nan");
122 }
123 if (!result.emplace(name, value).second) {
124 throw std::invalid_argument("Duplicate missing-field fill policy for '" + name + "'");
125 }
126 }
127 return result;
128}

◆ processingFields()

std::vector< std::string > point_cloud_fusion::detail::processingFields ( const std::vector< std::string > & requested_fields,
const std::string & time_field_name,
bool motion_compensation )
inline

Build the field list used internally while processing a batch.

An empty requested list already retains every field. For an explicit list, append the point-time field when motion compensation needs it internally.

Definition at line 254 of file point_cloud_layout.hpp.

256 {
257 if (requested_fields.empty() || !motion_compensation || fieldIsPublished(requested_fields, time_field_name)) {
258 return requested_fields;
259 }
260 auto result = requested_fields;
261 result.push_back(time_field_name);
262 return result;
263}
bool fieldIsPublished(const std::vector< std::string > &requested_fields, const std::string &name)

◆ sameType()

bool point_cloud_fusion::detail::sameType ( const sensor_msgs::msg::PointField & lhs,
const sensor_msgs::msg::PointField & rhs )
inline

Check whether two fields have the same name and element type.

Parameters
lhsFirst field descriptor.
rhsSecond field descriptor.
Returns
True when the field names, datatypes, and element counts match.

Definition at line 213 of file point_cloud_layout.hpp.

213 {
214 return lhs.name == rhs.name && lhs.datatype == rhs.datatype && lhs.count == rhs.count;
215}

◆ validField()

bool point_cloud_fusion::detail::validField ( const sensor_msgs::msg::PointField & field,
uint32_t point_step )
inline

Check whether a field descriptor fits within a point record.

Parameters
fieldField descriptor to validate.
point_stepSize in bytes of one point record.
Returns
True when the datatype and count are valid and the field is in bounds.

Definition at line 225 of file point_cloud_layout.hpp.

225 {
226 const auto size = fieldDatatypeSize(field.datatype);
227 return size > 0 && field.count > 0 && static_cast<std::size_t>(field.offset) + size * field.count <= point_step;
228}

◆ validXyzField()

bool point_cloud_fusion::detail::validXyzField ( const sensor_msgs::msg::PointField * field,
uint32_t point_step )
inline

Check whether a field is a valid scalar FLOAT32 XYZ component.

Parameters
fieldField descriptor to validate; may be nullptr.
point_stepSize in bytes of one point record.
Returns
True when the field is a valid in-bounds scalar FLOAT32 value.

Definition at line 272 of file point_cloud_layout.hpp.

272 {
273 return field != nullptr && field->datatype == sensor_msgs::msg::PointField::FLOAT32 && field->count == 1 &&
274 validField(*field, point_step);
275}