-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTLREADER_H.h
More file actions
57 lines (45 loc) · 1.85 KB
/
Copy pathSTLREADER_H.h
File metadata and controls
57 lines (45 loc) · 1.85 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
#ifndef STLREADER_H
#define STLREADER_H
#include <cstdint>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <array>
#include <regex>
#include "Point3D.h"
inline std::string readFileIntoString(const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "File could not be opened: " << filePath << std::endl;
return "";
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
inline void parseSTL(const std::string& stlContent, std::vector<std::array<Vector3D, 4>>& facets) {
std::regex facetRegex(R"(facet normal\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?)\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?)\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?))");
std::regex vertexRegex(R"(vertex\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?)\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?)\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?))");
auto facetBegin = std::sregex_iterator(stlContent.begin(), stlContent.end(), facetRegex);
auto facetEnd = std::sregex_iterator();
auto vertexBegin = std::sregex_iterator(stlContent.begin(), stlContent.end(), vertexRegex);
auto vertexEnd = std::sregex_iterator();
for (auto it = facetBegin; it != facetEnd; ++it) {
std::smatch match = *it;
std::array<Vector3D, 4> facetArray;
facetArray[0] = Vector3D(std::stof(match[1].str()), std::stof(match[2].str()), std::stof(match[3].str()));
auto v_it = vertexBegin;
for (int i = 0; i < 3; ++i) {
if (v_it != vertexEnd) {
std::smatch vertexMatch = *v_it;
facetArray[i + 1] = Vector3D(std::stof(vertexMatch[1].str()), std::stof(vertexMatch[2].str()), std::stof(vertexMatch[3].str()));
++v_it;
}
}
vertexBegin = v_it;
facets.push_back(facetArray);
}
}
#endif // STLREADER_H