-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcadmodel.cpp
212 lines (182 loc) · 4.66 KB
/
cadmodel.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "cadmodel.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <assert.h>
std::string read_file(const std::string& filename) {
std::ifstream ifs(filename.c_str());
if(!ifs.is_open()) {
throw CadmodelError("Cannot open file: " + filename);
}
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
ifs.close();
return content;
}
std::vector<std::string> split(const std::string& text)
{
std::vector<std::string> lines;
std::string line;
std::istringstream iss(text);
while(std::getline(iss, line)) {
lines.push_back(line);
}
return lines;
}
std::vector<std::string> tokenize(const std::string& str) {
std::istringstream iss(str);
std::string token;
std::vector<std::string> tokens;
while (iss >> token) {
tokens.push_back(token);
}
return tokens;
}
Cadmodel::Cadmodel()
{
}
Cadmodel::~Cadmodel()
{
}
void Cadmodel::open(const std::string& filename)
{
std::ifstream ifs(filename.c_str());
if(!ifs.is_open()) {
throw CadmodelError("Cannot open file: " + filename);
}
auto text = read_file(filename);
read(text);
}
void Cadmodel::read(const std::string& text)
{
lines_ = split(text);
if (lines_.size() < 2) {
throw CadmodelError("Not enough lines in text");
}
validate_1line(lines_[0]);
validate_lastline(lines_[lines_.size() - 1]);
read_facets();
read_endsolid();
}
void Cadmodel::validate_1line(const std::string& line)
{
auto line1_tokens = tokenize(line);
if (line1_tokens.size() < 2) {
throw CadmodelError("Not enough tokens in line 1");
}
if (line1_tokens[0] != "solid") {
throw CadmodelError("Invalid first token in line 1");
}
}
void Cadmodel::validate_lastline(const std::string& line)
{
auto last_line_tokens = tokenize(line);
if (last_line_tokens[0] != "endsolid") {
throw CadmodelError("Invalid last line");
}
}
void Cadmodel::read_facets()
{
index_ = 1;
while (index_ < lines_.size()) {
if (index_ == lines_.size() - 1) {
break;
}
auto facet = read_facet();
facets_.push_back(facet);
}
}
Facet Cadmodel::read_facet()
{
Facet facet;
facet.normal() = read_facet_normal();
read_outer_loop();
auto p1 = read_vertex();
auto p2 = read_vertex();
auto p3 = read_vertex();
facet.points().push_back(p1);
facet.points().push_back(p2);
facet.points().push_back(p3);
read_endloop();
read_endfacet();
return facet;
}
Point Cadmodel::read_facet_normal()
{
auto tokens = get_line_tokens();
if(tokens.size() != 5) {
throw CadmodelError("Invalid facet normal");
}
if (tokens[0] != "facet") {
throw CadmodelError("Invalid facet normal");
}
if (tokens[1] != "normal") {
throw CadmodelError("Invalid facet normal");
}
double x = std::stod(tokens[2]);
double y = std::stod(tokens[3]);
double z = std::stod(tokens[4]);
Point normal(x, y, z);
return normal;
}
std::vector<std::string> Cadmodel::get_line_tokens()
{
if (index_ >= lines_.size()) {
throw CadmodelError("End of file");
}
auto line_tokens = tokenize(lines_[index_]);
index_++;
return line_tokens;
}
void Cadmodel::read_outer_loop()
{
std::vector<std::string> tokens = get_line_tokens();
if (tokens.size() != 2) {
throw CadmodelError("Invalid outer loop");
}
if (tokens[0] != "outer" && tokens[1] != "loop") {
throw CadmodelError("Invalid outer loop");
}
}
Point Cadmodel::read_vertex()
{
std::vector<std::string> tokens = get_line_tokens();
if (tokens.size() != 4) {
throw CadmodelError("Invalid vertex");
}
double x = std::stod(tokens[1]);
double y = std::stod(tokens[2]);
double z = std::stod(tokens[3]);
Point vertex(x, y, z);
return vertex;
}
void Cadmodel::read_endloop()
{
auto tokens = get_line_tokens();
if (tokens.size() != 1) {
throw CadmodelError("Invalid endloop");
}
if (tokens[0] != "endloop") {
throw CadmodelError("Invalid endloop");
}
}
void Cadmodel::read_endfacet()
{
auto tokens = get_line_tokens();
if (tokens.size() != 1) {
throw CadmodelError("Invalid endfacet");
}
if (tokens[0] != "endfacet") {
throw CadmodelError("Invalid endfacet");
}
}
void Cadmodel::read_endsolid()
{
auto tokens = get_line_tokens();
if (tokens.size() != 2) {
throw CadmodelError("Invalid endsolid");
}
if (tokens[0] != "endsolid") {
throw CadmodelError("Invalid endsolid");
}
}