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  // check if after a map is loaded, we need to replay
90  void CheckPlayAfterMapLoaded(void);
91 
92  // tick for the replayer
93  void Tick(float Time);
94 
95 private:
96 
97  bool Enabled;
98  bool bReplaySensors = false;
99  UCarlaEpisode *Episode = nullptr;
100  // binary file reader
101  std::ifstream File;
105  // positions (to be able to interpolate)
106  std::vector<CarlaRecorderPosition> CurrPos;
107  std::vector<CarlaRecorderPosition> PrevPos;
108  // mapping id
109  std::unordered_map<uint32_t, uint32_t> MappedId;
110  // times
111  double CurrentTime;
112  double TimeToStop;
113  double TotalTime;
114  // helper
116  // follow camera
117  uint32_t FollowId;
118  // speed (time factor)
119  double TimeFactor { 1.0 };
120  // ignore hero vehicles
121  bool IgnoreHero { false };
122  std::unordered_map<uint32_t, bool> IsHeroMap;
123 
124  // utils
125  bool ReadHeader();
126 
127  void SkipPacket();
128 
129  double GetTotalTime(void);
130 
131  void Rewind(void);
132 
133  // processing packets
134  void ProcessToTime(double Time, bool IsFirstTime = false);
135 
136  void ProcessEventsAdd(void);
137  void ProcessEventsDel(void);
138  void ProcessEventsParent(void);
139 
140  void ProcessPositions(bool IsFirstTime = false);
141 
142  void ProcessStates(void);
143 
144  void ProcessAnimVehicle(void);
145  void ProcessAnimWalker(void);
146 
147  void ProcessLightVehicle(void);
148  void ProcessLightScene(void);
149 
150  // positions
151  void UpdatePositions(double Per, double DeltaTime);
152 
153  void InterpolatePosition(const CarlaRecorderPosition &Start, const CarlaRecorderPosition &End, double Per, double DeltaTime);
154 };
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 ProcessLightVehicle(void)
void ProcessStates(void)
void ProcessAnimWalker(void)
CarlaReplayerHelper Helper
void SetIgnoreHero(bool InIgnoreHero)
Definition: CarlaReplayer.h:84
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
Definition: CarlaReplayer.h:99
std::vector< CarlaRecorderPosition > CurrPos
void SetEpisode(UCarlaEpisode *ThisEpisode)
A simulation episode.
Definition: CarlaEpisode.h:34
void SetEpisode(UCarlaEpisode *ThisEpisode)
Definition: CarlaReplayer.h:71
CarlaRecorderInfo RecInfo
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)