forked from picciau-g/superfacets-2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReader.cpp
executable file
·209 lines (166 loc) · 5.02 KB
/
Reader.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
#include "Reader.h"
#include <sstream>
#include <algorithm>
#include <iostream>
#include <fstream>
Reader::Reader() {}
Reader::Reader(const Reader& ) {}
Reader::~Reader() {}
/**
* @brief Reader::readMeshFile reads file with 2D coordinates of vertices and indices linking them
* @param mesh object in which information is put
* @param path file name
* @return
*/
bool Reader::readMeshFile(Mesh<Vertex2D, Triangle> &mesh, string path)
{
ifstream input(path.c_str());
if (input.is_open() == false) {
cerr << "Error in file " << path << "\nThe file could not exist, be unreadable or incorrect." << endl;
return false;
}
int num_vertices;
input >> num_vertices;
if (num_vertices == 0)
{
cerr << "This is not a valid .tri file: " << path << endl;
return false;
}
mesh.reserveVectorSpace_Vertices(num_vertices);
//legge i vertici aggiustando il dominio..
for (int i = 0; i < num_vertices; i++) {
double x, y;
input >> x;
input >> y;
if (input.eof())
break;
Vertex2D v = Vertex2D(x, y);
mesh.addVertex(v);
}
int num_topSimplexes;
input >> num_topSimplexes;
if(num_topSimplexes == 0)
{
cerr << "This is not a valid .tri file: " << path << endl;
return false;
}
mesh.reserveVectorSpace_TopSimplexes(num_topSimplexes);
//legge i top simplessi
for (int i = 0; i < num_topSimplexes; i++) {
int v[3];
for (int j = 0; j < 3; j++)
input >> v[j];
Triangle t = Triangle(v[0], v[1], v[2]);
mesh.addTopSimplex(t);
}
return true;
}
/**
* @brief Reader::readMeshFile reads file with 3D coordinates of vertices and indices linking them
* @param mesh object in which information is put
* @param path file name
* @return
*/
bool Reader::readMeshFile(Mesh<Vertex3D, Triangle> &mesh, string path)
{
ifstream input(path.c_str());
if (input.is_open() == false) {
cerr << "Error in file " << path << "\nThe file could not exist, be unreadable or incorrect." << endl;
return false;
}
int num_vertices;
input >> num_vertices;
if (num_vertices == 0)
{
cerr << "This is not a valid .tri file: " << path << endl;
return false;
}
mesh.reserveVectorSpace_Vertices(num_vertices);
//reads vertices
for (int i = 0; i < num_vertices; i++) {
double x, y, z;
input >> x;
input >> y;
input >> z;
if (input.eof())
break;
Vertex3D v = Vertex3D(x, y, z);
mesh.addVertex(v);
}
int num_topSimplexes;
input >> num_topSimplexes;
if(num_topSimplexes == 0)
{
cerr << "This is not a valid .tri file: " << path << endl;
return false;
}
mesh.reserveVectorSpace_TopSimplexes(num_topSimplexes);
//reads top simplexes
for (int i = 0; i < num_topSimplexes; i++) {
int v[3];
for (int j = 0; j < 3; j++)
input >> v[j];
Triangle t = Triangle(v[0], v[1], v[2]);
mesh.addTopSimplex(t);
}
return true;
}
/**
* @author Giulia Picciau
* @date 04-15-2014
* @brief Reader::readOFFMesh reads triangle meshes in .off format
* @param mesh where to put the information
* @param path path to file
* @return
*/
bool Reader::readOFFMesh(Mesh<Vertex3D, Triangle> &mesh, string path){
ifstream input(path.c_str());
if(input.is_open()==false){
cerr << "Error in file " << path << "\nThe file could not exist, be unreadable or incorrect." << endl;
return false;
}
string line;
getline(input,line);
if(line.compare("OFF")){
cerr << "Error in file " << path << "\nThe file could not exist, be unreadable or incorrect." << endl;
return false;
}
getline(input,line);
istringstream iss(line);
//iss.
unsigned long int num_vertices, num_topsimplexes, num_edges; //edges is useless
iss >> num_vertices;//input >> num_vertices;
iss >> num_topsimplexes;//input >> num_topsimplexes;
iss >> num_edges;
cout<<"V "<<num_vertices<<" T "<<num_topsimplexes<<endl;
if(num_vertices == 0 || num_topsimplexes == 0){
cerr<< "Number of simplexes is 0 Not a valid .off file "<<path<<endl;
return false;
}
mesh.reserveVectorSpace_Vertices(num_vertices);
//insert vertices
for(unsigned long int i=0;i<num_vertices;i++){
double x,y,z;
input >> x;
input >> y;
input >> z;
if(input.eof())
break;
Vertex3D v = Vertex3D(x,y,z);
mesh.addVertex(v);
}
mesh.reserveVectorSpace_TopSimplexes(num_topsimplexes);
//insert top simplexes
for(unsigned long int i=0;i<num_topsimplexes;i++){
int v[4];
for(int j=0;j<4;j++)
input >> v[j];
if(v[0] != 3){
cout<<"Where's the 3??? Not a valid .off file: "<<path<<endl;
return false;
}
Triangle t = Triangle(v[1],v[2],v[3]);
mesh.addTopSimplex(t);
}
return true;
}