Attach description to actor instance

This commit is contained in:
nsubiron 2018-07-24 12:19:45 +02:00
parent 0f636e84fd
commit a1663c6e03
14 changed files with 83 additions and 38 deletions

View File

@ -30,13 +30,9 @@ namespace client {
return _actor.id;
}
// const std::string &GetTypeId() const {
// return _actor.blueprint.type_id;
// }
// ActorBlueprint GetBlueprint() const {
// return _actor.blueprint;
// }
const std::string &GetTypeId() const {
return _actor.description.id;
}
SharedPtr<World> GetWorld() const {
return _world;

View File

@ -6,7 +6,8 @@
#pragma once
#include "carla/rpc/ActorDefinition.h"
#include "carla/Debug.h"
#include "carla/rpc/ActorDescription.h"
namespace carla {
namespace rpc {
@ -16,11 +17,23 @@ namespace rpc {
using id_type = uint32_t;
Actor() = default;
id_type id;
ActorDefinition definition;
ActorDescription description;
MSGPACK_DEFINE_ARRAY(id, definition);
#ifdef LIBCARLA_INCLUDED_FROM_UE4
Actor(FActorView View)
: id(View.GetActorId()),
description(*View.GetActorDescription()) {
DEBUG_ASSERT(View.IsValid());
}
#endif // LIBCARLA_INCLUDED_FROM_UE4
MSGPACK_DEFINE_ARRAY(id, description);
};
} // namespace rpc

View File

@ -79,6 +79,9 @@ namespace rpc {
#ifdef LIBCARLA_INCLUDED_FROM_UE4
ActorAttributeValue(const FActorAttribute &Attribute)
: ActorAttributeValue(ActorAttribute(Attribute)) {}
operator FActorAttribute() const {
FActorAttribute Attribute;
Attribute.Id = ToFString(id);

View File

@ -28,6 +28,15 @@ namespace rpc {
#ifdef LIBCARLA_INCLUDED_FROM_UE4
ActorDescription(const FActorDescription &Description)
: uid(Description.UId),
id(FromFString(Description.Id)) {
attributes.reserve(Description.Variations.Num());
for (const auto &Item : Description.Variations) {
attributes.push_back(Item);
}
}
operator FActorDescription() const {
FActorDescription Description;
Description.UId = uid;

View File

@ -0,0 +1,19 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "carla/rpc/Server.h"
#include <rpc/this_handler.h>
namespace carla {
namespace rpc {
void Server::RespondError(std::string error_message) {
::rpc::this_handler().respond_error(std::move(error_message));
}
} // namespace carla
} // namespace rpc

View File

@ -11,7 +11,6 @@
#include <boost/asio/io_service.hpp>
#include <rpc/server.h>
#include <rpc/this_handler.h>
#include <future>
@ -109,10 +108,7 @@ namespace detail {
_server.stop();
}
template <typename T>
static void RespondError(T &&error_object) {
::rpc::this_handler().respond_error(std::forward<T>(error_object));
}
static void RespondError(std::string error_message);
private:

View File

@ -27,11 +27,10 @@ void export_actor() {
class_<cc::Actor, boost::noncopyable, boost::shared_ptr<cc::Actor>>("Actor", no_init)
.add_property("id", &cc::Actor::GetId)
// .add_property("type_id", +[](const cc::Actor &self) -> std::string {
// // Needs to copy the string by value.
// return self.GetTypeId();
// })
// .add_property("blueprint", &cc::Actor::GetBlueprint)
.add_property("type_id", +[](const cc::Actor &self) -> std::string {
// Needs to copy the string by value.
return self.GetTypeId();
})
.def("get_world", &cc::Actor::GetWorld)
.def("apply_control", &cc::Actor::ApplyControl)
.def(self_ns::str(self_ns::self))

View File

@ -36,7 +36,7 @@ void FActorDispatcher::Bind(IActorSpawner &ActorSpawner)
TPair<EActorSpawnResultStatus, FActorView> FActorDispatcher::SpawnActor(
const FTransform &Transform,
const FActorDescription &Description)
FActorDescription Description)
{
if ((Description.UId == 0) || (Description.UId > SpawnFunctions.Num()))
{
@ -44,7 +44,7 @@ TPair<EActorSpawnResultStatus, FActorView> FActorDispatcher::SpawnActor(
return MakeTuple(EActorSpawnResultStatus::InvalidDescription, FActorView());
}
auto Result = SpawnFunctions[Description.UId - 1](Transform, Description);
auto View = Result.IsValid() ? Registry.Register(*Result.Actor) : FActorView();
auto View = Result.IsValid() ? Registry.Register(*Result.Actor, std::move(Description)) : FActorView();
return MakeTuple(Result.Status, View);
}

View File

@ -43,7 +43,7 @@ public:
/// view is invalid.
TPair<EActorSpawnResultStatus, FActorView> SpawnActor(
const FTransform &Transform,
const FActorDescription &ActorDescription);
FActorDescription ActorDescription);
/// Destroys an actor, properly removing it from the registry.
void DestroyActor(AActor *Actor);

View File

@ -9,13 +9,13 @@
#include "Carla.h"
#include "Carla/Actor/ActorRegistry.h"
FActorView FActorRegistry::Register(AActor &Actor)
FActorView FActorRegistry::Register(AActor &Actor, FActorDescription Description)
{
static IdType ID_COUNTER = 0u;
const auto Id = ++ID_COUNTER;
Actors.Emplace(Id, &Actor);
Ids.Emplace(&Actor, Id);
auto Result = ActorDatabase.emplace(Id, FActorView(Id, Actor));
auto Result = ActorDatabase.emplace(Id, FActorView(Id, Actor, std::move(Description)));
check(Result.second);
check(static_cast<size_t>(Actors.Num()) == ActorDatabase.size());
check(static_cast<size_t>(Ids.Num()) == ActorDatabase.size());

View File

@ -33,7 +33,7 @@ public:
/// actor.
///
/// @warning Undefined if an actor is registered more than once.
FActorView Register(AActor &Actor);
FActorView Register(AActor &Actor, FActorDescription Description);
void Deregister(IdType Id);

View File

@ -6,6 +6,8 @@
#pragma once
#include "Carla/Actor/ActorDescription.h"
class AActor;
/// A view over an actor and its properties.
@ -20,7 +22,7 @@ public:
bool IsValid() const
{
return TheActor != nullptr;
return (TheActor != nullptr) && Description.IsValid();
}
IdType GetActorId() const
@ -38,15 +40,25 @@ public:
return TheActor;
}
const FActorDescription *GetActorDescription() const
{
return Description.Get();
}
private:
friend class FActorRegistry;
FActorView(IdType ActorId, AActor &Actor)
FActorView(IdType ActorId, AActor &Actor, FActorDescription Description)
: Id(ActorId),
TheActor(&Actor) {}
TheActor(&Actor),
Description(MakeShared<FActorDescription>(std::move(Description))) {
check(Id != 0u);
}
IdType Id = 0u;
AActor *TheActor = nullptr;
TSharedPtr<const FActorDescription> Description = nullptr;
};

View File

@ -52,9 +52,9 @@ public:
/// view is invalid.
TPair<EActorSpawnResultStatus, FActorView> SpawnActorWithInfo(
const FTransform &Transform,
const FActorDescription &ActorDescription)
FActorDescription ActorDescription)
{
return ActorDispatcher.SpawnActor(Transform, ActorDescription);
return ActorDispatcher.SpawnActor(Transform, std::move(ActorDescription));
}
/// Spawns an actor based on @a ActorDescription at @a Transform. To properly
@ -66,9 +66,9 @@ public:
UFUNCTION(BlueprintCallable)
AActor *SpawnActor(
const FTransform &Transform,
const FActorDescription &ActorDescription)
FActorDescription ActorDescription)
{
return SpawnActorWithInfo(Transform, ActorDescription).Value.GetActor();
return SpawnActorWithInfo(Transform, std::move(ActorDescription)).Value.GetActor();
}
/// Destroys an actor, properly removing it from the registry.

View File

@ -85,16 +85,14 @@ void FTheNewCarlaServer::FPimpl::BindActions()
Server.BindSync("spawn_actor", [this](
const cr::Transform &Transform,
const cr::ActorDescription &Definition) {
cr::ActorDescription Description) -> cr::Actor {
RequireEpisode();
auto Result = Episode->SpawnActorWithInfo(Transform, Definition);
auto Result = Episode->SpawnActorWithInfo(Transform, std::move(Description));
if (Result.Key != EActorSpawnResultStatus::Success)
{
RespondError(FActorSpawnResult::StatusToString(Result.Key));
}
cr::Actor actor;
actor.id = Result.Value.GetActorId();
return actor;
return Result.Value;
});
}