-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathParser.cpp
More file actions
130 lines (104 loc) · 4.1 KB
/
Parser.cpp
File metadata and controls
130 lines (104 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "Parser.h"
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cctype>
#include <cstring>
#include <thread>
static std::vector<unsigned char> HexStringToBytes(const std::string& hex) {
std::string hexCleaned;
hexCleaned.reserve(hex.length());
std::copy_if(hex.begin(), hex.end(), std::back_inserter(hexCleaned), [](char c) {
return !std::isspace(c);
});
std::vector<unsigned char> bytes;
bytes.reserve(hexCleaned.length() / 2);
for (size_t i = 0; i < hexCleaned.length(); i += 2) {
std::string byteString = hexCleaned.substr(i, 2);
unsigned char byte = static_cast<unsigned char>(std::stoul(byteString, nullptr, 16));
bytes.push_back(byte);
}
return bytes;
}
Parser::Parser(const std::string& path) : DataPath(path) {
std::thread trianglesThread(&Parser::fetchTriangles, this);
std::thread verticesThread(&Parser::fetchVertices, this);
trianglesThread.join();
verticesThread.join();
for (size_t i = 0; i < TrianglesList.size(); ++i) {
const std::vector<Triangle>& triangles = TrianglesList[i];
const std::vector<Vector3>& vertices = VerticesList[i];
std::vector<TriangleCombined> Combined;
for (const Triangle& triangle : triangles) {
TriangleCombined t;
t.v0 = vertices[triangle.a];
t.v1 = vertices[triangle.b];
t.v2 = vertices[triangle.c];
Combined.push_back(t);
}
CombinedList.push_back(Combined);
}
}
template<typename T>
std::vector<T> Parser::ParseElements(const unsigned char* data, size_t dataSize) {
std::vector<T> elements;
size_t elementSize = sizeof(T);
elements.reserve(dataSize / elementSize);
for (size_t i = 0; i < dataSize; i += elementSize) {
T element;
std::memcpy(&element, data + i, elementSize);
elements.push_back(element);
}
return elements;
}
template<typename T>
std::vector<std::vector<T>> Parser::ParseSection(const unsigned char* fileData, size_t fileSize, const std::string& sectionName) {
std::vector<std::vector<T>> elementsLists;
std::istringstream fileStream(std::string(reinterpret_cast<const char*>(fileData), fileSize));
std::string line;
bool inMeshSection = false;
while (std::getline(fileStream, line)) {
if (line.find("m_meshes") != std::string::npos) {
inMeshSection = true;
}
if (inMeshSection && line.find(sectionName) != std::string::npos) {
std::getline(fileStream, line);
if (line.find("#[") != std::string::npos) {
std::string hexString;
while (std::getline(fileStream, line) && line.find("]") == std::string::npos) {
hexString += line;
}
auto bytes = HexStringToBytes(hexString);
auto parsedElements = ParseElements<T>(bytes.data(), bytes.size());
elementsLists.push_back(std::move(parsedElements));
}
}
}
return elementsLists;
}
std::vector<std::vector<Triangle>> Parser::GetTriangles() {
std::ifstream file(DataPath, std::ios::binary);
if (!file.is_open()) {
return {};
}
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<unsigned char> fileData(fileSize);
file.read(reinterpret_cast<char*>(fileData.data()), fileSize);
return ParseSection<Triangle>(fileData.data(), fileSize, "m_Triangles");
}
std::vector<std::vector<Vector3>> Parser::GetVertices() {
std::ifstream file(DataPath, std::ios::binary);
if (!file.is_open()) {
return {};
}
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<unsigned char> fileData(fileSize);
file.read(reinterpret_cast<char*>(fileData.data()), fileSize);
return ParseSection<Vector3>(fileData.data(), fileSize, "m_Vertices");
}