CARLA
ActorRegistry.cpp
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 #include "Carla.h"
10 
11 #include "Carla/Game/Tagger.h"
14 #include "Carla/Sensor/Sensor.h"
15 
17 #include "carla/streaming/Token.h"
20 
21 
22 namespace crp = carla::rpc;
23 
25 
27 {
28  if (!Actor)
29  {
31  }
32 
33  if (nullptr != Cast<ACarlaWheeledVehicle>(Actor))
34  {
36  }
37  else if (nullptr != Cast<ACharacter>(Actor))
38  {
40  }
41  else if (nullptr != Cast<ATrafficLightBase>(Actor))
42  {
44  }
45  else if (nullptr != Cast<ATrafficSignBase>(Actor))
46  {
48  }
49  else if (nullptr != Cast<ASensor>(Actor))
50  {
52  }
53  else
54  {
56  }
57 }
58 
59 FString CarlaGetRelevantTagAsString(const TSet<crp::CityObjectLabel> &SemanticTags)
60 {
61  for (auto &&Tag : SemanticTags)
62  {
64  {
65  auto Str = ATagger::GetTagAsString(Tag).ToLower();
66  return (Str.EndsWith(TEXT("s")) ? Str.LeftChop(1) : Str);
67  }
68  }
69  return TEXT("unknown");
70 }
71 
73 {
74 
75  FCarlaActor* CarlaActor = FindCarlaActor(DesiredId);
76  bool IsDormant = CarlaActor && (CarlaActor->IsDormant());
77  if(IsDormant)
78  {
79  CarlaActor->TheActor = &Actor;
80  Actors.Add(DesiredId, &Actor);
81  Ids.Add(&Actor, DesiredId);
82  return CarlaActor;
83  }
84 
86 
87  if (DesiredId != 0 && Id != DesiredId) {
88  // check if the desired Id is free, then use it instead
89  if (!Actors.Contains(DesiredId))
90  {
91  Id = DesiredId;
92  if (ID_COUNTER < Id)
93  ID_COUNTER = Id;
94  }
95  }
96 
97  Actors.Emplace(Id, &Actor);
98  if (Ids.Contains(&Actor))
99  {
100  UE_LOG(
101  LogCarla,
102  Warning,
103  TEXT("This actor's memory address is already registered, "
104  "either you forgot to deregister the actor "
105  "or the actor was garbage collected."));
106  }
107  Ids.Emplace(&Actor, Id);
108 
109  TSharedPtr<FCarlaActor> View =
110  MakeCarlaActor(Id, Actor, std::move(Description), crp::ActorState::Active);
111 
112  TSharedPtr<FCarlaActor>& Result = ActorDatabase.Emplace(Id, MoveTemp(View));
113 
114  check(static_cast<size_t>(Actors.Num()) == ActorDatabase.Num());
115  return Result.Get();
116 }
117 
119 {
120  FCarlaActor* CarlaActor = FindCarlaActor(Id);
121 
122  if(!CarlaActor) return;
123 
124  AActor *Actor = CarlaActor->GetActor();
125 
126  ActorDatabase.Remove(Id);
127  Actors.Remove(Id);
128  Ids.Remove(Actor);
129 
130  CarlaActor->TheActor = nullptr;
131 
132  check(static_cast<size_t>(Actors.Num()) == ActorDatabase.Num());
133 }
134 
136 {
137  check(Actor != nullptr);
138  FCarlaActor* CarlaActor = FindCarlaActor(Actor);
139  check(CarlaActor->GetActor() == Actor);
140  Deregister(CarlaActor->GetActorId());
141 }
142 
143 TSharedPtr<FCarlaActor> FActorRegistry::MakeCarlaActor(
144  IdType Id,
145  AActor &Actor,
146  FActorDescription Description,
147  crp::ActorState InState) const
148 {
149  auto Info = MakeShared<FActorInfo>();
150  Info->Description = std::move(Description);
151  ATagger::GetTagsOfTaggedActor(Actor, Info->SemanticTags);
152  Info->BoundingBox = UBoundingBoxCalculator::GetActorBoundingBox(&Actor);
153 
154  if (Info->Description.Id.IsEmpty())
155  {
156  // This is a fake actor, let's fake the id based on their semantic tags.
157  Info->Description.Id = TEXT("static.") + CarlaGetRelevantTagAsString(Info->SemanticTags);
158  }
159 
160  Info->SerializedData.id = Id;
161  Info->SerializedData.description = Info->Description;
162  Info->SerializedData.bounding_box = Info->BoundingBox;
163  Info->SerializedData.semantic_tags.reserve(Info->SemanticTags.Num());
164  for (auto &&Tag : Info->SemanticTags)
165  {
166  using tag_t = decltype(Info->SerializedData.semantic_tags)::value_type;
167  Info->SerializedData.semantic_tags.emplace_back(static_cast<tag_t>(Tag));
168  }
169  auto *Sensor = Cast<ASensor>(&Actor);
170  if (Sensor != nullptr)
171  {
172  const auto &Token = Sensor->GetToken();
173  Info->SerializedData.stream_token = decltype(Info->SerializedData.stream_token)(
174  std::begin(Token.data),
175  std::end(Token.data));
176  }
177  auto Type = FActorRegistry_GetActorType(&Actor);
178  TSharedPtr<FCarlaActor> CarlaActor =
180  Id, &Actor,
181  std::move(Info), Type,
182  InState, Actor.GetWorld());
183  return CarlaActor;
184 }
185 
187 {
188  FCarlaActor* CarlaActor = FindCarlaActor(Id);
189 
190  // update id maps
191  Actors[Id] = nullptr;
192  AActor* Actor = CarlaActor->GetActor();
193  if(Actor)
194  {
195  Ids.Remove(Actor);
196  }
197 
198  CarlaActor->PutActorToSleep(CarlaEpisode);
199  for (const FCarlaActor::IdType& ChildId : CarlaActor->GetChildren())
200  {
201  PutActorToSleep(ChildId, CarlaEpisode);
202  }
203  // TODO: update id maps
204 }
205 
207 {
208 
209  FCarlaActor* CarlaActor = FindCarlaActor(Id);
210  CarlaActor->WakeActorUp(CarlaEpisode);
211  AActor* Actor = CarlaActor->GetActor();
212  if (Actor)
213  {
214  // update ids
215  Actors[Id] = Actor;
216  Ids.Add(Actor, Id);
217  if (CarlaActor->GetParent())
218  {
219  FCarlaActor* ParentView = FindCarlaActor(CarlaActor->GetParent());
220  if (ParentView && !ParentView->IsDormant() && ParentView->GetActor())
221  {
222  AActor* ParentActor = ParentView->GetActor();
223  CarlaEpisode->AttachActors(
224  Actor,
225  ParentActor,
226  static_cast<EAttachmentType>(CarlaActor->GetAttachmentType()));
227  }
228  else
229  {
230  UE_LOG(LogCarla, Error, TEXT("Failed to attach actor %d to %d during wake up"), Id, CarlaActor->GetParent());
231  }
232  }
233  }
234  for (const FCarlaActor::IdType& ChildId : CarlaActor->GetChildren())
235  {
236  WakeActorUp(ChildId, CarlaEpisode);
237  }
238 }
239 
241 {
242  for (auto &Item : ActorDatabase)
243  {
244  // check for a sensor
245  ASensor *Sensor = Cast<ASensor>(Item.Value->GetActor());
246  if (Sensor == nullptr) continue;
247 
249  if (token.get_stream_id() == Id)
250  {
251  const FActorInfo *Info = Item.Value->GetActorInfo();
252  return Info->Description.Id;
253  }
254  }
255  return FString("");
256 }
void Deregister(IdType Id)
auto GetToken() const
Return the token that allows subscribing to this sensor&#39;s stream.
carla::rpc::AttachmentType GetAttachmentType() const
Definition: CarlaActor.h:145
TMap< IdType, AActor * > Actors
void WakeActorUp(UCarlaEpisode *CarlaEpisode)
Definition: CarlaActor.cpp:165
void PutActorToSleep(IdType Id, UCarlaEpisode *CarlaEpisode)
DatabaseType ActorDatabase
void PutActorToSleep(UCarlaEpisode *CarlaEpisode)
Definition: CarlaActor.cpp:154
AActor * GetActor()
Definition: CarlaActor.h:90
TMap< AActor *, IdType > Ids
static void GetTagsOfTaggedActor(const AActor &Actor, TSet< crp::CityObjectLabel > &Tags)
Retrieve the tags of an already tagged actor.
Definition: Tagger.cpp:236
uint32 IdType
Definition: CarlaActor.h:27
uint32_t stream_id_type
Definition: Types.h:18
void WakeActorUp(IdType Id, UCarlaEpisode *CarlaEpisode)
FString CarlaGetRelevantTagAsString(const TSet< crp::CityObjectLabel > &SemanticTags)
const TArray< IdType > & GetChildren() const
Definition: CarlaActor.h:135
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:38
Serializes a stream endpoint.
Definition: detail/Token.h:61
TSharedPtr< FCarlaActor > MakeCarlaActor(IdType Id, AActor &Actor, FActorDescription Description, carla::rpc::ActorState InState) const
FString GetDescriptionFromStream(carla::streaming::detail::stream_id_type Id)
static FString GetTagAsString(crp::CityObjectLabel Tag)
Retrieve the tags of an already tagged actor.
Definition: Tagger.cpp:250
static FBoundingBox GetActorBoundingBox(const AActor *Actor, uint8 InTagQueried=0xFF)
Compute the bounding box of the given Carla actor.
A description of a Carla Actor with all its variation.
FActorDescription Description
Definition: ActorInfo.h:26
static FCarlaActor::ActorType FActorRegistry_GetActorType(const AActor *Actor)
FCarlaActor::IdType IdType
Definition: ActorRegistry.h:24
void AttachActors(AActor *Child, AActor *Parent, EAttachmentType InAttachmentType=EAttachmentType::Rigid)
Attach Child to Parent.
A view over an actor and its properties.
Definition: ActorInfo.h:22
bool IsDormant() const
Definition: CarlaActor.h:70
FCarlaActor * Register(AActor &Actor, FActorDescription Description, IdType DesiredId=0)
Register the Actor in the database.
static IdType ID_COUNTER
IdType GetActorId() const
Definition: CarlaActor.h:80
FCarlaActor * FindCarlaActor(IdType Id)
Definition: ActorRegistry.h:69
static TSharedPtr< FCarlaActor > ConstructCarlaActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, ActorType Type, carla::rpc::ActorState InState, UWorld *World)
Definition: CarlaActor.cpp:121
A view over an actor and its properties.
Definition: CarlaActor.h:23
AActor * TheActor
Definition: CarlaActor.h:447
IdType GetParent() const
Definition: CarlaActor.h:120