From 69918f23c5d80cd2ef33c4ba93b0a35905408a90 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Fri, 6 Feb 2009 20:43:09 +0100
Subject: [PATCH] consistency update

- OSspecific: chmod() -> chMod(), even although it's not used anywhere

- ListOps get subset() and inplaceSubset() templated on BoolListType

- added UList<bool>::operator[](..) const specialization.
  Returns false (actually pTraits<bool>::zero) for out-of-range elements.
  This lets us use List<bool> with lazy evaluation and no noticeable
  change in performance.

- use rcIndex() and fcIndex() wherever possible.
  Could check if branching or modulus is faster for fcIndex().

- UList and FixedList get 'const T* cdata() const' and 'T* data()' members.
  Similar to the STL front() and std::string::data() methods, they return a
  pointer to the first element without needing to write '&myList[0]', recast
  begin() or violate const-ness.
---
 applications/test/PackedList/PackedListTest.C | 29 ++++++-
 .../test/primitivePatch/testPrimitivePatch.C  |  2 +-
 .../advanced/collapseEdges/collapseEdges.C    | 11 +--
 .../mesh/advanced/modifyMesh/cellSplitter.C   | 12 +--
 .../PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H     |  2 +-
 .../surfaceSplitNonManifolds.C                | 33 ++------
 src/OSspecific/Unix/Unix.C                    |  2 +-
 .../HashTables/HashTable/HashTable.C          |  4 +-
 .../HashTables/HashTable/HashTableI.H         |  2 +-
 .../StaticHashTable/StaticHashTableI.H        |  2 +-
 .../linkTypes/DLListBase/DLListBaseI.H        |  2 +-
 .../linkTypes/SLListBase/SLListBaseI.H        |  2 +-
 .../containers/Lists/FixedList/FixedList.H    | 19 ++++-
 .../containers/Lists/FixedList/FixedListI.H   | 23 +++++-
 .../containers/Lists/FixedList/FixedListIO.C  |  4 +-
 src/OpenFOAM/containers/Lists/List/ListIO.C   |  2 +-
 .../containers/Lists/ListOps/ListOps.H        | 35 +++++---
 .../Lists/ListOps/ListOpsTemplates.C          | 79 ++++++++++++++++---
 .../containers/Lists/PackedList/PackedList.H  |  2 +-
 .../containers/Lists/PtrList/PtrList.H        |  2 +-
 src/OpenFOAM/containers/Lists/UList/UList.H   | 21 ++++-
 src/OpenFOAM/containers/Lists/UList/UListI.H  | 45 ++++++++++-
 .../containers/Lists/UPtrList/UPtrList.H      |  2 +-
 src/OpenFOAM/db/Time/Time.C                   | 16 ++--
 src/OpenFOAM/db/Time/timeSelector.C           |  6 +-
 src/OpenFOAM/db/scalarRange/scalarRanges.C    |  4 +-
 src/OpenFOAM/include/OSspecific.H             |  2 +-
 .../interpolationTable/interpolationTable.C   |  2 +-
 src/OpenFOAM/memory/tmp/refCount.H            |  2 +-
 src/OpenFOAM/meshes/meshShapes/face/faceI.H   |  2 +-
 .../primitiveMeshCheck/primitiveMeshCheck.C   | 10 +--
 .../primitiveMesh/primitivePatch/walkPatch.C  | 12 +--
 .../boundaryMesh/octreeDataFaceList.C         | 10 +--
 src/dynamicMesh/meshCut/cellCuts/cellCuts.C   | 25 +++---
 .../meshCut/cellLooper/geomCellLooper.C       |  7 +-
 .../meshCutAndRemove/meshCutAndRemove.C       | 29 ++-----
 .../meshModifiers/meshCutter/meshCutter.C     | 27 ++-----
 .../polyMeshGeometry/polyMeshGeometry.C       |  2 +-
 .../polyTopoChange/combineFaces.C             |  4 +-
 .../polyTopoChange/faceCollapser.C            |  6 +-
 src/meshTools/cellFeatures/cellFeatures.C     | 10 +--
 src/meshTools/octree/octreeDataFace.C         |  6 +-
 .../primitiveMeshGeometry.C                   |  2 +-
 .../booleanSurface/booleanSurface.C           |  8 +-
 .../intersectedSurface/intersectedSurface.C   | 10 +--
 .../BasicMeshedSurface/BasicMeshedSurface.C   |  2 +-
 src/surfMesh/surfMesh/surfMesh.C              | 16 ++++
 src/surfMesh/surfMesh/surfMesh.H              |  3 +
 .../surfaceFormats/ac3d/AC3DsurfaceFormat.C   |  2 +-
 .../surfaceFormats/obj/OBJsurfaceFormat.C     |  2 +-
 .../surfaceFormats/off/OFFsurfaceFormat.C     |  2 +-
 .../surfaceFormats/stl/STLsurfaceFormat.C     |  4 +-
 .../surfaceFormats/tri/TRIsurfaceFormat.C     |  2 +-
 .../faceTriangulation/faceTriangulation.C     | 22 +++---
 .../triSurface/interfaces/OBJ/readOBJ.C       |  2 +-
 55 files changed, 363 insertions(+), 233 deletions(-)

diff --git a/applications/test/PackedList/PackedListTest.C b/applications/test/PackedList/PackedListTest.C
index a8488eb1836..41796562bad 100644
--- a/applications/test/PackedList/PackedListTest.C
+++ b/applications/test/PackedList/PackedListTest.C
@@ -77,14 +77,22 @@ int main(int argc, char *argv[])
         const PackedList<3>& constLst = list1;
         Info<< "\ntest operator[] const with out-of-range index\n";
         constLst.print(Info);
-        if (!constLst[20])
+        if (constLst[20])
+        {
+            Info<< "[20] is true (unexpected)\n";
+        }
+        else
         {
             Info<< "[20] is false (expected) list size should be unchanged (const)\n";
         }
         constLst.print(Info);
 
         Info<< "\ntest operator[] non-const with out-of-range index\n";
-        if (!list1[20])
+        if (list1[20])
+        {
+            Info<< "[20] is true (unexpected)\n";
+        }
+        else
         {
             Info<< "[20] is false (expected) but list was resized?? (non-const)\n";
         }
@@ -268,6 +276,23 @@ int main(int argc, char *argv[])
     Info<< "removed final value: " << list3.remove() << endl;
     list3.print(Info);
 
+
+    List<bool> list4(4, true);
+    {
+        const List<bool>& constLst = list4;
+        Info<< "\ntest operator[] const with out-of-range index\n";
+        Info<< constLst << endl;
+        if (constLst[20])
+        {
+            Info<< "[20] is true (unexpected)\n";
+        }
+        else
+        {
+            Info<< "[20] is false (expected) list size should be unchanged (const)\n";
+        }
+        Info<< constLst << endl;
+    }
+
     Info<< "\n\nDone.\n";
 
     return 0;
diff --git a/applications/test/primitivePatch/testPrimitivePatch.C b/applications/test/primitivePatch/testPrimitivePatch.C
index 7130e90628f..e19b7d0512b 100644
--- a/applications/test/primitivePatch/testPrimitivePatch.C
+++ b/applications/test/primitivePatch/testPrimitivePatch.C
@@ -69,7 +69,7 @@ void checkFaceEdges
 
         forAll(f, fp)
         {
-            label fp1 = (fp + 1) % f.size();
+            label fp1 = f.fcIndex(fp);
 
             if (edges[myEdges[fp]] != edge(f[fp], f[fp1]))
             {
diff --git a/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C b/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C
index c92418dc803..1dc59edb7c4 100644
--- a/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C
+++ b/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C
@@ -74,20 +74,17 @@ labelList getSortedEdges
         const edge& e = edges[edgeI];
 
         label fp = findIndex(f, e[0]);
-
-        label fp1 = (fp+1) % f.size();
+        label fp1 = f.fcIndex(fp);
 
         if (f[fp1] == e[1])
         {
-            // Edgei in fp-fp1 order
+            // EdgeI between fp -> fp1
             faceEdges[fp] = edgeI;
         }
         else
         {
-            // Edgei between fp-1 and fp
-            label fpMin1 = (fp == 0 ? f.size()-1 : fp-1);
-
-            faceEdges[fpMin1] = edgeI;
+            // EdgeI between fp-1 -> fp
+            faceEdges[f.rcIndex(fp)] = edgeI;
         }
     }
 
diff --git a/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C b/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C
index ce532b979f6..3ae39131a10 100644
--- a/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C
+++ b/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C
@@ -211,7 +211,7 @@ void Foam::cellSplitter::setRefinement
 
         // Add other pyramids
         for (label i = 1; i < cFaces.size(); i++)
-        {    
+        {
             label addedCellI =
                 meshMod.setAction
                 (
@@ -277,7 +277,7 @@ void Foam::cellSplitter::setRefinement
 
                 label index = findIndex(f0, e[0]);
 
-                bool edgeInFaceOrder = (f0[(index+1) % f0.size()] == e[1]);
+                bool edgeInFaceOrder = (f0[f0.fcIndex(index)] == e[1]);
 
                 // Check if cellI is the face owner
 
@@ -323,7 +323,7 @@ void Foam::cellSplitter::setRefinement
 
                 label index = findIndex(f1, e[0]);
 
-                bool edgeInFaceOrder = (f1[(index+1) % f1.size()] == e[1]);
+                bool edgeInFaceOrder = (f1[f1.fcIndex(index)] == e[1]);
 
                 // Check if cellI is the face owner
 
@@ -362,7 +362,7 @@ void Foam::cellSplitter::setRefinement
             }
         }
     }
-            
+
 
     //
     // Update all existing faces for split owner or neighbour.
@@ -441,7 +441,7 @@ void Foam::cellSplitter::setRefinement
 
                 label patchID, zoneID, zoneFlip;
                 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
-                
+
                 meshMod.setAction
                 (
                     polyModifyFace
@@ -458,7 +458,7 @@ void Foam::cellSplitter::setRefinement
                     )
                 );
             }
-    
+
             faceUpToDate[faceI] = true;
         }
     }
diff --git a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H
index cb25ac50895..af0fb849a4f 100644
--- a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H
+++ b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H
@@ -156,7 +156,7 @@ class vtkPV3Foam
 
             bool empty() const
             {
-                return (size_ == 0);
+                return !size_;
             }
 
             void reset()
diff --git a/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C b/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C
index 8968831e644..f7131d87db8 100644
--- a/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C
+++ b/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C
@@ -351,7 +351,7 @@ label otherEdge
 
     return -1;
 }
-    
+
 
 // Starting from startPoint on startEdge on startFace walk along border
 // and insert faces along the way. Walk keeps always one point or one edge
@@ -461,18 +461,8 @@ label sharedFace
 
     label startIndex = findIndex(f, e.start());
 
-    bool edgeOrder;
-
-    if (f[(startIndex + 1) % f.size()] == e.end())
-    {
-        // points in face in same order as edge
-        edgeOrder = true;
-    }
-    else
-    {
-        // points in face in reverse order as edge
-        edgeOrder = false;
-    }
+    // points in face in same order as edge
+    bool edgeOrder = (f[f.fcIndex(startIndex)] == e.end());
 
     // Get faces using edge in sorted order. (sorted such that walking
     // around them in anti-clockwise order corresponds to edge vector
@@ -485,25 +475,18 @@ label sharedFace
     if (edgeOrder)
     {
         // Get face before firstFaceI
-        if (faceIndex == 0)
-        {
-            return eFaces[eFaces.size() - 1];
-        }
-        else
-        {
-            return eFaces[faceIndex - 1];
-        }
+        return eFaces[eFaces.rcIndex(faceIndex)];
     }
     else
     {
         // Get face after firstFaceI
-        return eFaces[(faceIndex+1) % eFaces.size()];
+        return eFaces[eFaces.fcIndex(faceIndex)];
     }
 }
 
 
 // Calculate (inward pointing) normals on edges shared by faces in faceToEdge and
-// averages them to pointNormals. 
+// averages them to pointNormals.
 void calcPointVecs
 (
     const triSurface& surf,
@@ -602,7 +585,7 @@ void calcPointVecs
             }
 
             scalar magMidVec = mag(midVec);
-    
+
             if (magMidVec > SMALL)
             {
                 midVec /= magMidVec;
@@ -925,7 +908,7 @@ int main(int argc, char *argv[])
                 newPoints[newPointI] = newPoints[pointI] + 0.1 * minLen * n;
             }
         }
-        
+
 
         //
         // Renumber all faces in connectedFaces
diff --git a/src/OSspecific/Unix/Unix.C b/src/OSspecific/Unix/Unix.C
index f243c48ed4a..9378a21c7ca 100644
--- a/src/OSspecific/Unix/Unix.C
+++ b/src/OSspecific/Unix/Unix.C
@@ -432,7 +432,7 @@ bool Foam::mkDir(const fileName& pathName, mode_t mode)
 
 
 // Set the file mode
-bool Foam::chmod(const fileName& name, const mode_t m)
+bool Foam::chMod(const fileName& name, const mode_t m)
 {
     return ::chmod(name.c_str(), m) == 0;
 }
diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
index ae9bcc27187..3ed474b32db 100644
--- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
+++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
@@ -226,7 +226,7 @@ bool Foam::HashTable<T, Key, Hash>::set
     const bool protect
 )
 {
-    if (tableSize_ == 0)
+    if (!tableSize_)
     {
         resize(2);
     }
@@ -556,7 +556,7 @@ void Foam::HashTable<T, Key, Hash>::operator=
     }
 
     // could be zero-sized from a previous transfer()
-    if (tableSize_ == 0)
+    if (!tableSize_)
     {
         resize(rhs.tableSize_);
     }
diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H
index 79ff3741453..aea5f3043ad 100644
--- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H
+++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H
@@ -54,7 +54,7 @@ inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
 template<class T, class Key, class Hash>
 inline bool Foam::HashTable<T, Key, Hash>::empty() const
 {
-    return (nElmts_ == 0);
+    return !nElmts_;
 }
 
 
diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H
index 329437a07a4..5cd380357a7 100644
--- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H
+++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H
@@ -41,7 +41,7 @@ inline Foam::label Foam::StaticHashTable<T, Key, Hash>::size() const
 template<class T, class Key, class Hash>
 inline bool Foam::StaticHashTable<T, Key, Hash>::empty() const
 {
-    return (nElmts_ == 0);
+    return !nElmts_;
 }
 
 
diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H
index 9b80941bf9a..bcc20289ca3 100644
--- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H
+++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H
@@ -83,7 +83,7 @@ inline Foam::label Foam::DLListBase::size() const
 
 inline bool Foam::DLListBase::empty() const
 {
-    return (nElmts_ == 0);
+    return !nElmts_;
 }
 
 
diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H
index de34257d342..339963da5e5 100644
--- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H
+++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H
@@ -73,7 +73,7 @@ inline Foam::label Foam::SLListBase::size() const
 
 inline bool Foam::SLListBase::empty() const
 {
-    return (nElmts_ == 0);
+    return !nElmts_;
 }
 
 
diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H
index 2e3ddf408bd..0079f13a029 100644
--- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H
+++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H
@@ -135,10 +135,21 @@ public:
             inline label fcIndex(const label i) const;
 
             //- Return the reverse circular index, i.e. the previous index
-            //  which returns to the last at the begining of the list
+            //  which returns to the last at the beginning of the list
             inline label rcIndex(const label i) const;
 
 
+            //- Return a const pointer to the first data element,
+            //  similar to the STL front() method and the string::data() method
+            //  This can be used (with caution) when interfacing with C code.
+            inline const T* cdata() const;
+
+            //- Return a pointer to the first data element,
+            //  similar to the STL front() method and the string::data() method
+            //  This can be used (with caution) when interfacing with C code.
+            inline T* data();
+
+
         // Check
 
             //- Check start is within valid range (0 ... size-1).
@@ -174,10 +185,10 @@ public:
 
     // Member operators
 
-        //- Return subscript-checked element of FixedList.
+        //- Return element of FixedList.
         inline T& operator[](const label);
 
-        //- Return subscript-checked element of constant FixedList.
+        //- Return element of constant FixedList.
         inline const T& operator[](const label) const;
 
         //- Assignment from array operator. Takes linear time.
@@ -282,7 +293,7 @@ public:
         //- Return size of the largest possible FixedList.
         inline label max_size() const;
 
-        //- Return true if the FixedList is empty (i.e., if size() == 0).
+        //- Return true if the FixedList is empty (ie, size() is zero).
         inline bool empty() const;
 
         //- Swap two FixedLists of the same type in constant time.
diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H
index d618cb6e386..a800b04b1d1 100644
--- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H
+++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H
@@ -121,7 +121,7 @@ inline Foam::label Foam::FixedList<T, Size>::fcIndex(const label i) const
 template<class T, Foam::label Size>
 inline Foam::label Foam::FixedList<T, Size>::rcIndex(const label i) const
 {
-    return (i == 0 ? Size-1 : i-1);
+    return (i ? i-1 : Size-1);
 }
 
 
@@ -195,9 +195,26 @@ inline void Foam::FixedList<T, Size>::transfer(const FixedList<T, Size>& lst)
     }
 }
 
+
+template<class T, Foam::label Size>
+inline const T*
+Foam::FixedList<T, Size>::cdata() const
+{
+    return v_;
+}
+
+
+template<class T, Foam::label Size>
+inline T*
+Foam::FixedList<T, Size>::data()
+{
+    return v_;
+}
+
+
 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
 
-// Return subscript-checked element access
+// element access
 template<class T, Foam::label Size>
 inline T& Foam::FixedList<T, Size>::operator[](const label i)
 {
@@ -208,7 +225,7 @@ inline T& Foam::FixedList<T, Size>::operator[](const label i)
 }
 
 
-// Return subscript-checked const element access
+// const element access
 template<class T, Foam::label Size>
 inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
 {
diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C
index b76bf3be242..49caad2a0ad 100644
--- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C
+++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C
@@ -119,7 +119,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
     }
     else
     {
-        is.read(reinterpret_cast<char*>(L.begin()), Size*sizeof(T));
+        is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
 
         is.fatalCheck
         (
@@ -231,7 +231,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
     }
     else
     {
-        os.write(reinterpret_cast<const char*>(L.v_), Size*sizeof(T));
+        os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
     }
 
     // Check state of IOstream
diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C
index b7cb86c4ee7..7cfdfb0397f 100644
--- a/src/OpenFOAM/containers/Lists/List/ListIO.C
+++ b/src/OpenFOAM/containers/Lists/List/ListIO.C
@@ -117,7 +117,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
         {
             if (s)
             {
-                is.read(reinterpret_cast<char*>(L.begin()), s*sizeof(T));
+                is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
 
                 is.fatalCheck
                 (
diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H
index b7e90d53d13..7de9239d04d 100644
--- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H
+++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H
@@ -90,17 +90,31 @@ void duplicateOrder(const UList<T>&, labelList& order);
 template<class T>
 void uniqueOrder(const UList<T>&, labelList& order);
 
-//- Extract elements of List whose region is certain value.
-//  Use e.g. to extract all selected elements:
-//    subset<boolList, labelList>(selectedElems, true, lst);
+//- Extract elements of List when select is a certain value.
+//  eg, to extract all selected elements:
+//    subset<bool, labelList>(selectedElems, true, lst);
 template<class T, class ListType>
-ListType subset(const UList<T>& regions, const T& region, const ListType&);
+ListType subset(const UList<T>& select, const T& value, const ListType&);
 
-//- Inplace extract elements of List whose region is certain value. Use e.g.
-//  to extract all selected elements:
-//  inplaceSubset<boolList, labelList>(selectedElems, true, lst);
+//- Inplace extract elements of List when select is a certain value.
+//  eg, to extract all selected elements:
+//    inplaceSubset<bool, labelList>(selectedElems, true, lst);
 template<class T, class ListType>
-void inplaceSubset(const UList<T>& regions, const T& region, ListType&);
+void inplaceSubset(const UList<T>& select, const T& value, ListType&);
+
+//- Extract elements of List when select is true
+//  eg, to extract all selected elements:
+//    subset<boolList, labelList>(selectedElems, lst);
+//  Note a labelHashSet could also be used for the bool-list
+template<class BoolListType, class ListType>
+ListType subset(const BoolListType& select, const ListType&);
+
+//- Inplace extract elements of List when select is true
+//  eg, to extract all selected elements:
+//    inplaceSubset<boolList, labelList>(selectedElems, lst);
+//  Note a labelHashSet could also be used for the bool-list
+template<class BoolListType, class ListType>
+void inplaceSubset(const BoolListType& select, ListType&);
 
 //- Invert one-to-one map. Unmapped elements will be -1.
 labelList invert(const label len, const UList<label>&);
@@ -108,8 +122,9 @@ labelList invert(const label len, const UList<label>&);
 //- Invert one-to-many map. Unmapped elements will be size 0.
 labelListList invertOneToMany(const label len, const UList<label>&);
 
-//- Invert many-to-many. Input and output types need to be inherited
-//  from List. E.g. faces to pointFaces.
+//- Invert many-to-many.
+//  Input and output types need to be inherited from List.
+//  eg, faces to pointFaces.
 template<class InList, class OutList>
 void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
 
diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C
index f12b5efd554..f2178ee38df 100644
--- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C
+++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C
@@ -243,16 +243,17 @@ void Foam::uniqueOrder
 template<class T, class ListType>
 ListType Foam::subset
 (
-    const UList<T>& regions,
-    const T& region,
+    const UList<T>& select,
+    const T& value,
     const ListType& lst
 )
 {
-    if (regions.size() < lst.size())
+    // select must at least cover the list range
+    if (select.size() < lst.size())
     {
         FatalErrorIn("subset(const UList<T>&, const T&, const ListType&)")
-            << "Regions is of size " << regions.size()
-            << "; list it is supposed to index is of size " << lst.size()
+            << "select is of size " << select.size()
+            << "; but it must index a list of size " << lst.size()
             << abort(FatalError);
     }
 
@@ -261,7 +262,7 @@ ListType Foam::subset
     label nElem = 0;
     forAll(lst, elemI)
     {
-        if (regions[elemI] == region)
+        if (select[elemI] == value)
         {
             newLst[nElem++] = lst[elemI];
         }
@@ -275,23 +276,77 @@ ListType Foam::subset
 template<class T, class ListType>
 void Foam::inplaceSubset
 (
-    const UList<T>& regions,
-    const T& region,
+    const UList<T>& select,
+    const T& value,
     ListType& lst
 )
 {
-    if (regions.size() < lst.size())
+    // select must at least cover the list range
+    if (select.size() < lst.size())
     {
         FatalErrorIn("inplaceSubset(const UList<T>&, const T&, ListType&)")
-            << "Regions is of size " << regions.size()
-            << "; list it is supposed to index is of size " << lst.size()
+            << "select is of size " << select.size()
+            << "; but it must index a list of size " << lst.size()
             << abort(FatalError);
     }
 
     label nElem = 0;
     forAll(lst, elemI)
     {
-        if (regions[elemI] == region)
+        if (select[elemI] == value)
+        {
+            if (nElem != elemI)
+            {
+                lst[nElem] = lst[elemI];
+            }
+            ++nElem;
+        }
+    }
+
+    lst.setSize(nElem);
+}
+
+
+template<class BoolListType, class ListType>
+ListType Foam::subset
+(
+    const BoolListType& select,
+    const ListType& lst
+)
+{
+    // select can have a different size
+    // eg, when it is a PackedBoolList or a labelHashSet
+
+    ListType newLst(lst.size());
+
+    label nElem = 0;
+    forAll(lst, elemI)
+    {
+        if (select[elemI])
+        {
+            newLst[nElem++] = lst[elemI];
+        }
+    }
+    newLst.setSize(nElem);
+
+    return newLst;
+}
+
+
+template<class BoolListType, class ListType>
+void Foam::inplaceSubset
+(
+    const BoolListType& select,
+    ListType& lst
+)
+{
+    // select can have a different size
+    // eg, when it is a PackedBoolList or a labelHashSet
+
+    label nElem = 0;
+    forAll(lst, elemI)
+    {
+        if (select[elemI])
         {
             if (nElem != elemI)
             {
diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H
index a9cd59da86c..5f71976aa9d 100644
--- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H
+++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H
@@ -185,7 +185,7 @@ public:
         //- Number of entries.
         inline label size() const;
 
-        //- Return true if the list is empty (i.e., size() == 0).
+        //- Return true if the list is empty (ie, size() is zero).
         inline bool empty() const;
 
         //- Get value at index I.
diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H
index 5de1345f10d..b5167e5ef89 100644
--- a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H
+++ b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H
@@ -158,7 +158,7 @@ public:
             //- Return the number of elements in the PtrList
             inline label size() const;
 
-            //- Return true if the PtrList is empty (i.e., if size() == 0).
+            //- Return true if the PtrList is empty (ie, size() is zero).
             inline bool empty() const;
 
 
diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H
index 83c0913a5fe..80bf066e19c 100644
--- a/src/OpenFOAM/containers/Lists/UList/UList.H
+++ b/src/OpenFOAM/containers/Lists/UList/UList.H
@@ -27,7 +27,7 @@ Class
 
 Description
     A 1D vector of objects of type \<T\>, where the size of the vector is
-    known and used for subscript bounds checking, etc.
+    known and can be used for subscript bounds checking, etc.
 
     Storage is not allocated during construction or use but is supplied to
     the constructor as an argument.  This type of list is particularly useful
@@ -140,6 +140,17 @@ public:
             label byteSize() const;
 
 
+            //- Return a const pointer to the first data element,
+            //  similar to the STL front() method and the string::data() method
+            //  This can be used (with caution) when interfacing with C code.
+            inline const T* cdata() const;
+
+            //- Return a pointer to the first data element,
+            //  similar to the STL front() method and the string::data() method
+            //  This can be used (with caution) when interfacing with C code.
+            inline T* data();
+
+
         // Check
 
             //- Check start is within valid range (0 ... size-1).
@@ -164,10 +175,12 @@ public:
 
     // Member operators
 
-        //- Return subscript-checked element of UList.
+        //- Return element of UList.
         inline T& operator[](const label);
 
-        //- Return subscript-checked element of constant UList.
+        //- Return element of constant UList.
+        //  Note that the bool specialization adds lazy evaluation so reading
+        //  an out-of-range element returns false without any ill-effects
         inline const T& operator[](const label) const;
 
         //- Allow cast to a const List<T>&
@@ -266,7 +279,7 @@ public:
         //- Return size of the largest possible UList.
         inline label max_size() const;
 
-        //- Return true if the UList is empty (i.e., if size() == 0).
+        //- Return true if the UList is empty (ie, size() is zero).
         inline bool empty() const;
 
         //- Swap two ULists of the same type in constant time.
diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H
index 7d8d87006c8..5c1df4be15c 100644
--- a/src/OpenFOAM/containers/Lists/UList/UListI.H
+++ b/src/OpenFOAM/containers/Lists/UList/UListI.H
@@ -25,6 +25,7 @@ License
 \*---------------------------------------------------------------------------*/
 
 #include "error.H"
+#include "pTraits.H"
 #include "Swap.H"
 
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
@@ -64,7 +65,7 @@ inline Foam::label Foam::UList<T>::fcIndex(const label i) const
 template<class T>
 inline Foam::label Foam::UList<T>::rcIndex(const label i) const
 {
-    return (i == 0 ? size()-1 : i-1);
+    return (i ? i-1 : size()-1);
 }
 
 
@@ -113,9 +114,24 @@ inline void Foam::UList<T>::checkIndex(const label i) const
 }
 
 
+template<class T>
+inline const T* Foam::UList<T>::cdata() const
+{
+    return v_;
+}
+
+
+template<class T>
+inline T* Foam::UList<T>::data()
+{
+    return v_;
+}
+
+
 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
 
-// Return subscript-checked element access
+
+// element access
 template<class T>
 inline T& Foam::UList<T>::operator[](const label i)
 {
@@ -126,7 +142,28 @@ inline T& Foam::UList<T>::operator[](const label i)
 }
 
 
-// Return subscript-checked const element access
+namespace Foam
+{
+
+    // Template specialization for bool
+    template<>
+    inline const bool& Foam::UList<bool>::operator[](const label i) const
+    {
+        // lazy evaluation - return false for out-of-range
+        if (i < size_)
+        {
+            return v_[i];
+        }
+        else
+        {
+            return Foam::pTraits<bool>::zero;
+        }
+    }
+
+} // end of namespace Foam
+
+
+// const element access
 template<class T>
 inline const T& Foam::UList<T>::operator[](const label i) const
 {
@@ -248,7 +285,7 @@ inline Foam::label Foam::UList<T>::max_size() const
 template<class T>
 inline bool Foam::UList<T>::empty() const
 {
-    return (size_ == 0);
+    return !size_;
 }
 
 
diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H
index cfcae367184..a334e812543 100644
--- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H
+++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H
@@ -124,7 +124,7 @@ public:
             //- Return the number of elements in the UPtrList
             inline label size() const;
 
-            //- Return true if the UPtrList is empty (i.e., if size() == 0).
+            //- Return true if the UPtrList is empty (ie, size() is zero).
             inline bool empty() const;
 
 
diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C
index f20055509e8..f2096b79688 100644
--- a/src/OpenFOAM/db/Time/Time.C
+++ b/src/OpenFOAM/db/Time/Time.C
@@ -31,11 +31,7 @@ License
 
 // * * * * * * * * * * * * * Static Member Data  * * * * * * * * * * * * * * //
 
-namespace Foam
-{
-    defineTypeNameAndDebug(Time, 0);
-}
-
+defineTypeNameAndDebug(Foam::Time, 0);
 
 template<>
 const char* Foam::NamedEnum<Foam::Time::stopAtControls, 4>::names[] =
@@ -100,9 +96,11 @@ void Foam::Time::adjustDeltaT()
 void Foam::Time::setControls()
 {
     // default is to resume calculation from "latestTime"
-    word startFrom("latestTime");
-
-    controlDict_.readIfPresent("startFrom", startFrom);
+    word startFrom = controlDict_.lookupOrDefault<word>
+    (
+        "startFrom",
+        "latestTime"
+    );
 
     if (startFrom == "startTime")
     {
@@ -421,7 +419,7 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const
     label nearestIndex = -1;
     scalar deltaT = GREAT;
 
-    for (label i=1; i<times.size(); i++)
+    for (label i=1; i < times.size(); i++)
     {
         scalar diff = mag(times[i].value() - t);
         if (diff < deltaT)
diff --git a/src/OpenFOAM/db/Time/timeSelector.C b/src/OpenFOAM/db/Time/timeSelector.C
index 8b91422fac4..8cd7940ccff 100644
--- a/src/OpenFOAM/db/Time/timeSelector.C
+++ b/src/OpenFOAM/db/Time/timeSelector.C
@@ -103,7 +103,7 @@ Foam::List<Foam::instant> Foam::timeSelector::select
     const List<instant>& Times
 ) const
 {
-    return subset(selected(Times), true, Times);
+    return subset(selected(Times), Times);
 }
 
 
@@ -112,7 +112,7 @@ void Foam::timeSelector::inplaceSelect
     List<instant>& Times
 ) const
 {
-    inplaceSubset(selected(Times), true, Times);
+    inplaceSubset(selected(Times), Times);
 }
 
 
@@ -219,7 +219,7 @@ Foam::List<Foam::instant> Foam::timeSelector::select
             }
         }
 
-        return subset(selectTimes, true, timeDirs);
+        return subset(selectTimes, timeDirs);
     }
     else
     {
diff --git a/src/OpenFOAM/db/scalarRange/scalarRanges.C b/src/OpenFOAM/db/scalarRange/scalarRanges.C
index c7c7f484867..c59e2b5a9d6 100644
--- a/src/OpenFOAM/db/scalarRange/scalarRanges.C
+++ b/src/OpenFOAM/db/scalarRange/scalarRanges.C
@@ -123,7 +123,7 @@ Foam::List<Foam::scalar> Foam::scalarRanges::select
     const List<scalar>& values
 ) const
 {
-    return subset(selected(values), true, values);
+    return subset(selected(values), values);
 }
 
 
@@ -132,7 +132,7 @@ void Foam::scalarRanges::inplaceSelect
     List<scalar>& values
 ) const
 {
-    inplaceSubset(selected(values), true, values);
+    inplaceSubset(selected(values), values);
 }
 
 
diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H
index 5aaf2f66263..e4227879a6a 100644
--- a/src/OpenFOAM/include/OSspecific.H
+++ b/src/OpenFOAM/include/OSspecific.H
@@ -110,7 +110,7 @@ fileName findEtcFile(const fileName&, bool mandatory=false);
 bool mkDir(const fileName&, mode_t=0777);
 
 //- Set the file mode
-bool chmod(const fileName&, const mode_t);
+bool chMod(const fileName&, const mode_t);
 
 //- Return the file mode
 mode_t mode(const fileName&);
diff --git a/src/OpenFOAM/interpolations/interpolationTable/interpolationTable.C b/src/OpenFOAM/interpolations/interpolationTable/interpolationTable.C
index 345aed54cc7..aa6d0d750fe 100644
--- a/src/OpenFOAM/interpolations/interpolationTable/interpolationTable.C
+++ b/src/OpenFOAM/interpolations/interpolationTable/interpolationTable.C
@@ -44,7 +44,7 @@ void Foam::interpolationTable<Type>::readTable()
     // Check that the data are okay
     check();
 
-    if (this->size() == 0)
+    if (this->empty())
     {
         FatalErrorIn
         (
diff --git a/src/OpenFOAM/memory/tmp/refCount.H b/src/OpenFOAM/memory/tmp/refCount.H
index d297997cb43..6af7317cedc 100644
--- a/src/OpenFOAM/memory/tmp/refCount.H
+++ b/src/OpenFOAM/memory/tmp/refCount.H
@@ -82,7 +82,7 @@ public:
         //- Return true if the reference count is zero
         bool okToDelete() const
         {
-            return (count_ == 0);
+            return !count_;
         }
 
 
diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
index 500798e532a..57f5eeaf99a 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H
+++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
@@ -36,7 +36,7 @@ inline Foam::label Foam::face::right(const label i) const
 // Edge to the left of face vertex i
 inline Foam::label Foam::face::left(const label i) const
 {
-    return i == 0 ? size() - 1 : (i - 1);
+    return i ? i-1 : size()-1;
 }
 
 
diff --git a/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshCheck/primitiveMeshCheck.C b/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshCheck/primitiveMeshCheck.C
index 8938faf69bc..e73d1d078d2 100644
--- a/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshCheck/primitiveMeshCheck.C
+++ b/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshCheck/primitiveMeshCheck.C
@@ -867,7 +867,7 @@ bool primitiveMesh::checkFaceAngles
         forAll(f, fp0)
         {
             // Get vertex after fp
-            label fp1 = (fp0 + 1) % f.size();
+            label fp1 = f.fcIndex(fp0);
 
             // Normalized vector between two consecutive points
             vector e10(p[f[fp1]] - p[f[fp0]]);
@@ -1636,12 +1636,12 @@ bool primitiveMesh::checkCommonOrder
 
 
                     // Vertices before and after on curFace
-                    label fpPlus1 = (fp+1) % curFace.size();
-                    label fpMin1 = (fp == 0 ? curFace.size()-1 : fp-1);
+                    label fpPlus1 = curFace.fcIndex(fp);
+                    label fpMin1  = curFace.rcIndex(fp);
 
                     // Vertices before and after on nbFace
-                    label nbPlus1 = (nb+1) % nbFace.size();
-                    label nbMin1 = (nb == 0 ? nbFace.size()-1 : nb-1);
+                    label nbPlus1 = nbFace.fcIndex(nb);
+                    label nbMin1  = nbFace.rcIndex(nb);
 
                     // Find order of walking by comparing next points on both
                     // faces.
diff --git a/src/OpenFOAM/meshes/primitiveMesh/primitivePatch/walkPatch.C b/src/OpenFOAM/meshes/primitiveMesh/primitivePatch/walkPatch.C
index f49c8beca8b..022cf60ab2e 100644
--- a/src/OpenFOAM/meshes/primitiveMesh/primitivePatch/walkPatch.C
+++ b/src/OpenFOAM/meshes/primitiveMesh/primitivePatch/walkPatch.C
@@ -155,20 +155,10 @@ void Foam::walkPatch::faceToFace
 
             indexInFace_.append(fp);
 
-
             // Visit neighbouring faces in order, starting at fp.
             for (label i = 0; i < f.size(); i++)
             {
-                label fp1;
-                if (reverse_)
-                {
-                    fp1 = (fp == 0 ? f.size()-1 : fp-1);
-                }
-                else
-                {
-                    fp1 = (fp + 1) % f.size();
-                }
-
+                label fp1 = reverse_ ? f.rcIndex(fp) : f.fcIndex(fp);
                 label nbr = getNeighbour(faceI, fp, f[fp], f[fp1]);
 
                 if
diff --git a/src/dynamicMesh/boundaryMesh/octreeDataFaceList.C b/src/dynamicMesh/boundaryMesh/octreeDataFaceList.C
index 575a0a0ef4f..c44c455c74f 100644
--- a/src/dynamicMesh/boundaryMesh/octreeDataFaceList.C
+++ b/src/dynamicMesh/boundaryMesh/octreeDataFaceList.C
@@ -340,8 +340,8 @@ Foam::label Foam::octreeDataFaceList::getSampleType
             // Face intersection point lies on edge between two face triangles
 
             // Calculate edge normal as average of the two triangle normals
-            label fpPrev = (fp == 0 ? f.size()-1 : fp-1);
-            label fpNext = (fp + 1) % f.size();
+            label fpPrev = f.rcIndex(fp);
+            label fpNext = f.fcIndex(fp);
 
             vector e = points[f[fp]] - ctr;
             vector ePrev = points[f[fpPrev]] - ctr;
@@ -434,7 +434,7 @@ bool Foam::octreeDataFaceList::intersects
     // Disable picking up intersections behind us.
     scalar oldTol = intersection::setPlanarTol(0.0);
 
-    pointHit inter = 
+    pointHit inter =
         f.ray
         (
             start,
@@ -486,7 +486,7 @@ bool Foam::octreeDataFaceList::findTightest
     else
     {
         // Construct bb around sample and myFar
-        const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z())); 
+        const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z()));
 
         tightest.min() = sample - dist2;
         tightest.max() = sample + dist2;
@@ -558,7 +558,7 @@ Foam::scalar Foam::octreeDataFaceList::calcNearest
     }
     return nearHit.distance();
 }
-    
+
 
 void Foam::octreeDataFaceList::write
 (
diff --git a/src/dynamicMesh/meshCut/cellCuts/cellCuts.C b/src/dynamicMesh/meshCut/cellCuts/cellCuts.C
index 7e0e1fbb8d0..78bae37eb60 100644
--- a/src/dynamicMesh/meshCut/cellCuts/cellCuts.C
+++ b/src/dynamicMesh/meshCut/cellCuts/cellCuts.C
@@ -374,7 +374,7 @@ void Foam::cellCuts::calcFaceCuts() const
         label cutI = 0;
 
         // Do point-edge-point walk over face and collect all cuts.
-        // Problem is that we want to start from one of the endpoints of a 
+        // Problem is that we want to start from one of the endpoints of a
         // string of connected cuts; we don't want to start somewhere in the
         // middle.
 
@@ -387,17 +387,16 @@ void Foam::cellCuts::calcFaceCuts() const
 
             if (pointIsCut_[v0])
             {
-                label fpMin1 = (fp == 0 ? f.size()-1 : fp-1);
-                label vMin1 = f[fpMin1];
+                label vMin1 = f[f.rcIndex(fp)];
 
-                if 
+                if
                 (
                     !pointIsCut_[vMin1]
                  && !edgeIsCut_[findEdge(faceI, v0, vMin1)]
                 )
                 {
                     cuts[cutI++] = vertToEVert(v0);
-                    startFp = (fp+1) % f.size();
+                    startFp = f.fcIndex(fp);
                     break;
                 }
             }
@@ -408,7 +407,7 @@ void Foam::cellCuts::calcFaceCuts() const
         {
             forAll(f, fp)
             {
-                label fp1 = (fp+1) % f.size();
+                label fp1 = f.fcIndex(fp);
 
                 label v0 = f[fp];
                 label v1 = f[fp1];
@@ -438,7 +437,7 @@ void Foam::cellCuts::calcFaceCuts() const
 
         forAll(f, i)
         {
-            label fp1 = (fp+1) % f.size();
+            label fp1 = f.fcIndex(fp);
 
             // Get the three items: current vertex, next vertex and edge
             // inbetween
@@ -659,7 +658,7 @@ bool Foam::cellCuts::crossEdge
         return true;
     }
     else
-    {    
+    {
         // No success. Restore state (i.e. backtrack)
         nVisited = oldNVisited;
 
@@ -794,7 +793,7 @@ bool Foam::cellCuts::walkFace
 
 
 
-// Walk across cuts (cut edges or cut vertices) of cell. Stops when hit cut 
+// Walk across cuts (cut edges or cut vertices) of cell. Stops when hit cut
 // already visited. Returns true when loop of 3 or more vertices found.
 bool Foam::cellCuts::walkCell
 (
@@ -1202,7 +1201,7 @@ Foam::labelList Foam::cellCuts::nonAnchorPoints
 
         if
         (
-            findIndex(anchorPoints, pointI) == -1 
+            findIndex(anchorPoints, pointI) == -1
          && findIndex(loop, vertToEVert(pointI)) == -1
         )
         {
@@ -1556,7 +1555,7 @@ bool Foam::cellCuts::calcAnchors
                 }
                 prevStat = eStat;
             }
-        }        
+        }
     }
 
 
@@ -2135,7 +2134,7 @@ bool Foam::cellCuts::setFromCellLoop
         // Storage for points on one side of cell
         labelList anchorPoints;
 
-        okLoop = 
+        okLoop =
             validLoop(cellI, loop, loopWeights, faceSplitCuts, anchorPoints);
 
         if (okLoop)
@@ -3007,7 +3006,7 @@ void Foam::cellCuts::flip(const label cellI)
             mesh().cellPoints()[cellI],
             cellAnchorPoints_[cellI],
             loop
-        );  
+        );
 }
 
 
diff --git a/src/dynamicMesh/meshCut/cellLooper/geomCellLooper.C b/src/dynamicMesh/meshCut/cellLooper/geomCellLooper.C
index 2576ff98673..1cca5d6111e 100644
--- a/src/dynamicMesh/meshCut/cellLooper/geomCellLooper.C
+++ b/src/dynamicMesh/meshCut/cellLooper/geomCellLooper.C
@@ -53,7 +53,6 @@ namespace Foam
 {
 
 defineTypeNameAndDebug(geomCellLooper, 0);
-
 addToRunTimeSelectionTable(cellLooper, geomCellLooper, word);
 
 }
@@ -187,8 +186,8 @@ bool Foam::geomCellLooper::edgeEndsCut
 
     const edge& e = mesh().edges()[edgeI];
 
-    label prevCut = loop[(index == 0 ? loop.size()-1 : index-1)];
-    label nextCut = loop[(index == loop.size()-1 ? 0 : index+1)];
+    const label prevCut = loop[loop.rcIndex(index)];
+    const label nextCut = loop[loop.fcIndex(index)];
 
     if (!isEdge(prevCut) && !isEdge(nextCut))
     {
@@ -360,7 +359,7 @@ bool Foam::geomCellLooper::cut
                 }
                 else
                 {
-                    // Cut through edge end point. Might be duplicate 
+                    // Cut through edge end point. Might be duplicate
                     // since connected edge might also be snapped to same
                     // endpoint so only insert if unique.
                     label cut = vertToEVert(cutVertI);
diff --git a/src/dynamicMesh/meshCut/meshModifiers/meshCutAndRemove/meshCutAndRemove.C b/src/dynamicMesh/meshCut/meshModifiers/meshCutAndRemove/meshCutAndRemove.C
index 6cbf8ecfc0d..a8dfe9ffcae 100644
--- a/src/dynamicMesh/meshCut/meshModifiers/meshCutAndRemove/meshCutAndRemove.C
+++ b/src/dynamicMesh/meshCut/meshModifiers/meshCutAndRemove/meshCutAndRemove.C
@@ -76,24 +76,11 @@ bool Foam::meshCutAndRemove::isIn
         return false;
     }
 
-    label nextIndex = (index + 1) % cuts.size();
-
-    if (cuts[nextIndex] == twoCuts[1])
-    {
-        return true;
-    }
-
-
-    label prevIndex = (index == 0 ? cuts.size()-1 : index - 1);
-
-    if (cuts[prevIndex] == twoCuts[1])
-    {
-        return true;
-    }
-    else
-    {
-        return false;
-    }
+    return
+    (
+        cuts[cuts.fcIndex(index)] == twoCuts[1]
+     || cuts[cuts.rcIndex(index)] == twoCuts[1]
+    );
 }
 
 
@@ -502,11 +489,11 @@ Foam::face Foam::meshCutAndRemove::addEdgeCutsToFace(const label faceI) const
 
     forAll(f, fp)
     {
-        // Duplicate face vertex .
+        // Duplicate face vertex.
         newFace[newFp++] = f[fp];
 
         // Check if edge has been cut.
-        label fp1 = (fp + 1) % f.size();
+        label fp1 = f.fcIndex(fp);
 
         HashTable<label, edge, Hash<edge> >::const_iterator fnd =
             addedPoints_.find(edge(f[fp], f[fp1]));
@@ -558,7 +545,7 @@ Foam::face Foam::meshCutAndRemove::loopToFace
 
             newFace[newFaceI++] = vertI;
 
-            label nextCut = loop[(fp+1) % loop.size()];
+            label nextCut = loop[loop.fcIndex(fp)];
 
             if (!isEdge(nextCut))
             {
diff --git a/src/dynamicMesh/meshCut/meshModifiers/meshCutter/meshCutter.C b/src/dynamicMesh/meshCut/meshModifiers/meshCutter/meshCutter.C
index 6a97db62568..80e18af319a 100644
--- a/src/dynamicMesh/meshCut/meshModifiers/meshCutter/meshCutter.C
+++ b/src/dynamicMesh/meshCut/meshModifiers/meshCutter/meshCutter.C
@@ -72,24 +72,11 @@ bool Foam::meshCutter::isIn
         return false;
     }
 
-    label nextIndex = (index + 1) % cuts.size();
-
-    if (cuts[nextIndex] == twoCuts[1])
-    {
-        return true;
-    }
-
-
-    label prevIndex = (index == 0 ? cuts.size()-1 : index - 1);
-
-    if (cuts[prevIndex] == twoCuts[1])
-    {
-        return true;
-    }
-    else
-    {
-        return false;
-    }
+    return
+    (
+        cuts[cuts.fcIndex(index)] == twoCuts[1]
+     || cuts[cuts.rcIndex(index)] == twoCuts[1]
+    );
 }
 
 
@@ -459,7 +446,7 @@ Foam::face Foam::meshCutter::addEdgeCutsToFace(const label faceI) const
         newFace[newFp++] = f[fp];
 
         // Check if edge has been cut.
-        label fp1 = (fp + 1) % f.size();
+        label fp1 = f.fcIndex(fp);
 
         HashTable<label, edge, Hash<edge> >::const_iterator fnd =
             addedPoints_.find(edge(f[fp], f[fp1]));
@@ -511,7 +498,7 @@ Foam::face Foam::meshCutter::loopToFace
 
             newFace[newFaceI++] = vertI;
 
-            label nextCut = loop[(fp+1) % loop.size()];
+            label nextCut = loop[loop.fcIndex(fp)];
 
             if (!isEdge(nextCut))
             {
diff --git a/src/dynamicMesh/motionSmoother/polyMeshGeometry/polyMeshGeometry.C b/src/dynamicMesh/motionSmoother/polyMeshGeometry/polyMeshGeometry.C
index 851c8cdec42..f2f7ec17bee 100644
--- a/src/dynamicMesh/motionSmoother/polyMeshGeometry/polyMeshGeometry.C
+++ b/src/dynamicMesh/motionSmoother/polyMeshGeometry/polyMeshGeometry.C
@@ -1284,7 +1284,7 @@ bool Foam::polyMeshGeometry::checkFaceAngles
         forAll(f, fp0)
         {
             // Get vertex after fp
-            label fp1 = (fp0 + 1) % f.size();
+            label fp1 = f.fcIndex(fp0);
 
             // Normalized vector between two consecutive points
             vector e10(p[f[fp1]] - p[f[fp0]]);
diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/combineFaces.C b/src/dynamicMesh/polyTopoChange/polyTopoChange/combineFaces.C
index caa3642f5c9..e0fae6f9712 100644
--- a/src/dynamicMesh/polyTopoChange/polyTopoChange/combineFaces.C
+++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/combineFaces.C
@@ -69,7 +69,7 @@ bool Foam::combineFaces::convexFace
     forAll(f, fp0)
     {
         // Get vertex after fp
-        label fp1 = (fp0 + 1) % f.size();
+        label fp1 = f.fcIndex(fp0);
 
         // Normalized vector between two consecutive points
         vector e10(points[f[fp1]] - points[f[fp0]]);
@@ -730,7 +730,7 @@ void Foam::combineFaces::setRefinement
     }
     else
     {
-        // Count removed points 
+        // Count removed points
         label n = 0;
         forAll(nPointFaces, pointI)
         {
diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/faceCollapser.C b/src/dynamicMesh/polyTopoChange/polyTopoChange/faceCollapser.C
index 3bb5c345d3d..da303ca404d 100644
--- a/src/dynamicMesh/polyTopoChange/polyTopoChange/faceCollapser.C
+++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/faceCollapser.C
@@ -71,7 +71,7 @@ Foam::label Foam::faceCollapser::findEdge
         label edgeI = edgeLabels[i];
 
         const edge& e = edges[edgeI];
-    
+
         if
         (
             (e[0] == v0 && e[1] == v1)
@@ -113,7 +113,7 @@ void Foam::faceCollapser::filterFace
         newFace.append(v0);
 
         // Look ahead one to get edge.
-        label fp1 = (fp + 1) % f.size();
+        label fp1 = f.fcIndex(fp);
 
         label v1 = f[fp1];
 
@@ -410,7 +410,7 @@ void Foam::faceCollapser::setRefinement
 
         forAll(f, fp)
         {
-            label fp1 = (fp + 1) % f.size();
+            label fp1 = f.fcIndex(fp);
 
             // Get index in sorted list
             label sorted0 = sortedFp[fp];
diff --git a/src/meshTools/cellFeatures/cellFeatures.C b/src/meshTools/cellFeatures/cellFeatures.C
index 605624473b5..8d3170dca39 100644
--- a/src/meshTools/cellFeatures/cellFeatures.C
+++ b/src/meshTools/cellFeatures/cellFeatures.C
@@ -50,7 +50,7 @@ bool Foam::cellFeatures::faceAlignedEdge(const label faceI, const label edgeI)
     {
         if (f[fp] == e.start())
         {
-            label fp1 = (fp + 1) % f.size();
+            label fp1 = f.fcIndex(fp);
 
             return f[fp1] == e.end();
         }
@@ -112,7 +112,7 @@ Foam::label Foam::cellFeatures::nextEdge
         << thisEdgeI << " at vertex " << thisVertI << endl
         << "This might mean that the externalEdges do not form a closed loop"
         << abort(FatalError);
-    
+
     return -1;
 }
 
@@ -146,12 +146,12 @@ bool Foam::cellFeatures::isCellFeatureEdge
     const face& f0 = mesh_.faces()[face0];
 
     label face0Start = findIndex(f0, e.start());
-    label face0End = (face0Start + 1) % f0.size();
+    label face0End   = f0.fcIndex(face0Start);
 
     const face& f1 = mesh_.faces()[face1];
 
     label face1Start = findIndex(f1, e.start());
-    label face1End = (face1Start + 1) % f1.size();
+    label face1End   = f1.fcIndex(face1Start);
 
     if
     (
@@ -268,7 +268,7 @@ void Foam::cellFeatures::calcSuperFaces() const
     {
         faceMap_[superI].shrink();
     }
-        
+
 
     // Construct superFaces
 
diff --git a/src/meshTools/octree/octreeDataFace.C b/src/meshTools/octree/octreeDataFace.C
index c8b9e7943c6..d93234f05c9 100644
--- a/src/meshTools/octree/octreeDataFace.C
+++ b/src/meshTools/octree/octreeDataFace.C
@@ -440,8 +440,8 @@ Foam::label Foam::octreeDataFace::getSampleType
             // Face intersection point lies on edge between two face triangles
 
             // Calculate edge normal as average of the two triangle normals
-            label fpPrev = (fp == 0 ? f.size()-1 : fp-1);
-            label fpNext = (fp + 1) % f.size();
+            const label fpPrev = f.rcIndex(fp);
+            const label fpNext = f.fcIndex(fp);
 
             vector e = points[f[fp]] - mesh_.faceCentres()[faceI];
             vector ePrev = points[f[fpPrev]] - mesh_.faceCentres()[faceI];
@@ -530,7 +530,7 @@ bool Foam::octreeDataFace::overlaps
 
     forAll(f, fp)
     {
-        label fp1 = (fp == f.size()-1 ? 0 : fp+1);
+        const label fp1 = f.fcIndex(fp);
 
         bool triIntersects = triangleFuncs::intersectBb
         (
diff --git a/src/meshTools/primitiveMeshGeometry/primitiveMeshGeometry.C b/src/meshTools/primitiveMeshGeometry/primitiveMeshGeometry.C
index a0a237b8f1a..0b226e3e024 100644
--- a/src/meshTools/primitiveMeshGeometry/primitiveMeshGeometry.C
+++ b/src/meshTools/primitiveMeshGeometry/primitiveMeshGeometry.C
@@ -808,7 +808,7 @@ bool Foam::primitiveMeshGeometry::checkFaceAngles
         forAll(f, fp0)
         {
             // Get vertex after fp
-            label fp1 = (fp0 + 1) % f.size();
+            label fp1 = f.fcIndex(fp0);
 
             // Normalized vector between two consecutive points
             vector e10(p[f[fp1]] - p[f[fp0]]);
diff --git a/src/meshTools/triSurface/booleanOps/booleanSurface/booleanSurface.C b/src/meshTools/triSurface/booleanOps/booleanSurface/booleanSurface.C
index ba61f6cb41c..3e007c880e8 100644
--- a/src/meshTools/triSurface/booleanOps/booleanSurface/booleanSurface.C
+++ b/src/meshTools/triSurface/booleanOps/booleanSurface/booleanSurface.C
@@ -241,14 +241,14 @@ void Foam::booleanSurface::propagateEdgeSide
     {
         // Edge (and hence eFaces) in same order as prevVert0.
         // Take next face from sorted list
-        nextInd = (ind + 1) % eFaces.size();
-        prevInd = (ind > 0 ? ind - 1 : eFaces.size()-1);
+        nextInd = eFaces.fcIndex(ind);
+        prevInd = eFaces.rcIndex(ind);
     }
     else
     {
         // Take previous face from sorted neighbours
-        nextInd = (ind > 0 ? ind - 1 : eFaces.size()-1);
-        prevInd = (ind + 1) % eFaces.size();
+        nextInd = eFaces.rcIndex(ind);
+        prevInd = eFaces.fcIndex(ind);
     }
 
 
diff --git a/src/meshTools/triSurface/booleanOps/intersectedSurface/intersectedSurface.C b/src/meshTools/triSurface/booleanOps/intersectedSurface/intersectedSurface.C
index eadfeb7c4b6..94508650811 100644
--- a/src/meshTools/triSurface/booleanOps/intersectedSurface/intersectedSurface.C
+++ b/src/meshTools/triSurface/booleanOps/intersectedSurface/intersectedSurface.C
@@ -222,11 +222,11 @@ bool Foam::intersectedSurface::sameEdgeOrder
         {
             // Get prev/next vertex on fA
             label vA1 = fA[(fpA + 1) % 3];
-            label vAMin1 = fA[fpA == 0 ? 2 : fpA-1];
+            label vAMin1 = fA[fpA ? fpA-1 : 2];
 
             // Get prev/next vertex on fB
             label vB1 = fB[(fpB + 1) % 3];
-            label vBMin1 = fB[fpB == 0 ? 2 : fpB-1];
+            label vBMin1 = fB[fpB ? fpB-1 : 2];
 
             if (vA1 == vB1 || vAMin1 == vBMin1)
             {
@@ -1308,14 +1308,14 @@ Foam::intersectedSurface::intersectedSurface
     // Construct mapping back into original surface
     faceMap_.setSize(size());
 
-    for(label faceI = 0; faceI < surf.size()-1; faceI++)
+    for (label faceI = 0; faceI < surf.size()-1; faceI++)
     {
-        for(label triI = startTriI[faceI]; triI < startTriI[faceI+1]; triI++)
+        for (label triI = startTriI[faceI]; triI < startTriI[faceI+1]; triI++)
         {
             faceMap_[triI] = faceI;
         }
     }
-    for(label triI = startTriI[surf.size()-1]; triI < size(); triI++)
+    for (label triI = startTriI[surf.size()-1]; triI < size(); triI++)
     {
         faceMap_[triI] = surf.size()-1;
     }
diff --git a/src/surfMesh/BasicMeshedSurface/BasicMeshedSurface.C b/src/surfMesh/BasicMeshedSurface/BasicMeshedSurface.C
index 62c4bc452be..41295b44b61 100644
--- a/src/surfMesh/BasicMeshedSurface/BasicMeshedSurface.C
+++ b/src/surfMesh/BasicMeshedSurface/BasicMeshedSurface.C
@@ -473,7 +473,7 @@ Foam::label Foam::BasicMeshedSurface<Face>::triangulate
 
             for (label fp = 1; fp < f.size() - 1; ++fp)
             {
-                label fp1 = (fp + 1) % f.size();
+                label fp1 = f.fcIndex(fp);
 
                 newFaces[newFaceI] = triFace(f[0], f[fp], f[fp1]);
                 faceMap[newFaceI] = faceI;
diff --git a/src/surfMesh/surfMesh/surfMesh.C b/src/surfMesh/surfMesh/surfMesh.C
index a29e6cfeedd..c6166345da5 100644
--- a/src/surfMesh/surfMesh/surfMesh.C
+++ b/src/surfMesh/surfMesh/surfMesh.C
@@ -269,6 +269,22 @@ void Foam::surfMesh::transfer
 }
 
 
+Foam::Xfer< Foam::MeshedSurface<Foam::face> >
+Foam::surfMesh::xfer()
+{
+    Xfer< MeshedSurface<face> > xf;
+
+    xf().storedPoints().transfer(storedPoints_);
+    xf().storedFaces().transfer(storedFaces_);
+    xf().storedZones().transfer(surfZones_);
+
+    // Clear addressing.
+    MeshReference::clearGeom();
+
+    return xf;
+}
+
+
 void Foam::surfMesh::rename(const word& newName)
 {
     FatalErrorIn
diff --git a/src/surfMesh/surfMesh/surfMesh.H b/src/surfMesh/surfMesh/surfMesh.H
index b18080a6f5a..358620fd617 100644
--- a/src/surfMesh/surfMesh/surfMesh.H
+++ b/src/surfMesh/surfMesh/surfMesh.H
@@ -315,6 +315,9 @@ public:
 
         //  Storage management
 
+            //- Transfer contents to the Xfer container as a MeshedSurface
+            Xfer< MeshedSurface<face> > xfer();
+
             //- Clear geometry
             void clearGeom();
 
diff --git a/src/surfMesh/surfaceFormats/ac3d/AC3DsurfaceFormat.C b/src/surfMesh/surfaceFormats/ac3d/AC3DsurfaceFormat.C
index 167a98003b3..8347bdeee8f 100644
--- a/src/surfMesh/surfaceFormats/ac3d/AC3DsurfaceFormat.C
+++ b/src/surfMesh/surfaceFormats/ac3d/AC3DsurfaceFormat.C
@@ -227,7 +227,7 @@ bool Foam::fileFormats::AC3DsurfaceFormat<Face>::read
                         // points may be incomplete
                         for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
                         {
-                            label fp2 = (fp1 + 1) % f.size();
+                            label fp2 = f.fcIndex(fp1);
 
                             dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
                             sizes[zoneI]++;
diff --git a/src/surfMesh/surfaceFormats/obj/OBJsurfaceFormat.C b/src/surfMesh/surfaceFormats/obj/OBJsurfaceFormat.C
index 8500ab66d18..db4295ce64d 100644
--- a/src/surfMesh/surfaceFormats/obj/OBJsurfaceFormat.C
+++ b/src/surfMesh/surfaceFormats/obj/OBJsurfaceFormat.C
@@ -182,7 +182,7 @@ bool Foam::fileFormats::OBJsurfaceFormat<Face>::read
                 // points may be incomplete
                 for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
                 {
-                    label fp2 = (fp1 + 1) % f.size();
+                    label fp2 = f.fcIndex(fp1);
 
                     dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
                     dynZones.append(zoneI);
diff --git a/src/surfMesh/surfaceFormats/off/OFFsurfaceFormat.C b/src/surfMesh/surfaceFormats/off/OFFsurfaceFormat.C
index 748f390d996..7e73affa17d 100644
--- a/src/surfMesh/surfaceFormats/off/OFFsurfaceFormat.C
+++ b/src/surfMesh/surfaceFormats/off/OFFsurfaceFormat.C
@@ -130,7 +130,7 @@ bool Foam::fileFormats::OFFsurfaceFormat<Face>::read
                 // cannot use face::triangulation (points may be incomplete)
                 for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
                 {
-                    label fp2 = (fp1 + 1) % f.size();
+                    label fp2 = f.fcIndex(fp1);
 
                     dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
                 }
diff --git a/src/surfMesh/surfaceFormats/stl/STLsurfaceFormat.C b/src/surfMesh/surfaceFormats/stl/STLsurfaceFormat.C
index 6f5dac06aea..bbc1c097303 100644
--- a/src/surfMesh/surfaceFormats/stl/STLsurfaceFormat.C
+++ b/src/surfMesh/surfaceFormats/stl/STLsurfaceFormat.C
@@ -54,7 +54,7 @@ inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
     const point& p0 = pointLst[f[0]];
     for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
     {
-        label fp2 = (fp1 + 1) % f.size();
+        label fp2 = f.fcIndex(fp1);
 
         const point& p1 = pointLst[f[fp1]];
         const point& p2 = pointLst[f[fp2]];
@@ -95,7 +95,7 @@ inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
     const point& p0 = pointLst[f[0]];
     for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
     {
-        label fp2 = (fp1 + 1) % f.size();
+        label fp2 = f.fcIndex(fp1);
 
         STLtriangle stlTri
         (
diff --git a/src/surfMesh/surfaceFormats/tri/TRIsurfaceFormat.C b/src/surfMesh/surfaceFormats/tri/TRIsurfaceFormat.C
index 5246cfdfc2b..2058b3e8f3c 100644
--- a/src/surfMesh/surfaceFormats/tri/TRIsurfaceFormat.C
+++ b/src/surfMesh/surfaceFormats/tri/TRIsurfaceFormat.C
@@ -45,7 +45,7 @@ inline void Foam::fileFormats::TRIsurfaceFormat<Face>::writeShell
     const point& p0 = pointLst[f[0]];
     for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
     {
-        label fp2 = (fp1 + 1) % f.size();
+        label fp2 = f.fcIndex(fp1);
 
         const point& p1 = pointLst[f[fp1]];
         const point& p2 = pointLst[f[fp2]];
diff --git a/src/triSurface/faceTriangulation/faceTriangulation.C b/src/triSurface/faceTriangulation/faceTriangulation.C
index 0317c3a9463..a2f1ed2aa08 100644
--- a/src/triSurface/faceTriangulation/faceTriangulation.C
+++ b/src/triSurface/faceTriangulation/faceTriangulation.C
@@ -33,17 +33,17 @@ License
 const Foam::scalar Foam::faceTriangulation::edgeRelTol = 1E-6;
 
 
-//- Edge to the right of face vertex i
+// Edge to the right of face vertex i
 Foam::label Foam::faceTriangulation::right(const label, label i)
 {
     return i;
 }
 
 
-//- Edge to the left of face vertex i
+// Edge to the left of face vertex i
 Foam::label Foam::faceTriangulation::left(const label size, label i)
 {
-    return i == 0 ? size - 1 : (i - 1);
+    return i ? i-1 : size-1;
 }
 
 
@@ -60,10 +60,8 @@ Foam::tmp<Foam::vectorField> Foam::faceTriangulation::calcEdges
 
     forAll(f, i)
     {
-        label ni = f.fcIndex(i);
-
         point thisPt = points[f[i]];
-        point nextPt = points[f[ni]];
+        point nextPt = points[f[f.fcIndex(i)]];
 
         vector vec(nextPt - thisPt);
         vec /= mag(vec) + VSMALL;
@@ -109,7 +107,7 @@ void Foam::faceTriangulation::calcHalfAngle
 // Return true and intersection point if intersection between p1 and p2.
 Foam::pointHit Foam::faceTriangulation::rayEdgeIntersect
 (
-    const vector& normal, 
+    const vector& normal,
     const point& rayOrigin,
     const vector& rayDir,
     const point& p1,
@@ -225,10 +223,10 @@ void Foam::faceTriangulation::findDiagonal
     label minIndex = -1;
     scalar minPosOnEdge = GREAT;
 
-    for(label i = 0; i < f.size() - 2; i++)
+    for (label i = 0; i < f.size() - 2; i++)
     {
         scalar posOnEdge;
-        pointHit inter = 
+        pointHit inter =
             rayEdgeIntersect
             (
                 normal,
@@ -258,7 +256,7 @@ void Foam::faceTriangulation::findDiagonal
 
         index1 = -1;
         index2 = -1;
-        return;   
+        return;
     }
 
     const label leftIndex = minIndex;
@@ -299,7 +297,7 @@ void Foam::faceTriangulation::findDiagonal
 
     // all vertices except for startIndex and ones to left and right of it.
     faceVertI = f.fcIndex(f.fcIndex(startIndex));
-    for(label i = 0; i < f.size() - 3; i++)
+    for (label i = 0; i < f.size() - 3; i++)
     {
         const point& pt = points[f[faceVertI]];
 
@@ -400,7 +398,7 @@ Foam::label Foam::faceTriangulation::findStart
 
     return minIndex;
 }
-            
+
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
diff --git a/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C b/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C
index cfab7e726f8..20c3d9cc65d 100644
--- a/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C
+++ b/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C
@@ -153,7 +153,7 @@ bool triSurface::readOBJ(const fileName& OBJfileName)
             // Cannot use face::triangulation since no complete points yet.
             for (label fp = 1; fp < verts.size() - 1; fp++)
             {
-                label fp1 = (fp + 1) % verts.size();
+                label fp1 = verts.fcIndex(fp);
 
                 labelledTri tri(verts[0], verts[fp], verts[fp1], groupID);
 
-- 
GitLab