CARLA
ActorRegistry.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
10 
11 #include "Containers/Map.h"
12 
14 #include <carla/Iterator.h>
16 
17 #include <unordered_map>
18 
19 /// A registry of all the Carla actors.
21 {
22 public:
23 
25  using ValueType = TSharedPtr<FCarlaActor>;
26 
27 private:
28 
29  // using DatabaseType = std::unordered_map<IdType, FCarlaActor>;
30  using DatabaseType = TMap<IdType, TSharedPtr<FCarlaActor>>;
31 
32  // ===========================================================================
33  /// @name Actor registry functions
34  // ===========================================================================
35  /// @{
36 public:
37 
38  /// Register the @a Actor in the database. A new ID will be assign to this
39  /// actor.
40  ///
41  /// @warning Undefined if an actor is registered more than once.
42  FCarlaActor* Register(AActor &Actor, FActorDescription Description, IdType DesiredId = 0);
43 
44  void Deregister(IdType Id);
45 
46  void Deregister(AActor *Actor);
47 
48  /// @}
49  // ===========================================================================
50  /// @name Look up functions
51  // ===========================================================================
52  /// @{
53 
54  int32 Num() const
55  {
56  return Actors.Num();
57  }
58 
59  bool IsEmpty() const
60  {
61  return Num() == 0;
62  }
63 
64  bool Contains(uint32 Id) const
65  {
66  return ActorDatabase.Find(Id) != nullptr;
67  }
68 
70  {
71  ValueType* CarlaActorPtr = ActorDatabase.Find(Id);
72  return CarlaActorPtr ? CarlaActorPtr->Get() : nullptr;
73  }
74 
76  {
77  const ValueType* CarlaActorPtr = ActorDatabase.Find(Id);
78  return CarlaActorPtr ? CarlaActorPtr->Get() : nullptr;
79  }
80 
82  {
83  IdType* PtrToId = Ids.Find(Actor);
84  return PtrToId ? FindCarlaActor(*PtrToId) : nullptr;
85  }
86 
87  const FCarlaActor* FindCarlaActor(const AActor *Actor) const
88  {
89  const IdType* PtrToId = Ids.Find(Actor);
90  return PtrToId ? FindCarlaActor(*PtrToId) : nullptr;
91  }
92 
94 
95  void PutActorToSleep(IdType Id, UCarlaEpisode* CarlaEpisode);
96 
97  void WakeActorUp(IdType Id, UCarlaEpisode* CarlaEpisode);
98 
99  /// @}
100  // ===========================================================================
101  /// @name Range iteration support
102  // ===========================================================================
103  /// @{
104 public:
105 
106  auto begin() const noexcept
107  {
108  return ActorDatabase.begin();
109  }
110 
111  auto end() const noexcept
112  {
113  return ActorDatabase.end();
114  }
115 
116  /// @}
117 private:
118 
119  TSharedPtr<FCarlaActor> MakeCarlaActor(
120  IdType Id,
121  AActor &Actor,
122  FActorDescription Description,
123  carla::rpc::ActorState InState) const;
124 
126  AActor &Actor) const;
127 
128  TMap<IdType, AActor *> Actors;
129 
130  TMap<AActor *, IdType> Ids;
131 
133 
135 };
void Deregister(IdType Id)
TMap< IdType, AActor * > Actors
auto end() const noexcept
TMap< IdType, TSharedPtr< FCarlaActor > > DatabaseType
Definition: ActorRegistry.h:30
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
FCarlaActor * FindCarlaActor(const AActor *Actor)
Definition: ActorRegistry.h:81
void PutActorToSleep(IdType Id, UCarlaEpisode *CarlaEpisode)
DatabaseType ActorDatabase
TSharedPtr< FCarlaActor > ValueType
Definition: ActorRegistry.h:25
auto begin() const noexcept
FCarlaActor MakeFakeActor(AActor &Actor) const
TMap< AActor *, IdType > Ids
bool IsEmpty() const
Definition: ActorRegistry.h:59
uint32 IdType
Definition: CarlaActor.h:27
uint32_t stream_id_type
Definition: Types.h:18
void WakeActorUp(IdType Id, UCarlaEpisode *CarlaEpisode)
int32 Num() const
Definition: ActorRegistry.h:54
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:38
TSharedPtr< FCarlaActor > MakeCarlaActor(IdType Id, AActor &Actor, FActorDescription Description, carla::rpc::ActorState InState) const
bool Contains(uint32 Id) const
Definition: ActorRegistry.h:64
FString GetDescriptionFromStream(carla::streaming::detail::stream_id_type Id)
const FCarlaActor * FindCarlaActor(const AActor *Actor) const
Definition: ActorRegistry.h:87
A description of a Carla Actor with all its variation.
FCarlaActor::IdType IdType
Definition: ActorRegistry.h:24
FCarlaActor * Register(AActor &Actor, FActorDescription Description, IdType DesiredId=0)
Register the Actor in the database.
static IdType ID_COUNTER
FCarlaActor * FindCarlaActor(IdType Id)
Definition: ActorRegistry.h:69
A view over an actor and its properties.
Definition: CarlaActor.h:23
const FCarlaActor * FindCarlaActor(IdType Id) const
Definition: ActorRegistry.h:75