CARLA
CarlaRecorderFrames.cpp
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 #include "CarlaRecorder.h"
8 #include "CarlaRecorderFrames.h"
9 #include "CarlaRecorderHelpers.h"
10 
11 void CarlaRecorderFrame::Read(std::istream &InFile)
12 {
13  ReadValue<CarlaRecorderFrame>(InFile, *this);
14 }
15 
16 void CarlaRecorderFrame::Write(std::ostream &OutFile)
17 {
18  WriteValue<CarlaRecorderFrame>(OutFile, *this);
19 }
20 
21 // ---------------------------------------------
22 
24 {
25  Reset();
26 }
27 
29 {
30  Frame.Id = 0;
31  Frame.DurationThis = 0.0f;
32  Frame.Elapsed = 0.0f;
33  OffsetPreviousFrame = 0;
34 }
35 
36 void CarlaRecorderFrames::SetFrame(double DeltaSeconds)
37 {
38  if (Frame.Id == 0)
39  {
40  Frame.Elapsed = 0.0f;
41  Frame.DurationThis = 0.0f;
42  }
43  else
44  {
45  Frame.DurationThis = DeltaSeconds;
46  Frame.Elapsed += DeltaSeconds;
47  }
48 
49  ++Frame.Id;
50 }
51 
52 void CarlaRecorderFrames::WriteStart(std::ostream &OutFile)
53 {
54  std::streampos Pos, Offset;
55  double Dummy = -1.0f;
56 
57  // write the packet id
58  WriteValue<char>(OutFile, static_cast<char>(CarlaRecorderPacketId::FrameStart));
59 
60  // write the packet size
61  uint32_t Total = sizeof(CarlaRecorderFrame);
62  WriteValue<uint32_t>(OutFile, Total);
63 
64  // write frame record
65  WriteValue<uint64_t>(OutFile, Frame.Id);
66  Offset = OutFile.tellp();
67  WriteValue<double>(OutFile, Dummy);
68  WriteValue<double>(OutFile, Frame.Elapsed);
69 
70  // we need to write this duration to previous frame
71  if (OffsetPreviousFrame > 0)
72  {
73  Pos = OutFile.tellp();
74  OutFile.seekp(OffsetPreviousFrame, std::ios::beg);
75  WriteValue<double>(OutFile, Frame.DurationThis);
76  OutFile.seekp(Pos, std::ios::beg);
77  }
78 
79  // save position for next actualization
80  OffsetPreviousFrame = Offset;
81 }
82 
83 void CarlaRecorderFrames::WriteEnd(std::ostream &OutFile)
84 {
85  // write the packet id
86  WriteValue<char>(OutFile, static_cast<char>(CarlaRecorderPacketId::FrameEnd));
87 
88  // write the packet size (0)
89  uint32_t Total = 0;
90  WriteValue<uint32_t>(OutFile, Total);
91 }
void Read(std::istream &InFile)
void Write(std::ostream &OutFile)
void SetFrame(double DeltaSeconds)
void WriteStart(std::ostream &OutFile)
void WriteEnd(std::ostream &OutFile)