Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/objMeshOrientable.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 #if defined(_WIN32) || defined(WIN32)
30 #pragma warning(disable : 4996)
31 #pragma warning(disable : 4267)
32 #pragma warning(disable : 4244)
33 #endif
34
35 #include "float.h"
36 #include <vector>
37 #include <set>
38 #include <string>
39 #include <map>
40 #include <fstream>
41 #include <sstream>
42 #include <algorithm>
43 #include <assert.h>
44 using namespace std;
45 #include "objMeshOrientable.h"
46
47 /*
48 Generates a half edge datastructure, assuming tha the given obj mesh is orientable.
49 Author: Jernej Barbic, 2004
50 */
51
52 void ObjMeshOrientable::HalfEdge::flipOrientation()
53 {
54 int buffer = startVertex_;
55 startVertex_ = endVertex_;
56 endVertex_ = buffer;
57 }
58
59 ObjMeshOrientable::~ObjMeshOrientable()
60 {
61 if (internalAllocation)
62 delete(objMesh);
63 }
64
65 ObjMeshOrientable::ObjMeshOrientable(const std::string & filename, int generateHalfEdges, int * numOrientationFlips_, int verbose)
66 {
67 internalAllocation = 1;
68 objMesh = new ObjMesh(filename, ObjMesh::ASCII, verbose);
69 Init(generateHalfEdges, numOrientationFlips_, verbose);
70 }
71
72 ObjMeshOrientable::ObjMeshOrientable(ObjMesh * objMesh, int generateHalfEdges, int * numOrientationFlips_, int verbose)
73 {
74 internalAllocation = 0;
75 this->objMesh = objMesh;
76 Init(generateHalfEdges, numOrientationFlips_, verbose);
77 }
78
79 void ObjMeshOrientable::Init(int generateHalfEdges, int * numOrientationFlips_, int verbose)
80 {
81 if (generateHalfEdges)
82 {
83 // TODO: culprit
84 int numOrientationFlips = GenerateHalfEdgeDataStructure(verbose);
85
86 if (numOrientationFlips_ != NULL)
87 *numOrientationFlips_ = numOrientationFlips;
88
89 if (numOrientationFlips < 0)
90 throw 1;
91 }
92 }
93
94 void ObjMeshOrientable::PrintHalfEdges()
95 {
96 for (unsigned int i=0; i<halfEdges_.size(); i++)
97 {
98 cout << "Half edge "<< i << " :" << endl;
99 cout << " Opposite edge: " << halfEdges_[i].opposite() << endl;
100 cout << " Next edge: " << halfEdges_[i].next() << endl;
101 cout << " Group: " << halfEdges_[i].groupID() << endl;
102 cout << " Face: " << halfEdges_[i].face() << endl;
103 cout << " Start vertex: " << halfEdges_[i].startVertex() << endl;
104 cout << " End vertex: " << halfEdges_[i].endVertex() << endl;
105 cout << " Start vertex (local): " << halfEdges_[i].startV() << endl;
106 cout << " End vertex (local): " << halfEdges_[i].endV() << endl;
107 cout << " Is boundary: " << halfEdges_[i].isBoundary() << endl;
108 }
109 }
110
111 // returns the number of edges flipped
112 int ObjMeshOrientable::GenerateHalfEdgeDataStructure(int verbose)
113 {
114 if (verbose)
115 std::cout << "Building the half edge data structure..." << std::endl;
116
117 // Step 1: iterate over all faces
118 // for each face, add all the edges onto the list of half-edges
119
120 if (verbose)
121 std::cout << "Step 1: Generating the list of half edges..." << std::endl;
122
123 //typedef std::vector<ObjMesh::Group> SGroup;
124
125 int coutCounter = 0;
126
127 for(unsigned int i = 0; i < objMesh->getNumGroups(); i++ )
128 {
129 const ObjMesh::Group * currentGroup = objMesh->getGroupHandle(i);
130
131 if (verbose)
132 {
133 std::cout << " Processing obj group '" << currentGroup->getName() << std::endl;
134 std::cout << " Iterating through group faces..." << std::endl;
135 }
136
137 for( unsigned int iFace = 0; iFace < currentGroup->getNumFaces(); ++iFace )
138 {
139 ObjMesh::Face face = currentGroup->getFace(iFace); // get face whose number is iFace
140
141 if (coutCounter < 100)
142 {
143 if (verbose)
144 std::cout << face.getNumVertices() ;
145 coutCounter++;
146 }
147 if (coutCounter == 100)
148 {
149 if (verbose)
150 cout << "...[and more]";
151 coutCounter++;
152 }
153
154 unsigned int edgesSoFar = halfEdges_.size();
155
156 for ( unsigned int iVertex = 0; iVertex < face.getNumVertices(); ++iVertex )
157 {
158 // create a half edge for each edge, store -1 for half-edge adjacent edge for now
159 // index vertices starting from 0
160 int nextEdge = edgesSoFar + ((iVertex + 1) % face.getNumVertices());
161 HalfEdge halfEdge(edgesSoFar + iVertex, face.getVertex(iVertex).getPositionIndex(), face.getVertex((iVertex + 1) % face.getNumVertices()).getPositionIndex(), iVertex, (iVertex + 1) % face.getNumVertices(), i, iFace, -1, nextEdge);
162
163 halfEdges_.push_back(halfEdge);
164 }
165 }
166 if (verbose)
167 std::cout << std::endl;
168 }
169
170 /*
171 for (unsigned int i=0; i<halfEdges_.size(); i++)
172 {
173 cout << "Half edge "<< i << " :" << endl;
174 cout << " Opposite edge: " << halfEdges_[i].opposite() << endl;
175 cout << " Next edge: " << halfEdges_[i].next() << endl;
176 cout << " Group: " << halfEdges_[i].groupID() << endl;
177 cout << " Face: " << halfEdges_[i].face() << endl;
178 cout << " Start vertex: " << halfEdges_[i].startVertex() << endl;
179 cout << " End vertex: " << halfEdges_[i].endVertex() << endl;
180 cout << " Start vertex (local): " << halfEdges_[i].startV() << endl;
181 cout << " End vertex (local): " << halfEdges_[i].endV() << endl;
182 cout << " Is boundary: " << halfEdges_[i].isBoundary() << endl;
183 }
184 */
185
186 // Step 2: build correspondence among half-dges
187 // for each half-edge, search for the opposite half-edge, if it exists
188
189 if (verbose)
190 {
191 std::cout << "Step 2: Building correspondence among half-edges..." << std::endl;
192 std::cout << "Boundary edges: ";
193 }
194
195 // insert all edges into a binary tree
196
197 typedef std::multimap<std::pair< unsigned int, unsigned int > , unsigned int> BinaryTree;
198 BinaryTree edges;
199
200 for (unsigned int i=0; i < halfEdges_.size(); i++)
201 {
202 int vertex1 = halfEdges_[i].startVertex();
203 int vertex2 = halfEdges_[i].endVertex();
204
205 if (vertex1 == vertex2)
206 {
207 if (verbose)
208 {
209 std::cout << "Error: encountered a degenerated edge with equal starting and ending vertex." << std::endl;
210 std::cout << " Group:" << halfEdges_[i].groupID() << " Face #: " << halfEdges_[i].face() << "Vertex ID: " << vertex1 << std::endl;
211 }
212 return -1;
213 }
214
215 if (vertex1 > vertex2) // swap
216 {
217 int buffer = vertex1;
218 vertex1 = vertex2;
219 vertex2 = buffer;
220 }
221
222 std::pair<unsigned int, unsigned int> vertices(vertex1,vertex2);
223 edges.insert(std::make_pair(vertices,i));
224 }
225
226 // retrieve one by one and build correspondence
227 for (unsigned int i=0; i < halfEdges_.size(); i++)
228 {
229 int vertex1 = halfEdges_[i].startVertex();
230 int vertex2 = halfEdges_[i].endVertex();
231
232 if (vertex1 > vertex2) // swap
233 {
234 int buffer = vertex1;
235 vertex1 = vertex2;
236 vertex2 = buffer;
237 }
238
239 std::pair<unsigned int, unsigned int> vertices(vertex1,vertex2);
240
241 // search for the edge
242
243 int hits = 0;
244 int candidates = 0;
245 BinaryTree::iterator pos;
246 for (pos = edges.lower_bound(vertices); pos != edges.upper_bound(vertices); ++pos)
247 {
248 candidates++;
249 // check if we found ourselves
250 if (pos->second != i)
251 {
252 // not ourselves
253 halfEdges_[i].setOpposite(pos->second);
254 hits++;
255 }
256 }
257
258 if (candidates >= 3)
259 {
260 if (verbose)
261 std::cout << "Error: encountered an edge that appears in more than two triangles. Geometry is non-manifold. Exiting." << std::endl;
262 int faceNum = halfEdges_[i].face();
263 if (verbose)
264 {
265 std::cout << " Group:" << halfEdges_[i].groupID() << std::endl << " Face #: " << faceNum << std::endl;
266 std::cout << " Edge: " << vertex1 << " " << vertex2 << std::endl;
267 std::cout << " Vertices: " << objMesh->getPosition(vertex1) << " " << objMesh->getPosition(vertex2) << std::endl;
268 }
269 return -1;
270 }
271
272 if (hits == 0) // boundary edge
273 {
274 //std::cout << "B";
275 if (verbose)
276 std::cout << "B(" << vertex1 << "," << vertex2 << ") ";
277 boundaryEdges_.push_back(i);
278 }
279 }
280
281 if (verbose)
282 std::cout << " total: " << boundaryEdges_.size() << std::endl;
283
284 /*
285 for (unsigned int i=0; i<halfEdges_.size(); i++)
286 {
287 cout << "Half edge "<< i << " :" << endl;
288 cout << " Opposite edge: " << halfEdges_[i].opposite() << endl;
289 cout << " Next edge: " << halfEdges_[i].next() << endl;
290 cout << " Group: " << halfEdges_[i].groupID() << endl;
291 cout << " Face: " << halfEdges_[i].face() << endl;
292 cout << " Start vertex: " << halfEdges_[i].startVertex() << endl;
293 cout << " End vertex: " << halfEdges_[i].endVertex() << endl;
294 cout << " Start vertex (local): " << halfEdges_[i].startV() << endl;
295 cout << " End vertex (local): " << halfEdges_[i].endV() << endl;
296 cout << " Is boundary: " << halfEdges_[i].isBoundary() << endl;
297 }
298 */
299 // now, each half-edge knows its mirror edge, but orientations of faces might be inconsistent
300
301 // orient all half-edges consistently
302 if (verbose)
303 std::cout << "Step 3: Attempting to orient the faces coherently..." << std::endl;
304
305 // generate marks for all the edges
306 std::vector<int> marks(halfEdges_.size(), 0);
307
308 // initialize queue
309 std::set<int> queue;
310
311 connectedComponents = 0;
312 int numOrientationFlips = 0;
313
314 while(1) // breakable
315 {
316 // find the first un-marked edge and queue it
317 unsigned int unmarkedEdge;
318 for (unmarkedEdge = 0; unmarkedEdge < halfEdges_.size(); unmarkedEdge++)
319 {
320 if (marks[unmarkedEdge] == 0)
321 break; // found an unmarked edge
322 }
323
324 if (unmarkedEdge == halfEdges_.size()) // no unmarked edge was found
325 {
326 break; // out of while; we are done
327 }
328 else
329 {
330 if (verbose)
331 cout << "Found a new connected component. Seed half-edge is: " << unmarkedEdge << endl;
332 connectedComponents++;
333 queue.insert(unmarkedEdge);
334
335 while(queue.size() > 0)
336 {
337 int edge = *(queue.begin());
338 queue.erase(queue.begin());
339
340 //std::cout << "Retrieved edge from queue: " << edge << " Queue size: " << queue.size() << std::endl;
341 //cout << "The edge is boundary: " << halfEdges_[edge].isBoundary() << endl;
342
343 //std::cout << "Marking all the edges on this face: ";
344 // first, mark all the edges on this face as visited
345 int loop = edge;
346 do
347 {
348 marks[loop] = 1;
349 //std::cout << loop << " ";
350 loop = halfEdges_[loop].next();
351 }
352 while (loop != edge);
353 //std::cout << std::endl;
354
355 // check if edge is consistent with the opposite edge orientation
356 // careful: edge might be on the boundary
357 // find a non-boundary edge on the same face (if it exists)
358 //std::cout << "Seeking for a non-boundary edge on this face...";
359 loop = edge;
360 int exitFlag = 0;
361
362 while ((halfEdges_[loop].isBoundary()) && (exitFlag == 0))
363 {
364 //cout << loop << " ";
365 loop = halfEdges_[loop].next();
366 if (loop == edge) // all edges are boundary
367 exitFlag = 1;
368 }
369
370 if (exitFlag == 1) // all edges are boundary; this is an isolated face
371 {
372 if (verbose)
373 cout << "Encountered an isolated face." << endl;
374 //cout << "none found." << endl;
375 continue; // no need to queue anything or flip anything, this was an isolated face
376 // also, this case can only happen during the first iteration of the while loop, which will also be the last one
377 }
378
379 edge = loop; // now, edge is a non-boundary halfedge
380
381 //std::cout << "found non-boundary edge: " << edge << std::endl;
382 //std::cout << "opposite edge is: " << halfEdges_[edge].opposite() << std::endl;
383
384 bool orientationFlipNecessary = (marks[halfEdges_[edge].opposite()] == 1) && (halfEdges_[edge].startVertex() == (edgeOpposite(halfEdges_[edge])).startVertex());
385
386 //std::cout << "Orientation flip necessary for this face: " << orientationFlipNecessary << std::endl;
387
388 if (orientationFlipNecessary)
389 {
390 // flip all edges along this face
391 //cout << "Orientation flip" << endl;
392 numOrientationFlips++;
393 loop = edge;
394 int cache = 0;
395 do
396 {
397 int nextO = halfEdges_[loop].next();
398 halfEdges_[loop].setNext(cache);
399 cache = loop;
400 halfEdges_[loop].flipOrientation(); // flip orientation
401 loop = nextO;
402 }
403 while (loop != edge);
404 halfEdges_[loop].setNext(cache);
405
406 int groupID = halfEdges_[loop].groupID();
407 int faceID = halfEdges_[loop].face();
408
409 ObjMesh::Group * currentGroup = (ObjMesh::Group*) objMesh->getGroupHandle(groupID);
410
411 if (verbose)
412 currentGroup->getFace(faceID).printVertices();
413
414 currentGroup->reverseFace(faceID);
415
416 if (verbose)
417 currentGroup->getFace(faceID).printVertices();
418 }
419
420 // check if new orientation is consistent eveywhere along the face
421 // if not, surface is not orientable
422 // at the same time, queue the opposite edges if they are not marked already
423 loop = edge;
424 do
425 {
426 if (!halfEdges_[loop].isBoundary()) // skip boundary edges
427 {
428 // if opposite unmarked, queue the opposite edge
429 if (marks[halfEdges_[loop].opposite()] == 0)
430 {
431 queue.insert(halfEdges_[loop].opposite());
432 //marks[halfEdges_[loop].opposite()] = 1; // JNB, 2008
433 //std::cout << "visiting edge: " << loop << " pushing opposite: " << halfEdges_[loop].opposite() << std::endl;
434 }
435 else
436 {
437 // opposite edge is marked as already visited
438 // if orientation consistent, do nothing
439 // if orientation not consistent, surface is not orientable
440
441 bool orientationConsistent = (halfEdges_[loop].startVertex() == (edgeOpposite(halfEdges_[loop])).endVertex());
442
443 //std::cout << "visiting edge: " << loop << " opposite marked " << std::endl;
444
445 if (!orientationConsistent)
446 {
447 if (verbose)
448 std::cout << "Error: surface is non-orientable. Offending edge: [" << halfEdges_[loop].startVertex() << "," << halfEdges_[loop].endVertex() << "]" << std::endl;
449 return -1;
450 }
451 }
452 }
453 loop = halfEdges_[loop].next();
454 }
455 while (loop != edge);
456
457 }
458 }
459 } // end of while
460
461 if (verbose)
462 printf("Consistent orientation generated. Performed %d orientation flips.\n", numOrientationFlips);
463
464 //PrintHalfEdges();
465
466 // step 4: for every vertex, find a half-edge emanating out of it
467 if (verbose)
468 {
469 std::cout << "Step 4: For every vertex, caching a half-edge emanating out of it..." << std::endl;
470 std::cout << " For every face, caching a half-edge on it..." << std::endl;
471 }
472
473 for (unsigned int i=0; i< objMesh->getNumVertices(); i++)
474 edgesAtVertices_.push_back(-1); // value of -1 corresponds to no edge (i.e. isolated vertex)
475
476 for (unsigned int i=0; i < halfEdges_.size(); i++)
477 {
478 //cout << i << " " << halfEdges_[i].startVertex() << " " << halfEdges_[i].endVertex() << endl;
479 edgesAtVertices_[halfEdges_[i].endVertex()] = i;
480 }
481
482 // if vertex is on the boundary, rotate the edge until it is an incoming boundary edge
483 // rotate edge until it is either on the boundary, or we come around to the same edge
484 int numIsolatedVertices = 0;
485 for (unsigned int i=0; i < objMesh->getNumVertices(); i++)
486 {
487 if (isIsolatedVertex(i))
488 {
489 numIsolatedVertices++;
490 continue;
491 }
492 HalfEdge * loop = &edgeAtVertex(i);
493 HalfEdge * start = loop;
494 do
495 {
496 if (loop->isBoundary())
497 {
498 // set the edge to the current edge
499 edgesAtVertices_[i] = loop->position();
500 break;
501 }
502 loop = &edgePrevious(edgeOpposite(*loop));
503 }
504 while (*loop != *start);
505 // if we came around, no need to change edgeAtVertices[i]
506 }
507
508 if (numIsolatedVertices > 0)
509 if (verbose)
510 printf("Warning: mesh has %d isolated vertices.\n", numIsolatedVertices);
511
512 // build the cache for faces, first reset to -1
513 for (unsigned int i=0; i < objMesh->getNumGroups(); i++)
514 {
515 const ObjMesh::Group * currentGroup = objMesh->getGroupHandle(i);
516 std::vector<int> dataForThisGroup;
517 dataForThisGroup.clear();
518 for (unsigned int j=0; j < currentGroup->getNumFaces(); j++)
519 {
520 dataForThisGroup.push_back(-1);
521 }
522 edgesAtFaces_.push_back(dataForThisGroup);
523 }
524 for (unsigned int i=0; i < halfEdges_.size(); i++)
525 edgesAtFaces_[halfEdges_[i].groupID()][halfEdges_[i].face()] = i;
526
527 // sanity check: none of the face entries should be -1
528 for (unsigned int i=0; i < objMesh->getNumGroups(); i++)
529 {
530 const ObjMesh::Group * currentGroup = objMesh->getGroupHandle(i);
531 for (unsigned int j=0; j < currentGroup->getNumFaces(); j++)
532 if (edgesAtFaces_[i][j] == -1)
533 {
534 if (verbose)
535 cout << "Warning: face on group " << i << "(" << currentGroup->getName() << "), position " << j << " has no edges." << endl;
536 }
537 }
538
539 determineIfSurfaceHasBoundary();
540
541 // testing: previous edge capability
542 /*
543 cout << "Testing previous edge capability..." << endl;
544 for (unsigned int i=0; i < halfEdges_.size(); i++)
545 {
546 cout << i << ": " << edgePrevious(halfEdges_[i]).position() << endl;
547 }
548
549 // testing: print out associated edges for every vertex
550 for (unsigned int i=0; i < vertexPositions_.size(); i++)
551 {
552 cout << "Halfedge into vertex " << i << ": " << edgeAtVertex(i).position() << endl;
553 }
554
555 // testing: print out associated edges for every face
556 for (unsigned int i=0; i < groups_.size(); i++)
557 for (unsigned int j=0; j < groups_[i].getNumFaces(); j++)
558 {
559 cout << "Halfedge on face " << i << " " << j << ": " << edgeAtFace(i,j).position() << endl;
560 }
561
562 // testing: loop around every vertex
563 for (unsigned int i=0; i < vertexPositions_.size(); i++)
564 {
565 cout << "Looping around vertex " << i << ":";
566 int flag = 0;
567 HalfEdge * start = &edgeAtVertex(i);
568 HalfEdge * loop = start;
569 do
570 {
571 cout << loop->position() << " ";
572
573 if (flag != 0) // boundary edge, exit the loop
574 {
575 cout << " B";
576 break;
577 }
578 loop = &loopVertex(*loop,flag);
579 }
580 while (loop->position() != start->position());
581
582 cout << endl;
583 }
584 */
585
586 if (verbose)
587 {
588 std::cout << "Half-edge datastructure constructed successfully." << std::endl;
589 std::cout << "Statistics: " << std::endl;
590 std::cout << " Half-edges: " << halfEdges_.size() << std::endl;
591 std::cout << " Boundary half-edges: " << boundaryEdges_.size() << std::endl;
592 std::cout << " Connected components: " << connectedComponents << std::endl;
593 }
594
595 return numOrientationFlips;
596 }
597
598 // returns the previous halfedge to the given half-edge
599 // does so by looping around the face (pointers to previous edges are not explicitly stored), so this is slower than edgeNext
600 ObjMeshOrientable::HalfEdge & ObjMeshOrientable::edgePrevious ( HalfEdge & halfedge )
601 {
602 HalfEdge * loop = &halfedge;
603 while (edgeNext(*loop) != halfedge)
604 loop = &(edgeNext(*loop));
605
606 HalfEdge & prevEdge = *loop;
607
608 return prevEdge;
609 }
610
611 // loops around the vertex (vertex is defined as the ending position of the halfedge)
612 // consists of taking the next edge, then taking the opposite edge
613 // if boundary edge encountered, can't take the opposite edge; it this case flag=1 is returned
614 // and the edge returned is the boundary edge pointing away from the vertex
615 // if taking the opposite edge is possible, the returned edge points into the vertex and flag is set to 0
616 ObjMeshOrientable::HalfEdge & ObjMeshOrientable::loopVertex(HalfEdge & halfedge, int & flag)
617 {
618 HalfEdge * loop = &halfedge;
619 loop = &(edgeNext(*loop));
620
621 if (loop->isBoundary())
622 {
623 flag = 1;
624 // return boundary edge pointing away from the vertex (there is no corresponding edge pointing into the vertex)
625 HalfEdge & result = *loop;
626 return result;
627 }
628 else
629 {
630 flag = 0;
631 loop = &(edgeOpposite(*loop));
632 // return edge pointing into the vertex
633 HalfEdge & result = *loop;
634 return result;
635 }
636 }
637
638 void ObjMeshOrientable::determineIfSurfaceHasBoundary()
639 {
640 for (unsigned int i=0; i < halfEdges_.size(); i++)
641 {
642 if (halfEdges_[i].isBoundary())
643 {
644 hasBoundary_ = true;
645 return;
646 }
647 }
648
649 hasBoundary_ = false;
650 }
651
652 void ObjMeshOrientable::CopyHalfEdgeTopologyFrom(ObjMeshOrientable * source) // makes the half-edge topological info equal to that of source
653 {
654
655 halfEdges_ = source->halfEdges_;
656 boundaryEdges_ = source->boundaryEdges_;
657 connectedComponents = source->connectedComponents;
658 edgesAtVertices_ = source->edgesAtVertices_;
659 edgesAtFaces_ = source->edgesAtFaces_;
660 hasBoundary_ = source->hasBoundary_;
661 }
662
663 ObjMesh * ObjMeshOrientable::GenerateOrientedMesh()
664 {
665 ObjMesh * outputObjMesh = new ObjMesh(*objMesh);
666
667 for(unsigned int i=0; i < outputObjMesh->getNumGroups(); i++) // over all groups
668 {
669 ObjMesh::Group * groupHandle = (ObjMesh::Group*) outputObjMesh->getGroupHandle(i);
670 for (unsigned int j=0; j < groupHandle->getNumFaces(); j++) // over all faces
671 {
672 const ObjMesh::Face * face = groupHandle->getFaceHandle(j);
673 if (face->getNumVertices() < 3)
674 {
675 printf("Warning: encountered a face with fewer than 3 vertices.\n");
676 continue;
677 }
678
679 HalfEdge edge = edgeAtFace(i, j);
680
681 // loop until edge is first edge
682 unsigned int startPosition = face->getVertex(0).getPositionIndex();
683 while (edgeStartVertex(edge).getPositionIndex() != startPosition)
684 edge = edgeNext(edge);
685
686 // reverse the face if not correctly oriented
687 if (edgeEndVertex(edge).getPositionIndex() != face->getVertex(1).getPositionIndex())
688 groupHandle->reverseFace(j);
689 }
690 }
691
692 return outputObjMesh;
693 }