-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_ply_stl.cc
More file actions
150 lines (131 loc) · 4.33 KB
/
file_ply_stl.cc
File metadata and controls
150 lines (131 loc) · 4.33 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//****************************************************************************** // class to read ply files of triangles
//
// Domingo Martín Perandres (c) 2017
//
// Gnu Public Licence
//******************************************************************************
#include "file_ply_stl.h"
using namespace std;
//******************************************************************************
//
//******************************************************************************
int _file_ply::open(const string &File_name)
{
File.open(File_name.c_str(),ios::in);
if (File.good()) return 1;
else {
cout << "Error: the file " << File_name << " cannot be opened" << endl;
return 0;
}
}
//******************************************************************************
//
//******************************************************************************
void _file_ply::get_token(istringstream &Line_stream1,string &Token1)
{
// cout << "buscando en:" << Line_stream1.str() << endl;
getline(Line_stream1,Token1,' ');
// cout << "salida:" << Token1 << endl;
}
//******************************************************************************
//
//******************************************************************************
bool _file_ply::get_new_line(ifstream &File,istringstream &Line_stream)
{
string Line;
if (getline(File,Line)){
// cout << "leido:" << Line << endl;
Line_stream.str(Line);
Line_stream.seekg(0);
return true;
}
else return false;
}
//******************************************************************************
//
//******************************************************************************
void _file_ply::read(vector<_vertex3f> &Vertices,vector<_vertex3i> &Triangles)
{
string Token;
istringstream Line_stream;
setlocale(LC_ALL, "C");
get_new_line(File,Line_stream);
get_token(Line_stream,Token);
if(Token=="ply"){
get_new_line(File,Line_stream);
get_token(Line_stream,Token);
if(Token=="format"){
get_token(Line_stream,Token);
if(Token=="ascii"){
do{
get_new_line(File,Line_stream);
get_token(Line_stream,Token);
} while (Token!="element");
get_token(Line_stream,Token);
if(Token=="vertex"){
get_token(Line_stream,Token);
Vertices.resize(stoi(Token));
do{
get_new_line(File,Line_stream);
get_token(Line_stream,Token);
} while (Token!="element");
get_token(Line_stream,Token);
if(Token=="face"){
get_token(Line_stream,Token);
Triangles.resize(stoi(Token));
do{
get_new_line(File,Line_stream);
get_token(Line_stream,Token);
} while (Token!="end_header");
for (unsigned int i=0;i<Vertices.size();i++){
get_new_line(File,Line_stream);
//
get_token(Line_stream,Token);
Vertices[i].x=stof(Token);
get_token(Line_stream,Token);
Vertices[i].y=stof(Token);
get_token(Line_stream,Token);
Vertices[i].z=stof(Token);
// cout << " leido " << Vertices[i].x << " " << Vertices[i].y << " " << Vertices[i].z << endl;
}
for (unsigned int i=0;i<Triangles.size();i++){
get_new_line(File,Line_stream);
//
get_token(Line_stream,Token);
if (stoi(Token)==3){
get_token(Line_stream,Token);
Triangles[i]._0=stoi(Token);
get_token(Line_stream,Token);
Triangles[i]._1=stoi(Token);
get_token(Line_stream,Token);
Triangles[i]._2=stoi(Token);
}
}
}
else{
cerr << "Error: no face element found" << endl;
}
}
else{
cerr << "Error: no vertex element found" << endl;
}
}
else{
cerr << "Error: only ascii format is allowed" << endl;
}
}
else{
cerr << "Error: no format found" << endl;
}
}
else{
cerr << "Error: the file is not a PLY file" << endl;
}
}
//******************************************************************************
//
//******************************************************************************
void _file_ply::close()
{
File.close();
}