CARLA
Loading...
Searching...
No Matches
World.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include "carla/Memory.h"
10#include "carla/Time.h"
20#include "carla/rpc/Actor.h"
25#include "carla/rpc/MapLayer.h"
29#include "carla/rpc/Texture.h"
31
32#include <optional>
33
34namespace carla {
35namespace client {
36
37 class Actor;
38 class ActorBlueprint;
39 class ActorList;
40 class BlueprintLibrary;
41 class Map;
42 class TrafficLight;
43 class TrafficSign;
44
45 class World {
46 public:
47
48 explicit World(detail::EpisodeProxy episode) : _episode(std::move(episode)) {}
49
51
52 World(const World &) = default;
53 World(World &&) = default;
54
55 World &operator=(const World &) = default;
56 World &operator=(World &&) = default;
57
58 /// Get the id of the episode associated with this world.
59 uint64_t GetId() const {
60 return _episode.GetId();
61 }
62
63 /// Return the map that describes this world.
64 SharedPtr<Map> GetMap() const;
65
66 void LoadLevelLayer(rpc::MapLayer map_layers) const;
67
68 void UnloadLevelLayer(rpc::MapLayer map_layers) const;
69
70 /// Return the list of blueprints available in this world. This blueprints
71 /// can be used to spawning actor into the world.
73
74 /// Returns a list of pairs where the firts element is the vehicle ID
75 /// and the second one is the light state
77
78 /// Get a random location from the pedestrians navigation mesh
79 std::optional<geom::Location> GetRandomLocationFromNavigation() const;
80
81 /// Return the spectator actor. The spectator controls the view in the
82 /// simulator window.
84
86
87 /// @return The id of the frame when the settings were applied.
88 uint64_t ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout);
89
90 /// Retrieve the weather parameters currently active in the world.
92
93 /// Change the weather in the simulation.
94 void SetWeather(const rpc::WeatherParameters &weather);
95
96 /// Get gravity value used for the IMU sensor accelerometer calculation.
97 float GetIMUSensorGravity() const;
98
99 /// Set gravity value used for the IMU sensor accelerometer calculation.
100 void SetIMUSensorGravity(float gravity);
101
102 /// Query if the Weather is enabled or not
103 bool IsWeatherEnabled() const;
104
105 /// Return a snapshot of the world at this moment.
107
108 /// Find actor by id, return nullptr if not found.
110
111 /// Return a list with all the actors currently present in the world.
113
114 /// Return a list with the actors requested by ActorId.
115 SharedPtr<ActorList> GetActors(const std::vector<ActorId> &actor_ids) const;
116
117 /// Spawn an actor into the world based on the @a blueprint provided at @a
118 /// transform. If a @a parent is provided, the actor is attached to
119 /// @a parent.
121 const ActorBlueprint &blueprint,
122 const geom::Transform &transform,
123 Actor *parent = nullptr,
125
126 /// Same as SpawnActor but return nullptr on failure instead of throwing an
127 /// exception.
129 const ActorBlueprint &blueprint,
130 const geom::Transform &transform,
131 Actor *parent = nullptr,
132 rpc::AttachmentType attachment_type = rpc::AttachmentType::Rigid) noexcept;
133
134 /// Block calling thread until a world tick is received.
136
137 /// Register a @a callback to be called every time a world tick is received.
138 ///
139 /// @return ID of the callback, use it to remove the callback.
140 size_t OnTick(std::function<void(WorldSnapshot)> callback);
141
142 /// Remove a callback registered with OnTick.
143 void RemoveOnTick(size_t callback_id);
144
145 /// Signal the simulator to continue to next tick (only has effect on
146 /// synchronous mode).
147 ///
148 /// @return The id of the frame that this call started.
149 uint64_t Tick(time_duration timeout);
150
151 /// set the probability that an agent could cross the roads in its path following
152 /// percentage of 0.0f means no pedestrian can cross roads
153 /// percentage of 0.5f means 50% of all pedestrians can cross roads
154 /// percentage of 1.0f means all pedestrians can cross roads if needed
155 void SetPedestriansCrossFactor(float percentage);
156
157 /// set the seed to use with random numbers in the pedestrians module
158 void SetPedestriansSeed(unsigned int seed);
159
160 SharedPtr<Actor> GetTrafficSign(const Landmark& landmark) const;
161
162 SharedPtr<Actor> GetTrafficLight(const Landmark& landmark) const;
163
165
167
168 /// Spawns a static procedural mesh with collision. Vertices are a flat
169 /// (x, y, z) triple list in metres, client coordinate frame; triangles
170 /// index vertex triples. Material is a semantic hint ("grass", "road",
171 /// "dirt", "sidewalk") or a full UE object path. The mesh lives until
172 /// the world is reloaded.
173 void SpawnCustomMesh(
174 const std::vector<float> &vertices,
175 const std::vector<uint32_t> &triangles,
176 const std::string &material);
177
179
181 return DebugHelper{_episode};
182 }
183
185 return _episode;
186 };
187
188 void FreezeAllTrafficLights(bool frozen);
189
190 /// Returns all the BBs of all the elements of the level
191 std::vector<geom::BoundingBox> GetLevelBBs(uint8_t queried_tag) const;
192
193 std::vector<rpc::EnvironmentObject> GetEnvironmentObjects(uint8_t queried_tag) const;
194
196 std::vector<uint64_t> env_objects_ids,
197 bool enable) const;
198
199 std::optional<rpc::LabelledPoint> ProjectPoint(
200 geom::Location location, geom::Vector3D direction, float search_distance = 10000.f) const;
201
202 std::optional<rpc::LabelledPoint> GroundProjection(
203 geom::Location location, float search_distance = 10000.0) const;
204
205 std::vector<rpc::LabelledPoint> CastRay(
206 geom::Location start_location, geom::Location end_location) const;
207
208 std::vector<SharedPtr<Actor>> GetTrafficLightsFromWaypoint(
209 const Waypoint& waypoint, double distance) const;
210
211 std::vector<SharedPtr<Actor>> GetTrafficLightsInJunction(
212 const road::JuncId junc_id) const;
213
214 // std::vector<std::string> GetObjectNameList();
215
217 const std::string &actor_name,
218 const rpc::MaterialParameter& parameter,
219 const rpc::TextureColor& Texture);
220
222 const std::vector<std::string> &objects_names,
223 const rpc::MaterialParameter& parameter,
224 const rpc::TextureColor& Texture);
225
227 const std::string &actor_name,
228 const rpc::MaterialParameter& parameter,
229 const rpc::TextureFloatColor& Texture);
230
232 const std::vector<std::string> &objects_names,
233 const rpc::MaterialParameter& parameter,
234 const rpc::TextureFloatColor& Texture);
235
236 /// Light the scene with an environment map: an equirectangular linear
237 /// RGB(A) float panorama in the CARLA world frame (column 0 = azimuth 0 =
238 /// +X, azimuth grows toward +Y; row 0 = zenith) becomes the sky light's
239 /// cubemap (ambient + reflections) at the given intensity, replacing the
240 /// real-time atmosphere capture until ClearSkyLightMap. Persists across
241 /// weather changes; dropped by a level load (LoadWorld / ReloadWorld), so
242 /// set it again after loading. face_size is the cubemap resolution.
243 void SetSkyLightMap(
244 const rpc::TextureFloatColor& panorama,
245 float intensity = 1.0f,
246 int32_t face_size = 512);
247
248 void ClearSkyLightMap();
249
250 bool HasSkyLightMap() const;
251
253 const std::string &actor_name,
254 const rpc::TextureColor& diffuse_texture,
255 const rpc::TextureFloatColor& emissive_texture,
256 const rpc::TextureFloatColor& normal_texture,
257 const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture);
258
260 const std::vector<std::string> &objects_names,
261 const rpc::TextureColor& diffuse_texture,
262 const rpc::TextureFloatColor& emissive_texture,
263 const rpc::TextureFloatColor& normal_texture,
264 const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture);
265
266 std::vector<std::string> GetNamesOfAllObjects() const;
267
268 /// Set whether to publish ROS2 TF information
269 void SetPublishTF(bool publish_tf);
270
271 /// Get whether ROS2 TF information is being published
272 bool GetPublishTF() const;
273
274 /// Get all ego spawn points available in the level.
275 std::vector<geom::Transform> GetEgoSpawnPoints() const;
276
277 private:
278
280 };
281
282} // namespace client
283} // namespace carla
float distance
float intensity
Contains all the necessary information for spawning an Actor.
Represents an actor in the simulation.
Class containing a reference to RoadInfoSignal.
Definition Landmark.h:22
void ClearSkyLightMap()
Definition World.cpp:360
float GetIMUSensorGravity() const
Get gravity value used for the IMU sensor accelerometer calculation.
Definition World.cpp:102
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
Definition World.cpp:126
void ApplyColorTextureToObject(const std::string &actor_name, const rpc::MaterialParameter &parameter, const rpc::TextureColor &Texture)
Definition World.cpp:325
std::vector< geom::BoundingBox > GetLevelBBs(uint8_t queried_tag) const
Returns all the BBs of all the elements of the level.
Definition World.cpp:253
void LoadLevelLayer(rpc::MapLayer map_layers) const
Definition World.cpp:30
SharedPtr< BlueprintLibrary > GetBlueprintLibrary() const
Return the list of blueprints available in this world.
Definition World.cpp:38
World(const World &)=default
void EnableEnvironmentObjects(std::vector< uint64_t > env_objects_ids, bool enable) const
Definition World.cpp:261
void FreezeAllTrafficLights(bool frozen)
Definition World.cpp:249
std::vector< geom::Transform > GetEgoSpawnPoints() const
Get all ego spawn points available in the level.
Definition World.cpp:380
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
Definition World.cpp:114
DebugHelper MakeDebugHelper() const
Definition World.h:180
void ResetAllTrafficLights()
Definition World.cpp:234
bool HasSkyLightMap() const
Definition World.cpp:364
void SetPublishTF(bool publish_tf)
Set whether to publish ROS2 TF information.
Definition World.cpp:372
World & operator=(const World &)=default
rpc::WeatherParameters GetWeather() const
Retrieve the weather parameters currently active in the world.
Definition World.cpp:94
SharedPtr< Actor > GetSpectator() const
Return the spectator actor.
Definition World.cpp:50
uint64_t GetId() const
Get the id of the episode associated with this world.
Definition World.h:59
rpc::VehicleLightStateList GetVehiclesLightStates() const
Returns a list of pairs where the firts element is the vehicle ID and the second one is the light sta...
Definition World.cpp:42
SharedPtr< Actor > GetActor(ActorId id) const
Find actor by id, return nullptr if not found.
Definition World.cpp:118
SharedPtr< Actor > GetTrafficLight(const Landmark &landmark) const
Definition World.cpp:203
SharedPtr< Actor > TrySpawnActor(const ActorBlueprint &blueprint, const geom::Transform &transform, Actor *parent=nullptr, rpc::AttachmentType attachment_type=rpc::AttachmentType::Rigid) noexcept
Same as SpawnActor but return nullptr on failure instead of throwing an exception.
Definition World.cpp:146
World & operator=(World &&)=default
void SetPedestriansCrossFactor(float percentage)
set the probability that an agent could cross the roads in its path following percentage of 0....
Definition World.cpp:179
World(World &&)=default
uint64_t Tick(time_duration timeout)
Signal the simulator to continue to next tick (only has effect on synchronous mode).
Definition World.cpp:173
detail::EpisodeProxy _episode
Definition World.h:279
std::optional< rpc::LabelledPoint > GroundProjection(geom::Location location, float search_distance=10000.0) const
Definition World.cpp:276
detail::EpisodeProxy GetEpisode() const
Definition World.h:184
World(detail::EpisodeProxy episode)
Definition World.h:48
std::optional< rpc::LabelledPoint > ProjectPoint(geom::Location location, geom::Vector3D direction, float search_distance=10000.f) const
Definition World.cpp:267
bool IsWeatherEnabled() const
Query if the Weather is enabled or not.
Definition World.cpp:110
WorldSnapshot WaitForTick(time_duration timeout) const
Block calling thread until a world tick is received.
Definition World.cpp:158
std::vector< SharedPtr< Actor > > GetTrafficLightsInJunction(const road::JuncId junc_id) const
Definition World.cpp:307
void ApplyTexturesToObject(const std::string &actor_name, const rpc::TextureColor &diffuse_texture, const rpc::TextureFloatColor &emissive_texture, const rpc::TextureFloatColor &normal_texture, const rpc::TextureFloatColor &ao_roughness_metallic_emissive_texture)
Definition World.cpp:384
std::vector< std::string > GetNamesOfAllObjects() const
Definition World.cpp:368
uint64_t ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout)
Definition World.cpp:58
void SetSkyLightMap(const rpc::TextureFloatColor &panorama, float intensity=1.0f, int32_t face_size=512)
Light the scene with an environment map: an equirectangular linear RGB(A) float panorama in the CARLA...
Definition World.cpp:353
std::vector< rpc::EnvironmentObject > GetEnvironmentObjects(uint8_t queried_tag) const
Definition World.cpp:257
SharedPtr< Map > GetMap() const
Return the map that describes this world.
Definition World.cpp:26
std::vector< rpc::LabelledPoint > CastRay(geom::Location start_location, geom::Location end_location) const
Definition World.cpp:282
std::vector< SharedPtr< Actor > > GetTrafficLightsFromWaypoint(const Waypoint &waypoint, double distance) const
Definition World.cpp:287
void SetIMUSensorGravity(float gravity)
Set gravity value used for the IMU sensor accelerometer calculation.
Definition World.cpp:106
void SpawnCustomMesh(const std::vector< float > &vertices, const std::vector< uint32_t > &triangles, const std::string &material)
Spawns a static procedural mesh with collision.
Definition World.cpp:238
bool GetPublishTF() const
Get whether ROS2 TF information is being published.
Definition World.cpp:376
void ApplyColorTextureToObjects(const std::vector< std::string > &objects_names, const rpc::MaterialParameter &parameter, const rpc::TextureColor &Texture)
Definition World.cpp:332
void ApplyFloatColorTextureToObjects(const std::vector< std::string > &objects_names, const rpc::MaterialParameter &parameter, const rpc::TextureFloatColor &Texture)
Definition World.cpp:346
SharedPtr< Actor > SpawnActor(const ActorBlueprint &blueprint, const geom::Transform &transform, Actor *parent=nullptr, rpc::AttachmentType attachment_type=rpc::AttachmentType::Rigid)
Spawn an actor into the world based on the blueprint provided at transform.
Definition World.cpp:138
void SetPedestriansSeed(unsigned int seed)
set the seed to use with random numbers in the pedestrians module
Definition World.cpp:183
void ApplyTexturesToObjects(const std::vector< std::string > &objects_names, const rpc::TextureColor &diffuse_texture, const rpc::TextureFloatColor &emissive_texture, const rpc::TextureFloatColor &normal_texture, const rpc::TextureFloatColor &ao_roughness_metallic_emissive_texture)
Definition World.cpp:412
SharedPtr< LightManager > GetLightManager() const
Definition World.cpp:245
std::optional< geom::Location > GetRandomLocationFromNavigation() const
Get a random location from the pedestrians navigation mesh.
Definition World.cpp:46
SharedPtr< Actor > GetTrafficSign(const Landmark &landmark) const
Definition World.cpp:187
void ApplyFloatColorTextureToObject(const std::string &actor_name, const rpc::MaterialParameter &parameter, const rpc::TextureFloatColor &Texture)
Definition World.cpp:339
void RemoveOnTick(size_t callback_id)
Remove a callback registered with OnTick.
Definition World.cpp:169
SharedPtr< Actor > GetTrafficLightFromOpenDRIVE(const road::SignId &sign_id) const
Definition World.cpp:219
size_t OnTick(std::function< void(WorldSnapshot)> callback)
Register a callback to be called every time a world tick is received.
Definition World.cpp:165
void SetWeather(const rpc::WeatherParameters &weather)
Change the weather in the simulation.
Definition World.cpp:98
rpc::EpisodeSettings GetSettings() const
Definition World.cpp:54
void UnloadLevelLayer(rpc::MapLayer map_layers) const
Definition World.cpp:34
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Positive time duration up to milliseconds resolution.
Definition Time.h:20
int32_t JuncId
Definition RoadTypes.h:17
std::string SignId
Definition RoadTypes.h:25
std::vector< std::pair< ActorId, VehicleLightState::flag_type > > VehicleLightStateList
carla::SharedPtr< cc::Actor > Actor
carla::SharedPtr< cc::ActorList > ActorList
Definition ALSM.h:35
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15