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