-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMeshIO.cpp
More file actions
604 lines (504 loc) · 18.4 KB
/
Copy pathMeshIO.cpp
File metadata and controls
604 lines (504 loc) · 18.4 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//============================================================
// MeshIO.cpp
// Keenan Crane
//
#include "MeshIO.h"
#include "HalfEdge.h"
#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <cmath>
#include <set>
using namespace std;
namespace tcods
{
class Index
{
public:
Index( void )
{}
Index( int p, int t, int n )
: position( p ), texcoord( t ), normal( n )
{}
int position;
int texcoord;
int normal;
};
class MeshData
{
public:
vector<Vector> positions;
vector<Vector> texcoords;
vector<Vector> normals;
vector< vector< Index > > indices;
};
void MeshIO :: readOBJ( istream& in, Mesh& mesh )
{
MeshData data;
readMeshData( in, data );
buildMesh( data, mesh );
}
void MeshIO :: writeOBJ( ostream& out, const Mesh& mesh )
{
int currentIndex = 1;
map< VertexCIter, int > vertexIndex;
const vector<Vertex>& vertices( mesh.vertices );
const vector<Face>& faces( mesh.faces );
for( VertexCIter i = vertices.begin(); i != vertices.end(); i++ )
{
out << "v " << i->position[0] << " "
<< i->position[1] << " "
<< i->position[2] << endl;
vertexIndex[ i ] = currentIndex;
currentIndex++;
}
for( FaceCIter f = faces.begin(); f != faces.end(); f++ )
{
HalfEdgeIter he = f->he;
for( int j = 0; j < 3; j++ )
{
out << "vt " << he->texcoord.x << " " << he->texcoord.y << endl;
he = he->next;
}
}
for( FaceCIter i = faces.begin(); i != faces.end(); i++ )
{
HalfEdgeIter he = i->he;
// don't write boundary faces
if( he->onBoundary )
{
continue;
}
out << "f ";
int j = 0;
do
{
out << vertexIndex[ he->from ] << "/" << 1+(i->index*3+j) << " ";
he = he->next;
j++;
}
while( he != i->he );
out << endl;
}
}
void MeshIO :: writeOBJX( ostream& out, const Mesh& mesh )
{
out.precision( 10 );
int currentIndex = 1;
map< VertexCIter, int > vertexIndex;
const vector<Vertex>& vertices( mesh.vertices );
const vector<Face>& faces( mesh.faces );
for( VertexCIter i = vertices.begin(); i != vertices.end(); i++ )
{
out << "v " << i->position[0] << " "
<< i->position[1] << " "
<< i->position[2] << endl;
vertexIndex[ i ] = currentIndex;
currentIndex++;
}
for( VertexCIter i = vertices.begin(); i != vertices.end(); i++ )
{
Vector u( 0., 0., 0. );
HalfEdgeIter he = i->out;
do
{
FaceCIter f = he->face;
if( !f->isBoundary() )
{
double alpha = f->alpha;
Vector w( cos(alpha), sin(alpha), 0. );
u += f->toGlobal( w );
}
he = he->flip->next;
}
while( he != i->out );
u.normalize();
out << "vf " << u.x << " " << u.y << " " << u.z << endl;
}
for( FaceCIter i = faces.begin(); i != faces.end(); i++ )
{
HalfEdgeIter he = i->he;
// don't write boundary faces
if( he->onBoundary )
{
continue;
}
out << "f ";
do
{
out << vertexIndex[ he->from ] << " ";
he = he->next;
}
while( he != i->he );
out << endl;
}
}
void MeshIO :: writeEOBJ( ostream& out, const Mesh& mesh )
{
out.precision( 10 );
int currentIndex = 1;
map< VertexCIter, int > vertexIndex;
map< FaceCIter, int > faceIndex;
const vector<Vertex>& vertices( mesh.vertices );
const vector<Face>& faces( mesh.faces );
for( VertexCIter i = vertices.begin(); i != vertices.end(); i++ )
{
out << "v " << i->position[0] << " "
<< i->position[1] << " "
<< i->position[2] << endl;
vertexIndex[ i ] = currentIndex;
currentIndex++;
}
currentIndex = 1;
for( FaceCIter i = faces.begin(); i != faces.end(); i++ )
{
HalfEdgeIter he = i->he;
// don't write boundary faces
if( he->onBoundary )
{
continue;
}
faceIndex[ i ] = currentIndex;
currentIndex++;
out << "f ";
do
{
out << vertexIndex[ he->from ] << " ";
he = he->next;
}
while( he != i->he );
out << endl;
}
for( FaceCIter i = faces.begin(); i != faces.end(); i++ )
{
HalfEdgeIter he = i->he;
// don't write vectors on boundary faces
if( he->onBoundary )
{
continue;
}
out << "# attrs f " << faceIndex[ i ] << " ";
double alpha = i->alpha;
Vector w( cos(alpha), sin(alpha), 0. );
Vector u = i->toGlobal( w );
out << u.x << " " << u.y << " " << u.z;
out << endl;
}
}
int modulo( int a, int b )
{
a += b*(1+abs(a/b));
return a % b;
}
void MeshIO :: writeJVX( ostream& out, const Mesh& mesh, const vector<int>& singularityIndices )
{
out.precision( 10 );
out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>\n";
out << "<!DOCTYPE jvx-model SYSTEM \"http://www.javaview.de/rsrc/jvx.dtd\">\n";
out << "<jvx-model>\n";
out << "\t<geometries>\n";
out << "\t\t<geometry name=\"trivialconnections_output\">\n";
out << "\t\t\t<pointSet dim=\"3\">\n";
out << "\t\t\t\t<points num=\"" << (int)mesh.vertices.size() << "\">\n";
for( vector<VertexIter>::const_iterator i = mesh.index2vertex.begin();
i != mesh.index2vertex.end();
i ++ )
{
Vector p = (*i)->position;
out << "\t\t\t\t\t<p>" << p.x << " " << p.y << " " << p.z << "</p>\n";
}
out << "\t\t\t\t</points>\n";
out << "\t\t\t</pointSet>\n";
out << "\t\t\t<faceSet>\n";
out << "\t\t\t\t<faces num=\"" << (int)mesh.faces.size() << "\">\n";
for( vector<FaceIter>::const_iterator i = mesh.index2face.begin();
i != mesh.index2face.end();
i ++ )
{
FaceCIter f = *i;
int ii = f->he->from->index;
int j = f->he->next->from->index;
int k = f->he->next->next->from->index;
out << "\t\t\t\t\t<f>" << ii << " " << j << " " << k << "</f>\n";
}
out << "\t\t\t\t</faces>\n";
out << "\t\t\t</faceSet>\n";
out << "\t\t\t<vectorField name=\"First field\" base=\"element\">\n";
out << "\t\t\t\t<vectors num=\"" << (int)mesh.faces.size() << "\">\n";
for( vector<FaceIter>::const_iterator i = mesh.index2face.begin();
i != mesh.index2face.end();
i ++ )
{
FaceCIter f = *i;
double alpha = f->alpha;
Vector e1, e2; f->frame( e1, e2 );
Vector v = cos(alpha)*e1 + sin(alpha)*e2;
out << "\t\t\t\t\t<v>" << v.x << " " << v.y << " " << v.z << "</v>\n";
}
out << "\t\t\t\t</vectors>\n";
out << "\t\t\t</vectorField>\n";
out << "\t\t\t<vectorField name=\"Second field\" base=\"element\">\n";
out << "\t\t\t\t<vectors num=\"" << (int)mesh.faces.size() << "\">\n";
for( vector<FaceIter>::const_iterator i = mesh.index2face.begin();
i != mesh.index2face.end();
i ++ )
{
FaceCIter f = *i;
double alpha = f->alpha + M_PI/2.;
Vector e1, e2; f->frame( e1, e2 );
Vector v = cos(alpha)*e1 + sin(alpha)*e2;
out << "\t\t\t\t\t<v>" << v.x << " " << v.y << " " << v.z << "</v>\n";
}
out << "\t\t\t\t</vectors>\n";
out << "\t\t\t</vectorField>\n";
out << "\t\t\t<matchingSet>\n";
out << "\t\t\t\t<matchings num=\"" << (int)mesh.edges.size() << "\">\n";
for( vector<EdgeIter>::const_iterator i = mesh.index2edge.begin();
i != mesh.index2edge.end();
i ++ )
{
EdgeCIter e = *i;
HalfEdgeCIter he = e->he;
if( he->from->index > he->flip->from->index ) he = he->flip;
double theta = e->theta;
double alphaI = he->face->alpha;
double alphaJ = he->flip->face->alpha;
double alphaIp = Mesh::parallelTransport( alphaI, he ) - theta;
int m = (int) round(( alphaJ - alphaIp ) / ( M_PI / 2. ));
m = modulo( m, 4 );
int ii = he->face->index;
int j = he->flip->face->index;
out << "\t\t\t\t\t<m>" << ii << " " << j << " " << m << "</m>\n";
}
out << "\t\t\t\t</matchings>\n";
out << "\t\t\t</matchingSet>\n";
out << "\t\t</geometry>\n";
out << "\t</geometries>\n";
out << "<!-- singular vertices -->\n";
for( vector<int>::const_iterator i = singularityIndices.begin(); i != singularityIndices.end(); i++ )
{
out << "<!-- " << *i << " -->\n";
}
out << "</jvx-model>\n";
}
void MeshIO :: readMeshData( istream& in, MeshData& data )
{
string line;
while( getline( in, line ))
{
stringstream ss( line );
string token;
ss >> token;
if( token == "v" ) readPosition( ss, data );
if( token == "vt" ) readTexCoord( ss, data );
if( token == "vn" ) readNormal ( ss, data );
if( token == "f" ) readFace ( ss, data );
}
}
void MeshIO :: preallocateMeshElements( const MeshData& data, Mesh& mesh )
{
// count the number of edges
set< pair<int,int> > edges;
for( vector< vector< Index > >::const_iterator f = data.indices.begin();
f != data.indices.end();
f ++ )
{
for( unsigned int I = 0; I < f->size(); I++ )
{
int J = (I+1) % f->size();
int i = (*f)[I].position;
int j = (*f)[J].position;
if( i > j ) swap( i, j );
edges.insert( pair<int,int>( i, j ));
}
}
int nV = static_cast<int>(data.positions.size());
int nE = static_cast<int>(edges.size());
int nF = static_cast<int>(data.indices.size());
int nHE = 2*nE;
int chi = nV - nE + nF;
int nB = 2 - chi; // (conservative approximation of number of boundary cycles)
mesh.vertices.reserve( nV );
mesh.edges.reserve( nE );
mesh.faces.reserve( nF + nB );
mesh.halfedges.reserve( nHE );
}
void MeshIO :: buildMesh( const MeshData& data, Mesh& mesh )
{
map< pair< int, int >, HalfEdgeIter > existingHalfEdges;
map< int, VertexIter > indexToVertex;
map< HalfEdgeIter, bool > hasFlipEdge;
preallocateMeshElements( data, mesh );
// allocate a vertex for each position in the data and construct
// a map from vertex indices to vertex pointers
for( unsigned int i = 0; i < data.positions.size(); i++ )
{
VertexIter newVertex = mesh.vertices.insert( mesh.vertices.end(), Vertex() );
newVertex->position = data.positions[ i ];
indexToVertex[ i ] = newVertex;
}
// insert each face into the mesh
for( vector< vector< Index > >::const_iterator f = data.indices.begin();
f != data.indices.end();
f ++ )
{
// create a new face
FaceIter newFace = mesh.faces.insert( mesh.faces.end(), Face());
// create a new half edge for each edge of the current face
int N = static_cast<int>(f->size());
vector< HalfEdgeIter > hes( N );
for( int i = 0; i < N; i++ )
{
hes[ i ] = mesh.halfedges.insert( mesh.halfedges.end(), HalfEdge());
}
// initialize these new halfedges
for( int i = 0; i < N; i++ )
{
// the current halfedge goes from vertex a to vertex b
int a = (*f)[ i ].position;
int b = (*f)[ (i+1) % N ].position;
// set current halfedge's attributes
hes[ i ]->next = hes[ (i+1) % N ];
hes[ i ]->from = indexToVertex[ a ];
int t = (*f)[i].texcoord;
int n = (*f)[i].normal;
if( t >= 0 ) hes[ i ]->texcoord = data.texcoords[ t ];
else hes[ i ]->texcoord = Vector( 0., 0., 0. );
if( n >= 0 ) hes[ i ]->normal = data.normals [ n ];
else hes[ i ]->normal = Vector( 0., 0., 0. );
hes[ i ]->onBoundary = false;
// keep track of which halfedges have flip edges defined (for detecting boundaries)
hasFlipEdge[ hes[ i ]] = false;
// point vertex a at the current halfedge
indexToVertex[ a ]->out = hes[ i ];
// point the new face and this half edge to each-other
hes[ i ]->face = newFace;
newFace->he = hes[ i ];
// if we've created an edge between a and b in the past, it is the
// flip edge of the current halfedge
if( existingHalfEdges.find( pair<int,int>( a, b )) != existingHalfEdges.end())
{
hes[ i ]->flip = existingHalfEdges[ pair<int,int>( a, b ) ];
hes[ i ]->flip->flip = hes[ i ];
hes[ i ]->edge = hes[ i ]->flip->edge;
hasFlipEdge[ hes[ i ]] = true;
hasFlipEdge[ hes[ i ]->flip ] = true;
}
else // otherwise, create an edge connected to the current halfedge
{
hes[ i ]->edge = mesh.edges.insert( mesh.edges.end(), Edge());
hes[ i ]->edge->he = hes[i];
}
// record the fact that we've created a halfedge from a to b
existingHalfEdges[ pair<int,int>( a, b ) ] = hes[ i ];
existingHalfEdges[ pair<int,int>( b, a ) ] = hes[ i ];
}
}
// insert extra faces for each boundary cycle
for( HalfEdgeIter currentHE = mesh.halfedges.begin();
currentHE != mesh.halfedges.end();
currentHE ++ )
{
// if we find a halfedge with no flip edge defined, create
// a new face and link it to the corresponding boundary cycle
if( !hasFlipEdge[ currentHE ] )
{
// create a new face
FaceIter newFace = mesh.faces.insert( mesh.faces.end(), Face());
// walk along this boundary cycle
vector<HalfEdgeIter> boundaryCycle;
HalfEdgeIter he = currentHE;
do
{
// create a new halfedge on the boundary face
HalfEdgeIter newHE = mesh.halfedges.insert( mesh.halfedges.end(), HalfEdge());
// mark only the halfedge on the boundary face as being on the boundary
newHE->onBoundary = true;
// link the current halfedge in the cycle to its new flip edge
he->flip = newHE;
// grab the next halfedge along the boundary by finding
// the next halfedge around the current vertex that doesn't
// have a flip edge defined
HalfEdgeIter nextHE = he->next;
while( hasFlipEdge[ nextHE ] )
{
nextHE = nextHE->flip->next;
}
// set attributes for the flip edge (we'll set ->next below)
newHE->flip = he;
newHE->from = nextHE->from;
newHE->edge = he->edge;
newHE->face = newFace;
newHE->normal = nextHE->normal;
newHE->texcoord = nextHE->texcoord;
// point the new face to this half edge
newFace->he = newHE;
// keep track of all the new halfedges in the boundary cycle
boundaryCycle.push_back( newHE );
// continue to walk along the cycle
he = nextHE;
} while( he != currentHE );
// link together the cycle of boundary halfedges
unsigned int N = static_cast<unsigned int>(boundaryCycle.size());
for( unsigned int i = 0; i < N; i++ )
{
boundaryCycle[ i ]->next = boundaryCycle[ (i+N-1)%N ];
hasFlipEdge[ boundaryCycle[i] ] = true;
hasFlipEdge[ boundaryCycle[i]->flip ] = true;
}
}
}
}
void MeshIO :: readPosition( stringstream& ss, MeshData& data )
{
double x, y, z;
ss >> x >> y >> z;
data.positions.push_back( Vector( x, y, z ));
}
void MeshIO :: readTexCoord( stringstream& ss, MeshData& data )
{
double u, v;
ss >> u >> v;
data.texcoords.push_back( Vector( u, v, 0. ));
}
void MeshIO :: readNormal( stringstream& ss, MeshData& data )
{
double x, y, z;
ss >> x >> y >> z;
data.normals.push_back( Vector( x, y, z ));
}
void MeshIO :: readFace( stringstream& ss, MeshData &data )
{
vector<Index> faceIndices;
string token;
while( ss >> token )
{
faceIndices.push_back( parseFaceIndex( token ));
}
data.indices.push_back( faceIndices );
}
Index MeshIO :: parseFaceIndex( const string& token )
{
// parse indices of the form
//
// p/[t]/[n]
//
// where p is an index into positions, t is an index into
// texcoords, n is an index into normals, and [.] indicates
// that an index is optional
stringstream in( token );
string indexstring;
int indices[3] = { -1, -1, -1 };
int i = 0;
while( getline( in, indexstring, '/' ))
{
stringstream ss( indexstring );
ss >> indices[i++];
}
// decrement since indices in OBJ files are 1-based
return Index( indices[0]-1,
indices[1]-1,
indices[2]-1 );
}
}