CARLA
World.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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"
12 #include "carla/client/Landmark.h"
13 #include "carla/client/Waypoint.h"
14 #include "carla/client/Junction.h"
16 #include "carla/client/Timestamp.h"
19 #include "carla/geom/Transform.h"
20 #include "carla/rpc/Actor.h"
25 #include "carla/rpc/MapLayer.h"
29 #include "carla/rpc/Texture.h"
31 
32 #include <boost/optional.hpp>
33 
34 namespace carla {
35 namespace 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 
50  ~World(){}
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  boost::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  /// Return a snapshot of the world at this moment.
97  WorldSnapshot GetSnapshot() const;
98 
99  /// Find actor by id, return nullptr if not found.
101 
102  /// Return a list with all the actors currently present in the world.
104 
105  /// Return a list with the actors requested by ActorId.
106  SharedPtr<ActorList> GetActors(const std::vector<ActorId> &actor_ids) const;
107 
108  /// Spawn an actor into the world based on the @a blueprint provided at @a
109  /// transform. If a @a parent is provided, the actor is attached to
110  /// @a parent.
112  const ActorBlueprint &blueprint,
113  const geom::Transform &transform,
114  Actor *parent = nullptr,
116 
117  /// Same as SpawnActor but return nullptr on failure instead of throwing an
118  /// exception.
120  const ActorBlueprint &blueprint,
121  const geom::Transform &transform,
122  Actor *parent = nullptr,
123  rpc::AttachmentType attachment_type = rpc::AttachmentType::Rigid) noexcept;
124 
125  /// Block calling thread until a world tick is received.
126  WorldSnapshot WaitForTick(time_duration timeout) const;
127 
128  /// Register a @a callback to be called every time a world tick is received.
129  ///
130  /// @return ID of the callback, use it to remove the callback.
131  size_t OnTick(std::function<void(WorldSnapshot)> callback);
132 
133  /// Remove a callback registered with OnTick.
134  void RemoveOnTick(size_t callback_id);
135 
136  /// Signal the simulator to continue to next tick (only has effect on
137  /// synchronous mode).
138  ///
139  /// @return The id of the frame that this call started.
140  uint64_t Tick(time_duration timeout);
141 
142  /// set the probability that an agent could cross the roads in its path following
143  /// percentage of 0.0f means no pedestrian can cross roads
144  /// percentage of 0.5f means 50% of all pedestrians can cross roads
145  /// percentage of 1.0f means all pedestrians can cross roads if needed
146  void SetPedestriansCrossFactor(float percentage);
147 
148  /// set the seed to use with random numbers in the pedestrians module
149  void SetPedestriansSeed(unsigned int seed);
150 
151  SharedPtr<Actor> GetTrafficSign(const Landmark& landmark) const;
152 
153  SharedPtr<Actor> GetTrafficLight(const Landmark& landmark) const;
154 
156 
157  void ResetAllTrafficLights();
158 
160 
162  return DebugHelper{_episode};
163  }
164 
166  return _episode;
167  };
168 
169  void FreezeAllTrafficLights(bool frozen);
170 
171  /// Returns all the BBs of all the elements of the level
172  std::vector<geom::BoundingBox> GetLevelBBs(uint8_t queried_tag) const;
173 
174  std::vector<rpc::EnvironmentObject> GetEnvironmentObjects(uint8_t queried_tag) const;
175 
177  std::vector<uint64_t> env_objects_ids,
178  bool enable) const;
179 
180  boost::optional<rpc::LabelledPoint> ProjectPoint(
181  geom::Location location, geom::Vector3D direction, float search_distance = 10000.f) const;
182 
183  boost::optional<rpc::LabelledPoint> GroundProjection(
184  geom::Location location, float search_distance = 10000.0) const;
185 
186  std::vector<rpc::LabelledPoint> CastRay(
187  geom::Location start_location, geom::Location end_location) const;
188 
189  std::vector<SharedPtr<Actor>> GetTrafficLightsFromWaypoint(
190  const Waypoint& waypoint, double distance) const;
191 
192  std::vector<SharedPtr<Actor>> GetTrafficLightsInJunction(
193  const road::JuncId junc_id) const;
194 
195  // std::vector<std::string> GetObjectNameList();
196 
198  const std::string &actor_name,
199  const rpc::MaterialParameter& parameter,
200  const rpc::TextureColor& Texture);
201 
203  const std::vector<std::string> &objects_names,
204  const rpc::MaterialParameter& parameter,
205  const rpc::TextureColor& Texture);
206 
208  const std::string &actor_name,
209  const rpc::MaterialParameter& parameter,
210  const rpc::TextureFloatColor& Texture);
211 
213  const std::vector<std::string> &objects_names,
214  const rpc::MaterialParameter& parameter,
215  const rpc::TextureFloatColor& Texture);
216 
218  const std::string &actor_name,
219  const rpc::TextureColor& diffuse_texture,
220  const rpc::TextureFloatColor& emissive_texture,
221  const rpc::TextureFloatColor& normal_texture,
222  const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture);
223 
225  const std::vector<std::string> &objects_names,
226  const rpc::TextureColor& diffuse_texture,
227  const rpc::TextureFloatColor& emissive_texture,
228  const rpc::TextureFloatColor& normal_texture,
229  const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture);
230 
231  std::vector<std::string> GetNamesOfAllObjects() const;
232 
233  private:
234 
236  };
237 
238 } // namespace client
239 } // namespace carla
std::vector< std::string > GetNamesOfAllObjects() const
Definition: World.cpp:326
SharedPtr< BlueprintLibrary > GetBlueprintLibrary() const
Return the list of blueprints available in this world.
Definition: World.cpp:36
std::vector< SharedPtr< Actor > > GetTrafficLightsInJunction(const road::JuncId junc_id) const
Definition: World.cpp:280
void RemoveOnTick(size_t callback_id)
Remove a callback registered with OnTick.
Definition: World.cpp:149
std::string SignId
Definition: RoadTypes.h:25
World & operator=(const World &)=default
void SetWeather(const rpc::WeatherParameters &weather)
Change the weather in the simulation.
Definition: World.cpp:90
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
Definition: World.cpp:106
void SetPedestriansCrossFactor(float percentage)
set the probability that an agent could cross the roads in its path following percentage of 0...
Definition: World.cpp:159
World(detail::EpisodeProxy episode)
Definition: World.h:48
size_t OnTick(std::function< void(WorldSnapshot)> callback)
Register a callback to be called every time a world tick is received.
Definition: World.cpp:145
boost::optional< rpc::LabelledPoint > ProjectPoint(geom::Location location, geom::Vector3D direction, float search_distance=10000.f) const
Definition: World.cpp:240
void UnloadLevelLayer(rpc::MapLayer map_layers) const
Definition: World.cpp:32
SharedPtr< Actor > GetSpectator() const
Return the spectator actor.
Definition: World.cpp:48
SharedPtr< LightManager > GetLightManager() const
Definition: World.cpp:218
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
std::vector< std::pair< ActorId, VehicleLightState::flag_type > > VehicleLightStateList
std::vector< rpc::LabelledPoint > CastRay(geom::Location start_location, geom::Location end_location) const
Definition: World.cpp:255
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
SharedPtr< Map > GetMap() const
Return the map that describes this world.
Definition: World.cpp:24
void ApplyColorTextureToObject(const std::string &actor_name, const rpc::MaterialParameter &parameter, const rpc::TextureColor &Texture)
Definition: World.cpp:298
Class containing a reference to RoadInfoSignal.
Definition: Landmark.h:22
int32_t JuncId
Definition: RoadTypes.h:17
uint64_t Tick(time_duration timeout)
Signal the simulator to continue to next tick (only has effect on synchronous mode).
Definition: World.cpp:153
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:40
detail::EpisodeProxy _episode
Definition: World.h:235
uint64_t GetId() const
Get the id of the episode associated with this world.
Definition: World.h:59
SharedPtr< Actor > GetTrafficLightFromOpenDRIVE(const road::SignId &sign_id) const
Definition: World.cpp:199
void ApplyColorTextureToObjects(const std::vector< std::string > &objects_names, const rpc::MaterialParameter &parameter, const rpc::TextureColor &Texture)
Definition: World.cpp:305
carla::SharedPtr< cc::Actor > Actor
carla::ActorId ActorId
SharedPtr< Actor > GetTrafficSign(const Landmark &landmark) const
Definition: World.cpp:167
void ResetAllTrafficLights()
Definition: World.cpp:214
void LoadLevelLayer(rpc::MapLayer map_layers) const
Definition: World.cpp:28
SharedPtr< Actor > GetActor(ActorId id) const
Find actor by id, return nullptr if not found.
Definition: World.cpp:98
void SetPedestriansSeed(unsigned int seed)
set the seed to use with random numbers in the pedestrians module
Definition: World.cpp:163
void ApplyFloatColorTextureToObject(const std::string &actor_name, const rpc::MaterialParameter &parameter, const rpc::TextureFloatColor &Texture)
Definition: World.cpp:312
boost::optional< rpc::LabelledPoint > GroundProjection(geom::Location location, float search_distance=10000.0) const
Definition: World.cpp:249
rpc::WeatherParameters GetWeather() const
Retrieve the weather parameters currently active in the world.
Definition: World.cpp:86
void FreezeAllTrafficLights(bool frozen)
Definition: World.cpp:222
detail::EpisodeProxy GetEpisode() const
Definition: World.h:165
void EnableEnvironmentObjects(std::vector< uint64_t > env_objects_ids, bool enable) const
Definition: World.cpp:234
std::vector< SharedPtr< Actor > > GetTrafficLightsFromWaypoint(const Waypoint &waypoint, double distance) const
Definition: World.cpp:260
Represents an actor in the simulation.
Definition: client/Actor.h:18
uint64_t ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout)
Definition: World.cpp:56
std::vector< geom::BoundingBox > GetLevelBBs(uint8_t queried_tag) const
Returns all the BBs of all the elements of the level.
Definition: World.cpp:226
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
Contains all the necessary information for spawning an Actor.
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:126
rpc::EpisodeSettings GetSettings() const
Definition: World.cpp:52
carla::SharedPtr< cc::ActorList > ActorList
Definition: ALSM.h:33
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:118
void ApplyFloatColorTextureToObjects(const std::vector< std::string > &objects_names, const rpc::MaterialParameter &parameter, const rpc::TextureFloatColor &Texture)
Definition: World.cpp:319
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
Definition: World.cpp:94
std::vector< rpc::EnvironmentObject > GetEnvironmentObjects(uint8_t queried_tag) const
Definition: World.cpp:230
boost::optional< geom::Location > GetRandomLocationFromNavigation() const
Get a random location from the pedestrians navigation mesh.
Definition: World.cpp:44
SharedPtr< Actor > GetTrafficLight(const Landmark &landmark) const
Definition: World.cpp:183
DebugHelper MakeDebugHelper() const
Definition: World.h:161
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:330
WorldSnapshot WaitForTick(time_duration timeout) const
Block calling thread until a world tick is received.
Definition: World.cpp:138
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:358