From 73a716e24d7c750b6584899c0a0f75f5078f7de7 Mon Sep 17 00:00:00 2001 From: MarcelPiNacy-CVC <169088301+MarcelPiNacy-CVC@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:07:46 +0200 Subject: [PATCH] Add get_actor_name & get_actor_class_name. (#8165) * Add initial GetUnrealName & GetUnrealClassName code. * Remove snapshot-based code. * Code cleanup. --- LibCarla/source/carla/client/Actor.cpp | 10 +++++++ LibCarla/source/carla/client/Actor.h | 12 ++++++++ .../source/carla/client/detail/Client.cpp | 10 +++++++ LibCarla/source/carla/client/detail/Client.h | 4 +++ .../source/carla/client/detail/Simulator.h | 10 +++++++ .../carla/sensor/data/ActorDynamicState.h | 1 - PythonAPI/carla/src/Actor.cpp | 2 ++ .../Source/Carla/Sensor/WorldObserver.cpp | 5 +++- .../Carla/Source/Carla/Server/CarlaServer.cpp | 28 +++++++++++++++++++ 9 files changed, 80 insertions(+), 2 deletions(-) diff --git a/LibCarla/source/carla/client/Actor.cpp b/LibCarla/source/carla/client/Actor.cpp index 7c6be65a8..5d3e9451b 100644 --- a/LibCarla/source/carla/client/Actor.cpp +++ b/LibCarla/source/carla/client/Actor.cpp @@ -32,6 +32,16 @@ namespace client { return GetEpisode().Lock()->GetActorAcceleration(*this); } + std::string Actor::GetActorName() const + { + return GetEpisode().Lock()->GetActorName(*this); + } + + std::string Actor::GetActorClassName() const + { + return GetEpisode().Lock()->GetActorClassName(*this); + } + void Actor::SetLocation(const geom::Location &location) { GetEpisode().Lock()->SetActorLocation(*this, location); } diff --git a/LibCarla/source/carla/client/Actor.h b/LibCarla/source/carla/client/Actor.h index f424b5149..286b04483 100644 --- a/LibCarla/source/carla/client/Actor.h +++ b/LibCarla/source/carla/client/Actor.h @@ -60,6 +60,18 @@ namespace client { /// acceleration calculated after the actor's velocity. geom::Vector3D GetAcceleration() const; + /// Return the name of the underlying Unreal actor. + /// + /// @note This function does not call the simulator, it returns the + /// acceleration calculated after the actor's velocity. + std::string GetActorName() const; + + /// Return the name of the underlying actor's Unreal class. + /// + /// @note This function does not call the simulator, it returns the + /// acceleration calculated after the actor's velocity. + std::string GetActorClassName() const; + /// Teleport the actor to @a location. void SetLocation(const geom::Location &location); diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index d9dc67a17..c94b1338e 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -710,6 +710,16 @@ namespace detail { return _pimpl->CallAndWait("cast_ray", start_location, end_location); } + std::string Client::GetActorName(rpc::ActorId actor) const + { + return _pimpl->CallAndWait("get_actor_name", actor); + } + + std::string Client::GetActorClassName(rpc::ActorId actor) const + { + return _pimpl->CallAndWait("get_actor_class_name", actor); + } + } // namespace detail } // namespace client } // namespace carla diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index d31ff9cef..533a52142 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -439,6 +439,10 @@ namespace detail { std::vector CastRay( geom::Location start_location, geom::Location end_location) const; + std::string GetActorName(rpc::ActorId actor) const; + + std::string GetActorClassName(rpc::ActorId actor) const; + private: class Pimpl; diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index adec96675..4b2579307 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -438,6 +438,16 @@ namespace detail { return GetActorSnapshot(actor).acceleration; } + std::string GetActorName(const Actor& actor) const + { + return _client.GetActorName(actor.GetId()); + } + + std::string GetActorClassName(const Actor& actor) const + { + return _client.GetActorClassName(actor.GetId()); + } + void SetActorLocation(Actor &actor, const geom::Location &location) { _client.SetActorLocation(actor.GetId(), location); } diff --git a/LibCarla/source/carla/sensor/data/ActorDynamicState.h b/LibCarla/source/carla/sensor/data/ActorDynamicState.h index 9d290987e..eefac0c01 100644 --- a/LibCarla/source/carla/sensor/data/ActorDynamicState.h +++ b/LibCarla/source/carla/sensor/data/ActorDynamicState.h @@ -134,7 +134,6 @@ namespace detail { geom::Vector3D angular_velocity; geom::Vector3D acceleration; - union TypeDependentState { detail::TrafficLightData traffic_light_data; detail::TrafficSignData traffic_sign_data; diff --git a/PythonAPI/carla/src/Actor.cpp b/PythonAPI/carla/src/Actor.cpp index 456d1dde6..da8f24ce1 100644 --- a/PythonAPI/carla/src/Actor.cpp +++ b/PythonAPI/carla/src/Actor.cpp @@ -111,6 +111,8 @@ void export_actor() { .def("get_velocity", &cc::Actor::GetVelocity) .def("get_angular_velocity", &cc::Actor::GetAngularVelocity) .def("get_acceleration", &cc::Actor::GetAcceleration) + .def("get_actor_name", &cc::Actor::GetActorName) + .def("get_actor_class_name", &cc::Actor::GetActorClassName) .def("set_location", &cc::Actor::SetLocation, (arg("location"))) .def("set_transform", &cc::Actor::SetTransform, (arg("transform"))) .def("set_target_velocity", &cc::Actor::SetTargetVelocity, (arg("velocity"))) diff --git a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp index 843dc01b1..6e11e0102 100644 --- a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp +++ b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp @@ -353,6 +353,9 @@ static carla::Buffer FWorldObserver_Serialize( } ActorTransform = View->GetActorGlobalTransform(); + auto ActorPtr = View->GetActor(); + check(ActorPtr != nullptr); + ActorDynamicState info = { View->GetActorId(), View->GetActorState(), @@ -360,7 +363,7 @@ static carla::Buffer FWorldObserver_Serialize( carla::geom::Vector3D(Velocity.X, Velocity.Y, Velocity.Z), AngularVelocity, Acceleration, - State, + State }; write_data(info); } diff --git a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index 40c3e6f0c..7e9b9d1c5 100644 --- a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -2650,6 +2650,34 @@ BIND_SYNC(is_sensor_enabled_for_ros) << [this](carla::streaming::detail::stream_ return URayTracer::CastRay(StartLocation, EndLocation, World); }; + BIND_SYNC(get_actor_name) << [this]( + cr::ActorId ActorID) -> R + { + REQUIRE_CARLA_EPISODE(); + auto CarlaActor = Episode->FindCarlaActor(ActorID); + check(CarlaActor != nullptr); + auto Actor = CarlaActor->GetActor(); + check(Actor != nullptr); + auto Name = Actor->GetName(); + auto NameStr = StringCast(*Name, Name.Len()); + return std::string((const char*)NameStr.Get(), NameStr.Length()); + }; + + BIND_SYNC(get_actor_class_name) << [this]( + cr::ActorId ActorID) -> R + { + REQUIRE_CARLA_EPISODE(); + auto CarlaActor = Episode->FindCarlaActor(ActorID); + check(CarlaActor != nullptr); + auto Actor = CarlaActor->GetActor(); + check(Actor != nullptr); + auto Class = Actor->GetClass(); + check(Class != nullptr); + auto Name = Class->GetName(); + auto NameStr = StringCast(*Name, Name.Len()); + return std::string((const char*)NameStr.Get(), NameStr.Length()); + }; + } // =============================================================================