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