From 2fbfda2f242144c2a185e4f82d2cd4bc87942b66 Mon Sep 17 00:00:00 2001 From: doterop Date: Wed, 14 Oct 2020 15:16:53 +0200 Subject: [PATCH] Renamed and added enable/disable call --- LibCarla/source/carla/client/World.cpp | 10 ++++- LibCarla/source/carla/client/World.h | 8 +++- .../source/carla/client/detail/Client.cpp | 12 ++++-- LibCarla/source/carla/client/detail/Client.h | 8 +++- .../source/carla/client/detail/Simulator.h | 10 ++++- .../carla/rpc/{Mesh.h => EnvironmentObject.h} | 12 +++--- PythonAPI/carla/source/libcarla/World.cpp | 42 ++++++++++++------- .../Source/Carla/Game/CarlaGameModeBase.cpp | 42 ++++++++++++++----- .../Source/Carla/Game/CarlaGameModeBase.h | 10 +++-- .../Carla/Source/Carla/Server/CarlaServer.cpp | 27 ++++++++++-- 10 files changed, 131 insertions(+), 50 deletions(-) rename LibCarla/source/carla/rpc/{Mesh.h => EnvironmentObject.h} (73%) diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index f52a050f0..cb27e99ac 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -166,8 +166,14 @@ namespace client { return _episode.Lock()->GetLevelBBs(queried_tag); } - std::vector World::GetMeshes() const { - return _episode.Lock()->GetMeshes(); + std::vector World::GetEnvironmentObjects() const { + return _episode.Lock()->GetEnvironmentObjects(); + } + + void World::EnableEnvironmentObjects( + std::vector env_objects_ids, + bool enable) const { + _episode.Lock()->EnableEnvironmentObjects(env_objects_ids, enable); } } // namespace client diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index 50c19c35a..473b50ca6 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -18,7 +18,7 @@ #include "carla/rpc/Actor.h" #include "carla/rpc/AttachmentType.h" #include "carla/rpc/EpisodeSettings.h" -#include "carla/rpc/Mesh.h" +#include "carla/rpc/EnvironmentObject.h" #include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/WeatherParameters.h" #include "carla/rpc/VehicleLightStateList.h" @@ -156,7 +156,11 @@ namespace client { /// Returns all the BBs of all the elements of the level std::vector GetLevelBBs(uint8_t queried_tag) const; - std::vector GetMeshes() const; + std::vector GetEnvironmentObjects() const; + + void EnableEnvironmentObjects( + std::vector env_objects_ids, + bool enable) const; private: diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index becb28031..52d0e4bd4 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -450,9 +450,15 @@ namespace detail { return _pimpl->CallAndWait("get_all_level_BBs", queried_tag); } - std::vector Client::GetMeshes() const { - using return_t = std::vector; - return _pimpl->CallAndWait("get_meshes"); + std::vector Client::GetEnvironmentObjects() const { + using return_t = std::vector; + return _pimpl->CallAndWait("get_environment_objects"); + } + + void Client::EnableEnvironmentObjects( + std::vector env_objects_ids, + bool enable) const { + _pimpl->AsyncCall("enable_environment_objects", std::move(env_objects_ids), enable); } } // namespace detail diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index 713ca1b2c..830c6eec1 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -19,7 +19,7 @@ #include "carla/rpc/EpisodeSettings.h" #include "carla/rpc/LightState.h" #include "carla/rpc/MapInfo.h" -#include "carla/rpc/Mesh.h" +#include "carla/rpc/EnvironmentObject.h" #include "carla/rpc/TrafficLightState.h" #include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/VehicleLightState.h" @@ -291,7 +291,11 @@ namespace detail { /// Returns all the BBs of all the elements of the level std::vector GetLevelBBs(uint8_t queried_tag) const; - std::vector GetMeshes() const; + std::vector GetEnvironmentObjects() const; + + void EnableEnvironmentObjects( + std::vector env_objects_ids, + bool enable) const; private: diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index 77d4587c4..56f7c67dc 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -231,8 +231,14 @@ namespace detail { return _client.GetLevelBBs(queried_tag); } - std::vector GetMeshes() const { - return _client.GetMeshes(); + std::vector GetEnvironmentObjects() const { + return _client.GetEnvironmentObjects(); + } + + void EnableEnvironmentObjects( + std::vector env_objects_ids, + bool enable) const { + _client.EnableEnvironmentObjects(env_objects_ids, enable); } /// @} diff --git a/LibCarla/source/carla/rpc/Mesh.h b/LibCarla/source/carla/rpc/EnvironmentObject.h similarity index 73% rename from LibCarla/source/carla/rpc/Mesh.h rename to LibCarla/source/carla/rpc/EnvironmentObject.h index 71a29bad5..2944d5108 100644 --- a/LibCarla/source/carla/rpc/Mesh.h +++ b/LibCarla/source/carla/rpc/EnvironmentObject.h @@ -18,7 +18,7 @@ namespace carla { namespace rpc { // Name is under discussion - struct Mesh { + struct EnvironmentObject { Transform transform; geom::BoundingBox bounding_box; uint64_t id = 0; @@ -27,11 +27,11 @@ namespace rpc { #ifdef LIBCARLA_INCLUDED_FROM_UE4 - Mesh(const FCarlaMesh &CarlaMesh) - : transform(CarlaMesh.Transform), - bounding_box(CarlaMesh.BoundingBox), - id(CarlaMesh.Id), - name(TCHAR_TO_UTF8(*CarlaMesh.Name)) {} + EnvironmentObject(const FEnvironmentObject &EnvironmentObject) + : transform(EnvironmentObject.Transform), + bounding_box(EnvironmentObject.BoundingBox), + id(EnvironmentObject.Id), + name(TCHAR_TO_UTF8(*EnvironmentObject.Name)) {} #endif // LIBCARLA_INCLUDED_FROM_UE4 diff --git a/PythonAPI/carla/source/libcarla/World.cpp b/PythonAPI/carla/source/libcarla/World.cpp index 99c1b38cb..6af54fcb9 100644 --- a/PythonAPI/carla/source/libcarla/World.cpp +++ b/PythonAPI/carla/source/libcarla/World.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include @@ -38,11 +38,11 @@ namespace rpc { return out; } - std::ostream &operator<<(std::ostream &out, const Mesh &mesh) { - out << "Mesh(id=" << mesh.id << ", "; - out << "name=" << mesh.name << ", "; - out << "transform=" << mesh.transform << ", "; - out << "bounding_box=" << mesh.bounding_box << ")"; + std::ostream &operator<<(std::ostream &out, const EnvironmentObject &environment_object) { + out << "Mesh(id=" << environment_object.id << ", "; + out << "name=" << environment_object.name << ", "; + out << "transform=" << environment_object.transform << ", "; + out << "bounding_box=" << environment_object.bounding_box << ")"; return out; } @@ -89,15 +89,28 @@ static auto GetLevelBBs(const carla::client::World &self, uint8_t queried_tag) { return result; } -static auto GetMeshes(const carla::client::World &self) { +static auto GetEnvironmentObjects(const carla::client::World &self) { carla::PythonUtil::ReleaseGIL unlock; boost::python::list result; - for (const auto &geometry : self.GetMeshes()) { + for (const auto &geometry : self.GetEnvironmentObjects()) { result.append(geometry); } return result; } +static void EnableEnvironmentObjects( + carla::client::World &self, + const boost::python::object& py_env_objects_ids, + const bool enable ) { + + std::vector env_objects_ids { + boost::python::stl_input_iterator(py_env_objects_ids), + boost::python::stl_input_iterator() + }; + + self.EnableEnvironmentObjects(env_objects_ids, enable); +} + void export_world() { using namespace boost::python; namespace cc = carla::client; @@ -149,11 +162,11 @@ void export_world() { .def(self_ns::str(self_ns::self)) ; - class_("Mesh", no_init) - .def_readwrite("transform", &cr::Mesh::transform) - .def_readwrite("bounding_box", &cr::Mesh::bounding_box) - .def_readwrite("id", &cr::Mesh::id) - .def_readwrite("name", &cr::Mesh::name) + class_("EnvironmentObject", no_init) + .def_readwrite("transform", &cr::EnvironmentObject::transform) + .def_readwrite("bounding_box", &cr::EnvironmentObject::bounding_box) + .def_readwrite("id", &cr::EnvironmentObject::id) + .def_readwrite("name", &cr::EnvironmentObject::name) .def(self_ns::str(self_ns::self)) ; @@ -232,7 +245,8 @@ void export_world() { .def("get_lightmanager", CONST_CALL_WITHOUT_GIL(cc::World, GetLightManager)) .def("freeze_all_traffic_lights", &cc::World::FreezeAllTrafficLights, (arg("frozen"))) .def("get_level_bbs", &GetLevelBBs, (arg("actor_type")=cr::CityObjectLabel::None)) - .def("get_meshes", &GetMeshes) + .def("get_environment_objects", &GetEnvironmentObjects) + .def("enable_environment_objects", &EnableEnvironmentObjects, (arg("env_objects_ids"), arg("enable"))) .def(self_ns::str(self_ns::self)) ; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp index 8f0114db7..713d9c89e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp @@ -12,7 +12,7 @@ #include #include "carla/opendrive/OpenDriveParser.h" #include "carla/road/element/RoadInfoSignal.h" -#include +#include #include #include @@ -155,7 +155,7 @@ void ACarlaGameModeBase::BeginPlay() Recorder->GetReplayer()->CheckPlayAfterMapLoaded(); } - RegisterMeshes(); + RegisterEnvironmentObject(); } @@ -350,12 +350,12 @@ TArray ACarlaGameModeBase::GetAllBBsOfLevel(uint8 TagQueried) return BoundingBoxes; } -TArray ACarlaGameModeBase::GetMeshes() const +TArray ACarlaGameModeBase::GetEnvironmentObjects() const { - return Meshes; + return EnvironmentObjects; } -void ACarlaGameModeBase::RegisterMeshes() +void ACarlaGameModeBase::RegisterEnvironmentObject() { UWorld* World = GetWorld(); @@ -364,18 +364,38 @@ void ACarlaGameModeBase::RegisterMeshes() UGameplayStatics::GetAllActorsOfClass(World, AActor::StaticClass(), FoundActors); // Empties the array but doesn't change memory allocations - Meshes.Reset(); + EnvironmentObjects.Reset(); for(AActor* Actor : FoundActors) { FString ActorName = Actor->GetName(); const char* ActorNameChar = TCHAR_TO_ANSI(*ActorName); - FCarlaMesh CarlaMesh; - CarlaMesh.Transform = Actor->GetActorTransform(); - CarlaMesh.Id = CityHash64(ActorNameChar, ActorName.Len()); - CarlaMesh.Name = ActorName; + FEnvironmentObject EnvironmentObject; + EnvironmentObject.Transform = Actor->GetActorTransform(); + EnvironmentObject.Id = CityHash64(ActorNameChar, ActorName.Len()); + EnvironmentObject.Name = ActorName; + EnvironmentObject.Actor = Actor; - Meshes.Add(CarlaMesh); + EnvironmentObjects.Emplace(EnvironmentObject); } +} + +void ACarlaGameModeBase::EnableEnvironmentObjects( + const TSet& EnvObjectIds, + bool Enable) +{ + + for(FEnvironmentObject& EnvironmentObject : EnvironmentObjects) + { + if(EnvObjectIds.Contains(EnvironmentObject.Id)) + { + AActor* Actor = EnvironmentObject.Actor; + + Actor->SetActorHiddenInGame(!Enable); + Actor->SetActorEnableCollision(Enable); + Actor->SetActorTickEnabled(Enable); + } + } + } \ No newline at end of file diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h index 9cdb453d9..4406caeed 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h @@ -16,7 +16,7 @@ #include "Carla/Settings/CarlaSettingsDelegate.h" #include "Carla/Weather/Weather.h" #include "Carla/Traffic/TrafficLightManager.h" -#include "Carla/Util/CarlaMesh.h" +#include "Carla/Util/EnvironmentObject.h" #include "CoreMinimal.h" @@ -58,7 +58,9 @@ public: TArray GetAllBBsOfLevel(uint8 TagQueried = 0); UFUNCTION(Category = "Carla Game Mode", BlueprintCallable, CallInEditor, Exec) - TArray GetMeshes() const; + TArray GetEnvironmentObjects() const; + + void EnableEnvironmentObjects(const TSet& EnvObjectIds, bool Enable); protected: @@ -78,7 +80,7 @@ private: void ParseOpenDrive(const FString &MapName); - void RegisterMeshes(); + void RegisterEnvironmentObject(); UPROPERTY() UCarlaGameInstance *GameInstance = nullptr; @@ -95,7 +97,7 @@ private: UPROPERTY() ACarlaRecorder *Recorder = nullptr; - TArray Meshes; + TArray EnvironmentObjects; /// The class of Weather to spawn. UPROPERTY(Category = "CARLA Game Mode", EditAnywhere) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index d5c7d034f..a76fc27db 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include @@ -322,7 +322,7 @@ void FCarlaServer::FPimpl::BindActions() return MakeVectorFromTArray(Result); }; - BIND_SYNC(get_meshes) << [this]() -> R> + BIND_SYNC(get_environment_objects) << [this]() -> R> { REQUIRE_CARLA_EPISODE(); ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); @@ -330,8 +330,27 @@ void FCarlaServer::FPimpl::BindActions() { RESPOND_ERROR("unable to find CARLA game mode"); } - TArray Result = GameMode->GetMeshes(); - return MakeVectorFromTArray(Result); + TArray Result = GameMode->GetEnvironmentObjects(); + return MakeVectorFromTArray(Result); + }; + + BIND_SYNC(enable_environment_objects) << [this](std::vector EnvObjectIds, bool Enable) -> R + { + REQUIRE_CARLA_EPISODE(); + ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); + if (!GameMode) + { + RESPOND_ERROR("unable to find CARLA game mode"); + } + + TSet EnvObjectIdsSet; + for(uint64 Id : EnvObjectIds) + { + EnvObjectIdsSet.Emplace(Id); + } + + GameMode->EnableEnvironmentObjects(EnvObjectIdsSet, Enable); + return R::Success(); }; // ~~ Weather ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~