CARLA
World.cpp
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 #include "carla/client/World.h"
8 
9 #include "carla/Logging.h"
10 #include "carla/client/Actor.h"
12 #include "carla/client/ActorList.h"
14 #include "carla/StringUtil.h"
15 
16 #include <exception>
17 
18 namespace carla {
19 namespace client {
20 
22  return _episode.Lock()->GetCurrentMap();
23  }
24 
25  void World::LoadLevelLayer(rpc::MapLayer map_layers) const {
26  _episode.Lock()->LoadLevelLayer(map_layers);
27  }
28 
29  void World::UnloadLevelLayer(rpc::MapLayer map_layers) const {
30  _episode.Lock()->UnloadLevelLayer(map_layers);
31  }
32 
34  return _episode.Lock()->GetBlueprintLibrary();
35  }
36 
38  return _episode.Lock()->GetVehiclesLightStates();
39  }
40 
41  boost::optional<geom::Location> World::GetRandomLocationFromNavigation() const {
42  return _episode.Lock()->GetRandomLocationFromNavigation();
43  }
44 
46  return _episode.Lock()->GetSpectator();
47  }
48 
50  return _episode.Lock()->GetEpisodeSettings();
51  }
52 
53  uint64_t World::ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout) {
54  rpc::EpisodeSettings new_settings = settings;
55  uint64_t id = _episode.Lock()->SetEpisodeSettings(settings);
56  if (settings.fixed_delta_seconds.has_value()) {
57  using namespace std::literals::chrono_literals;
58 
59  const auto number_of_attemps = 30u;
60  uint64_t tics_correct = 0;
61  for (auto i = 0u; i < number_of_attemps; i++) {
62  const auto curr_snapshot = GetSnapshot();
63 
64  const double error = abs(new_settings.fixed_delta_seconds.get() - curr_snapshot.GetTimestamp().delta_seconds);
65  if (error < std::numeric_limits<float>::epsilon())
66  tics_correct++;
67 
68  if (tics_correct >= 2)
69  return id;
70 
71  Tick(timeout);
72  }
73 
74  log_warning("World::ApplySettings: After", number_of_attemps, " attemps, the settings were not correctly set. Please check that everything is consistent.");
75  }
76  return id;
77  }
78 
80  return _episode.Lock()->GetWeatherParameters();
81  }
82 
84  _episode.Lock()->SetWeatherParameters(weather);
85  }
86 
88  return _episode.Lock()->GetWorldSnapshot();
89  }
90 
92  auto simulator = _episode.Lock();
93  auto description = simulator->GetActorById(id);
94  return description.has_value() ?
95  simulator->MakeActor(std::move(*description)) :
96  nullptr;
97  }
98 
100  return SharedPtr<ActorList>{new ActorList{
101  _episode,
102  _episode.Lock()->GetAllTheActorsInTheEpisode()}};
103  }
104 
105  SharedPtr<ActorList> World::GetActors(const std::vector<ActorId> &actor_ids) const {
106  return SharedPtr<ActorList>{new ActorList{
107  _episode,
108  _episode.Lock()->GetActorsById(actor_ids)}};
109  }
110 
112  const ActorBlueprint &blueprint,
113  const geom::Transform &transform,
114  Actor *parent_actor,
115  rpc::AttachmentType attachment_type) {
116  return _episode.Lock()->SpawnActor(blueprint, transform, parent_actor, attachment_type);
117  }
118 
120  const ActorBlueprint &blueprint,
121  const geom::Transform &transform,
122  Actor *parent_actor,
123  rpc::AttachmentType attachment_type) noexcept {
124  try {
125  return SpawnActor(blueprint, transform, parent_actor, attachment_type);
126  } catch (const std::exception &) {
127  return nullptr;
128  }
129  }
130 
132  return _episode.Lock()->WaitForTick(timeout);
133  }
134 
135  size_t World::OnTick(std::function<void(WorldSnapshot)> callback) {
136  return _episode.Lock()->RegisterOnTickEvent(std::move(callback));
137  }
138 
139  void World::RemoveOnTick(size_t callback_id) {
140  _episode.Lock()->RemoveOnTickEvent(callback_id);
141  }
142 
143  uint64_t World::Tick(time_duration timeout) {
144  return _episode.Lock()->Tick(timeout);
145  }
146 
147  void World::SetPedestriansCrossFactor(float percentage) {
148  _episode.Lock()->SetPedestriansCrossFactor(percentage);
149  }
150 
152  SharedPtr<ActorList> actors = GetActors();
153  SharedPtr<TrafficSign> result;
154  std::string landmark_id = landmark.GetId();
155  for (size_t i = 0; i < actors->size(); i++) {
156  SharedPtr<Actor> actor = actors->at(i);
157  if (StringUtil::Match(actor->GetTypeId(), "*traffic.*")) {
158  TrafficSign* sign = static_cast<TrafficSign*>(actor.get());
159  if(sign && (sign->GetSignId() == landmark_id)) {
160  return actor;
161  }
162  }
163  }
164  return nullptr;
165  }
166 
168  SharedPtr<ActorList> actors = GetActors();
170  std::string landmark_id = landmark.GetId();
171  for (size_t i = 0; i < actors->size(); i++) {
172  SharedPtr<Actor> actor = actors->at(i);
173  if (StringUtil::Match(actor->GetTypeId(), "*traffic_light*")) {
174  TrafficLight* tl = static_cast<TrafficLight*>(actor.get());
175  if(tl && (tl->GetSignId() == landmark_id)) {
176  return actor;
177  }
178  }
179  }
180  return nullptr;
181  }
182 
184  _episode.Lock()->ResetAllTrafficLights();
185  }
186 
188  return _episode.Lock()->GetLightManager();
189  }
190 
191  void World::FreezeAllTrafficLights(bool frozen) {
192  _episode.Lock()->FreezeAllTrafficLights(frozen);
193  }
194 
195  std::vector<geom::BoundingBox> World::GetLevelBBs(uint8_t queried_tag) const {
196  return _episode.Lock()->GetLevelBBs(queried_tag);
197  }
198 
199  std::vector<rpc::EnvironmentObject> World::GetEnvironmentObjects(uint8_t queried_tag) const {
200  return _episode.Lock()->GetEnvironmentObjects(queried_tag);
201  }
202 
204  std::vector<uint64_t> env_objects_ids,
205  bool enable) const {
206  _episode.Lock()->EnableEnvironmentObjects(env_objects_ids, enable);
207  }
208 
209  boost::optional<rpc::LabelledPoint> World::ProjectPoint(
210  geom::Location location, geom::Vector3D direction, float search_distance) const {
211  auto result = _episode.Lock()->ProjectPoint(location, direction, search_distance);
212  if (result.first) {
213  return result.second;
214  }
215  return {};
216  }
217 
218  boost::optional<rpc::LabelledPoint> World::GroundProjection(
219  geom::Location location, float search_distance) const {
220  const geom::Vector3D DownVector(0,0,-1);
221  return ProjectPoint(location, DownVector, search_distance);
222  }
223 
224  std::vector<rpc::LabelledPoint> World::CastRay(
225  geom::Location start_location, geom::Location end_location) const {
226  return _episode.Lock()->CastRay(start_location, end_location);
227  }
228 
229 } // namespace client
230 } // 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
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
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
std::string GetId() const
Definition: Landmark.h:49
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
Definition: StringUtil.cpp:17
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
carla::road::SignId GetSignId() const
Definition: TrafficSign.cpp:14
static void log_warning(Args &&... args)
Definition: Logging.h:96
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
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
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
rpc::EpisodeSettings GetSettings() const
Definition: World.cpp:49
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
WorldSnapshot WaitForTick(time_duration timeout) const
Block calling thread until a world tick is received.
Definition: World.cpp:131