CARLA
Loading...
Searching...
No Matches
worldState.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2// This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#pragma once
6
8
9#include <algorithm>
10#include <array>
11#include <cstdint>
12#include <istream>
13#include <iterator>
14#include <optional>
15#include <ostream>
16#include <sstream>
17#include <string>
18#include <utility>
19#include <vector>
20
21namespace carla {
22namespace multigpu {
23
24 /// Frame data packet 28, on every frame: world-wide state a client can
25 /// change on the primary that changes what sensors see or measure.
26 struct WorldGlobals {
27
28 static constexpr uint8_t kPacketId = 28u;
29
30 static constexpr uint32_t kPayloadSizeV1 = 12u;
31
32 /// rpc::MapLayer bits of the layers loaded.
33 uint16_t map_layers = 0xFFFFu;
34
35 /// The IMU's gravity, m/s^2.
36 float imu_gravity = 9.81f;
37
38 /// UCarlaLightSubsystem: whether street lights follow the sun, and the
39 /// last day/night state it broadcast (if any).
40 bool day_night_cycle = true;
41 bool has_day_time_state = false;
42 bool last_is_day = true;
43
44 /// ROS 2: whether TF frames are published.
45 bool publish_tf = true;
46
47 void Write(std::ostream &out) const {
51 const uint8_t flags =
52 (day_night_cycle ? 1u : 0u) |
53 (has_day_time_state ? 2u : 0u) |
54 (last_is_day ? 4u : 0u) |
55 (publish_tf ? 8u : 0u);
56 detail::WritePod(out, flags);
57 const uint8_t reserved[5] = {0u, 0u, 0u, 0u, 0u};
58 out.write(reinterpret_cast<const char *>(reserved), sizeof(reserved));
59 }
60
61 bool Read(std::istream &in, uint32_t size) {
62 if (size < kPayloadSizeV1) {
63 in.setstate(std::ios_base::failbit);
64 return false;
65 }
66 BoundedReader reader(in, size);
67 uint8_t flags = 0u;
68 uint8_t reserved[5];
69 if (!reader.Read(map_layers) || !reader.Read(imu_gravity) || !reader.Read(flags) ||
70 !reader.ReadBytes(reserved, sizeof(reserved))) {
71 return false;
72 }
73 day_night_cycle = (flags & 1u) != 0u;
74 has_day_time_state = (flags & 2u) != 0u;
75 last_is_day = (flags & 4u) != 0u;
76 publish_tf = (flags & 8u) != 0u;
77 return true;
78 }
79 };
80
81 /// Frame data packet 29, on change and on every full resync: map objects
82 /// whose state is not part of any actor. Scene lights travel in packet 11,
83 /// the recorder's SceneLight.
84 struct WorldObjects {
85
86 static constexpr uint8_t kPacketId = 29u;
87
88 enum Field : uint8_t {
89 /// The complete, sorted set of environment objects disabled.
91 };
92
93 std::optional<std::vector<uint64_t>> disabled_environment_objects;
94
95 bool empty() const {
97 }
98
99 void Write(std::ostream &out) const {
100 std::ostringstream payload;
102 std::ostringstream value;
103 detail::WritePod(value, static_cast<uint32_t>(disabled_environment_objects->size()));
104 for (uint64_t id : *disabled_environment_objects) {
105 detail::WritePod(value, id);
106 }
107 WriteField(payload, kDisabledEnvironmentObjects, value.str());
108 }
109 WriteFrameDataPacket(out, kPacketId, payload.str());
110 }
111
112 bool Read(std::istream &in, uint32_t size) {
114 BoundedReader reader(in, size);
115 return reader.ReadFields([&](uint8_t kind, BoundedReader &value) {
116 switch (kind) {
118 uint32_t count = 0u;
119 if (!value.Read(count) || (count > value.Remaining() / sizeof(uint64_t))) {
120 return false;
121 }
122 std::vector<uint64_t> ids(count);
123 for (uint64_t &id : ids) {
124 if (!value.Read(id)) {
125 return false;
126 }
127 }
128 disabled_environment_objects = std::move(ids);
129 return true;
130 }
131 default:
132 return true;
133 }
134 });
135 }
136 };
137
138 /// An actor's appearance on the primary that a mirror does not get from
139 /// its spawn attributes or pose.
141 uint32_t actor_id = 0u;
142 std::optional<std::array<double, 3>> scale;
143 std::optional<bool> collisions;
144 std::optional<bool> hidden;
145 };
146
147 /// Frame data packet 30, on change and on every full resync.
149
150 static constexpr uint8_t kPacketId = 30u;
151
152 enum Field : uint8_t {
153 kScale = 1u,
155 kHidden = 3u
156 };
157
158 std::vector<ActorAppearance> actors;
159
160 void Write(std::ostream &out) const {
161 std::ostringstream payload;
162 detail::WritePod(payload, static_cast<uint32_t>(actors.size()));
163 for (const ActorAppearance &actor : actors) {
164 std::ostringstream record;
165 detail::WritePod(record, actor.actor_id);
166 if (actor.scale) {
167 std::ostringstream value;
168 for (double axis : *actor.scale) {
169 detail::WritePod(value, axis);
170 }
171 WriteField(record, kScale, value.str());
172 }
173 if (actor.collisions) {
174 WriteField(record, kCollisions, std::string(1u, *actor.collisions ? '\1' : '\0'));
175 }
176 if (actor.hidden) {
177 WriteField(record, kHidden, std::string(1u, *actor.hidden ? '\1' : '\0'));
178 }
179 WriteSizedBytes(payload, record.str());
180 }
181 WriteFrameDataPacket(out, kPacketId, payload.str());
182 }
183
184 bool Read(std::istream &in, uint32_t size) {
185 actors.clear();
186 BoundedReader reader(in, size);
187 uint32_t count = 0u;
188 if (!reader.ReadVariableListCount(count)) {
189 return false;
190 }
191 for (uint32_t i = 0u; i < count; ++i) {
192 ActorAppearance actor;
193 const bool ok = reader.ReadSizedRecord([&](BoundedReader &record) {
194 return record.Read(actor.actor_id) && record.ReadFields([&](uint8_t kind, BoundedReader &value) {
195 switch (kind) {
196 case kScale: {
197 std::array<double, 3> scale;
198 for (double &axis : scale) {
199 if (!value.Read(axis)) {
200 return false;
201 }
202 }
203 actor.scale = scale;
204 return true;
205 }
206 case kCollisions:
207 case kHidden: {
208 uint8_t flag = 0u;
209 if (!value.Read(flag)) {
210 return false;
211 }
212 (kind == kCollisions ? actor.collisions : actor.hidden) = (flag != 0u);
213 return true;
214 }
215 default:
216 return true;
217 }
218 });
219 });
220 if (!ok) {
221 return false;
222 }
223 actors.push_back(std::move(actor));
224 }
225 return true;
226 }
227 };
228
229 /// The map layers to load and to unload to go from @a current to @a desired.
230 inline std::pair<uint16_t, uint16_t> ComputeMapLayerDiff(uint16_t current, uint16_t desired) {
231 return {static_cast<uint16_t>(desired & ~current), static_cast<uint16_t>(current & ~desired)};
232 }
233
234 /// Ids in @a desired and not in @a current (first), and the other way round
235 /// (second). Both inputs sorted.
236 inline std::pair<std::vector<uint64_t>, std::vector<uint64_t>> DiffSortedIds(
237 const std::vector<uint64_t> &current,
238 const std::vector<uint64_t> &desired) {
239 std::pair<std::vector<uint64_t>, std::vector<uint64_t>> diff;
240 std::set_difference(desired.begin(), desired.end(), current.begin(), current.end(),
241 std::back_inserter(diff.first));
242 std::set_difference(current.begin(), current.end(), desired.begin(), desired.end(),
243 std::back_inserter(diff.second));
244 return diff;
245 }
246
247} // namespace multigpu
248} // namespace carla
Reads the fields of one packet (or one record inside it) without ever consuming more than its declare...
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 ReadBytes(void *out, uint32_t bytes)
bool ReadFields(F &&read)
Reads TLV fields until the reader is exhausted.
void WritePod(std::ostream &out, const T &value)
void WriteField(std::ostream &out, uint8_t kind, const std::string &value)
std::pair< std::vector< uint64_t >, std::vector< uint64_t > > DiffSortedIds(const std::vector< uint64_t > &current, const std::vector< uint64_t > &desired)
Ids in desired and not in current (first), and the other way round (second).
Definition worldState.h:236
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.
std::pair< uint16_t, uint16_t > ComputeMapLayerDiff(uint16_t current, uint16_t desired)
The map layers to load and to unload to go from current to desired.
Definition worldState.h:230
void WriteFrameDataPacket(std::ostream &out, uint8_t id, const std::string &payload)
Writes a packet whose payload was built beforehand.
This file contains definitions of common data structures used in traffic manager.
An actor's appearance on the primary that a mirror does not get from its spawn attributes or pose.
Definition worldState.h:140
std::optional< bool > hidden
Definition worldState.h:144
std::optional< std::array< double, 3 > > scale
Definition worldState.h:142
std::optional< bool > collisions
Definition worldState.h:143
Frame data packet 30, on change and on every full resync.
Definition worldState.h:148
std::vector< ActorAppearance > actors
Definition worldState.h:158
bool Read(std::istream &in, uint32_t size)
Definition worldState.h:184
void Write(std::ostream &out) const
Definition worldState.h:160
static constexpr uint8_t kPacketId
Definition worldState.h:150
Frame data packet 28, on every frame: world-wide state a client can change on the primary that change...
Definition worldState.h:26
float imu_gravity
The IMU's gravity, m/s^2.
Definition worldState.h:36
bool publish_tf
ROS 2: whether TF frames are published.
Definition worldState.h:45
uint16_t map_layers
rpc::MapLayer bits of the layers loaded.
Definition worldState.h:33
bool Read(std::istream &in, uint32_t size)
Definition worldState.h:61
void Write(std::ostream &out) const
Definition worldState.h:47
static constexpr uint32_t kPayloadSizeV1
Definition worldState.h:30
static constexpr uint8_t kPacketId
Definition worldState.h:28
bool day_night_cycle
UCarlaLightSubsystem: whether street lights follow the sun, and the last day/night state it broadcast...
Definition worldState.h:40
Frame data packet 29, on change and on every full resync: map objects whose state is not part of any ...
Definition worldState.h:84
bool Read(std::istream &in, uint32_t size)
Definition worldState.h:112
void Write(std::ostream &out) const
Definition worldState.h:99
static constexpr uint8_t kPacketId
Definition worldState.h:86
@ kDisabledEnvironmentObjects
The complete, sorted set of environment objects disabled.
Definition worldState.h:90
std::optional< std::vector< uint64_t > > disabled_environment_objects
Definition worldState.h:93