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