CARLA
 
载入中...
搜索中...
未找到
World.cpp
浏览该文件的文档.
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
8
9#include "carla/Logging.h"
10#include "carla/client/Actor.h"
14#include "carla/StringUtil.h"
16#include "carla/road/Junction.h"
18
19#include <exception>
20
21namespace carla {
22namespace client {
23
25 return _episode.Lock()->GetCurrentMap();
26 }
27
28 void World::LoadLevelLayer(rpc::MapLayer map_layers) const {
29 _episode.Lock()->LoadLevelLayer(map_layers);
30 }
31
32 void World::UnloadLevelLayer(rpc::MapLayer map_layers) const {
33 _episode.Lock()->UnloadLevelLayer(map_layers);
34 }
35
37 return _episode.Lock()->GetBlueprintLibrary();
38 }
39
41 return _episode.Lock()->GetVehiclesLightStates();
42 }
43
44 boost::optional<geom::Location> World::GetRandomLocationFromNavigation() const {
45 return _episode.Lock()->GetRandomLocationFromNavigation();
46 }
47
49 return _episode.Lock()->GetSpectator();
50 }
51
53 return _episode.Lock()->GetEpisodeSettings();
54 }
55
56 uint64_t World::ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout) {
57 rpc::EpisodeSettings new_settings = settings;
58 uint64_t id = _episode.Lock()->SetEpisodeSettings(settings);
59
60 time_duration local_timeout = timeout.milliseconds() == 0 ?
61 _episode.Lock()->GetNetworkingTimeout() : timeout;
62
63 if (settings.fixed_delta_seconds.has_value()) {
64 using namespace std::literals::chrono_literals;
65
66 const auto number_of_attemps = 30u;
67 uint64_t tics_correct = 0;
68 for (auto i = 0u; i < number_of_attemps; i++) {
69 const auto curr_snapshot = GetSnapshot();
70
71 const double error = abs(new_settings.fixed_delta_seconds.get() - curr_snapshot.GetTimestamp().delta_seconds);
72 if (error < std::numeric_limits<float>::epsilon())
73 tics_correct++;
74
75 if (tics_correct >= 2)
76 return id;
77
78 Tick(local_timeout);
79 }
80
81 log_warning("World::ApplySettings: After", number_of_attemps, " attemps, the settings were not correctly set. Please check that everything is consistent.");
82 }
83 return id;
84 }
85
87 return _episode.Lock()->GetWeatherParameters();
88 }
89
91 _episode.Lock()->SetWeatherParameters(weather);
92 }
93
95 return _episode.Lock()->GetIMUISensorGravity();
96 }
97
98 void World::SetIMUISensorGravity(float NewIMUISensorGravity) {
99 _episode.Lock()->SetIMUISensorGravity(NewIMUISensorGravity);
100 }
101
102
104 return _episode.Lock()->GetWorldSnapshot();
105 }
106
108 auto simulator = _episode.Lock();
109 auto description = simulator->GetActorById(id);
110 return description.has_value() ?
111 simulator->MakeActor(std::move(*description)) :
112 nullptr;
113 }
114
117 _episode,
118 _episode.Lock()->GetAllTheActorsInTheEpisode()}};
119 }
120
121 SharedPtr<ActorList> World::GetActors(const std::vector<ActorId> &actor_ids) const {
123 _episode,
124 _episode.Lock()->GetActorsById(actor_ids)}};
125 }
126
128 const ActorBlueprint &blueprint,
129 const geom::Transform &transform,
130 Actor *parent_actor,
131 rpc::AttachmentType attachment_type,
132 const std::string& socket_name) {
133 return _episode.Lock()->SpawnActor(blueprint, transform, parent_actor, attachment_type, GarbageCollectionPolicy::Inherit, socket_name);
134 }
135
137 const ActorBlueprint &blueprint,
138 const geom::Transform &transform,
139 Actor *parent_actor,
140 rpc::AttachmentType attachment_type,
141 const std::string& socket_name) noexcept {
142 try {
143 return SpawnActor(blueprint, transform, parent_actor, attachment_type, socket_name);
144 } catch (const std::exception &) {
145 return nullptr;
146 }
147 }
148
150 time_duration local_timeout = timeout.milliseconds() == 0 ?
151 _episode.Lock()->GetNetworkingTimeout() : timeout;
152
153 return _episode.Lock()->WaitForTick(local_timeout);
154 }
155
156 size_t World::OnTick(std::function<void(WorldSnapshot)> callback) {
157 return _episode.Lock()->RegisterOnTickEvent(std::move(callback));
158 }
159
160 void World::RemoveOnTick(size_t callback_id) {
161 _episode.Lock()->RemoveOnTickEvent(callback_id);
162 }
163
164 uint64_t World::Tick(time_duration timeout) {
165 time_duration local_timeout = timeout.milliseconds() == 0 ?
166 _episode.Lock()->GetNetworkingTimeout() : timeout;
167 return _episode.Lock()->Tick(local_timeout);
168 }
169
170 void World::SetPedestriansCrossFactor(float percentage) {
171 _episode.Lock()->SetPedestriansCrossFactor(percentage);
172 }
173
174 void World::SetPedestriansSeed(unsigned int seed) {
175 _episode.Lock()->SetPedestriansSeed(seed);
176 }
177
181 std::string landmark_id = landmark.GetId();
182 for (size_t i = 0; i < actors->size(); i++) {
183 SharedPtr<Actor> actor = actors->at(i);
184 if (StringUtil::Match(actor->GetTypeId(), "*traffic.*")) {
185 TrafficSign* sign = static_cast<TrafficSign*>(actor.get());
186 if(sign && (sign->GetSignId() == landmark_id)) {
187 return actor;
188 }
189 }
190 }
191 return nullptr;
192 }
193
197 std::string landmark_id = landmark.GetId();
198 for (size_t i = 0; i < actors->size(); i++) {
199 SharedPtr<Actor> actor = actors->at(i);
200 if (StringUtil::Match(actor->GetTypeId(), "*traffic_light*")) {
201 TrafficLight* tl = static_cast<TrafficLight*>(actor.get());
202 if(tl && (tl->GetSignId() == landmark_id)) {
203 return actor;
204 }
205 }
206 }
207 return nullptr;
208 }
209
213 for (size_t i = 0; i < actors->size(); i++) {
214 SharedPtr<Actor> actor = actors->at(i);
215 if (StringUtil::Match(actor->GetTypeId(), "*traffic_light*")) {
216 TrafficLight* tl = static_cast<TrafficLight*>(actor.get());
217 if(tl && (tl->GetSignId() == sign_id)) {
218 return actor;
219 }
220 }
221 }
222 return nullptr;
223 }
224
226 _episode.Lock()->ResetAllTrafficLights();
227 }
228
230 return _episode.Lock()->GetLightManager();
231 }
232
234 _episode.Lock()->FreezeAllTrafficLights(frozen);
235 }
236
237 std::vector<geom::BoundingBox> World::GetLevelBBs(uint8_t queried_tag) const {
238 return _episode.Lock()->GetLevelBBs(queried_tag);
239 }
240
241 std::vector<rpc::EnvironmentObject> World::GetEnvironmentObjects(uint8_t queried_tag) const {
242 return _episode.Lock()->GetEnvironmentObjects(queried_tag);
243 }
244
246 std::vector<uint64_t> env_objects_ids,
247 bool enable) const {
248 _episode.Lock()->EnableEnvironmentObjects(env_objects_ids, enable);
249 }
250
251 boost::optional<rpc::LabelledPoint> World::ProjectPoint(
252 geom::Location location, geom::Vector3D direction, float search_distance) const {
253 auto result = _episode.Lock()->ProjectPoint(location, direction, search_distance);
254 if (result.first) {
255 return result.second;
256 }
257 return {};
258 }
259
260 boost::optional<rpc::LabelledPoint> World::GroundProjection(
261 geom::Location location, float search_distance) const {
262 const geom::Vector3D DownVector(0,0,-1);
263 return ProjectPoint(location, DownVector, search_distance);
264 }
265
266 std::vector<rpc::LabelledPoint> World::CastRay(
267 geom::Location start_location, geom::Location end_location) const {
268 return _episode.Lock()->CastRay(start_location, end_location);
269 }
270
271 std::vector<SharedPtr<Actor>> World::GetTrafficLightsFromWaypoint(
272 const Waypoint& waypoint, double distance) const {
273 std::vector<SharedPtr<Actor>> Result;
274 std::vector<SharedPtr<Landmark>> landmarks =
275 waypoint.GetAllLandmarksInDistance(distance);
276 std::set<std::string> added_signals;
277 for (auto& landmark : landmarks) {
278 if (road::SignalType::IsTrafficLight(landmark->GetType())) {
279 SharedPtr<Actor> traffic_light = GetTrafficLight(*(landmark.get()));
280 if (traffic_light) {
281 if(added_signals.count(landmark->GetId()) == 0) {
282 Result.emplace_back(traffic_light);
283 added_signals.insert(landmark->GetId());
284 }
285 }
286 }
287 }
288 return Result;
289 }
290
291 std::vector<SharedPtr<Actor>> World::GetTrafficLightsInJunction(
292 const road::JuncId junc_id) const {
293 std::vector<SharedPtr<Actor>> Result;
294 SharedPtr<Map> map = GetMap();
295 const road::Junction* junction = map->GetMap().GetJunction(junc_id);
296 for (const road::ContId& cont_id : junction->GetControllers()) {
297 const std::unique_ptr<road::Controller>& controller =
298 map->GetMap().GetControllers().at(cont_id);
299 for (road::SignId sign_id : controller->GetSignals()) {
300 SharedPtr<Actor> traffic_light = GetTrafficLightFromOpenDRIVE(sign_id);
301 if (traffic_light) {
302 Result.emplace_back(traffic_light);
303 }
304 }
305 }
306 return Result;
307 }
308
310 const std::string &object_name,
311 const rpc::MaterialParameter& parameter,
312 const rpc::TextureColor& Texture) {
313 _episode.Lock()->ApplyColorTextureToObjects({object_name}, parameter, Texture);
314 }
315
317 const std::vector<std::string> &objects_name,
318 const rpc::MaterialParameter& parameter,
319 const rpc::TextureColor& Texture) {
320 _episode.Lock()->ApplyColorTextureToObjects(objects_name, parameter, Texture);
321 }
322
324 const std::string &object_name,
325 const rpc::MaterialParameter& parameter,
326 const rpc::TextureFloatColor& Texture) {
327 _episode.Lock()->ApplyColorTextureToObjects({object_name}, parameter, Texture);
328 }
329
331 const std::vector<std::string> &objects_name,
332 const rpc::MaterialParameter& parameter,
333 const rpc::TextureFloatColor& Texture) {
334 _episode.Lock()->ApplyColorTextureToObjects(objects_name, parameter, Texture);
335 }
336
337 std::vector<std::string> World::GetNamesOfAllObjects() const {
338 return _episode.Lock()->GetNamesOfAllObjects();
339 }
340
342 const std::string &object_name,
343 const rpc::TextureColor& diffuse_texture,
344 const rpc::TextureFloatColor& emissive_texture,
345 const rpc::TextureFloatColor& normal_texture,
346 const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture)
347 {
348 if (diffuse_texture.GetWidth() && diffuse_texture.GetHeight()) {
350 object_name, rpc::MaterialParameter::Tex_Diffuse, diffuse_texture);
351 }
352 if (normal_texture.GetWidth() && normal_texture.GetHeight()) {
354 object_name, rpc::MaterialParameter::Tex_Normal, normal_texture);
355 }
356 if (ao_roughness_metallic_emissive_texture.GetWidth() &&
357 ao_roughness_metallic_emissive_texture.GetHeight()) {
359 object_name,
361 ao_roughness_metallic_emissive_texture);
362 }
363 if (emissive_texture.GetWidth() && emissive_texture.GetHeight()) {
365 object_name, rpc::MaterialParameter::Tex_Emissive, emissive_texture);
366 }
367 }
368
370 const std::vector<std::string> &objects_names,
371 const rpc::TextureColor& diffuse_texture,
372 const rpc::TextureFloatColor& emissive_texture,
373 const rpc::TextureFloatColor& normal_texture,
374 const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture)
375 {
376 if (diffuse_texture.GetWidth() && diffuse_texture.GetHeight()) {
378 objects_names, rpc::MaterialParameter::Tex_Diffuse, diffuse_texture);
379 }
380 if (normal_texture.GetWidth() && normal_texture.GetHeight()) {
382 objects_names, rpc::MaterialParameter::Tex_Normal, normal_texture);
383 }
384 if (ao_roughness_metallic_emissive_texture.GetWidth() &&
385 ao_roughness_metallic_emissive_texture.GetHeight()) {
387 objects_names,
389 ao_roughness_metallic_emissive_texture);
390 }
391 if (emissive_texture.GetWidth() && emissive_texture.GetHeight()) {
393 objects_names, rpc::MaterialParameter::Tex_Emissive, emissive_texture);
394 }
395 }
396
397} // namespace client
398} // namespace carla
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
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
std::string GetId() const
Definition Landmark.h:49
carla::road::SignId GetSignId() const
std::vector< SharedPtr< Landmark > > GetAllLandmarksInDistance(double distance, bool stop_at_junction=false) const
Returns a list of landmarks from the current position to a certain distance
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
Definition World.cpp:115
void ApplyColorTextureToObject(const std::string &actor_name, const rpc::MaterialParameter &parameter, const rpc::TextureColor &Texture)
Definition World.cpp:309
std::vector< geom::BoundingBox > GetLevelBBs(uint8_t queried_tag) const
Returns all the BBs of all the elements of the level
Definition World.cpp:237
void LoadLevelLayer(rpc::MapLayer map_layers) const
Definition World.cpp:28
SharedPtr< BlueprintLibrary > GetBlueprintLibrary() const
Return the list of blueprints available in this world.
Definition World.cpp:36
void EnableEnvironmentObjects(std::vector< uint64_t > env_objects_ids, bool enable) const
Definition World.cpp:245
void FreezeAllTrafficLights(bool frozen)
Definition World.cpp:233
boost::optional< rpc::LabelledPoint > GroundProjection(geom::Location location, float search_distance=10000.0) const
Definition World.cpp:260
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
Definition World.cpp:103
void ResetAllTrafficLights()
Definition World.cpp:225
rpc::WeatherParameters GetWeather() const
Retrieve the weather parameters currently active in the world.
Definition World.cpp:86
SharedPtr< Actor > GetSpectator() const
Return the spectator actor.
Definition World.cpp:48
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
SharedPtr< Actor > GetActor(ActorId id) const
Find actor by id, return nullptr if not found.
Definition World.cpp:107
SharedPtr< Actor > GetTrafficLight(const Landmark &landmark) const
Definition World.cpp:194
void SetPedestriansCrossFactor(float percentage)
set the probability that an agent could cross the roads in its path following percentage of 0....
Definition World.cpp:170
uint64_t Tick(time_duration timeout)
Signal the simulator to continue to next tick (only has effect on synchronous mode).
Definition World.cpp:164
detail::EpisodeProxy _episode
Definition World.h:244
SharedPtr< Actor > TrySpawnActor(const ActorBlueprint &blueprint, const geom::Transform &transform, Actor *parent=nullptr, rpc::AttachmentType attachment_type=rpc::AttachmentType::Rigid, const std::string &socket_name="") noexcept
Same as SpawnActor but return nullptr on failure instead of throwing an exception.
Definition World.cpp:136
boost::optional< rpc::LabelledPoint > ProjectPoint(geom::Location location, geom::Vector3D direction, float search_distance=10000.f) const
Definition World.cpp:251
WorldSnapshot WaitForTick(time_duration timeout) const
Block calling thread until a world tick is received.
Definition World.cpp:149
std::vector< SharedPtr< Actor > > GetTrafficLightsInJunction(const road::JuncId junc_id) const
Definition World.cpp:291
void SetIMUISensorGravity(float NewIMUISensorGravity)
Set Gravity value used for IMUI Sensor accelerometer calculation
Definition World.cpp:98
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:341
std::vector< std::string > GetNamesOfAllObjects() const
Definition World.cpp:337
uint64_t ApplySettings(const rpc::EpisodeSettings &settings, time_duration timeout)
Definition World.cpp:56
boost::optional< geom::Location > GetRandomLocationFromNavigation() const
Get a random location from the pedestrians navigation mesh
Definition World.cpp:44
std::vector< rpc::EnvironmentObject > GetEnvironmentObjects(uint8_t queried_tag) const
Definition World.cpp:241
SharedPtr< Map > GetMap() const
Return the map that describes this world.
Definition World.cpp:24
std::vector< rpc::LabelledPoint > CastRay(geom::Location start_location, geom::Location end_location) const
Definition World.cpp:266
std::vector< SharedPtr< Actor > > GetTrafficLightsFromWaypoint(const Waypoint &waypoint, double distance) const
Definition World.cpp:271
void ApplyColorTextureToObjects(const std::vector< std::string > &objects_names, const rpc::MaterialParameter &parameter, const rpc::TextureColor &Texture)
Definition World.cpp:316
void ApplyFloatColorTextureToObjects(const std::vector< std::string > &objects_names, const rpc::MaterialParameter &parameter, const rpc::TextureFloatColor &Texture)
Definition World.cpp:330
void SetPedestriansSeed(unsigned int seed)
set the seed to use with random numbers in the pedestrians module
Definition World.cpp:174
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:369
SharedPtr< LightManager > GetLightManager() const
Definition World.cpp:229
SharedPtr< Actor > SpawnActor(const ActorBlueprint &blueprint, const geom::Transform &transform, Actor *parent=nullptr, rpc::AttachmentType attachment_type=rpc::AttachmentType::Rigid, const std::string &socket_name="")
Spawn an actor into the world based on the blueprint provided at transform.
Definition World.cpp:127
SharedPtr< Actor > GetTrafficSign(const Landmark &landmark) const
Definition World.cpp:178
void ApplyFloatColorTextureToObject(const std::string &actor_name, const rpc::MaterialParameter &parameter, const rpc::TextureFloatColor &Texture)
Definition World.cpp:323
void RemoveOnTick(size_t callback_id)
Remove a callback registered with OnTick.
Definition World.cpp:160
SharedPtr< Actor > GetTrafficLightFromOpenDRIVE(const road::SignId &sign_id) const
Definition World.cpp:210
size_t OnTick(std::function< void(WorldSnapshot)> callback)
Register a callback to be called every time a world tick is received.
Definition World.cpp:156
float GetIMUISensorGravity() const
Get Gravity value used for IMUI Sensor accelerometer calculation
Definition World.cpp:94
void SetWeather(const rpc::WeatherParameters &weather)
Change the weather in the simulation.
Definition World.cpp:90
rpc::EpisodeSettings GetSettings() const
Definition World.cpp:52
void UnloadLevelLayer(rpc::MapLayer map_layers) const
Definition World.cpp:32
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
const std::set< ContId > & GetControllers() const
static bool IsTrafficLight(const std::string &type)
uint32_t GetWidth() const
Definition Texture.h:29
uint32_t GetHeight() const
Definition Texture.h:33
Positive time duration up to milliseconds resolution.
Definition Time.h:19
static time_duration milliseconds(size_t timeout)
Definition Time.h:26
int32_t JuncId
Definition RoadTypes.h:17
std::string ContId
Definition RoadTypes.h:29
std::string SignId
Definition RoadTypes.h:25
std::vector< std::pair< ActorId, VehicleLightState::flag_type > > VehicleLightStateList
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
static void log_warning(Args &&... args)
Definition Logging.h:96
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