CARLA
Loading...
Searching...
No Matches
CarlaReplayer.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include "CarlaRecorderInfo.h"
10#include "CarlaRecorderFrames.h"
16#include "CarlaRecorderState.h"
18#include "CarlaReplayerHelper.h"
19
20#include <fstream>
21#include <sstream>
22#include <unordered_map>
23#include <unordered_set>
24#include <functional>
25
26class UCarlaEpisode;
27
29{
30 #pragma pack(push, 1)
31 struct Header
32 {
33 char Id;
34 uint32_t Size;
35 };
36 #pragma pack(pop)
37
38public:
40 {
41 bool Enabled;
42 std::string Filename;
43 FString Mapfile;
44 double TimeStart;
45 double Duration;
46 uint32_t FollowId;
47 FTransform FollowOffset;
48 double TimeFactor;
51 };
52
54
57
58 std::string ReplayFile(std::string Filename, double TimeStart = 0.0f, double Duration = 0.0f,
59 uint32_t FollowId = 0, const FTransform& Offset = FTransform(), bool ReplaySensors = false,
60 bool ReplayWeather = false, std::string MapOverride = "");
61
62 // void Start(void);
63 void Stop(bool KeepActors = false);
64
65 /// Destroys the actors this replayer spawned that are still alive, even
66 /// when the replay already ended by itself (which keeps them). Called by
67 /// stop_replayer(keep_actors=False).
68 void DestroySpawnedActors(void);
69
70 /// Forgets the spawned actors, so a later DestroySpawnedActors() leaves
71 /// them alone. Called by stop_replayer(keep_actors=True).
73 {
74 SpawnedIds.clear();
75 }
76
77 void Enable(void);
78
79 void Disable(void);
80
81 bool IsEnabled(void)
82 {
83 return Enabled;
84 }
85
86 // set episode
87 void SetEpisode(UCarlaEpisode *ThisEpisode)
88 {
89 // ids of a previous episode mean nothing in this one
90 SpawnedIds.clear();
91 Episode = ThisEpisode;
92 Helper.SetEpisode(ThisEpisode);
93 }
94
95 // playback speed (time factor)
96 void SetTimeFactor(double NewTimeFactor)
97 {
98 TimeFactor = NewTimeFactor;
99 }
100
101 // set ignore hero
102 void SetIgnoreHero(bool InIgnoreHero)
103 {
104 IgnoreHero = InIgnoreHero;
105 }
106
107 // set ignore spectator
108 void SetIgnoreSpectator(bool InIgnoreSpectator)
109 {
110 IgnoreSpectator = InIgnoreSpectator;
111 }
112
113 // check if after a map is loaded, we need to replay
114 void CheckPlayAfterMapLoaded(void);
115
116 // tick for the replayer
117 void Tick(float Time);
118
119private:
120
122 bool bReplaySensors = false;
123 bool bReplayWeather = false;
125 // binary file reader
126 std::ifstream File;
130 // positions (to be able to interpolate)
131 std::vector<CarlaRecorderPosition> CurrPos;
132 std::vector<CarlaRecorderPosition> PrevPos;
133 // wheels (interpolated like the positions)
134 std::vector<CarlaRecorderAnimWheels> CurrWheels;
135 std::vector<CarlaRecorderAnimWheels> PrevWheels;
136 // mapping id
137 std::unordered_map<uint32_t, uint32_t> MappedId;
138 // ids of the actors this replayer spawned (not the ones it reused, such as
139 // traffic lights), kept across replays until stop_replayer decides on them
140 std::unordered_set<uint32_t> SpawnedIds;
141 // times
144 double TotalTime;
145 // helper
147 // follow camera
148 uint32_t FollowId;
149 // follow camera offset
150 FTransform FollowOffset;
151 // speed (time factor)
152 double TimeFactor { 1.0 };
153 // ignore hero vehicles
154 bool IgnoreHero { false };
155 bool IgnoreSpectator { true };
156 std::unordered_map<uint32_t, bool> IsHeroMap;
157
158 // utils
159 bool ReadHeader();
160
161 void SkipPacket();
162
163 double GetTotalTime(void);
164
165 void Rewind(void);
166
167 // processing packets
168 void ProcessToTime(double Time, bool IsFirstTime = false);
169
170 void ProcessVisualTime(void);
171
172 void ProcessEventsAdd(void);
173 void ProcessEventsDel(void);
174 void ProcessEventsParent(void);
175
176 void ProcessPositions(bool IsFirstTime = false);
177
178 void ProcessStates(void);
179
180 void ProcessAnimVehicle(void);
181 void ProcessAnimVehicleWheels(bool IsFirstTime = false);
182 void ProcessAnimWalker(void);
183 void ProcessAnimBiker(void);
184
185 void ProcessLightVehicle(void);
186 void ProcessLightScene(void);
187
188 void ProcessDoorVehicle(void);
189
190 void ProcessWalkerBones(void);
191
192 void ProcessWeather(void);
193
194 // positions
195 void UpdatePositions(double Per, double DeltaTime);
196
197 // wheels, at the same point between frames as the positions
198 void UpdateWheels(double Per);
199
200 void InterpolatePosition(const CarlaRecorderPosition &Start, const CarlaRecorderPosition &End, double Per, double DeltaTime);
201};
void SetEpisode(UCarlaEpisode *ThisEpisode)
void ProcessEventsDel(void)
void ProcessDoorVehicle(void)
void SetEpisode(UCarlaEpisode *ThisEpisode)
std::vector< CarlaRecorderPosition > PrevPos
FTransform FollowOffset
void Rewind(void)
void ProcessVisualTime(void)
void ProcessLightVehicle(void)
double GetTotalTime(void)
std::vector< CarlaRecorderPosition > CurrPos
std::vector< CarlaRecorderAnimWheels > PrevWheels
void InterpolatePosition(const CarlaRecorderPosition &Start, const CarlaRecorderPosition &End, double Per, double DeltaTime)
void ProcessAnimVehicle(void)
void SetIgnoreHero(bool InIgnoreHero)
void ProcessWalkerBones(void)
void ForgetSpawnedActors(void)
Forgets the spawned actors, so a later DestroySpawnedActors() leaves them alone.
void Tick(float Time)
void ProcessPositions(bool IsFirstTime=false)
void SetIgnoreSpectator(bool InIgnoreSpectator)
void ProcessEventsParent(void)
void SetTimeFactor(double NewTimeFactor)
std::string ReplayFile(std::string Filename, double TimeStart=0.0f, double Duration=0.0f, uint32_t FollowId=0, const FTransform &Offset=FTransform(), bool ReplaySensors=false, bool ReplayWeather=false, std::string MapOverride="")
void ProcessWeather(void)
void UpdatePositions(double Per, double DeltaTime)
uint32_t FollowId
void ProcessEventsAdd(void)
void Enable(void)
void DestroySpawnedActors(void)
Destroys the actors this replayer spawned that are still alive, even when the replay already ended by...
static PlayAfterLoadMap Autoplay
std::ifstream File
UCarlaEpisode * Episode
void UpdateWheels(double Per)
CarlaRecorderFrame Frame
void ProcessAnimWalker(void)
void ProcessStates(void)
void ProcessAnimBiker(void)
bool IsEnabled(void)
std::vector< CarlaRecorderAnimWheels > CurrWheels
CarlaRecorderInfo RecInfo
CarlaReplayerHelper Helper
void ProcessLightScene(void)
std::unordered_set< uint32_t > SpawnedIds
void CheckPlayAfterMapLoaded(void)
std::unordered_map< uint32_t, bool > IsHeroMap
void ProcessToTime(double Time, bool IsFirstTime=false)
void Disable(void)
std::unordered_map< uint32_t, uint32_t > MappedId
void Stop(bool KeepActors=false)
void ProcessAnimVehicleWheels(bool IsFirstTime=false)
A simulation episode.