CARLA
FrameData.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022 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 "FrameData.h"
10 #include "Carla/Game/CarlaEngine.h"
12 
13 
14 void FFrameData::GetFrameData(UCarlaEpisode *ThisEpisode, bool bAdditionalData, bool bIncludeActorsAgain)
15 {
16  Episode = ThisEpisode;
17  // PlatformTime.UpdateTime();
18  const FActorRegistry &Registry = Episode->GetActorRegistry();
19 
20  if (bIncludeActorsAgain)
21  {
23  }
24 
25  // through all actors in registry
26  for (auto It = Registry.begin(); It != Registry.end(); ++It)
27  {
28  FCarlaActor* View = It.Value().Get();
29 
30  switch (View->GetActorType())
31  {
32  // save the transform for props
35  AddActorPosition(View);
36  break;
37 
38  // save the transform of all vehicles
40  AddActorPosition(View);
41  AddVehicleAnimation(View);
42  AddVehicleLight(View);
43  if (bAdditionalData)
44  {
45  AddActorKinematics(View);
46  }
47  break;
48 
49  // save the transform of all walkers
51  AddActorPosition(View);
52  AddWalkerAnimation(View);
53  if (bAdditionalData)
54  {
55  AddActorKinematics(View);
56  }
57  break;
58 
59  // save the state of each traffic light
62  break;
63  }
64  }
66 }
67 
69  UCarlaEpisode *ThisEpisode,
70  std::unordered_map<uint32_t, uint32_t>& MappedId)
71 {
72 
74  {
75  uint32_t OldId = EventAdd.DatabaseId;
76  // Todo: check memory corruption of EventAdd.DatabaseId
77  auto Result = ProcessReplayerEventAdd(
78  EventAdd.Location,
79  EventAdd.Rotation,
80  EventAdd.Description,
81  EventAdd.DatabaseId,
82  false,
83  true,
84  MappedId);
85  switch (Result.first)
86  {
87  // actor not created
88  case 0:
89  UE_LOG(LogCarla, Log, TEXT("actor could not be created"));
90  break;
91 
92  // actor created but with different id
93  case 1:
94  // mapping id (recorded Id is a new Id in replayer)
95  MappedId[OldId] = Result.second;
96  UE_LOG(LogCarla, Log, TEXT("actor created"));
97  break;
98 
99  // actor reused from existing
100  case 2:
101  // mapping id (say desired Id is mapped to what)
102  MappedId[OldId] = Result.second;
103  UE_LOG(LogCarla, Log, TEXT("actor reused"));
104  break;
105  }
106  }
107 
109  {
110  ProcessReplayerEventDel(MappedId[EventDel.DatabaseId]);
111  MappedId.erase(EventDel.DatabaseId);
112  }
113 
115  {
117  auto NewId = MappedId.find(Pos.DatabaseId);
118  if (NewId != MappedId.end())
119  {
120  Pos.DatabaseId = NewId->second;
121  ProcessReplayerPosition(Pos, Pos, 0.0, 0.0);
122  }
123  }
124 
126  {
127  CarlaRecorderStateTrafficLight StateTrafficLight = State;
128  StateTrafficLight.DatabaseId = MappedId[StateTrafficLight.DatabaseId];
129  ProcessReplayerStateTrafficLight(StateTrafficLight);
130  }
131 
133  {
135  Vehicle.DatabaseId = MappedId[Vehicle.DatabaseId];
137  }
138 
140  {
142  Walker.DatabaseId = MappedId[Walker.DatabaseId];
144  }
145 
146  for (const CarlaRecorderLightVehicle &LightVehicle : LightVehicles.GetLightVehicles())
147  {
148  CarlaRecorderLightVehicle Light = LightVehicle;
149  Light.DatabaseId = MappedId[Light.DatabaseId];
151  }
152 
153  for (const CarlaRecorderLightScene &Light : LightScenes.GetLights())
154  {
156  }
157 
158  SetFrameCounter();
159 }
160 
162 {
163  EventsAdd.Clear();
164  EventsDel.Clear();
166  Collisions.Clear();
167  Positions.Clear();
168  States.Clear();
169  Vehicles.Clear();
170  Walkers.Clear();
172  LightScenes.Clear();
173  Kinematics.Clear();
179 }
180 
181 void FFrameData::Write(std::ostream& OutStream)
182 {
183  EventsAdd.Write(OutStream);
184  EventsDel.Write(OutStream);
185  EventsParent.Write(OutStream);
186  Positions.Write(OutStream);
187  States.Write(OutStream);
188  Vehicles.Write(OutStream);
189  Walkers.Write(OutStream);
190  LightVehicles.Write(OutStream);
191  LightScenes.Write(OutStream);
192  TrafficLightTimes.Write(OutStream);
193  FrameCounter.Write(OutStream);
194 }
195 void FFrameData::Read(std::istream& InStream)
196 {
197  Clear();
198  while(!InStream.eof())
199  {
200  Header header;
201  ReadValue<char>(InStream, header.Id);
202  ReadValue<uint32_t>(InStream, header.Size);
203  switch (header.Id)
204  {
205  // events add
206  case static_cast<char>(CarlaRecorderPacketId::EventAdd):
207  EventsAdd.Read(InStream);
208  break;
209 
210  // events del
211  case static_cast<char>(CarlaRecorderPacketId::EventDel):
212  EventsDel.Read(InStream);
213  break;
214 
215  // events parent
216  case static_cast<char>(CarlaRecorderPacketId::EventParent):
217  EventsParent.Read(InStream);
218  break;
219 
220  // positions
221  case static_cast<char>(CarlaRecorderPacketId::Position):
222  Positions.Read(InStream);
223  break;
224 
225  // states
226  case static_cast<char>(CarlaRecorderPacketId::State):
227  States.Read(InStream);
228  break;
229 
230  // vehicle animation
231  case static_cast<char>(CarlaRecorderPacketId::AnimVehicle):
232  Vehicles.Read(InStream);
233  break;
234 
235  // walker animation
236  case static_cast<char>(CarlaRecorderPacketId::AnimWalker):
237  Walkers.Read(InStream);
238  break;
239 
240  // vehicle light animation
241  case static_cast<char>(CarlaRecorderPacketId::VehicleLight):
242  LightVehicles.Read(InStream);
243  break;
244 
245  // scene lights animation
246  case static_cast<char>(CarlaRecorderPacketId::SceneLight):
247  LightScenes.Read(InStream);
248  break;
249 
250  case static_cast<char>(CarlaRecorderPacketId::FrameCounter):
251  FrameCounter.Read(InStream);
252  break;
253 
254  // unknown packet, just skip
255  default:
256  // skip packet
257  InStream.seekg(header.Size, std::ios::cur);
258  break;
259 
260  }
261  }
262 }
263 
265  uint32_t DatabaseId,
266  uint8_t Type,
267  const FTransform &Transform,
268  FActorDescription ActorDescription,
269  bool bAddOtherRelatedInfo)
270 {
271  CarlaRecorderActorDescription Description;
272  Description.UId = ActorDescription.UId;
273  Description.Id = ActorDescription.Id;
274 
275  // attributes
276  Description.Attributes.reserve(ActorDescription.Variations.Num());
277  for (const auto &item : ActorDescription.Variations)
278  {
280  Attr.Type = static_cast<uint8_t>(item.Value.Type);
281  Attr.Id = item.Value.Id;
282  Attr.Value = item.Value.Value;
283  // check for empty attributes
284  if (!Attr.Id.IsEmpty())
285  {
286  Description.Attributes.emplace_back(std::move(Attr));
287  }
288  }
289 
290  // recorder event
291  CarlaRecorderEventAdd RecEvent
292  {
293  DatabaseId,
294  Type,
295  Transform.GetTranslation(),
296  Transform.GetRotation().Euler(),
297  std::move(Description)
298  };
299  AddEvent(std::move(RecEvent));
300 
301  if (!bAddOtherRelatedInfo)
302  {
303  return;
304  }
305 
306  // Other events related to spawning actors
307  FCarlaActor* CarlaActor = Episode->FindCarlaActor(DatabaseId);
308  if (!CarlaActor)
309  {
310  return;
311  }
312 
313  // check if it is a vehicle to get initial physics control
314  ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(CarlaActor->GetActor());
315  if (Vehicle)
316  {
317  AddPhysicsControl(*Vehicle);
318  }
319 
320  ATrafficLightBase* TrafficLight = Cast<ATrafficLightBase>(CarlaActor->GetActor());
321  if (TrafficLight)
322  {
323  AddTrafficLightTime(*TrafficLight);
324  }
325 
326  ATrafficSignBase* TrafficSign = Cast<ATrafficSignBase>(CarlaActor->GetActor());
327  if (TrafficSign)
328  {
329  // Trigger volume in global coordinates
330  AddTriggerVolume(*TrafficSign);
331  }
332  else
333  {
334  // Bounding box in local coordinates
335  AddActorBoundingBox(CarlaActor);
336  }
337 }
338 
339 
341 {
342  check(CarlaActor != nullptr);
343 
344  FTransform Transform = CarlaActor->GetActorGlobalTransform();
345  // get position of the vehicle
347  {
348  CarlaActor->GetActorId(),
349  Transform.GetLocation(),
350  Transform.GetRotation().Euler()
351  });
352 }
353 
354 
356 {
357  check(CarlaActor != nullptr);
358 
359  if (CarlaActor->IsPendingKill())
360  {
361  return;
362  }
363 
364  FVehicleControl Control;
365  CarlaActor->GetVehicleControl(Control);
366 
367  // save
369  Record.DatabaseId = CarlaActor->GetActorId();
370  Record.Steering = Control.Steer;
371  Record.Throttle = Control.Throttle;
372  Record.Brake = Control.Brake;
373  Record.bHandbrake = Control.bHandBrake;
374  Record.Gear = Control.Gear;
375  AddAnimVehicle(Record);
376 }
377 
379 {
380  check(CarlaActor != nullptr);
381 
382  if (!CarlaActor->IsPendingKill())
383  {
384  FWalkerControl Control;
385  CarlaActor->GetWalkerControl(Control);
387  {
388  CarlaActor->GetActorId(),
389  Control.Speed
390  });
391  }
392 }
393 
395 {
396  check(CarlaActor != nullptr);
397 
398  ETrafficLightState LightState = CarlaActor->GetTrafficLightState();
399  UTrafficLightController* Controller = CarlaActor->GetTrafficLightController();
400  if (Controller)
401  {
402  ATrafficLightGroup* Group = Controller->GetGroup();
403  if (Group)
404  {
406  {
407  CarlaActor->GetActorId(),
408  Group->IsFrozen(),
409  Controller->GetElapsedTime(),
410  static_cast<char>(LightState)
411  });
412  }
413  }
414 }
415 
417 {
418  check(CarlaActor != nullptr);
419 
420  FVehicleLightState LightState;
421  CarlaActor->GetVehicleLightState(LightState);
422  CarlaRecorderLightVehicle LightVehicle;
423  LightVehicle.DatabaseId = CarlaActor->GetActorId();
424  LightVehicle.State = carla::rpc::VehicleLightState(LightState).light_state;
425  AddLightVehicle(LightVehicle);
426 }
427 
429 {
430  check(CarlaActor != nullptr);
431 
432  FVector Velocity, AngularVelocity;
433  constexpr float TO_METERS = 1e-2;
434  Velocity = TO_METERS* CarlaActor->GetActorVelocity();
435  AngularVelocity = CarlaActor->GetActorAngularVelocity();
436  CarlaRecorderKinematics Kinematic =
437  {
438  CarlaActor->GetActorId(),
439  Velocity,
440  AngularVelocity
441  };
442  AddKinematics(Kinematic);
443 }
445 {
446  check(CarlaActor != nullptr);
447 
448  const auto &Box = CarlaActor->GetActorInfo()->BoundingBox;
450  {
451  CarlaActor->GetActorId(),
452  {Box.Origin, Box.Extent}
453  };
454 
455  AddBoundingBox(BoundingBox);
456 }
457 
459 {
460  TArray<UBoxComponent*> Triggers = TrafficSign.GetTriggerVolumes();
461  if(!Triggers.Num())
462  {
463  return;
464  }
465  UBoxComponent* Trigger = Triggers.Top();
466  auto VolumeOrigin = Trigger->GetComponentLocation();
467  auto VolumeExtent = Trigger->GetScaledBoxExtent();
469  {
471  {VolumeOrigin, VolumeExtent}
472  };
473  TriggerVolumes.Add(TriggerVolume);
474 }
475 
477 {
479  Control.DatabaseId = Episode->GetActorRegistry().FindCarlaActor(&Vehicle)->GetActorId();
481  PhysicsControls.Add(Control);
482 }
483 
485 {
486  auto DatabaseId = Episode->GetActorRegistry().FindCarlaActor(&TrafficLight)->GetActorId();
488  DatabaseId,
489  TrafficLight.GetGreenTime(),
490  TrafficLight.GetYellowTime(),
491  TrafficLight.GetRedTime()
492  };
494 }
495 
496 
498 {
499  Positions.Add(Position);
500 }
501 
503 {
504  EventsAdd.Add(std::move(Event));
505 }
506 
508 {
509  EventsDel.Add(std::move(Event));
510 }
511 
513 {
514  EventsParent.Add(std::move(Event));
515 }
516 
517 void FFrameData::AddCollision(AActor *Actor1, AActor *Actor2)
518 {
520 
521  // // some inits
522  // Collision.Id = NextCollisionId++;
523  // Collision.IsActor1Hero = false;
524  // Collision.IsActor2Hero = false;
525 
526  // // check actor 1
527  // FCarlaActor *FoundActor1 = Episode->GetActorRegistry().FindCarlaActor(Actor1);
528  // if (FoundActor1 != nullptr) {
529  // if (FoundActor1->GetActorInfo() != nullptr)
530  // {
531  // auto Role = FoundActor1->GetActorInfo()->Description.Variations.Find("role_name");
532  // if (Role != nullptr)
533  // Collision.IsActor1Hero = (Role->Value == "hero");
534  // }
535  // Collision.DatabaseId1 = FoundActor1->GetActorId();
536  // }
537  // else {
538  // Collision.DatabaseId1 = uint32_t(-1); // actor1 is not a registered Carla actor
539  // }
540 
541  // // check actor 2
542  // FCarlaActor *FoundActor2 = Episode->GetActorRegistry().FindCarlaActor(Actor2);
543  // if (FoundActor2 != nullptr) {
544  // if (FoundActor2->GetActorInfo() != nullptr)
545  // {
546  // auto Role = FoundActor2->GetActorInfo()->Description.Variations.Find("role_name");
547  // if (Role != nullptr)
548  // Collision.IsActor2Hero = (Role->Value == "hero");
549  // }
550  // Collision.DatabaseId2 = FoundActor2->GetActorId();
551  // }
552  // else {
553  // Collision.DatabaseId2 = uint32_t(-1); // actor2 is not a registered Carla actor
554  // }
555 
556  Collisions.Add(std::move(Collision));
557 }
558 
560 {
561  States.Add(State);
562 }
563 
565 {
566  Vehicles.Add(Vehicle);
567 }
568 
570 {
571  Walkers.Add(Walker);
572 }
573 
575 {
576  LightVehicles.Add(LightVehicle);
577 }
578 
579 void FFrameData::AddEventLightSceneChanged(const UCarlaLight* Light)
580 {
581  CarlaRecorderLightScene LightScene =
582  {
583  Light->GetId(),
584  Light->GetLightIntensity(),
585  Light->GetLightColor(),
586  Light->GetLightOn(),
587  static_cast<uint8>(Light->GetLightType())
588  };
589 
590  LightScenes.Add(LightScene);
591 }
592 
594 {
595  Kinematics.Add(ActorKinematics);
596 }
597 
599 {
600  BoundingBoxes.Add(ActorBoundingBox);
601 }
602 
604 {
606 }
607 
608 // create or reuse an actor for replaying
609 std::pair<int, FCarlaActor*> FFrameData::CreateOrReuseActor(
610  FVector &Location,
611  FVector &Rotation,
612  FActorDescription &ActorDesc,
613  uint32_t DesiredId,
614  bool SpawnSensors,
615  std::unordered_map<uint32_t, uint32_t>& MappedId)
616 {
617  check(Episode != nullptr);
618 
619  // check type of actor we need
620  if (ActorDesc.Id.StartsWith("traffic."))
621  {
622  FCarlaActor* CarlaActor = FindTrafficLightAt(Location);
623  if (CarlaActor != nullptr)
624  {
625  // reuse that actor
626  UE_LOG(LogCarla, Log, TEXT("TrafficLight found"));
627  return std::pair<int, FCarlaActor*>(2, CarlaActor);
628  }
629  else
630  {
631  // actor not found
632  UE_LOG(LogCarla, Log, TEXT("TrafficLight not found"));
633  return std::pair<int, FCarlaActor*>(0, nullptr);
634  }
635  }
636  else if (SpawnSensors || !ActorDesc.Id.StartsWith("sensor."))
637  {
638  // check if an actor of that type already exist with same id
639  if (Episode->GetActorRegistry().Contains(DesiredId))
640  {
641  auto* CarlaActor = Episode->FindCarlaActor(DesiredId);
642  const FActorDescription *desc = &CarlaActor->GetActorInfo()->Description;
643  if (desc->Id == ActorDesc.Id)
644  {
645  // we don't need to create, actor of same type already exist
646  // relocate
647  FRotator Rot = FRotator::MakeFromEuler(Rotation);
648  FTransform Trans2(Rot, Location, FVector(1, 1, 1));
649  CarlaActor->SetActorGlobalTransform(Trans2);
650  return std::pair<int, FCarlaActor*>(2, CarlaActor);
651  }
652  }
653  else if (MappedId.find(DesiredId) != MappedId.end() && Episode->GetActorRegistry().Contains(MappedId[DesiredId]))
654  {
655  auto* CarlaActor = Episode->FindCarlaActor(MappedId[DesiredId]);
656  const FActorDescription *desc = &CarlaActor->GetActorInfo()->Description;
657  if (desc->Id == ActorDesc.Id)
658  {
659  // we don't need to create, actor of same type already exist
660  // relocate
661  FRotator Rot = FRotator::MakeFromEuler(Rotation);
662  FTransform Trans2(Rot, Location, FVector(1, 1, 1));
663  CarlaActor->SetActorGlobalTransform(Trans2);
664  return std::pair<int, FCarlaActor*>(2, CarlaActor);
665  }
666  }
667  // create new actor
668  // create the transform
669  FRotator Rot = FRotator::MakeFromEuler(Rotation);
670  FTransform Trans(Rot, FVector(0, 0, 100000), FVector(1, 1, 1));
671  // create as new actor
672  TPair<EActorSpawnResultStatus, FCarlaActor*> Result = Episode->SpawnActorWithInfo(Trans, ActorDesc, DesiredId);
673  if (Result.Key == EActorSpawnResultStatus::Success)
674  {
675  // relocate
676  FTransform Trans2(Rot, Location, FVector(1, 1, 1));
677  Result.Value->SetActorGlobalTransform(Trans2);
678  ALargeMapManager * LargeMapManager = UCarlaStatics::GetLargeMapManager(Episode->GetWorld());
679  if (LargeMapManager)
680  {
681  LargeMapManager->OnActorSpawned(*Result.Value);
682  }
683  return std::pair<int, FCarlaActor*>(1, Result.Value);
684  }
685  else
686  {
687  UE_LOG(LogCarla, Log, TEXT("Actor could't be created"));
688  return std::pair<int, FCarlaActor*>(0, Result.Value);
689  }
690  }
691  else
692  {
693  // actor ignored
694  return std::pair<int, FCarlaActor*>(0, nullptr);
695  }
696 }
697 
698 // replay event for creating actor
699 std::pair<int, uint32_t> FFrameData::ProcessReplayerEventAdd(
700  FVector Location,
701  FVector Rotation,
702  CarlaRecorderActorDescription Description,
703  uint32_t DesiredId,
704  bool bIgnoreHero,
705  bool ReplaySensors,
706  std::unordered_map<uint32_t, uint32_t>& MappedId)
707 {
708  check(Episode != nullptr);
709  FActorDescription ActorDesc;
710  bool IsHero = false;
711 
712  // prepare actor description
713  ActorDesc.UId = Description.UId;
714  ActorDesc.Id = Description.Id;
715  for (const auto &Item : Description.Attributes)
716  {
717  FActorAttribute Attr;
718  Attr.Type = static_cast<EActorAttributeType>(Item.Type);
719  Attr.Id = Item.Id;
720  Attr.Value = Item.Value;
721  ActorDesc.Variations.Add(Attr.Id, std::move(Attr));
722  // check for hero
723  if (Item.Id == "role_name" && Item.Value == "hero")
724  IsHero = true;
725  }
726 
727  auto result = CreateOrReuseActor(
728  Location,
729  Rotation,
730  ActorDesc,
731  DesiredId,
732  ReplaySensors,
733  MappedId);
734 
735  if (result.first != 0)
736  {
737  // disable physics and autopilot on vehicles
738  if (result.second->GetActorType() == FCarlaActor::ActorType::Vehicle)
739  {
740  // ignore hero ?
741  if (!(bIgnoreHero && IsHero))
742  {
743  // disable physics
744  SetActorSimulatePhysics(result.second, false);
745  // disable autopilot
746  // SetActorAutopilot(result.second, false, false);
747  }
748  else
749  {
750  // reenable physics just in case
751  SetActorSimulatePhysics(result.second, true);
752  }
753  }
754  return std::make_pair(result.first, result.second->GetActorId());
755  }
756  return std::make_pair(result.first, 0);
757 }
758 
759 // replay event for removing actor
760 bool FFrameData::ProcessReplayerEventDel(uint32_t DatabaseId)
761 {
762  check(Episode != nullptr);
763  FCarlaActor* CarlaActor = Episode->FindCarlaActor(DatabaseId);
764  if (CarlaActor == nullptr)
765  {
766  UE_LOG(LogCarla, Log, TEXT("Actor %d not found to destroy"), DatabaseId);
767  return false;
768  }
769  Episode->DestroyActor(CarlaActor->GetActorId());
770  return true;
771 }
772 
773 // replay event for parenting actors
774 bool FFrameData::ProcessReplayerEventParent(uint32_t ChildId, uint32_t ParentId)
775 {
776  check(Episode != nullptr);
777  FCarlaActor * Child = Episode->FindCarlaActor(ChildId);
778  FCarlaActor * Parent = Episode->FindCarlaActor(ParentId);
779  if(!Child)
780  {
781  UE_LOG(LogCarla, Log, TEXT("Parenting Child actors not found"));
782  return false;
783  }
784  if(!Parent)
785  {
786  UE_LOG(LogCarla, Log, TEXT("Parenting Parent actors not found"));
787  return false;
788  }
789  Child->SetParent(ParentId);
791  Parent->AddChildren(Child->GetActorId());
792  if(!Parent->IsDormant())
793  {
794  if(!Child->IsDormant())
795  {
797  Child->GetActor(),
798  Parent->GetActor(),
800  }
801  }
802  else
803  {
805  }
806  return true;
807 }
808 
809 // reposition actors
811 {
812  check(Episode != nullptr);
813  FCarlaActor* CarlaActor = Episode->FindCarlaActor(Pos1.DatabaseId);
814  FVector Location;
815  FRotator Rotation;
816  if(CarlaActor)
817  {
818  // check to assign first position or interpolate between both
819  if (Per == 0.0)
820  {
821  // assign position 1
822  Location = FVector(Pos1.Location);
823  Rotation = FRotator::MakeFromEuler(Pos1.Rotation);
824  }
825  else
826  {
827  // interpolate positions
828  Location = FMath::Lerp(FVector(Pos1.Location), FVector(Pos2.Location), Per);
829  Rotation = FMath::Lerp(FRotator::MakeFromEuler(Pos1.Rotation), FRotator::MakeFromEuler(Pos2.Rotation), Per);
830  }
831  // set new transform
832  FTransform Trans(Rotation, Location, FVector(1, 1, 1));
833  CarlaActor->SetActorGlobalTransform(Trans, ETeleportType::None);
834  return true;
835  }
836  return false;
837 }
838 
839 // reposition the camera
840 bool FFrameData::SetCameraPosition(uint32_t Id, FVector Offset, FQuat Rotation)
841 {
842  check(Episode != nullptr);
843 
844  // get the actor to follow
845  FCarlaActor* CarlaActor = Episode->FindCarlaActor(Id);
846  if (!CarlaActor)
847  return false;
848  // get specator pawn
849  APawn *Spectator = Episode->GetSpectatorPawn();
850  if (!Spectator)
851  return false;
852 
853  FCarlaActor* CarlaSpectator = Episode->FindCarlaActor(Spectator);
854  if (!CarlaSpectator)
855  return false;
856 
857  FTransform ActorTransform = CarlaActor->GetActorGlobalTransform();
858  // set the new position
859  FQuat ActorRot = ActorTransform.GetRotation();
860  FVector Pos = ActorTransform.GetTranslation() + (ActorRot.RotateVector(Offset));
861  CarlaSpectator->SetActorGlobalTransform(FTransform(ActorRot * Rotation, Pos, FVector(1,1,1)));
862 
863  return true;
864 }
865 
867 {
868  check(Episode != nullptr);
869  FCarlaActor* CarlaActor = Episode->FindCarlaActor(State.DatabaseId);
870  if(CarlaActor)
871  {
872  CarlaActor->SetTrafficLightState(static_cast<ETrafficLightState>(State.State));
873  UTrafficLightController* Controller = CarlaActor->GetTrafficLightController();
874  if(Controller)
875  {
876  Controller->SetElapsedTime(State.ElapsedTime);
877  ATrafficLightGroup* Group = Controller->GetGroup();
878  if (Group)
879  {
880  Group->SetFrozenGroup(State.IsFrozen);
881  }
882  }
883  return true;
884  }
885  return false;
886 }
887 
888 // set the animation for Vehicles
890 {
891  check(Episode != nullptr);
892  FCarlaActor *CarlaActor = Episode->FindCarlaActor(Vehicle.DatabaseId);
893  if (CarlaActor)
894  {
895  FVehicleControl Control;
896  Control.Throttle = Vehicle.Throttle;
897  Control.Steer = Vehicle.Steering;
898  Control.Brake = Vehicle.Brake;
899  Control.bHandBrake = Vehicle.bHandbrake;
900  Control.bReverse = (Vehicle.Gear < 0);
901  Control.Gear = Vehicle.Gear;
902  Control.bManualGearShift = false;
903  CarlaActor->ApplyControlToVehicle(Control, EVehicleInputPriority::User);
904  }
905 }
906 
907 // set the lights for vehicles
909 {
910  check(Episode != nullptr);
911  FCarlaActor * CarlaActor = Episode->FindCarlaActor(LightVehicle.DatabaseId);
912  if (CarlaActor)
913  {
914  carla::rpc::VehicleLightState LightState(LightVehicle.State);
915  CarlaActor->SetVehicleLightState(FVehicleLightState(LightState));
916  }
917 }
918 
920 {
921  check(Episode != nullptr);
922  UWorld* World = Episode->GetWorld();
923  if(World)
924  {
925  UCarlaLightSubsystem* CarlaLightSubsystem = World->GetSubsystem<UCarlaLightSubsystem>();
926  if (!CarlaLightSubsystem)
927  {
928  return;
929  }
930  auto* CarlaLight = CarlaLightSubsystem->GetLight(LightScene.LightId);
931  if (CarlaLight)
932  {
933  CarlaLight->SetLightIntensity(LightScene.Intensity);
934  CarlaLight->SetLightColor(LightScene.Color);
935  CarlaLight->SetLightOn(LightScene.bOn);
936  CarlaLight->SetLightType(static_cast<ELightType>(LightScene.Type));
937  }
938  }
939 }
940 
941 // set the animation for walkers
943 {
944  SetWalkerSpeed(Walker.DatabaseId, Walker.Speed);
945 }
946 
947 // replay finish
948 bool FFrameData::ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map<uint32_t, bool> &IsHero)
949 {
950  // set autopilot and physics to all AI vehicles
951  const FActorRegistry& Registry = Episode->GetActorRegistry();
952  for (auto& It : Registry)
953  {
954  FCarlaActor* CarlaActor = It.Value.Get();
955 
956  // enable physics only on vehicles
957  switch (CarlaActor->GetActorType())
958  {
959 
960  // vehicles
962  // check for hero
963  if (!(bIgnoreHero && IsHero[CarlaActor->GetActorId()]))
964  {
965  // stop all vehicles
966  SetActorSimulatePhysics(CarlaActor, true);
967  SetActorVelocity(CarlaActor, FVector(0, 0, 0));
968  FVehicleControl Control;
969  Control.Throttle = 0.0f;
970  Control.Steer = 0.0f;
971  Control.Brake = 0.0f;
972  Control.bHandBrake = false;
973  Control.bReverse = false;
974  Control.Gear = 1;
975  Control.bManualGearShift = false;
976  CarlaActor->ApplyControlToVehicle(Control, EVehicleInputPriority::User);
977  }
978  break;
979 
980  // walkers
982  // stop walker
983  SetWalkerSpeed(CarlaActor->GetActorId(), 0.0f);
984  break;
985  }
986  }
987  return true;
988 }
989 
990 void FFrameData::SetActorVelocity(FCarlaActor *CarlaActor, FVector Velocity)
991 {
992  if (!CarlaActor)
993  {
994  return;
995  }
996  CarlaActor->SetActorTargetVelocity(Velocity);
997 }
998 
999 // set the animation speed for walkers
1000 void FFrameData::SetWalkerSpeed(uint32_t ActorId, float Speed)
1001 {
1002  check(Episode != nullptr);
1003  FCarlaActor * CarlaActor = Episode->FindCarlaActor(ActorId);
1004  if (!CarlaActor)
1005  {
1006  return;
1007  }
1008  FWalkerControl Control;
1009  Control.Speed = Speed;
1010  CarlaActor->ApplyControlToWalker(Control);
1011 }
1012 
1013 // enable / disable physics for an actor
1014 bool FFrameData::SetActorSimulatePhysics(FCarlaActor* CarlaActor, bool bEnabled)
1015 {
1016  if (!CarlaActor)
1017  {
1018  return false;
1019  }
1020  ECarlaServerResponse Response =
1021  CarlaActor->SetActorSimulatePhysics(bEnabled);
1022  if (Response != ECarlaServerResponse::Success)
1023  {
1024  return false;
1025  }
1026  return true;
1027 }
1028 
1030 {
1032 }
1033 
1035 {
1036  check(Episode != nullptr);
1037  auto World = Episode->GetWorld();
1038  check(World != nullptr);
1039 
1040  // get its position (truncated as int's)
1041  int x = static_cast<int>(Location.X);
1042  int y = static_cast<int>(Location.Y);
1043  int z = static_cast<int>(Location.Z);
1044 
1045  const FActorRegistry &Registry = Episode->GetActorRegistry();
1046  // through all actors in registry
1047  for (auto It = Registry.begin(); It != Registry.end(); ++It)
1048  {
1049  FCarlaActor* CarlaActor = It.Value().Get();
1051  {
1052  FVector vec = CarlaActor->GetActorGlobalLocation();
1053  int x2 = static_cast<int>(vec.X);
1054  int y2 = static_cast<int>(vec.Y);
1055  int z2 = static_cast<int>(vec.Z);
1056  if ((x2 == x) && (y2 == y) && (z2 == z))
1057  {
1058  // actor found
1059  return CarlaActor;
1060  }
1061  }
1062  }
1063  // actor not found
1064  return nullptr;
1065 }
1066 
1068 {
1069  // registring all existing actors in first frame
1070  FActorRegistry Registry = Episode->GetActorRegistry();
1071  for (auto& It : Registry)
1072  {
1073  const FCarlaActor* CarlaActor = It.Value.Get();
1074  if (CarlaActor != nullptr)
1075  {
1076  // create event
1078  CarlaActor->GetActorId(),
1079  static_cast<uint8_t>(CarlaActor->GetActorType()),
1080  CarlaActor->GetActorGlobalTransform(),
1081  CarlaActor->GetActorInfo()->Description,
1082  false);
1083  }
1084  }
1085 }
UCarlaLight * GetLight(int Id)
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
Definition: CarlaEpisode.h:172
bool ProcessReplayerEventParent(uint32_t ChildId, uint32_t ParentId)
Definition: FrameData.cpp:774
void AddExistingActors(void)
Definition: FrameData.cpp:1067
void Add(const CarlaRecorderAnimVehicle &InObj)
virtual ECarlaServerResponse SetVehicleLightState(const FVehicleLightState &)
Definition: CarlaActor.h:271
void Read(std::istream &InFile)
auto end() const noexcept
void AddVehicleAnimation(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:355
void Write(std::ostream &OutFile)
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
void Add(const CarlaRecorderPosition &InObj)
bool DestroyActor(AActor *Actor)
Definition: CarlaEpisode.h:251
CarlaRecorderPhysicsControls PhysicsControls
Definition: FrameData.h:51
void Write(std::ostream &OutFile)
FVehiclePhysicsControl GetVehiclePhysicsControl() const
TArray< UBoxComponent * > GetTriggerVolumes() const
FVector GetActorAngularVelocity() const
Definition: CarlaActor.cpp:371
const std::vector< CarlaRecorderEventAdd > & GetEvents()
virtual ETrafficLightState GetTrafficLightState() const
Definition: CarlaActor.h:351
void AddTriggerVolume(const ATrafficSignBase &TrafficSign)
Definition: FrameData.cpp:458
virtual ECarlaServerResponse GetWalkerControl(FWalkerControl &)
Definition: CarlaActor.h:391
void Write(std::ostream &OutFile)
const std::vector< CarlaRecorderEventDel > & GetEvents()
void SetElapsedTime(float InElapsedTime)
EActorAttributeType
List of valid types for actor attributes.
AActor * GetActor()
Definition: CarlaActor.h:90
void Add(const CarlaRecorderCollision &Collision)
const std::vector< CarlaRecorderLightScene > & GetLights()
const std::vector< CarlaRecorderAnimWalker > & GetWalkers()
EAttachmentType
Definition: ActorAttacher.h:20
void AddAnimWalker(const CarlaRecorderAnimWalker &Walker)
Definition: FrameData.cpp:569
void Read(std::istream &InFile)
void AddEventLightSceneChanged(const UCarlaLight *Light)
Definition: FrameData.cpp:579
void Add(const CarlaRecorderLightScene &InObj)
void Add(const CarlaRecorderTrafficLightTime &InObj)
auto begin() const noexcept
CarlaRecorderTrafficLightTimes TrafficLightTimes
Definition: FrameData.h:52
void AddWalkerAnimation(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:378
Class which implements the state changing of traffic lights.
void AddAnimVehicle(const CarlaRecorderAnimVehicle &Vehicle)
Definition: FrameData.cpp:564
float GetGreenTime() const
void Add(const CarlaRecorderEventAdd &Event)
CarlaRecorderActorBoundingBoxes BoundingBoxes
Definition: FrameData.h:48
void AddBoundingBox(const CarlaRecorderActorBoundingBox &ActorBoundingBox)
Definition: FrameData.cpp:598
void Add(const CarlaRecorderStateTrafficLight &State)
void PutActorToSleep(carla::rpc::ActorId ActorId)
Definition: CarlaEpisode.h:279
CarlaRecorderActorsKinematics Kinematics
Definition: FrameData.h:47
void OnActorSpawned(const FCarlaActor &CarlaActor)
virtual ECarlaServerResponse ApplyControlToVehicle(const FVehicleControl &, const EVehicleInputPriority &)
Definition: CarlaActor.h:286
void ProcessReplayerAnimWalker(CarlaRecorderAnimWalker Walker)
Definition: FrameData.cpp:942
void AddVehicleLight(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:416
void Write(std::ostream &OutFile)
const std::vector< CarlaRecorderPosition > & GetPositions()
void Write(std::ostream &OutStream)
Definition: FrameData.cpp:181
bg::model::box< Point3D > Box
Definition: InMemoryMap.h:45
virtual ECarlaServerResponse SetTrafficLightState(const ETrafficLightState &)
Definition: CarlaActor.h:346
static void ResetFrameCounter(uint64_t Value=0)
Definition: CarlaEngine.h:75
void AddActorPosition(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:340
static uint64_t GetFrameCounter()
Definition: CarlaEngine.h:64
void Add(const CarlaRecorderPhysicsControl &InObj)
void Add(const CarlaRecorderLightVehicle &InObj)
geom::Location Location
Definition: rpc/Location.h:14
void SetFrameCounter()
Definition: FrameData.cpp:1029
void Write(std::ostream &OutFile)
CarlaRecorderActorTriggerVolumes TriggerVolumes
Definition: FrameData.h:49
const FActorRegistry & GetActorRegistry() const
Definition: CarlaEpisode.h:154
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
ECarlaServerResponse
UCarlaEpisode * Episode
Definition: FrameData.h:164
void ProcessReplayerLightVehicle(CarlaRecorderLightVehicle LightVehicle)
Definition: FrameData.cpp:908
void Add(const CarlaRecorderActorBoundingBox &InObj)
void Add(const CarlaRecorderKinematics &InObj)
A simulation episode.
Definition: CarlaEpisode.h:37
void SetWalkerSpeed(uint32_t ActorId, float Speed)
Definition: FrameData.cpp:1000
void AddPosition(const CarlaRecorderPosition &Position)
Definition: FrameData.cpp:497
void Write(std::ostream &OutFile)
CarlaRecorderEventsDel EventsDel
Definition: FrameData.h:38
void AddLightVehicle(const CarlaRecorderLightVehicle &LightVehicle)
Definition: FrameData.cpp:574
TPair< EActorSpawnResultStatus, FCarlaActor * > SpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription, FCarlaActor::IdType DesiredId=0)
Spawns an actor based on ActorDescription at Transform.
bool SetActorSimulatePhysics(FCarlaActor *CarlaActor, bool bEnabled)
Definition: FrameData.cpp:1014
void Write(std::ostream &OutFile)
void SetAttachmentType(carla::rpc::AttachmentType InAttachmentType)
Definition: CarlaActor.h:140
CarlaRecorderLightScenes LightScenes
Definition: FrameData.h:46
FCarlaActor * FindTrafficLightAt(FVector Location)
Definition: FrameData.cpp:1034
void AddActorBoundingBox(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:444
const std::vector< CarlaRecorderStateTrafficLight > & GetStates()
void GetFrameData(UCarlaEpisode *ThisEpisode, bool bAdditionalData=false, bool bIncludeActorsAgain=false)
Definition: FrameData.cpp:14
virtual ECarlaServerResponse ApplyControlToWalker(const FWalkerControl &)
Definition: CarlaActor.h:386
std::vector< CarlaRecorderActorAttribute > Attributes
bool Contains(uint32 Id) const
Definition: ActorRegistry.h:64
CarlaRecorderEventsAdd EventsAdd
Definition: FrameData.h:37
bool ProcessReplayerEventDel(uint32_t DatabaseId)
Definition: FrameData.cpp:760
std::pair< int, uint32_t > ProcessReplayerEventAdd(FVector Location, FVector Rotation, CarlaRecorderActorDescription Description, uint32_t DesiredId, bool bIgnoreHero, bool ReplaySensors, std::unordered_map< uint32_t, uint32_t > &MappedId)
Definition: FrameData.cpp:699
void Read(std::istream &InFile)
void Read(std::istream &InFile)
A description of a Carla Actor with all its variation.
void ProcessReplayerAnimVehicle(CarlaRecorderAnimVehicle Vehicle)
Definition: FrameData.cpp:889
FVector GetActorVelocity() const
Definition: CarlaActor.cpp:359
FActorDescription Description
Definition: ActorInfo.h:26
void Write(std::ostream &OutFile)
CarlaRecorderLightVehicles LightVehicles
Definition: FrameData.h:45
uint32_t ActorId
Definition: ActorId.h:14
void AddKinematics(const CarlaRecorderKinematics &ActorKinematics)
Definition: FrameData.cpp:593
const FActorInfo * GetActorInfo() const
Definition: CarlaActor.h:100
FBoundingBox BoundingBox
Definition: ActorInfo.h:30
bool IsPendingKill() const
Definition: CarlaActor.h:75
CarlaRecorderPositions Positions
Definition: FrameData.h:41
void AddTrafficLightTime(const ATrafficLightBase &TrafficLight)
Definition: FrameData.cpp:484
void Clear()
Definition: FrameData.cpp:161
float GetRedTime() const
bool ProcessReplayerPosition(CarlaRecorderPosition Pos1, CarlaRecorderPosition Pos2, double Per, double DeltaTime)
Definition: FrameData.cpp:810
float GetYellowTime() const
ECarlaServerResponse SetActorTargetVelocity(const FVector &Velocity)
Definition: CarlaActor.cpp:389
void AddState(const CarlaRecorderStateTrafficLight &State)
Definition: FrameData.cpp:559
void AddCollision(AActor *Actor1, AActor *Actor2)
Definition: FrameData.cpp:517
CarlaRecorderEventsParent EventsParent
Definition: FrameData.h:39
const std::vector< CarlaRecorderAnimVehicle > & GetVehicles()
bool ProcessReplayerStateTrafficLight(CarlaRecorderStateTrafficLight State)
Definition: FrameData.cpp:866
CarlaRecorderAnimWalkers Walkers
Definition: FrameData.h:44
geom::Rotation Rotation
Definition: rpc/Transform.h:14
CarlaRecorderCollisions Collisions
Definition: FrameData.h:40
APawn * GetSpectatorPawn() const
Definition: CarlaEpisode.h:143
void AttachActors(AActor *Child, AActor *Parent, EAttachmentType InAttachmentType=EAttachmentType::Rigid)
Attach Child to Parent.
void AddChildren(IdType ChildId)
Definition: CarlaActor.h:125
void AddActorKinematics(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:428
virtual UTrafficLightController * GetTrafficLightController()
Definition: CarlaActor.h:356
void AddTrafficLightState(FCarlaActor *CarlaActor)
Definition: FrameData.cpp:394
bool ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map< uint32_t, bool > &IsHero)
Definition: FrameData.cpp:948
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Definition: CarlaStatics.h:100
void AddPhysicsControl(const ACarlaWheeledVehicle &Vehicle)
Definition: FrameData.cpp:476
void Read(std::istream &InFile)
FTransform GetActorGlobalTransform() const
Definition: CarlaActor.cpp:198
void Read(std::istream &InStream)
Definition: FrameData.cpp:195
uint32 UId
UId of the definition in which this description was based.
void SetFrozenGroup(bool InFreeze)
const std::vector< CarlaRecorderLightVehicle > & GetLightVehicles()
bool IsDormant() const
Definition: CarlaActor.h:70
CarlaRecorderStates States
Definition: FrameData.h:42
virtual ECarlaServerResponse SetActorSimulatePhysics(bool bEnabled)
Definition: CarlaActor.cpp:559
void Read(std::istream &InFile)
virtual ECarlaServerResponse GetVehicleLightState(FVehicleLightState &)
Definition: CarlaActor.h:251
void SetParent(IdType InParentId)
Definition: CarlaActor.h:115
virtual ECarlaServerResponse GetVehicleControl(FVehicleControl &)
Definition: CarlaActor.h:298
IdType GetActorId() const
Definition: CarlaActor.h:80
void PlayFrameData(UCarlaEpisode *ThisEpisode, std::unordered_map< uint32_t, uint32_t > &MappedId)
Definition: FrameData.cpp:68
void Add(const CarlaRecorderEventParent &Event)
void Add(const CarlaRecorderAnimWalker &InObj)
FCarlaActor * FindCarlaActor(IdType Id)
Definition: ActorRegistry.h:69
void SetActorVelocity(FCarlaActor *CarlaActor, FVector Velocity)
Definition: FrameData.cpp:990
void ProcessReplayerLightScene(CarlaRecorderLightScene LightScene)
Definition: FrameData.cpp:919
ATrafficLightGroup * GetGroup()
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void CreateRecorderEventAdd(uint32_t DatabaseId, uint8_t Type, const FTransform &Transform, FActorDescription ActorDescription, bool bAddOtherRelatedInfo=true)
Definition: FrameData.cpp:264
void Add(const CarlaRecorderActorBoundingBox &InObj)
void Add(const CarlaRecorderEventDel &Event)
flag_type light_state
Lights state flag, all turned off by default.
void Read(std::istream &InFile)
Base class for CARLA wheeled vehicles.
Maps a controller from OpenDrive.
void Read(std::istream &InFile)
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
CarlaRecorderAnimVehicles Vehicles
Definition: FrameData.h:43
void Write(std::ostream &OutFile)
An actor attribute, may be an intrinsic (non-modifiable) attribute of the actor or an user-defined ac...
std::pair< int, FCarlaActor * > CreateOrReuseActor(FVector &Location, FVector &Rotation, FActorDescription &ActorDesc, uint32_t DesiredId, bool SpawnSensors, std::unordered_map< uint32_t, uint32_t > &MappedId)
Definition: FrameData.cpp:609
void GetFrameCounter()
Definition: FrameData.cpp:603
void SetActorGlobalTransform(const FTransform &Transform, ETeleportType Teleport=ETeleportType::TeleportPhysics)
Definition: CarlaActor.cpp:332
void Write(std::ostream &OutFile)
void AddEvent(const CarlaRecorderEventAdd &Event)
Definition: FrameData.cpp:502
geom::Transform Transform
Definition: rpc/Transform.h:16
FVehiclePhysicsControl VehiclePhysicsControl
CarlaRecorderFrameCounter FrameCounter
Definition: FrameData.h:53
FVector GetActorGlobalLocation() const
Definition: CarlaActor.cpp:239
ActorType GetActorType() const
Definition: CarlaActor.h:85
bool SetCameraPosition(uint32_t Id, FVector Offset, FQuat Rotation)
Definition: FrameData.cpp:840
A view over an actor and its properties.
Definition: CarlaActor.h:23