From 14447def90ead33673b090bc1f61b956353e730c Mon Sep 17 00:00:00 2001 From: javiergrCS <139075626+javiergrCS@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:34:59 +0200 Subject: [PATCH] Adding a new function that loads a map only if it is different (#6651) * Adding a new function to load a map only if it is different * Removing commented code * Updated AddVehicleWheelsAnimation function * Revert "Updated AddVehicleWheelsAnimation function" This reverts commit 55405a3de7d459b6968fa2a2722565deef34dec2. * Small correction: removing blank space * Changelog.md update --------- Co-authored-by: Adri Ollero Co-authored-by: Axel1092 <35765780+Axel1092@users.noreply.github.com> --- CHANGELOG.md | 1 + LibCarla/source/carla/client/Client.h | 18 ++++++++++++++++++ PythonAPI/carla/source/libcarla/Client.cpp | 1 + 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c4f0ad33..ca95c8134 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * Add keyword arguments for `carla.TrafficManager` Python API functions * Fixed bug causing the `FPixelReader::SavePixelsToDisk(PixelData, FilePath)` function to crash due to pixel array not set correctly. * Collisions detected by the CollisionSensor no longer generate more than one event per frame. + * Added API function to load a map only if it is different from the current one. ## CARLA 0.9.14 diff --git a/LibCarla/source/carla/client/Client.h b/LibCarla/source/carla/client/Client.h index a78bea0c2..34bea6148 100644 --- a/LibCarla/source/carla/client/Client.h +++ b/LibCarla/source/carla/client/Client.h @@ -8,6 +8,7 @@ #include "carla/client/detail/Simulator.h" #include "carla/client/World.h" +#include "carla/client/Map.h" #include "carla/PythonUtil.h" #include "carla/trafficmanager/TrafficManager.h" @@ -77,6 +78,23 @@ namespace client { return World{_simulator->LoadEpisode(std::move(map_name), reset_settings, map_layers)}; } + /// Return (and load) a new world (map) only when the requested map is different from the current one + + void LoadWorldIfDifferent( + std::string map_name, + bool reset_settings = true, + rpc::MapLayer map_layers = rpc::MapLayer::All) const { + carla::client::World world = GetWorld(); + carla::SharedPtr current_map = world.GetMap(); + std::string current_map_name = current_map->GetName(); + std::string map_name_prefix = "Carla/Maps/"; + std::string map_name_without_prefix = map_name; + std::string map_name_with_prefix = map_name_prefix + map_name; + if(!(map_name_without_prefix == current_map_name) && !(map_name_with_prefix == current_map_name)){ + World World{_simulator->LoadEpisode(std::move(map_name), reset_settings, map_layers)}; + }else{} + } + World GenerateOpenDriveWorld( std::string opendrive, const rpc::OpendriveGenerationParameters & params, diff --git a/PythonAPI/carla/source/libcarla/Client.cpp b/PythonAPI/carla/source/libcarla/Client.cpp index 91760d646..f773b8a5f 100644 --- a/PythonAPI/carla/source/libcarla/Client.cpp +++ b/PythonAPI/carla/source/libcarla/Client.cpp @@ -198,6 +198,7 @@ void export_client() { .def("request_file", &cc::Client::RequestFile, (arg("name"))) .def("reload_world", CONST_CALL_WITHOUT_GIL_1(cc::Client, ReloadWorld, bool), (arg("reset_settings")=true)) .def("load_world", CONST_CALL_WITHOUT_GIL_3(cc::Client, LoadWorld, std::string, bool, rpc::MapLayer), (arg("map_name"), arg("reset_settings")=true, arg("map_layers")=rpc::MapLayer::All)) + .def("load_world_if_different", &cc::Client::LoadWorldIfDifferent, (arg("map_name"), arg("reset_settings")=true, arg("map_layers")=rpc::MapLayer::All)) .def("generate_opendrive_world", CONST_CALL_WITHOUT_GIL_3(cc::Client, GenerateOpenDriveWorld, std::string, rpc::OpendriveGenerationParameters, bool), (arg("opendrive"), arg("parameters")=rpc::OpendriveGenerationParameters(), arg("reset_settings")=true))