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 private:
23 
24  // using DatabaseType = std::unordered_map<FCarlaActor::IdType, FCarlaActor>;
25  using DatabaseType = TMap<FCarlaActor::IdType, TSharedPtr<FCarlaActor>>;
26 
27 public:
28 
30  using ValueType = 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 
93 
94  void PutActorToSleep(FCarlaActor::IdType Id, UCarlaEpisode* CarlaEpisode);
95 
96  void WakeActorUp(FCarlaActor::IdType Id, UCarlaEpisode* CarlaEpisode);
97 
98  /// @}
99  // ===========================================================================
100  /// @name Range iteration support
101  // ===========================================================================
102  /// @{
103 public:
104 
105  auto begin() const noexcept
106  {
107  return ActorDatabase.begin();
108  }
109 
110  auto end() const noexcept
111  {
112  return ActorDatabase.end();
113  }
114 
115  /// @}
116 private:
117 
118  TSharedPtr<FCarlaActor> MakeCarlaActor(
119  IdType Id,
120  AActor &Actor,
121  FActorDescription Description,
122  carla::rpc::ActorState InState) const;
123 
125  AActor &Actor) const;
126 
127  TMap<IdType, AActor *> Actors;
128 
129  TMap<AActor *, IdType> Ids;
130 
132 
134 };
void Deregister(IdType Id)
TMap< IdType, AActor * > Actors
auto end() const noexcept
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
FCarlaActor * FindCarlaActor(const AActor *Actor)
Definition: ActorRegistry.h:81
DatabaseType ActorDatabase
TSharedPtr< FCarlaActor > ValueType
Definition: ActorRegistry.h:30
auto begin() const noexcept
FCarlaActor MakeFakeActor(AActor &Actor) const
TMap< AActor *, IdType > Ids
void WakeActorUp(FCarlaActor::IdType Id, UCarlaEpisode *CarlaEpisode)
bool IsEmpty() const
Definition: ActorRegistry.h:59
uint32 IdType
Definition: CarlaActor.h:27
int32 Num() const
Definition: ActorRegistry.h:54
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:37
TSharedPtr< FCarlaActor > MakeCarlaActor(IdType Id, AActor &Actor, FActorDescription Description, carla::rpc::ActorState InState) const
bool Contains(uint32 Id) const
Definition: ActorRegistry.h:64
const FCarlaActor * FindCarlaActor(const AActor *Actor) const
Definition: ActorRegistry.h:87
A description of a Carla Actor with all its variation.
void PutActorToSleep(FCarlaActor::IdType Id, UCarlaEpisode *CarlaEpisode)
FCarlaActor::IdType IdType
Definition: ActorRegistry.h:29
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
TMap< FCarlaActor::IdType, TSharedPtr< FCarlaActor > > DatabaseType
Definition: ActorRegistry.h:25
A view over an actor and its properties.
Definition: CarlaActor.h:23
const FCarlaActor * FindCarlaActor(IdType Id) const
Definition: ActorRegistry.h:75