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