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"
14 #include "carla/client/Timestamp.h"
17 #include "carla/geom/Transform.h"
18 #include "carla/rpc/Actor.h"
23 #include "carla/rpc/MapLayer.h"
27 
28 #include <boost/optional.hpp>
29 
30 namespace carla {
31 namespace client {
32 
33  class Actor;
34  class ActorBlueprint;
35  class ActorList;
36  class BlueprintLibrary;
37  class Map;
38  class TrafficLight;
39  class TrafficSign;
40 
41  class World {
42  public:
43 
44  explicit World(detail::EpisodeProxy episode) : _episode(std::move(episode)) {}
45 
46  ~World(){}
47 
48  World(const World &) = default;
49  World(World &&) = default;
50 
51  World &operator=(const World &) = default;
52  World &operator=(World &&) = default;
53 
54  /// Get the id of the episode associated with this world.
55  uint64_t GetId() const {
56  return _episode.GetId();
57  }
58 
59  /// Return the map that describes this world.
60  SharedPtr<Map> GetMap() const;
61 
62  void LoadLevelLayer(rpc::MapLayer map_layers) const;
63 
64  void UnloadLevelLayer(rpc::MapLayer map_layers) const;
65 
66  /// Return the list of blueprints available in this world. This blueprints
67  /// can be used to spawning actor into the world.
69 
70  /// Returns a list of pairs where the firts element is the vehicle ID
71  /// and the second one is the light state
73 
74  /// Get a random location from the pedestrians navigation mesh
75  boost::optional<geom::Location> GetRandomLocationFromNavigation() const;
76 
77  /// Return the spectator actor. The spectator controls the view in the
78  /// simulator window.
80 
82 
83  /// @return The id of the frame when the settings were applied.
84  uint64_t ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout);
85 
86  /// Retrieve the weather parameters currently active in the world.
88 
89  /// Change the weather in the simulation.
90  void SetWeather(const rpc::WeatherParameters &weather);
91 
92  /// Return a snapshot of the world at this moment.
93  WorldSnapshot GetSnapshot() const;
94 
95  /// Find actor by id, return nullptr if not found.
97 
98  /// Return a list with all the actors currently present in the world.
100 
101  /// Return a list with the actors requested by ActorId.
102  SharedPtr<ActorList> GetActors(const std::vector<ActorId> &actor_ids) const;
103 
104  /// Spawn an actor into the world based on the @a blueprint provided at @a
105  /// transform. If a @a parent is provided, the actor is attached to
106  /// @a parent.
108  const ActorBlueprint &blueprint,
109  const geom::Transform &transform,
110  Actor *parent = nullptr,
112 
113  /// Same as SpawnActor but return nullptr on failure instead of throwing an
114  /// exception.
116  const ActorBlueprint &blueprint,
117  const geom::Transform &transform,
118  Actor *parent = nullptr,
119  rpc::AttachmentType attachment_type = rpc::AttachmentType::Rigid) noexcept;
120 
121  /// Block calling thread until a world tick is received.
122  WorldSnapshot WaitForTick(time_duration timeout) const;
123 
124  /// Register a @a callback to be called every time a world tick is received.
125  ///
126  /// @return ID of the callback, use it to remove the callback.
127  size_t OnTick(std::function<void(WorldSnapshot)> callback);
128 
129  /// Remove a callback registered with OnTick.
130  void RemoveOnTick(size_t callback_id);
131 
132  /// Signal the simulator to continue to next tick (only has effect on
133  /// synchronous mode).
134  ///
135  /// @return The id of the frame that this call started.
136  uint64_t Tick(time_duration timeout);
137 
138  /// set the probability that an agent could cross the roads in its path following
139  /// percentage of 0.0f means no pedestrian can cross roads
140  /// percentage of 0.5f means 50% of all pedestrians can cross roads
141  /// percentage of 1.0f means all pedestrians can cross roads if needed
142  void SetPedestriansCrossFactor(float percentage);
143 
144  SharedPtr<Actor> GetTrafficSign(const Landmark& landmark) const;
145 
146  SharedPtr<Actor> GetTrafficLight(const Landmark& landmark) const;
147 
148  void ResetAllTrafficLights();
149 
151 
153  return DebugHelper{_episode};
154  }
155 
157  return _episode;
158  };
159 
160  void FreezeAllTrafficLights(bool frozen);
161 
162  /// Returns all the BBs of all the elements of the level
163  std::vector<geom::BoundingBox> GetLevelBBs(uint8_t queried_tag) const;
164 
165  std::vector<rpc::EnvironmentObject> GetEnvironmentObjects(uint8_t queried_tag) const;
166 
168  std::vector<uint64_t> env_objects_ids,
169  bool enable) const;
170 
171  boost::optional<rpc::LabelledPoint> ProjectPoint(
172  geom::Location location, geom::Vector3D direction, float search_distance = 10000.f) const;
173 
174  boost::optional<rpc::LabelledPoint> GroundProjection(
175  geom::Location location, float search_distance = 10000.0) const;
176 
177  std::vector<rpc::LabelledPoint> CastRay(
178  geom::Location start_location, geom::Location end_location) const;
179 
180  private:
181 
183  };
184 
185 } // namespace client
186 } // namespace carla
SharedPtr< BlueprintLibrary > GetBlueprintLibrary() const
Return the list of blueprints available in this world.
Definition: World.cpp:33
void RemoveOnTick(size_t callback_id)
Remove a callback registered with OnTick.
Definition: World.cpp:139
World & operator=(const World &)=default
void SetWeather(const rpc::WeatherParameters &weather)
Change the weather in the simulation.
Definition: World.cpp:83
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
Definition: World.cpp:99
void SetPedestriansCrossFactor(float percentage)
set the probability that an agent could cross the roads in its path following percentage of 0...
Definition: World.cpp:147
World(detail::EpisodeProxy episode)
Definition: World.h:44
size_t OnTick(std::function< void(WorldSnapshot)> callback)
Register a callback to be called every time a world tick is received.
Definition: World.cpp:135
boost::optional< rpc::LabelledPoint > ProjectPoint(geom::Location location, geom::Vector3D direction, float search_distance=10000.f) const
Definition: World.cpp:209
void UnloadLevelLayer(rpc::MapLayer map_layers) const
Definition: World.cpp:29
SharedPtr< Actor > GetSpectator() const
Return the spectator actor.
Definition: World.cpp:45
SharedPtr< LightManager > GetLightManager() const
Definition: World.cpp:187
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:224
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
SharedPtr< Map > GetMap() const
Return the map that describes this world.
Definition: World.cpp:21
Class containing a reference to RoadInfoSignal.
Definition: Landmark.h:22
uint64_t Tick(time_duration timeout)
Signal the simulator to continue to next tick (only has effect on synchronous mode).
Definition: World.cpp:143
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:37
detail::EpisodeProxy _episode
Definition: World.h:182
uint64_t GetId() const
Get the id of the episode associated with this world.
Definition: World.h:55
carla::SharedPtr< cc::Actor > Actor
carla::ActorId ActorId
SharedPtr< Actor > GetTrafficSign(const Landmark &landmark) const
Definition: World.cpp:151
void ResetAllTrafficLights()
Definition: World.cpp:183
void LoadLevelLayer(rpc::MapLayer map_layers) const
Definition: World.cpp:25
SharedPtr< Actor > GetActor(ActorId id) const
Find actor by id, return nullptr if not found.
Definition: World.cpp:91
boost::optional< rpc::LabelledPoint > GroundProjection(geom::Location location, float search_distance=10000.0) const
Definition: World.cpp:218
rpc::WeatherParameters GetWeather() const
Retrieve the weather parameters currently active in the world.
Definition: World.cpp:79
void FreezeAllTrafficLights(bool frozen)
Definition: World.cpp:191
detail::EpisodeProxy GetEpisode() const
Definition: World.h:156
void EnableEnvironmentObjects(std::vector< uint64_t > env_objects_ids, bool enable) const
Definition: World.cpp:203
Represents an actor in the simulation.
Definition: client/Actor.h:18
uint64_t ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout)
Definition: World.cpp:53
std::vector< geom::BoundingBox > GetLevelBBs(uint8_t queried_tag) const
Returns all the BBs of all the elements of the level.
Definition: World.cpp:195
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:119
rpc::EpisodeSettings GetSettings() const
Definition: World.cpp:49
carla::SharedPtr< cc::ActorList > ActorList
Definition: ALSM.h:32
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:111
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
Definition: World.cpp:87
std::vector< rpc::EnvironmentObject > GetEnvironmentObjects(uint8_t queried_tag) const
Definition: World.cpp:199
boost::optional< geom::Location > GetRandomLocationFromNavigation() const
Get a random location from the pedestrians navigation mesh.
Definition: World.cpp:41
SharedPtr< Actor > GetTrafficLight(const Landmark &landmark) const
Definition: World.cpp:167
DebugHelper MakeDebugHelper() const
Definition: World.h:152
WorldSnapshot WaitForTick(time_duration timeout) const
Block calling thread until a world tick is received.
Definition: World.cpp:131