-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfoxglove_voxelgrid_codec.cpp
More file actions
378 lines (351 loc) · 13.8 KB
/
Copy pathfoxglove_voxelgrid_codec.cpp
File metadata and controls
378 lines (351 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2026 PlotJuggler contributors
// SPDX-License-Identifier: MIT
#include "foxglove_voxelgrid_codec.hpp"
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/coded_stream.h>
#include <bit>
#include <limits>
#include <string>
#include "foxglove_descriptor_util.hpp"
namespace pj_protobuf {
namespace {
namespace gpio = google::protobuf::io;
using Datatype = PJ::sdk::PointField::Datatype;
// Protobuf wire types.
constexpr uint32_t kWireVarint = 0;
constexpr uint32_t kWireI64 = 1;
constexpr uint32_t kWireLen = 2;
constexpr uint32_t kWireI32 = 5;
// These small wire helpers intentionally mirror foxglove_pointcloud_codec.cpp's
// local helpers (the repo keeps each Foxglove codec TU self-contained). Factoring
// them into a shared header is a candidate follow-up once a third consumer needs
// them (Rule of Three).
/// Foxglove NumericType -> canonical PJ datatype. Swaps signed/unsigned relative
/// to ROS/SDK (UINT8=1 here vs INT8=1 there), identical to the PointCloud codec.
[[nodiscard]] Datatype mapFoxgloveNumericType(uint64_t t) {
switch (t) {
case 1:
return Datatype::kUint8;
case 2:
return Datatype::kInt8;
case 3:
return Datatype::kUint16;
case 4:
return Datatype::kInt16;
case 5:
return Datatype::kUint32;
case 6:
return Datatype::kInt32;
case 7:
return Datatype::kFloat32;
case 8:
return Datatype::kFloat64;
default:
return Datatype::kUnknown;
}
}
/// Skip a field whose value we do not consume, given its wire type.
[[nodiscard]] bool skipField(gpio::CodedInputStream& in, uint32_t wire_type) {
switch (wire_type) {
case kWireVarint: {
uint64_t v = 0;
return in.ReadVarint64(&v);
}
case kWireI64: {
uint64_t v = 0;
return in.ReadLittleEndian64(&v);
}
case kWireLen: {
uint32_t len = 0;
return in.ReadVarint32(&len) && in.Skip(static_cast<int>(len));
}
case kWireI32: {
uint32_t v = 0;
return in.ReadLittleEndian32(&v);
}
default:
return false; // groups (3/4) are not used by foxglove schemas.
}
}
[[nodiscard]] bool readDouble(gpio::CodedInputStream& in, double& out) {
uint64_t bits = 0;
if (!in.ReadLittleEndian64(&bits)) {
return false;
}
out = std::bit_cast<double>(bits);
return true;
}
/// Parse a google.protobuf.Timestamp submessage of `len` bytes into nanoseconds.
[[nodiscard]] bool readTimestampNs(gpio::CodedInputStream& in, uint32_t len, int64_t& ts_ns) {
const auto limit = in.PushLimit(static_cast<int>(len));
int64_t seconds = 0;
int64_t nanos = 0;
uint32_t tag = 0;
while ((tag = in.ReadTag()) != 0) {
const int field = static_cast<int>(tag >> 3);
const uint32_t wt = tag & 0x7u;
if (field == 1 && wt == kWireVarint) {
uint64_t v = 0;
if (!in.ReadVarint64(&v)) {
return false;
}
seconds = static_cast<int64_t>(v);
} else if (field == 2 && wt == kWireVarint) {
uint64_t v = 0;
if (!in.ReadVarint64(&v)) {
return false;
}
nanos = static_cast<int64_t>(static_cast<int32_t>(v));
} else if (!skipField(in, wt)) {
return false;
}
}
in.PopLimit(limit);
ts_ns = seconds * 1'000'000'000LL + nanos;
return true;
}
/// Parse a foxglove.Vector3 submessage (x=1, y=2, z=3 as double) of `len` bytes.
/// The nested geometry messages carry no frame_id and are never renumbered by
/// the variant schemas PR #153 handles, so their field numbers stay hardcoded.
[[nodiscard]] bool readVector3(gpio::CodedInputStream& in, uint32_t len, PJ::sdk::Vector3& out) {
const auto limit = in.PushLimit(static_cast<int>(len));
uint32_t tag = 0;
while ((tag = in.ReadTag()) != 0) {
const int field = static_cast<int>(tag >> 3);
const uint32_t wt = tag & 0x7u;
if (wt == kWireI64 && field >= 1 && field <= 3) {
double d = 0;
if (!readDouble(in, d)) {
return false;
}
(field == 1 ? out.x : field == 2 ? out.y : out.z) = d;
} else if (!skipField(in, wt)) {
return false;
}
}
in.PopLimit(limit);
return true;
}
/// Parse a foxglove.Pose submessage (position Vector3 = 1, orientation
/// Quaternion = 2) of `len` bytes into `out`. `out` is pre-seeded to identity so
/// an omitted position/orientation reads as identity.
[[nodiscard]] bool readPose(gpio::CodedInputStream& in, uint32_t len, PJ::sdk::Pose& out) {
out.position = {.x = 0.0, .y = 0.0, .z = 0.0};
out.orientation = {.x = 0.0, .y = 0.0, .z = 0.0, .w = 1.0};
const auto limit = in.PushLimit(static_cast<int>(len));
uint32_t tag = 0;
while ((tag = in.ReadTag()) != 0) {
const int field = static_cast<int>(tag >> 3);
const uint32_t wt = tag & 0x7u;
if (field == 1 && wt == kWireLen) { // position (Vector3)
uint32_t sub_len = 0;
if (!in.ReadVarint32(&sub_len) || !readVector3(in, sub_len, out.position)) {
return false;
}
} else if (field == 2 && wt == kWireLen) { // orientation (Quaternion{x=1,y=2,z=3,w=4})
uint32_t sub_len = 0;
if (!in.ReadVarint32(&sub_len)) {
return false;
}
const auto sub = in.PushLimit(static_cast<int>(sub_len));
uint32_t t = 0;
while ((t = in.ReadTag()) != 0) {
const int sf = static_cast<int>(t >> 3);
const uint32_t swt = t & 0x7u;
if (swt == kWireI64 && sf >= 1 && sf <= 4) {
double d = 0;
if (!readDouble(in, d)) {
return false;
}
(sf == 1 ? out.orientation.x
: sf == 2 ? out.orientation.y
: sf == 3 ? out.orientation.z
: out.orientation.w) = d;
} else if (!skipField(in, swt)) {
return false;
}
}
in.PopLimit(sub);
} else if (!skipField(in, wt)) {
return false;
}
}
in.PopLimit(limit);
return true;
}
/// Parse one PackedElementField submessage of `len` bytes.
[[nodiscard]] bool readPackedElementField(
gpio::CodedInputStream& in, uint32_t len, PJ::sdk::PointField& out, const VoxelGridFieldNumbers& fields) {
const auto limit = in.PushLimit(static_cast<int>(len));
uint32_t tag = 0;
while ((tag = in.ReadTag()) != 0) {
const int field = static_cast<int>(tag >> 3);
const uint32_t wt = tag & 0x7u;
if (field == fields.pef_name && wt == kWireLen) {
uint32_t s = 0;
if (!in.ReadVarint32(&s) || !in.ReadString(&out.name, static_cast<int>(s))) {
return false;
}
} else if (field == fields.pef_offset && wt == kWireI32) {
uint32_t off = 0;
if (!in.ReadLittleEndian32(&off)) {
return false;
}
out.offset = off;
} else if (field == fields.pef_type && wt == kWireVarint) {
uint64_t t = 0;
if (!in.ReadVarint64(&t)) {
return false;
}
out.datatype = mapFoxgloveNumericType(t);
} else if (!skipField(in, wt)) {
return false;
}
}
in.PopLimit(limit);
out.count = 1; // Foxglove PackedElementField has no `count`; one element per field.
return true;
}
/// Read a fixed32 scalar field into `out` (the Foxglove counts/strides are all
/// fixed32, like PointCloud's point_stride).
[[nodiscard]] bool readFixed32(gpio::CodedInputStream& in, uint32_t wire_type, uint32_t& out) {
return wire_type == kWireI32 && in.ReadLittleEndian32(&out);
}
} // namespace
VoxelGridFieldNumbers resolveVoxelGridFieldNumbers(const google::protobuf::Descriptor* descriptor) {
VoxelGridFieldNumbers n; // official defaults
n.timestamp = fieldNumberOr(descriptor, "timestamp", n.timestamp);
n.frame_id = fieldNumberOr(descriptor, "frame_id", n.frame_id);
n.pose = fieldNumberOr(descriptor, "pose", n.pose);
n.row_count = fieldNumberOr(descriptor, "row_count", n.row_count);
n.column_count = fieldNumberOr(descriptor, "column_count", n.column_count);
n.cell_size = fieldNumberOr(descriptor, "cell_size", n.cell_size);
n.slice_stride = fieldNumberOr(descriptor, "slice_stride", n.slice_stride);
n.row_stride = fieldNumberOr(descriptor, "row_stride", n.row_stride);
n.cell_stride = fieldNumberOr(descriptor, "cell_stride", n.cell_stride);
n.fields = fieldNumberOr(descriptor, "fields", n.fields);
n.data = fieldNumberOr(descriptor, "data", n.data);
// nested PackedElementField, the message type of the `fields` field.
const google::protobuf::Descriptor* pef = nestedDescriptor(descriptor, "fields");
n.pef_name = fieldNumberOr(pef, "name", n.pef_name);
n.pef_offset = fieldNumberOr(pef, "offset", n.pef_offset);
n.pef_type = fieldNumberOr(pef, "type", n.pef_type);
return n;
}
PJ::Expected<PJ::sdk::VoxelGrid> deserializeFoxgloveVoxelGridView(
const uint8_t* data, size_t size, PJ::sdk::BufferAnchor anchor, const VoxelGridFieldNumbers& fields) {
if (size > static_cast<size_t>(std::numeric_limits<int>::max())) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: message too large"));
}
PJ::sdk::VoxelGrid grid;
// Identity origin until a pose field overrides it (Foxglove may omit it).
grid.origin.position = {.x = 0.0, .y = 0.0, .z = 0.0};
grid.origin.orientation = {.x = 0.0, .y = 0.0, .z = 0.0, .w = 1.0};
gpio::CodedInputStream in(data, static_cast<int>(size));
in.SetTotalBytesLimit(std::numeric_limits<int>::max());
int64_t ts_ns = 0;
PJ::Span<const uint8_t> data_span;
uint32_t tag = 0;
while ((tag = in.ReadTag()) != 0) {
const int field = static_cast<int>(tag >> 3);
const uint32_t wt = tag & 0x7u;
if (field == fields.timestamp) {
if (wt != kWireLen) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: bad timestamp wire type"));
}
uint32_t len = 0;
if (!in.ReadVarint32(&len) || !readTimestampNs(in, len, ts_ns)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read timestamp"));
}
} else if (field == fields.frame_id) {
if (wt != kWireLen) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: bad frame_id wire type"));
}
uint32_t len = 0;
if (!in.ReadVarint32(&len) || !in.ReadString(&grid.frame_id, static_cast<int>(len))) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read frame_id"));
}
} else if (field == fields.pose) {
if (wt != kWireLen) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: bad pose wire type"));
}
uint32_t len = 0;
if (!in.ReadVarint32(&len) || !readPose(in, len, grid.origin)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read pose"));
}
} else if (field == fields.cell_size) {
if (wt != kWireLen) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: bad cell_size wire type"));
}
uint32_t len = 0;
if (!in.ReadVarint32(&len) || !readVector3(in, len, grid.cell_size)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read cell_size"));
}
} else if (field == fields.row_count) {
if (!readFixed32(in, wt, grid.row_count)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read row_count"));
}
} else if (field == fields.column_count) {
if (!readFixed32(in, wt, grid.column_count)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read column_count"));
}
} else if (field == fields.slice_stride) {
if (!readFixed32(in, wt, grid.slice_stride)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read slice_stride"));
}
} else if (field == fields.row_stride) {
if (!readFixed32(in, wt, grid.row_stride)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read row_stride"));
}
} else if (field == fields.cell_stride) {
if (!readFixed32(in, wt, grid.cell_stride)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read cell_stride"));
}
} else if (field == fields.fields) {
if (wt != kWireLen) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: bad fields wire type"));
}
uint32_t len = 0;
if (!in.ReadVarint32(&len)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read field length"));
}
PJ::sdk::PointField pf;
if (!readPackedElementField(in, len, pf, fields)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read PackedElementField"));
}
grid.fields.push_back(std::move(pf));
} else if (field == fields.data) {
if (wt != kWireLen) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: bad data wire type"));
}
uint32_t len = 0;
if (!in.ReadVarint32(&len)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to read data length"));
}
if (len > 0) {
// Zero-copy: alias the packed voxel bytes in place (a dense grid is
// multi-MB). GetDirectBufferPointer hands back a pointer into the
// original `data` buffer; the BufferAnchor keeps it alive past this call.
const void* ptr = nullptr;
int avail = 0;
if (!in.GetDirectBufferPointer(&ptr, &avail) || avail < static_cast<int>(len)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: data not contiguous"));
}
data_span = PJ::Span<const uint8_t>(static_cast<const uint8_t*>(ptr), len);
if (!in.Skip(static_cast<int>(len))) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: failed to skip data"));
}
}
} else if (!skipField(in, wt)) {
return PJ::unexpected(std::string("foxglove.VoxelGrid: malformed message"));
}
}
grid.timestamp_ns = ts_ns;
// Foxglove omits the depth (slice) count; derive it from the buffer like the
// PointCloud codec derives width from data.size() / point_stride.
grid.slice_count = grid.slice_stride > 0 ? static_cast<uint32_t>(data_span.size() / grid.slice_stride) : 0;
grid.data = data_span;
grid.anchor = std::move(anchor);
return grid;
}
} // namespace pj_protobuf