CARLA
 
载入中...
搜索中...
未找到
CarlaRecorderHelpers.cpp
浏览该文件的文档.
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"
12
13// 创建一个临时缓冲区,用于在 FString 和字节数组 (bytes) 之间转换
14static std::vector<uint8_t> CarlaRecorderHelperBuffer;
15
16// 获取最终的路径和文件名
17std::string GetRecorderFilename(std::string Filename)
18{
19 std::string Filename2;
20
21 // 检查是否指定了相对路径
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// 将FVector中的二进制数据写出
38void 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// 将FTransform中的二进制数据写出
46void WriteFTransform(std::ofstream &OutFile, const FTransform &InObj)
47{
48 WriteFVector(OutFile, InObj.GetTranslation());
49 WriteFVector(OutFile, InObj.GetRotation().Euler());
50}
51
52// 将FString中的二进制数据写出(包括长度和文本内容)
53void WriteFString(std::ostream &OutFile, const FString &InObj)
54{
55 // 将字符串编码为UTF-8以获知最终长度
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// 从二进制数据中读取到 FVector
68void 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// 从二进制数据中读取到 FVector
76void 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// 将FString中的二进制数据写出(包括长度和文本内容)
86void ReadFString(std::istream &InFile, FString &OutObj)
87{
88 uint16_t Length;
89 ReadValue<uint16_t>(InFile, Length);
90 // 在向量缓冲区中腾出空间
91 if (CarlaRecorderHelperBuffer.capacity() < Length + 1)
92 {
93 CarlaRecorderHelperBuffer.reserve(Length + 1);
94 }
96 // 将向量空间初始化为0
97 CarlaRecorderHelperBuffer.resize(Length + 1);
98 // 读取
99 InFile.read(reinterpret_cast<char *>(CarlaRecorderHelperBuffer.data()), Length);
100 // 将UTF-8编码的字符串转换为FString
101 OutObj = FString(UTF8_TO_TCHAR(CarlaRecorderHelperBuffer.data()));
102}
static std::vector< uint8_t > CarlaRecorderHelperBuffer
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)
void WriteFVector(std::ostream &OutFile, const FVector &InObj)
void ReadFVector(std::istream &InFile, FVector &OutObj)
std::string GetRecorderFilename(std::string Filename)