CARLA
CarlaRecorderHelpers.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 <vector>
8 
9 #include "UnrealString.h"
10 #include "CarlaRecorderHelpers.h"
11 
12 // create a temporal buffer to convert from and to FString and bytes
13 static std::vector<uint8_t> CarlaRecorderHelperBuffer;
14 
15 // get the final path + filename
16 std::string GetRecorderFilename(std::string Filename)
17 {
18  std::string Filename2;
19 
20  // check if a relative path was specified
21  if (Filename.find("\\") != std::string::npos || Filename.find("/") != std::string::npos || Filename.find(":") != std::string::npos)
22  Filename2 = Filename;
23  else
24  {
25  FString Path = FPaths::ConvertRelativePathToFull(FPaths::ProjectSavedDir());
26  Filename2 = TCHAR_TO_UTF8(*Path) + Filename;
27  }
28 
29  return Filename2;
30 }
31 
32 // ------
33 // write
34 // ------
35 
36 // write binary data from FVector
37 void WriteFVector(std::ofstream &OutFile, const FVector &InObj)
38 {
39  WriteValue<float>(OutFile, InObj.X);
40  WriteValue<float>(OutFile, InObj.Y);
41  WriteValue<float>(OutFile, InObj.Z);
42 }
43 
44 // write binary data from FTransform
45 // void WriteFTransform(std::ofstream &OutFile, const FTransform &InObj){
46 // WriteFVector(OutFile, InObj.GetTranslation());
47 // WriteFVector(OutFile, InObj.GetRotation().Euler());
48 // }
49 
50 // write binary data from FString (length + text)
51 void WriteFString(std::ofstream &OutFile, const FString &InObj)
52 {
53  // encode the string to UTF8 to know the final length
54  FTCHARToUTF8 EncodedString(*InObj);
55  int16_t Length = EncodedString.Length();
56  // write
57  WriteValue<uint16_t>(OutFile, Length);
58  OutFile.write(reinterpret_cast<char *>(TCHAR_TO_UTF8(*InObj)), Length);
59 }
60 
61 // -----
62 // read
63 // -----
64 
65 // read binary data to FVector
66 void ReadFVector(std::ifstream &InFile, FVector &OutObj)
67 {
68  ReadValue<float>(InFile, OutObj.X);
69  ReadValue<float>(InFile, OutObj.Y);
70  ReadValue<float>(InFile, OutObj.Z);
71 }
72 
73 // read binary data to FTransform
74 // void ReadFTransform(std::ifstream &InFile, FTransform &OutObj){
75 // FVector Vec;
76 // ReadFVector(InFile, Vec);
77 // OutObj.SetTranslation(Vec);
78 // ReadFVector(InFile, Vec);
79 // OutObj.GetRotation().MakeFromEuler(Vec);
80 // }
81 
82 // read binary data to FString (length + text)
83 void ReadFString(std::ifstream &InFile, FString &OutObj)
84 {
85  uint16_t Length;
86  ReadValue<uint16_t>(InFile, Length);
87  // make room in vector buffer
88  if (CarlaRecorderHelperBuffer.capacity() < Length + 1)
89  {
90  CarlaRecorderHelperBuffer.reserve(Length + 1);
91  }
93  // initialize the vector space with 0
94  CarlaRecorderHelperBuffer.resize(Length + 1);
95  // read
96  InFile.read(reinterpret_cast<char *>(CarlaRecorderHelperBuffer.data()), Length);
97  // convert from UTF8 to FString
98  OutObj = FString(UTF8_TO_TCHAR(CarlaRecorderHelperBuffer.data()));
99 }
std::vector< cg::Location > Path
void WriteFString(std::ofstream &OutFile, const FString &InObj)
void ReadFString(std::ifstream &InFile, FString &OutObj)
static std::vector< uint8_t > CarlaRecorderHelperBuffer
std::string GetRecorderFilename(std::string Filename)
void WriteFVector(std::ofstream &OutFile, const FVector &InObj)
void ReadFVector(std::ifstream &InFile, FVector &OutObj)