From 23c6c069c041a377f4efc1f91cfa828adbb8b3fd Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Tue, 11 Aug 2009 23:08:26 +0100
Subject: [PATCH] output g command even if not sorted

---
 .../triSurface/interfaces/OBJ/writeOBJ.C      | 43 +++++++++++++------
 1 file changed, 31 insertions(+), 12 deletions(-)

diff --git a/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C b/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C
index 7207bdae325..300cdddf957 100644
--- a/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C
+++ b/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C
@@ -41,8 +41,8 @@ namespace Foam
 void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const
 {
     // Write header
-    os  << "# Wavefront OBJ file" << endl
-        << "# Regions:" << endl;
+    os  << "# Wavefront OBJ file" << nl
+        << "# Regions:" << nl;
 
     labelList faceMap;
 
@@ -54,13 +54,13 @@ void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const
     forAll(myPatches, patchI)
     {
         os  << "#     " << patchI << "    "
-            << myPatches[patchI].name() << endl;
+            << myPatches[patchI].name() << nl;
     }
-    os  << "#" << endl;
+    os  << "#" << nl;
 
-    os  << "# points    : " << ps.size() << endl
-        << "# triangles : " << size() << endl
-        << "#" << endl;
+    os  << "# points    : " << ps.size() << nl
+        << "# triangles : " << size() << nl
+        << "#" << nl;
 
 
     // Write vertex coords
@@ -69,7 +69,7 @@ void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const
         os  << "v "
             << ps[pointi].x() << ' '
             << ps[pointi].y() << ' '
-            << ps[pointi].z() << endl;
+            << ps[pointi].z() << nl;
     }
 
     if (writeSorted)
@@ -80,7 +80,7 @@ void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const
         {
             // Print all faces belonging to this patch
 
-            os << "g " << myPatches[patchI].name() << endl;
+            os << "g " << myPatches[patchI].name() << nl;
 
             for
             (
@@ -96,20 +96,39 @@ void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const
                     << operator[](faceI)[1] + 1 << ' '
                     << operator[](faceI)[2] + 1
                     //<< "  # " << operator[](faceI).region()
-                    << endl;
+                    << nl;
             }
         }
     }
     else
     {
+        // Get patch (=compact region) per face
+        labelList patchIDs(size());
+        forAll(myPatches, patchI)
+        {
+            label faceI = myPatches[patchI].start();
+
+            forAll(myPatches[patchI], i)
+            {
+                patchIDs[faceMap[faceI++]] = patchI;
+            }
+        }
+
+
+        label prevPatchI = -1;
+
         forAll(*this, faceI)
         {
+            if (prevPatchI != patchIDs[faceI])
+            {
+                prevPatchI = patchIDs[faceI];
+                os << "g " << myPatches[patchIDs[faceI]].name() << nl;
+            }
             os  << "f "
                 << operator[](faceI)[0] + 1 << ' '
                 << operator[](faceI)[1] + 1 << ' '
                 << operator[](faceI)[2] + 1
-                //<< "  # " << operator[](faceI).region()
-                << endl;
+                << nl;
         }
     }
 }
-- 
GitLab