-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOFFRIO.hxx
More file actions
314 lines (254 loc) · 7.6 KB
/
OFFRIO.hxx
File metadata and controls
314 lines (254 loc) · 7.6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
////////////////////////////////////////////////////////////////////
//
// $Id: OFFRIO.hxx 2021/06/05 13:16:51 kanai Exp $
//
// Copyright (c) 2021 Takashi Kanai
// Released under the MIT license
//
////////////////////////////////////////////////////////////////////
#ifndef _OFFRIO_HXX
#define _OFFRIO_HXX 1
#include "envDep.h"
#if defined(_WINDOWS)
#include "stdafx.h"
#endif
#include "mydef.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
#include "timer.hxx"
#include "MeshR.hxx"
#include "RIO.hxx"
#include "gzstream.h"
class OFFRIO : public RIO {
public:
OFFRIO() : RIO() {};
OFFRIO( MeshR& mesh ) : RIO( mesh ) {};
~OFFRIO() {};
bool inputGZFromFile( const char* const filename ) {
//
// file open
//
igzstream ifs; ifs.open( filename );
if ( !ifs.is_open() )
{
std::cerr << "Cannot open " << filename << std::endl;
return false;
}
Timer t;
double time0 = t.get_seconds();
std::string cline;
getline(ifs, cline, '\n'); // OFF
if ( cline.find("OFF") == string::npos )
//if ( cline.compare("OFF") && cline.compare("COFF") )
{
std::cerr << "This is not OFF file. " << filename << std::endl;
return false;
}
// count #vertices, #faces
getline(ifs, cline, '\n'); // OFF
std::istringstream isstr( cline );
int v_count, f_count;
isstr >> v_count;
if ( v_count ) mesh().reservePoints( v_count );
isstr >> f_count;
if ( f_count ) mesh().reserveIndices( f_count );
// read vertices
int v_id = 0;
for ( int i = 0; i < v_count; ++i )
{
getline(ifs, cline, '\n');
char xc[BUFSIZ], yc[BUFSIZ], zc[BUFSIZ];
sscanf( cline.c_str(), "%s%s%s", &xc, &yc, &zc );
float x = atof( xc );
float y = atof( yc );
float z = atof( zc );
mesh().setPoint( v_id, x, y, z );
v_id += nXYZ;
}
// read faces
int f_id = 0;
for ( int i = 0; i < f_count; ++i )
{
getline(ifs, cline, '\n');
char dummy[BUFSIZ];
char xc[BUFSIZ], yc[BUFSIZ], zc[BUFSIZ];
sscanf( cline.c_str(), "%s%s%s%s", &dummy, &xc, &yc, &zc );
unsigned int id0 = atoi( xc );
unsigned int id1 = atoi( yc );
unsigned int id2 = atoi( zc );
mesh().setIndex( f_id, id0 ); ++f_id;
mesh().setIndex( f_id, id1 ); ++f_id;
mesh().setIndex( f_id, id2 ); ++f_id;
}
double time1 = t.get_seconds();
std::cout << "done. ellapsed time: " << time1 - time0 << " sec. " << std::endl;
ifs.close();
mesh().printInfo();
return true;
};
#if 0
bool inputFromFile( std::string& filestr ) {
std::istringstream ifs( filestr );
if ( ifs.fail() )
{
cout << "fail to load file." << endl;
return false;
}
Timer t;
double time0 = t.get_seconds();
std::string cline;
getline(ifs, cline, '\n'); // OFF
//cout << cline.compare("OFF\n") << endl;
#if 1
if ( cline.find("OFF") == string::npos )
{
std::cerr << "This is not OFF file. " << std::endl;
return false;
}
#endif
// count #vertices, #faces
getline(ifs, cline, '\n'); // OFF
std::istringstream isstr( cline );
int v_count, f_count;
isstr >> v_count;
if ( v_count ) mesh().reservePoints( v_count );
isstr >> f_count;
if ( f_count ) mesh().reserveIndices( f_count );
// read vertices
int v_id = 0;
for ( int i = 0; i < v_count; ++i )
{
getline(ifs, cline, '\n');
char xc[BUFSIZ], yc[BUFSIZ], zc[BUFSIZ];
sscanf( cline.c_str(), "%s%s%s", &xc, &yc, &zc );
float x = atof( xc );
float y = atof( yc );
float z = atof( zc );
mesh().setPoint( v_id, x, y, z );
v_id += nXYZ;
}
// read faces
int f_id = 0;
for ( int i = 0; i < f_count; ++i )
{
getline(ifs, cline, '\n');
char dummy[BUFSIZ];
char xc[BUFSIZ], yc[BUFSIZ], zc[BUFSIZ];
sscanf( cline.c_str(), "%s%s%s%s", &dummy, &xc, &yc, &zc );
unsigned int id0 = atoi( xc );
unsigned int id1 = atoi( yc );
unsigned int id2 = atoi( zc );
mesh().setIndex( f_id, id0 ); ++f_id;
mesh().setIndex( f_id, id1 ); ++f_id;
mesh().setIndex( f_id, id2 ); ++f_id;
}
double time1 = t.get_seconds();
std::cout << "done. ellapsed time: " << time1 - time0 << " sec. " << std::endl;
//ifs.close();
mesh().printInfo();
return true;
};
#endif
bool inputFromFile( const char* const filename ) {
//
// file open
//
std::ifstream ifs; ifs.open( filename );
if ( !ifs.is_open() )
{
std::cerr << "Cannot open " << filename << std::endl;
return false;
}
Timer t;
double time0 = t.get_seconds();
std::string cline;
getline(ifs, cline, '\n'); // OFF
if ( cline.find("OFF") == string::npos )
//if ( cline.compare("OFF") && cline.compare("COFF") )
{
std::cerr << "This is not OFF file. " << filename << std::endl;
return false;
}
// count #vertices, #faces
getline(ifs, cline, '\n'); // OFF
std::istringstream isstr( cline );
int v_count, f_count;
isstr >> v_count;
if ( v_count ) mesh().reservePoints( v_count );
isstr >> f_count;
if ( f_count ) mesh().reserveIndices( f_count );
// read vertices
int v_id = 0;
for ( int i = 0; i < v_count; ++i )
{
getline(ifs, cline, '\n');
char xc[BUFSIZ], yc[BUFSIZ], zc[BUFSIZ];
sscanf( cline.c_str(), "%s%s%s", &xc, &yc, &zc );
float x = atof( xc );
float y = atof( yc );
float z = atof( zc );
mesh().setPoint( v_id, x, y, z );
v_id += nXYZ;
}
// read faces
int f_id = 0;
for ( int i = 0; i < f_count; ++i )
{
getline(ifs, cline, '\n');
char dummy[BUFSIZ];
char xc[BUFSIZ], yc[BUFSIZ], zc[BUFSIZ];
sscanf( cline.c_str(), "%s%s%s%s", &dummy, &xc, &yc, &zc );
unsigned int id0 = atoi( xc );
unsigned int id1 = atoi( yc );
unsigned int id2 = atoi( zc );
mesh().setIndex( f_id, id0 ); ++f_id;
mesh().setIndex( f_id, id1 ); ++f_id;
mesh().setIndex( f_id, id2 ); ++f_id;
}
double time1 = t.get_seconds();
std::cout << "done. ellapsed time: " << time1 - time0 << " sec. " << std::endl;
ifs.close();
mesh().printInfo();
return true;
};
bool outputToFile( const char* const filename ) {
std::ofstream ofs( filename ); if ( !ofs ) return false;
std::cout << "Output to file: " << filename << endl;
mesh().printInfo();
// header
ofs << "OFF" << std::endl;
int vn = (int) ((float) mesh().points().size() / (float) nXYZ);
int fn = (int) ((float) mesh().indices().size() / (float) TRIANGLE);
ofs << vn << " " << fn << " 0 " << std::endl;
if ( vn )
{
std::vector<float>& points = mesh().points();
for ( int i = 0; i < vn; ++i )
{
ofs << points[ nXYZ * i ] << " "
<< points[ nXYZ * i + 1 ] << " "
<< points[ nXYZ * i + 2 ] << std::endl;
}
}
if ( fn )
{
std::vector<unsigned int>& indices = mesh().indices();
for ( int i = 0; i < fn; ++i )
{
ofs << "3 ";
for ( int j = 0; j < TRIANGLE; ++j )
{
unsigned int id = indices[ TRIANGLE * i + j ];
ofs << id << " ";
}
ofs << std::endl;
}
}
ofs.close();
return true;
};
};
#endif // _OFFRIO_H