CARLA
CarlaReplayer.h
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 #pragma once
8 
9 #include <fstream>
10 #include <sstream>
11 #include <unordered_map>
12 
13 #include <functional>
14 #include "CarlaRecorderInfo.h"
15 #include "CarlaRecorderFrames.h"
16 #include "CarlaRecorderEventAdd.h"
17 #include "CarlaRecorderEventDel.h"
19 #include "CarlaRecorderCollision.h"
20 #include "CarlaRecorderPosition.h"
21 #include "CarlaRecorderState.h"
22 #include "CarlaRecorderHelpers.h"
23 #include "CarlaReplayerHelper.h"
24 
25 class UCarlaEpisode;
26 
28 {
29  #pragma pack(push, 1)
30  struct Header
31  {
32  char Id;
33  uint32_t Size;
34  };
35  #pragma pack(pop)
36 
37 public:
39  {
40  bool Enabled;
41  std::string Filename;
42  FString Mapfile;
43  double TimeStart;
44  double Duration;
45  uint32_t FollowId;
46  double TimeFactor;
48  };
49 
51 
53  ~CarlaReplayer() { Stop(); };
54 
55  std::string ReplayFile(std::string Filename, double TimeStart = 0.0f, double Duration = 0.0f,
56  uint32_t FollowId = 0, bool ReplaySensors = false);
57 
58  // void Start(void);
59  void Stop(bool KeepActors = false);
60 
61  void Enable(void);
62 
63  void Disable(void);
64 
65  bool IsEnabled(void)
66  {
67  return Enabled;
68  }
69 
70  // set episode
71  void SetEpisode(UCarlaEpisode *ThisEpisode)
72  {
73  Episode = ThisEpisode;
74  Helper.SetEpisode(ThisEpisode);
75  }
76 
77  // playback speed (time factor)
78  void SetTimeFactor(double NewTimeFactor)
79  {
80  TimeFactor = NewTimeFactor;
81  }
82 
83  // set ignore hero
84  void SetIgnoreHero(bool InIgnoreHero)
85  {
86  IgnoreHero = InIgnoreHero;
87  }
88 
89  // set ignore spectator
90  void SetIgnoreSpectator(bool InIgnoreSpectator)
91  {
92  IgnoreSpectator = InIgnoreSpectator;
93  }
94 
95  // check if after a map is loaded, we need to replay
96  void CheckPlayAfterMapLoaded(void);
97 
98  // tick for the replayer
99  void Tick(float Time);
100 
101 private:
102 
103  bool Enabled;
104  bool bReplaySensors = false;
105  UCarlaEpisode *Episode = nullptr;
106  // binary file reader
107  std::ifstream File;
111  // positions (to be able to interpolate)
112  std::vector<CarlaRecorderPosition> CurrPos;
113  std::vector<CarlaRecorderPosition> PrevPos;
114  // mapping id
115  std::unordered_map<uint32_t, uint32_t> MappedId;
116  // times
117  double CurrentTime;
118  double TimeToStop;
119  double TotalTime;
120  // helper
122  // follow camera
123  uint32_t FollowId;
124  // speed (time factor)
125  double TimeFactor { 1.0 };
126  // ignore hero vehicles
127  bool IgnoreHero { false };
128  bool IgnoreSpectator { true };
129  std::unordered_map<uint32_t, bool> IsHeroMap;
130 
131  // utils
132  bool ReadHeader();
133 
134  void SkipPacket();
135 
136  double GetTotalTime(void);
137 
138  void Rewind(void);
139 
140  // processing packets
141  void ProcessToTime(double Time, bool IsFirstTime = false);
142 
143  void ProcessVisualTime(void);
144 
145  void ProcessEventsAdd(void);
146  void ProcessEventsDel(void);
147  void ProcessEventsParent(void);
148 
149  void ProcessPositions(bool IsFirstTime = false);
150 
151  void ProcessStates(void);
152 
153  void ProcessAnimVehicle(void);
154  void ProcessAnimVehicleWheels(void);
155  void ProcessAnimWalker(void);
156  void ProcessAnimBiker(void);
157 
158  void ProcessLightVehicle(void);
159  void ProcessLightScene(void);
160 
161  void ProcessWalkerBones(void);
162 
163  // positions
164  void UpdatePositions(double Per, double DeltaTime);
165 
166  void InterpolatePosition(const CarlaRecorderPosition &Start, const CarlaRecorderPosition &End, double Per, double DeltaTime);
167 };
void CheckPlayAfterMapLoaded(void)
bool IsEnabled(void)
Definition: CarlaReplayer.h:65
void ProcessLightScene(void)
void SetTimeFactor(double NewTimeFactor)
Definition: CarlaReplayer.h:78
double GetTotalTime(void)
void ProcessWalkerBones(void)
void ProcessLightVehicle(void)
void ProcessStates(void)
void ProcessAnimVehicleWheels(void)
void ProcessAnimWalker(void)
CarlaReplayerHelper Helper
void SetIgnoreHero(bool InIgnoreHero)
Definition: CarlaReplayer.h:84
void ProcessAnimBiker(void)
std::ifstream File
void InterpolatePosition(const CarlaRecorderPosition &Start, const CarlaRecorderPosition &End, double Per, double DeltaTime)
void ProcessAnimVehicle(void)
void ProcessPositions(bool IsFirstTime=false)
void Rewind(void)
void ProcessEventsAdd(void)
void ProcessToTime(double Time, bool IsFirstTime=false)
std::unordered_map< uint32_t, bool > IsHeroMap
std::unordered_map< uint32_t, uint32_t > MappedId
CarlaRecorderFrame Frame
UCarlaEpisode * Episode
std::vector< CarlaRecorderPosition > CurrPos
void SetEpisode(UCarlaEpisode *ThisEpisode)
A simulation episode.
Definition: CarlaEpisode.h:38
void SetEpisode(UCarlaEpisode *ThisEpisode)
Definition: CarlaReplayer.h:71
CarlaRecorderInfo RecInfo
void ProcessVisualTime(void)
double CurrentTime
uint32_t FollowId
std::string ReplayFile(std::string Filename, double TimeStart=0.0f, double Duration=0.0f, uint32_t FollowId=0, bool ReplaySensors=false)
void UpdatePositions(double Per, double DeltaTime)
std::vector< CarlaRecorderPosition > PrevPos
void Enable(void)
void Tick(float Time)
void Stop(bool KeepActors=false)
static PlayAfterLoadMap Autoplay
Definition: CarlaReplayer.h:50
void ProcessEventsDel(void)
void Disable(void)
void ProcessEventsParent(void)
void SetIgnoreSpectator(bool InIgnoreSpectator)
Definition: CarlaReplayer.h:90