CARLA
CarlaReplayerHelper.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"
9 
11 #include "Carla/Actor/CarlaActor.h"
22 #include "Engine/StaticMeshActor.h"
25 
29 
30 
31 #include "EngineUtils.h"
32 
33 // create or reuse an actor for replaying
35  FVector &Location,
36  FVector &Rotation,
37  FActorDescription &ActorDesc,
38  uint32_t DesiredId,
39  bool SpawnSensors)
40 {
41  check(Episode != nullptr);
42 
43  // check type of actor we need
44  if (ActorDesc.Id.StartsWith("traffic."))
45  {
46  FCarlaActor* CarlaActor = FindTrafficLightAt(Location);
47  if (CarlaActor != nullptr)
48  {
49  // reuse that actor
50  return std::pair<int, FCarlaActor*>(2, CarlaActor);
51  }
52  else
53  {
54  // actor not found
55  UE_LOG(LogCarla, Log, TEXT("TrafficLight not found"));
56  return std::pair<int, FCarlaActor*>(0, nullptr);
57  }
58  }
59  else if (SpawnSensors || !ActorDesc.Id.StartsWith("sensor."))
60  {
61  // check if an actor of that type already exist with same id
62  if (Episode->GetActorRegistry().Contains(DesiredId))
63  {
64  auto* CarlaActor = Episode->FindCarlaActor(DesiredId);
65  const FActorDescription *desc = &CarlaActor->GetActorInfo()->Description;
66  if (desc->Id == ActorDesc.Id)
67  {
68  // we don't need to create, actor of same type already exist
69  // relocate
70  FRotator Rot = FRotator::MakeFromEuler(Rotation);
71  FTransform Trans2(Rot, Location, FVector(1, 1, 1));
72  CarlaActor->SetActorGlobalTransform(Trans2);
73  return std::pair<int, FCarlaActor*>(2, CarlaActor);
74  }
75  }
76  // create the transform
77  FRotator Rot = FRotator::MakeFromEuler(Rotation);
78  FTransform Trans(Rot, FVector(0, 0, 100000), FVector(1, 1, 1));
79  // create as new actor
80  TPair<EActorSpawnResultStatus, FCarlaActor*> Result = Episode->SpawnActorWithInfo(Trans, ActorDesc, DesiredId);
81  if (Result.Key == EActorSpawnResultStatus::Success)
82  {
83  // relocate
84  FTransform Trans2(Rot, Location, FVector(1, 1, 1));
85  Result.Value->SetActorGlobalTransform(Trans2);
86  ALargeMapManager * LargeMapManager = UCarlaStatics::GetLargeMapManager(Episode->GetWorld());
87  if (LargeMapManager)
88  {
89  LargeMapManager->OnActorSpawned(*Result.Value);
90  }
91  return std::pair<int, FCarlaActor*>(1, Result.Value);
92  }
93  else
94  {
95  UE_LOG(LogCarla, Log, TEXT("Actor could't be created by replayer"));
96  return std::pair<int, FCarlaActor*>(0, Result.Value);
97  }
98  }
99  else
100  {
101  // actor ignored
102  return std::pair<int, FCarlaActor*>(0, nullptr);
103  }
104 }
105 
107 {
108  check(Episode != nullptr);
109  auto World = Episode->GetWorld();
110  check(World != nullptr);
111 
112  // get its position (truncated as int's)
113  int x = static_cast<int>(Location.X);
114  int y = static_cast<int>(Location.Y);
115  int z = static_cast<int>(Location.Z);
116 
117  const FActorRegistry &Registry = Episode->GetActorRegistry();
118  // through all actors in registry
119  for (auto It = Registry.begin(); It != Registry.end(); ++It)
120  {
121  FCarlaActor* CarlaActor = It.Value().Get();
123  {
124  FVector vec = CarlaActor->GetActorGlobalLocation();
125  int x2 = static_cast<int>(vec.X);
126  int y2 = static_cast<int>(vec.Y);
127  int z2 = static_cast<int>(vec.Z);
128  if ((x2 == x) && (y2 == y) && (z2 == z))
129  {
130  // actor found
131  return CarlaActor;
132  }
133  }
134  }
135  // actor not found
136  return nullptr;
137 }
138 
139 // enable / disable physics for an actor
141 {
142  if (!CarlaActor)
143  {
144  return false;
145  }
146  ECarlaServerResponse Response =
147  CarlaActor->SetActorSimulatePhysics(bEnabled);
148  if (Response != ECarlaServerResponse::Success)
149  {
150  return false;
151  }
152  return true;
153 }
154 
155 // enable / disable autopilot for an actor
156 bool CarlaReplayerHelper::SetActorAutopilot(FCarlaActor* CarlaActor, bool bEnabled, bool bKeepState)
157 {
158  if (!CarlaActor)
159  {
160  return false;
161  }
162  ECarlaServerResponse Response =
163  CarlaActor->SetActorAutopilot(bEnabled, bKeepState);
164  if (Response != ECarlaServerResponse::Success)
165  {
166  return false;
167  }
168  return true;
169 }
170 
171 // replay event for creating actor
173  FVector Location,
174  FVector Rotation,
175  CarlaRecorderActorDescription Description,
176  uint32_t DesiredId,
177  bool bIgnoreHero,
178  bool ReplaySensors)
179 {
180  check(Episode != nullptr);
181  FActorDescription ActorDesc;
182  bool IsHero = false;
183 
184  // prepare actor description
185  ActorDesc.UId = Description.UId;
186  ActorDesc.Id = Description.Id;
187  for (const auto &Item : Description.Attributes)
188  {
189  FActorAttribute Attr;
190  Attr.Type = static_cast<EActorAttributeType>(Item.Type);
191  Attr.Id = Item.Id;
192  Attr.Value = Item.Value;
193  ActorDesc.Variations.Add(Attr.Id, std::move(Attr));
194  // check for hero
195  if (Item.Id == "role_name" && Item.Value == "hero")
196  IsHero = true;
197  }
198 
199  auto result = TryToCreateReplayerActor(
200  Location,
201  Rotation,
202  ActorDesc,
203  DesiredId,
204  ReplaySensors);
205 
206  if (result.first != 0)
207  {
208  // disable physics and autopilot on vehicles
209  if (result.second->GetActorType() == FCarlaActor::ActorType::Vehicle)
210  {
211  // ignore hero ?
212  if (!(bIgnoreHero && IsHero))
213  {
214  // disable physics
215  SetActorSimulatePhysics(result.second, false);
216  // disable autopilot
217  SetActorAutopilot(result.second, false, false);
218  }
219  else
220  {
221  // reenable physics just in case
222  SetActorSimulatePhysics(result.second, true);
223  }
224  }
225  return std::make_pair(result.first, result.second->GetActorId());
226  }
227  return std::make_pair(result.first, 0);
228 }
229 
230 // replay event for removing actor
232 {
233  check(Episode != nullptr);
234  FCarlaActor* CarlaActor = Episode->FindCarlaActor(DatabaseId);
235  if (CarlaActor == nullptr)
236  {
237  UE_LOG(LogCarla, Log, TEXT("Actor %d not found to destroy"), DatabaseId);
238  return false;
239  }
240  Episode->DestroyActor(CarlaActor->GetActorId());
241  return true;
242 }
243 
244 // replay event for parenting actors
245 bool CarlaReplayerHelper::ProcessReplayerEventParent(uint32_t ChildId, uint32_t ParentId)
246 {
247  check(Episode != nullptr);
248  FCarlaActor * Child = Episode->FindCarlaActor(ChildId);
249  FCarlaActor * Parent = Episode->FindCarlaActor(ParentId);
250  if(!Child)
251  {
252  UE_LOG(LogCarla, Log, TEXT("Parenting Child actors not found"));
253  return false;
254  }
255  if(!Parent)
256  {
257  UE_LOG(LogCarla, Log, TEXT("Parenting Parent actors not found"));
258  return false;
259  }
260  Child->SetParent(ParentId);
262  Parent->AddChildren(Child->GetActorId());
263  if(!Parent->IsDormant())
264  {
265  if(!Child->IsDormant())
266  {
268  Child->GetActor(),
269  Parent->GetActor(),
271  }
272  }
273  else
274  {
276  }
277  return true;
278 }
279 
280 // reposition actors
282 {
283  check(Episode != nullptr);
284  FCarlaActor* CarlaActor = Episode->FindCarlaActor(Pos1.DatabaseId);
285  FVector Location;
286  FRotator Rotation;
287  if(CarlaActor)
288  {
289  // check to assign first position or interpolate between both
290  if (Per == 0.0)
291  {
292  // assign position 1
293  Location = FVector(Pos1.Location);
294  Rotation = FRotator::MakeFromEuler(Pos1.Rotation);
295  }
296  else
297  {
298  // interpolate positions
299  Location = FMath::Lerp(FVector(Pos1.Location), FVector(Pos2.Location), Per);
300  Rotation = FMath::Lerp(FRotator::MakeFromEuler(Pos1.Rotation), FRotator::MakeFromEuler(Pos2.Rotation), Per);
301  }
302  // set new transform
303  FTransform Trans(Rotation, Location, FVector(1, 1, 1));
304  CarlaActor->SetActorGlobalTransform(Trans, ETeleportType::None);
305  return true;
306  }
307  return false;
308 }
309 
310 // reposition the camera
311 bool CarlaReplayerHelper::SetCameraPosition(uint32_t Id, FVector Offset, FQuat Rotation)
312 {
313  check(Episode != nullptr);
314 
315  // get the actor to follow
316  FCarlaActor* CarlaActor = Episode->FindCarlaActor(Id);
317  if (!CarlaActor)
318  return false;
319  // get specator pawn
320  APawn *Spectator = Episode->GetSpectatorPawn();
321  if (!Spectator)
322  return false;
323 
324  FCarlaActor* CarlaSpectator = Episode->FindCarlaActor(Spectator);
325  if (!CarlaSpectator)
326  return false;
327 
328  FTransform ActorTransform = CarlaActor->GetActorGlobalTransform();
329  // set the new position
330  FQuat ActorRot = ActorTransform.GetRotation();
331  FVector Pos = ActorTransform.GetTranslation() + (ActorRot.RotateVector(Offset));
332  CarlaSpectator->SetActorGlobalTransform(FTransform(ActorRot * Rotation, Pos, FVector(1,1,1)));
333 
334  return true;
335 }
336 
338 {
339  check(Episode != nullptr);
340  FCarlaActor* CarlaActor = Episode->FindCarlaActor(State.DatabaseId);
341  if(CarlaActor)
342  {
343  CarlaActor->SetTrafficLightState(static_cast<ETrafficLightState>(State.State));
344  UTrafficLightController* Controller = CarlaActor->GetTrafficLightController();
345  if(Controller)
346  {
347  Controller->SetElapsedTime(State.ElapsedTime);
348  ATrafficLightGroup* Group = Controller->GetGroup();
349  if (Group)
350  {
351  Group->SetFrozenGroup(State.IsFrozen);
352  }
353  }
354  return true;
355  }
356  return false;
357 }
358 
359 // set the animation for Vehicles
361 {
362  check(Episode != nullptr);
363  FCarlaActor *CarlaActor = Episode->FindCarlaActor(Vehicle.DatabaseId);
364  if (CarlaActor)
365  {
366  FVehicleControl Control;
367  Control.Throttle = Vehicle.Throttle;
368  Control.Steer = Vehicle.Steering;
369  Control.Brake = Vehicle.Brake;
370  Control.bHandBrake = Vehicle.bHandbrake;
371  Control.bReverse = (Vehicle.Gear < 0);
372  Control.Gear = Vehicle.Gear;
373  Control.bManualGearShift = false;
374  CarlaActor->ApplyControlToVehicle(Control, EVehicleInputPriority::User);
375  }
376 }
377 
378 // set the lights for vehicles
380 {
381  check(Episode != nullptr);
382  FCarlaActor * CarlaActor = Episode->FindCarlaActor(LightVehicle.DatabaseId);
383  if (CarlaActor)
384  {
385  carla::rpc::VehicleLightState LightState(LightVehicle.State);
386  CarlaActor->SetVehicleLightState(FVehicleLightState(LightState));
387  }
388 }
389 
391 {
392  check(Episode != nullptr);
393  UWorld* World = Episode->GetWorld();
394  if(World)
395  {
396  UCarlaLightSubsystem* CarlaLightSubsystem = World->GetSubsystem<UCarlaLightSubsystem>();
397  if (!CarlaLightSubsystem)
398  {
399  return;
400  }
401  auto* CarlaLight = CarlaLightSubsystem->GetLight(LightScene.LightId);
402  if (CarlaLight)
403  {
404  CarlaLight->SetLightIntensity(LightScene.Intensity);
405  CarlaLight->SetLightColor(LightScene.Color);
406  CarlaLight->SetLightOn(LightScene.bOn);
407  CarlaLight->SetLightType(static_cast<ELightType>(LightScene.Type));
408  }
409  }
410 }
411 
412 // set the animation for walkers
414 {
415  SetWalkerSpeed(Walker.DatabaseId, Walker.Speed);
416 }
417 
418 // replay finish
419 bool CarlaReplayerHelper::ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map<uint32_t, bool> &IsHero)
420 {
421  // set autopilot and physics to all AI vehicles
422  const FActorRegistry& Registry = Episode->GetActorRegistry();
423  for (auto& It : Registry)
424  {
425  FCarlaActor* CarlaActor = It.Value.Get();
426 
427  // enable physics only on vehicles
428  switch (CarlaActor->GetActorType())
429  {
430 
431  // vehicles
433  // check for hero
434  if (!(bIgnoreHero && IsHero[CarlaActor->GetActorId()]))
435  {
436  // stop all vehicles
437  SetActorSimulatePhysics(CarlaActor, true);
438  SetActorVelocity(CarlaActor, FVector(0, 0, 0));
439  FVehicleControl Control;
440  Control.Throttle = 0.0f;
441  Control.Steer = 0.0f;
442  Control.Brake = 0.0f;
443  Control.bHandBrake = false;
444  Control.bReverse = false;
445  Control.Gear = 1;
446  Control.bManualGearShift = false;
447  CarlaActor->ApplyControlToVehicle(Control, EVehicleInputPriority::User);
448  }
449  break;
450 
451  // walkers
453  // stop walker
454  SetWalkerSpeed(CarlaActor->GetActorId(), 0.0f);
455  break;
456  }
457  }
458  return true;
459 }
460 
461 void CarlaReplayerHelper::SetActorVelocity(FCarlaActor *CarlaActor, FVector Velocity)
462 {
463  if (!CarlaActor)
464  {
465  return;
466  }
467  CarlaActor->SetActorTargetVelocity(Velocity);
468 }
469 
470 // set the animation speed for walkers
471 void CarlaReplayerHelper::SetWalkerSpeed(uint32_t ActorId, float Speed)
472 {
473  check(Episode != nullptr);
474  FCarlaActor * CarlaActor = Episode->FindCarlaActor(ActorId);
475  if (!CarlaActor)
476  {
477  return;
478  }
479  FWalkerControl Control;
480  Control.Speed = Speed;
481  CarlaActor->ApplyControlToWalker(Control);
482 }
483 
485 {
486  check(Episode != nullptr);
487  auto World = Episode->GetWorld();
488  for (TActorIterator<AStaticMeshActor> It(World); It; ++It)
489  {
490  auto Actor = *It;
491  check(Actor != nullptr);
492  auto MeshComponent = Actor->GetStaticMeshComponent();
493  check(MeshComponent != nullptr);
494  if (MeshComponent->Mobility == EComponentMobility::Movable)
495  {
496  Actor->Destroy();
497  }
498  }
499 }
UCarlaLight * GetLight(int Id)
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
Definition: CarlaEpisode.h:152
virtual ECarlaServerResponse SetVehicleLightState(const FVehicleLightState &)
Definition: CarlaActor.h:266
auto end() const noexcept
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
bool DestroyActor(AActor *Actor)
Definition: CarlaEpisode.h:223
FCarlaActor * FindTrafficLightAt(FVector Location)
void SetActorVelocity(FCarlaActor *CarlaActor, FVector Velocity)
void ProcessReplayerLightVehicle(CarlaRecorderLightVehicle LightVehicle)
void ProcessReplayerAnimVehicle(CarlaRecorderAnimVehicle Vehicle)
void SetElapsedTime(float InElapsedTime)
EActorAttributeType
List of valid types for actor attributes.
AActor * GetActor()
Definition: CarlaActor.h:90
EAttachmentType
Definition: ActorAttacher.h:20
bool SetActorSimulatePhysics(FCarlaActor *CarlaActor, bool bEnabled)
void SetWalkerSpeed(uint32_t ActorId, float Speed)
auto begin() const noexcept
bool ProcessReplayerStateTrafficLight(CarlaRecorderStateTrafficLight State)
Class which implements the state changing of traffic lights.
bool SetActorAutopilot(FCarlaActor *CarlaActor, bool bEnabled, bool bKeepState=false)
void PutActorToSleep(carla::rpc::ActorId ActorId)
Definition: CarlaEpisode.h:246
void OnActorSpawned(const FCarlaActor &CarlaActor)
virtual ECarlaServerResponse ApplyControlToVehicle(const FVehicleControl &, const EVehicleInputPriority &)
Definition: CarlaActor.h:281
virtual ECarlaServerResponse SetTrafficLightState(const ETrafficLightState &)
Definition: CarlaActor.h:320
geom::Location Location
Definition: rpc/Location.h:14
const FActorRegistry & GetActorRegistry() const
Definition: CarlaEpisode.h:134
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
ECarlaServerResponse
carla::SharedPtr< cc::Actor > Actor
void ProcessReplayerLightScene(CarlaRecorderLightScene LightScene)
TPair< EActorSpawnResultStatus, FCarlaActor * > SpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription, FCarlaActor::IdType DesiredId=0)
Spawns an actor based on ActorDescription at Transform.
bool ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map< uint32_t, bool > &IsHero)
void SetAttachmentType(carla::rpc::AttachmentType InAttachmentType)
Definition: CarlaActor.h:140
std::pair< int, uint32_t > ProcessReplayerEventAdd(FVector Location, FVector Rotation, CarlaRecorderActorDescription Description, uint32_t DesiredId, bool bIgnoreHero, bool ReplaySensors)
virtual ECarlaServerResponse ApplyControlToWalker(const FWalkerControl &)
Definition: CarlaActor.h:360
std::vector< CarlaRecorderActorAttribute > Attributes
bool Contains(uint32 Id) const
Definition: ActorRegistry.h:64
bool ProcessReplayerPosition(CarlaRecorderPosition Pos1, CarlaRecorderPosition Pos2, double Per, double DeltaTime)
A description of a Carla Actor with all its variation.
uint32_t ActorId
Definition: ActorId.h:14
bool ProcessReplayerEventParent(uint32_t ChildId, uint32_t ParentId)
virtual ECarlaServerResponse SetActorAutopilot(bool, bool bKeepState=false)
Definition: CarlaActor.h:292
ECarlaServerResponse SetActorTargetVelocity(const FVector &Velocity)
Definition: CarlaActor.cpp:387
geom::Rotation Rotation
Definition: rpc/Transform.h:14
APawn * GetSpectatorPawn() const
Definition: CarlaEpisode.h:123
void AttachActors(AActor *Child, AActor *Parent, EAttachmentType InAttachmentType=EAttachmentType::Rigid)
Attach Child to Parent.
void AddChildren(IdType ChildId)
Definition: CarlaActor.h:125
virtual UTrafficLightController * GetTrafficLightController()
Definition: CarlaActor.h:330
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Definition: CarlaStatics.h:100
FTransform GetActorGlobalTransform() const
Definition: CarlaActor.cpp:196
uint32 UId
UId of the definition in which this description was based.
void SetFrozenGroup(bool InFreeze)
bool IsDormant() const
Definition: CarlaActor.h:70
virtual ECarlaServerResponse SetActorSimulatePhysics(bool bEnabled)
Definition: CarlaActor.cpp:557
void SetParent(IdType InParentId)
Definition: CarlaActor.h:115
UCarlaEpisode * Episode
IdType GetActorId() const
Definition: CarlaActor.h:80
bool ProcessReplayerEventDel(uint32_t DatabaseId)
std::pair< int, FCarlaActor * > TryToCreateReplayerActor(FVector &Location, FVector &Rotation, FActorDescription &ActorDesc, uint32_t DesiredId, bool SpawnSensors)
ATrafficLightGroup * GetGroup()
Maps a controller from OpenDrive.
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
An actor attribute, may be an intrinsic (non-modifiable) attribute of the actor or an user-defined ac...
bool SetCameraPosition(uint32_t Id, FVector Offset, FQuat Rotation)
void SetActorGlobalTransform(const FTransform &Transform, ETeleportType Teleport=ETeleportType::TeleportPhysics)
Definition: CarlaActor.cpp:330
FVector GetActorGlobalLocation() const
Definition: CarlaActor.cpp:237
ActorType GetActorType() const
Definition: CarlaActor.h:85
A view over an actor and its properties.
Definition: CarlaActor.h:23
void ProcessReplayerAnimWalker(CarlaRecorderAnimWalker Walker)