17#include <unordered_map>
43 void Write(std::ostream &out)
const {
44 const uint32_t count =
static_cast<uint32_t
>(
positions.size());
49 for (
double value : position.location) {
52 for (
double value : position.rotation) {
60 bool Read(std::istream &in, uint32_t size) {
64 uint16_t record_size = 0u;
69 for (uint32_t i = 0u; i < count; ++i) {
72 bool read = record.
Read(position.
id);
73 for (
double &value : position.
location) {
74 read = read && record.
Read(value);
76 for (
double &value : position.
rotation) {
77 read = read && record.
Read(value);
102 std::array<double, 4>
rotation = {0.0, 0.0, 0.0, 1.0};
136 void Write(std::ostream &out)
const {
137 std::ostringstream payload;
140 std::ostringstream record;
143 std::ostringstream value;
145 for (
const WheelPose &wheel : vehicle.wheels) {
152 if (!vehicle.open_doors.empty()) {
153 std::ostringstream value;
155 for (
const DoorPose &door : vehicle.open_doors) {
157 for (
double axis : door.location) {
160 for (
double axis : door.rotation) {
166 if (vehicle.current_gear) {
167 std::ostringstream value;
169 const uint8_t flags =
170 (vehicle.manual_gear_shift ? 1u : 0u) | (vehicle.simulating_physics ? 2u : 0u);
179 bool Read(std::istream &in, uint32_t size) {
186 for (uint32_t i = 0u; i < count; ++i) {
192 return ReadList(value, kWheelRecordSizeV1, vehicle.wheels, [](BoundedReader &item, WheelPose &wheel) {
193 return item.Read(wheel.steer) && item.Read(wheel.spin) && item.Read(wheel.suspension_offset);
196 return ReadList(value, kDoorRecordSizeV1, vehicle.open_doors, [](BoundedReader &item, DoorPose &door) {
197 bool read = item.Read(door.door);
198 for (double &axis : door.location) {
199 read = read && item.Read(axis);
201 for (double &axis : door.rotation) {
202 read = read && item.Read(axis);
209 if (!value.Read(gear) || !value.Read(flags)) {
212 vehicle.current_gear = gear;
213 vehicle.manual_gear_shift = (flags & 1u) != 0u;
214 vehicle.simulating_physics = (flags & 2u) != 0u;
225 vehicles.push_back(std::move(vehicle));
232 template <
typename T,
typename F>
235 uint16_t record_size = 0u;
241 for (uint32_t i = 0u; i < count; ++i) {
258 const std::vector<uint32_t> &ids,
259 const std::unordered_map<uint32_t, uint32_t> &parents) {
260 std::unordered_map<uint32_t, int> in_list;
261 for (uint32_t
id : ids) {
262 in_list.emplace(
id, 0);
264 std::unordered_map<uint32_t, size_t> depth;
265 auto depth_of = [&](uint32_t id) {
268 uint32_t current = id;
269 while (steps <= ids.size()) {
270 const auto cached = depth.find(current);
271 if (cached != depth.end()) {
272 return cached->second + steps;
274 const auto parent = parents.find(current);
275 if ((parent == parents.end()) || (parent->second == 0u) ||
276 (in_list.find(parent->second) == in_list.end())) {
279 current = parent->second;
284 std::vector<std::pair<size_t, uint32_t>> ordered;
285 ordered.reserve(ids.size());
286 for (uint32_t
id : ids) {
287 const size_t d = depth_of(
id);
288 depth.emplace(
id, d);
289 ordered.emplace_back(d,
id);
291 std::stable_sort(ordered.begin(), ordered.end(), [](
const auto &a,
const auto &b) {
292 return a.first < b.first;
294 std::vector<uint32_t> result;
295 result.reserve(ordered.size());
296 for (
const auto &item : ordered) {
297 result.push_back(item.second);
Reads the fields of one packet (or one record inside it) without ever consuming more than its declare...
bool ReadFixedListHeader(uint16_t min_record_size, uint32_t &count, uint16_t &record_size)
Header of a list of fixed-size records, checked against the bytes left.
bool ReadRecord(uint32_t bytes, F &&read)
Runs read(record) on the next bytes as a reader of their own, then skips what it left unread (a recor...
bool ReadSizedRecord(F &&read)
Reads one [u32 record_size][record] entry of a variable-size list.
bool ReadVariableListCount(uint32_t &count)
Count of a list of variable-size records, checked against the bytes left.
bool ReadFields(F &&read)
Reads TLV fields until the reader is exhausted.
void WritePod(std::ostream &out, const T &value)
void WriteFixedListHeader(std::ostream &out, uint32_t count, uint16_t record_size)
Header of a list of fixed-size records.
void WriteField(std::ostream &out, uint8_t kind, const std::string &value)
void WriteFrameDataHeader(std::ostream &out, uint8_t id, uint32_t payload_size)
void WriteSizedBytes(std::ostream &out, const std::string &bytes)
One [u32 size][bytes] entry: a variable-size record or, after a kind byte, a TLV value.
void WriteFrameDataPacket(std::ostream &out, uint8_t id, const std::string &payload)
Writes a packet whose payload was built beforehand.
std::vector< uint32_t > OrderParentsFirst(const std::vector< uint32_t > &ids, const std::unordered_map< uint32_t, uint32_t > &parents)
Order in which to place actors so that every parent is placed before its children: by attachment dept...
This file contains definitions of common data structures used in traffic manager.
An open door, which on the primary is a physics body of its own: its pose relative to the vehicle,...
std::array< double, 3 > location
std::array< double, 4 > rotation
One record of frame data packet 34: an actor's world pose as the primary holds it,...
double rotation[4]
x, y, z, w
Frame data packet 34: every live actor's pose in this frame.
bool Read(std::istream &in, uint32_t size)
Reads a payload of size bytes; records that grew in a newer build are read up to the fields known her...
void Write(std::ostream &out) const
static constexpr uint16_t kRecordSizeV1
static constexpr uint8_t kPacketId
std::vector< MirroredPosition > positions
Frame data packet 32, every frame: the moving parts of every vehicle.
static constexpr uint8_t kPacketId
std::vector< VehicleParts > vehicles
static constexpr uint16_t kDoorRecordSizeV1
void Write(std::ostream &out) const
static bool ReadList(BoundedReader &value, uint16_t min_record_size, std::vector< T > &out, F &&read)
static constexpr uint16_t kWheelRecordSizeV1
bool Read(std::istream &in, uint32_t size)
A vehicle's moving parts: every wheel, by wheel index, and the doors open (any other door is closed),...
std::vector< DoorPose > open_doors
std::vector< WheelPose > wheels
std::optional< int32_t > current_gear
bool simulating_physics
Whether the primary's body simulates physics (it reports no angular velocity otherwise).
A wheel as the primary draws it: degrees and cm.