Add episode class

This commit is contained in:
nsubiron 2018-10-08 19:18:34 +02:00
parent 2c53287dcf
commit 1aa89af1a1
15 changed files with 154 additions and 52 deletions

View File

@ -13,25 +13,25 @@ namespace carla {
namespace client {
geom::Location Actor::GetLocation() const {
return GetClientImplementation()->GetActorLocation(*this);
return GetEpisode()->GetActorLocation(*this);
}
geom::Transform Actor::GetTransform() const {
return GetClientImplementation()->GetActorTransform(*this);
return GetEpisode()->GetActorTransform(*this);
}
void Actor::SetLocation(const geom::Location &location) {
GetClientImplementation()->SetActorLocation(*this, location);
GetEpisode()->SetActorLocation(*this, location);
}
void Actor::SetTransform(const geom::Transform &transform) {
GetClientImplementation()->SetActorTransform(*this, transform);
GetEpisode()->SetActorTransform(*this, transform);
}
void Actor::Destroy() {
if (_is_alive) {
// Let the exceptions leave the function, IsAlive() will still be true.
_is_alive = !GetClientImplementation()->DestroyActor(*this);
_is_alive = !GetEpisode()->DestroyActor(*this);
} else {
log_warning(
"attempting to destroy an actor that is already dead:",

View File

@ -42,12 +42,6 @@ namespace client {
virtual void Destroy();
protected:
auto GetClientImplementation() const {
return GetWorld().parent;
}
private:
bool _is_alive = true;

View File

@ -43,7 +43,7 @@ namespace client {
}
World GetWorld() const {
return _client_state->GetWorld();
return _client_state->GetCurrentEpisode();
}
private:

View File

@ -28,11 +28,11 @@ namespace client {
log_debug(GetDisplayId(), ": subscribing to stream");
if (_is_listening) {
log_warning(
"attempting to listen to stream but sensor is already listeninig:",
"attempting to listen to stream but sensor is already listening:",
GetDisplayId());
return;
}
GetClientImplementation()->SubscribeToStream(
GetEpisode()->SubscribeToStream(
GetActorDescription().GetStreamToken(),
std::move(callback));
_is_listening = true;
@ -45,7 +45,7 @@ namespace client {
GetDisplayId());
return;
}
GetClientImplementation()->UnSubscribeFromStream(
GetEpisode()->UnSubscribeFromStream(
GetActorDescription().GetStreamToken());
_is_listening = false;
}

View File

@ -12,13 +12,13 @@ namespace client {
void Vehicle::ApplyControl(const Control &control) {
if (control != _control) {
GetClientImplementation()->ApplyControlToVehicle(*this, control);
GetEpisode()->ApplyControlToVehicle(*this, control);
_control = control;
}
}
void Vehicle::SetAutopilot(bool enabled) {
GetClientImplementation()->SetVehicleAutopilot(*this, enabled);
GetEpisode()->SetVehicleAutopilot(*this, enabled);
}
} // namespace client

View File

@ -17,11 +17,11 @@ namespace carla {
namespace client {
SharedPtr<BlueprintLibrary> World::GetBlueprintLibrary() const {
return parent->GetBlueprintLibrary();
return _episode->GetBlueprintLibrary();
}
SharedPtr<Actor> World::GetSpectator() const {
return parent->GetSpectator();
return _episode->GetSpectator();
}
SharedPtr<Actor> World::SpawnActor(
@ -29,7 +29,7 @@ namespace client {
const geom::Transform &transform,
Actor *parent_actor) {
try {
return parent->SpawnActor(blueprint, transform, parent_actor);
return _episode->SpawnActor(blueprint, transform, parent_actor);
} catch (const std::exception &e) {
log_warning("SpawnActor: failed with:", e.what());
throw;

View File

@ -7,17 +7,12 @@
#pragma once
#include "carla/Memory.h"
#include "carla/client/GarbageCollectionPolicy.h"
#include "carla/client/detail/Episode.h"
#include "carla/geom/Transform.h"
namespace carla {
namespace client {
namespace detail {
class Client;
class ActorFactory;
}
class Actor;
class ActorBlueprint;
class BlueprintLibrary;
@ -25,6 +20,8 @@ namespace detail {
class World {
public:
World(detail::Episode episode) : _episode(std::move(episode)) {}
World(const World &) = default;
World(World &&) = default;
@ -47,13 +44,7 @@ namespace detail {
private:
friend class Actor;
friend class detail::ActorFactory;
friend class detail::Client;
World(SharedPtr<detail::Client> parent) : parent(std::move(parent)) {}
SharedPtr<detail::Client> parent;
detail::Episode _episode;
};
} // namespace client

View File

@ -63,17 +63,17 @@ namespace detail {
}
SharedPtr<Actor> ActorFactory::MakeActor(
World world,
Episode episode,
rpc::Actor description,
GarbageCollectionPolicy gc) {
const auto gcw = world.parent->GetGarbageCollectionPolicy();
const auto gcw = episode->GetGarbageCollectionPolicy();
const auto gca = (gc == GarbageCollectionPolicy::Inherit ? gcw : gc);
if (description.HasAStream()) {
return MakeActorImpl<Sensor>(ActorInitializer{description, world}, gca);
return MakeActorImpl<Sensor>(ActorInitializer{description, episode}, gca);
} else if (StringUtil::StartsWith(description.description.id, "vehicle.")) {
return MakeActorImpl<Vehicle>(ActorInitializer{description, world}, gca);
return MakeActorImpl<Vehicle>(ActorInitializer{description, episode}, gca);
}
return MakeActorImpl<Actor>(ActorInitializer{description, world}, gca);
return MakeActorImpl<Actor>(ActorInitializer{description, episode}, gca);
}
} // namespace detail

View File

@ -8,13 +8,13 @@
#include "carla/Memory.h"
#include "carla/client/GarbageCollectionPolicy.h"
#include "carla/client/detail/Episode.h"
#include "carla/rpc/Actor.h"
namespace carla {
namespace client {
class Actor;
class World;
namespace detail {
@ -22,7 +22,7 @@ namespace detail {
public:
static SharedPtr<Actor> MakeActor(
World world,
Episode episode,
rpc::Actor actor_description,
GarbageCollectionPolicy gc = GarbageCollectionPolicy::Inherit);
};

View File

@ -30,7 +30,7 @@ namespace detail {
std::string GetDisplayId() const;
World GetWorld() const {
return _parent;
return _episode;
}
protected:
@ -39,15 +39,23 @@ namespace detail {
return _description;
}
Episode &GetEpisode() {
return _episode;
}
const Episode &GetEpisode() const {
return _episode;
}
private:
ActorState(rpc::Actor description, World parent)
ActorState(rpc::Actor description, Episode episode)
: _description(std::move(description)),
_parent(std::move(parent)) {}
_episode(std::move(episode)) {}
rpc::Actor _description;
World _parent;
Episode _episode;
};
} // namespace detail

View File

@ -12,6 +12,7 @@
#include "carla/client/Vehicle.h"
#include "carla/client/World.h"
#include "carla/client/detail/ActorFactory.h"
#include "carla/client/detail/Episode.h"
#include "carla/rpc/Client.h"
#include "carla/sensor/Deserializer.h"
#include "carla/streaming/Client.h"
@ -94,7 +95,7 @@ namespace detail {
SharedPtr<Actor> Client::GetSpectator() {
auto spectator = _pimpl->CallAndWait<carla::rpc::Actor>("get_spectator");
return ActorFactory::MakeActor(
GetWorld(),
GetCurrentEpisode(),
spectator,
GarbageCollectionPolicy::Disabled);
}
@ -115,7 +116,7 @@ namespace detail {
transform,
blueprint.MakeActorDescription());
}
return ActorFactory::MakeActor(GetWorld(), actor, gc);
return ActorFactory::MakeActor(GetCurrentEpisode(), actor, gc);
}
bool Client::DestroyActor(Actor &actor) {

View File

@ -10,7 +10,7 @@
#include "carla/NonCopyable.h"
#include "carla/Time.h"
#include "carla/client/GarbageCollectionPolicy.h"
#include "carla/client/World.h"
#include "carla/client/detail/Episode.h"
#include "carla/geom/Transform.h"
#include <functional>
@ -24,7 +24,7 @@ namespace client {
class ActorBlueprint;
class BlueprintLibrary;
class Vehicle;
class World;
class Episode;
}
namespace rpc { class VehicleControl; }
namespace sensor { class SensorData; }
@ -35,6 +35,7 @@ namespace carla {
namespace client {
namespace detail {
/// @todo Make sure this class is really thread-safe.
class Client
: public EnableSharedFromThis<Client>,
private NonCopyable {
@ -56,10 +57,6 @@ namespace detail {
bool Ping();
World GetWorld() {
return shared_from_this();
}
SharedPtr<BlueprintLibrary> GetBlueprintLibrary();
SharedPtr<Actor> GetSpectator();
@ -94,12 +91,24 @@ namespace detail {
return _gc_policy;
}
size_t GetCurrentEpisodeId() const {
return _episode_id;
}
Episode GetCurrentEpisode() {
return shared_from_this();
}
private:
class Pimpl;
const std::unique_ptr<Pimpl> _pimpl;
const GarbageCollectionPolicy _gc_policy;
// At this point the id won't change because we cannot yet restart the
// episode from the client.
const size_t _episode_id = 0u;
};
} // namespace detail

View File

@ -0,0 +1,33 @@
// 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/client/detail/Episode.h"
#include "carla/client/detail/Client.h"
#include <exception>
namespace carla {
namespace client {
namespace detail {
Episode::Episode(SharedPtr<PersistentState> state)
: _state(std::move(state)),
_episode_id(_state->GetCurrentEpisodeId()) {}
PersistentState &Episode::GetPersistentStateWithChecks() const {
DEBUG_ASSERT(_state != nullptr);
if (_episode_id != _state->GetCurrentEpisodeId()) {
throw std::runtime_error(
"trying to access an expired episode; a new episode was started "
"in the simulation but an object tried accessing the old one.");
}
return *_state;
}
} // namespace detail
} // namespace client
} // namespace carla

View File

@ -0,0 +1,43 @@
// 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>.
#pragma once
#include "carla/Debug.h"
#include "carla/Memory.h"
#include "carla/client/detail/PersistentState.h"
namespace carla {
namespace client {
namespace detail {
class Episode {
public:
PersistentState &operator*() const {
return GetPersistentStateWithChecks();
}
PersistentState *operator->() const {
return &GetPersistentStateWithChecks();
}
private:
friend PersistentState;
Episode(SharedPtr<PersistentState> state);
PersistentState &GetPersistentStateWithChecks() const;
SharedPtr<PersistentState> _state;
size_t _episode_id;
};
} // namespace detail
} // namespace client
} // namespace carla

View File

@ -0,0 +1,23 @@
// 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>.
#pragma once
// #include "carla/client/detail/Client.h"
namespace carla {
namespace client {
namespace detail {
class Client;
/// At this point the client is the only persistent state we have, but
/// conceptually is nice to make the distinction.
using PersistentState = Client;
} // namespace detail
} // namespace client
} // namespace carla