From 0c4d2bcd76b098a09e3fd16668a8c46aeb15988b Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Sun, 7 May 2017 15:49:52 +0200
Subject: [PATCH] ENH: improve consistency in access for face, triFace, edge.

- ensure that each have found() and which() methods

- add faceTraits for handling compile-time differences between
  'normal' and tri-faces

- provide line::unitVec method (complimentary to edge::unitVec)
---
 src/OpenFOAM/containers/Lists/UList/UList.C   |  2 +
 src/OpenFOAM/meshes/meshShapes/edge/edge.H    |  6 +-
 src/OpenFOAM/meshes/meshShapes/edge/edgeI.H   | 22 +++++-
 src/OpenFOAM/meshes/meshShapes/face/face.H    |  7 +-
 src/OpenFOAM/meshes/meshShapes/face/faceI.H   | 12 ++-
 .../meshes/meshShapes/traits/faceTraits.H     | 73 +++++++++++++++++++
 .../meshes/meshShapes/triFace/triFace.H       |  9 ++-
 .../meshes/meshShapes/triFace/triFaceI.H      | 17 ++++-
 .../meshes/primitiveShapes/line/line.H        |  9 ++-
 .../meshes/primitiveShapes/line/lineI.H       | 10 +++
 10 files changed, 153 insertions(+), 14 deletions(-)
 create mode 100644 src/OpenFOAM/meshes/meshShapes/traits/faceTraits.H

diff --git a/src/OpenFOAM/containers/Lists/UList/UList.C b/src/OpenFOAM/containers/Lists/UList/UList.C
index f3feb5b8352..f4796987442 100644
--- a/src/OpenFOAM/containers/Lists/UList/UList.C
+++ b/src/OpenFOAM/containers/Lists/UList/UList.C
@@ -112,6 +112,7 @@ Foam::UList<T> Foam::UList<T>::operator[](const labelRange& range)
     return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList
 }
 
+
 template<class T>
 const Foam::UList<T> Foam::UList<T>::operator[](const labelRange& range) const
 {
@@ -132,6 +133,7 @@ Foam::UList<T> Foam::UList<T>::operator[]
     return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList
 }
 
+
 template<class T>
 const Foam::UList<T> Foam::UList<T>::operator[]
 (
diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edge.H b/src/OpenFOAM/meshes/meshShapes/edge/edge.H
index 52bf97cb7cf..aecb9a9c2e4 100644
--- a/src/OpenFOAM/meshes/meshShapes/edge/edge.H
+++ b/src/OpenFOAM/meshes/meshShapes/edge/edge.H
@@ -149,7 +149,11 @@ public:
 
         //- Return true if point label is found in edge.
         //  Always false for a negative label.
-        inline bool found(const label index) const;
+        inline bool found(const label pointLabel) const;
+
+        //- Return local index (0,1) of point label in edge -1 on failure
+        //  Always return -1 for a negative label.
+        inline label which(const label pointLabel) const;
 
         //- Do the edges share a common vertex index?
         //  Negative point labels never connect.
diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H
index 75a5ba23655..e4ca81e9386 100644
--- a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H
+++ b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H
@@ -206,10 +206,28 @@ inline Foam::label Foam::edge::maxVertex() const
 }
 
 
-inline bool Foam::edge::found(const label index) const
+inline bool Foam::edge::found(const label pointLabel) const
 {
     // -1: always false
-    return (index >= 0 && (index == start() || index == end()));
+    return (pointLabel >= 0 && (pointLabel == start() || pointLabel == end()));
+}
+
+
+inline Foam::label Foam::edge::which(const label pointLabel) const
+{
+    // -1: always false
+    if (pointLabel >= 0)
+    {
+        if (pointLabel == start())
+        {
+            return 0;
+        }
+        if (pointLabel == end())
+        {
+            return 1;
+        }
+    }
+    return -1;
 }
 
 
diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H
index 2d83def9209..822e3c4f9a6 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/face.H
+++ b/src/OpenFOAM/meshes/meshShapes/face/face.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -202,7 +202,10 @@ public:
 
         //- Navigation through face vertices
 
-            //- Which vertex on face (face index given a global index)
+            //- Return true if the global point label is found in face.
+            inline bool found(const label globalIndex) const;
+
+            //- Which local vertex on face given a global index.
             //  returns -1 if not found
             label which(const label globalIndex) const;
 
diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
index b45193f0e73..ce807528c65 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H
+++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -116,20 +116,24 @@ inline Foam::edge Foam::face::faceEdge(const label n) const
 }
 
 
-// Next vertex on face
+inline bool Foam::face::found(const label globalIndex) const
+{
+    return which(globalIndex) != -1;
+}
+
+
 inline Foam::label Foam::face::nextLabel(const label i) const
 {
     return operator[](fcIndex(i));
 }
 
 
-// Previous vertex on face
 inline Foam::label Foam::face::prevLabel(const label i) const
 {
     return operator[](rcIndex(i));
 }
 
-// Number of triangles directly known from number of vertices
+
 inline Foam::label Foam::face::nTriangles() const
 {
     return size() - 2;
diff --git a/src/OpenFOAM/meshes/meshShapes/traits/faceTraits.H b/src/OpenFOAM/meshes/meshShapes/traits/faceTraits.H
new file mode 100644
index 00000000000..523233a5699
--- /dev/null
+++ b/src/OpenFOAM/meshes/meshShapes/traits/faceTraits.H
@@ -0,0 +1,73 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Class
+    Foam::faceTraits
+
+Description
+    Traits class for faces
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef faceTraits_H
+#define faceTraits_H
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+
+// Forward declarations
+class triFace;
+class labelledTri;
+
+/*---------------------------------------------------------------------------*\
+                         Class faceTraits Declaration
+\*---------------------------------------------------------------------------*/
+
+template<class FaceType>
+class faceTraits
+{
+public:
+
+    //- Face-type only handles triangles. Not true in general.
+    inline static bool isTri()  { return false; }
+};
+
+
+template<>
+inline bool faceTraits<triFace>::isTri()        { return true; }
+
+template<>
+inline bool faceTraits<labelledTri>::isTri()    { return true; }
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
index e0e586673ac..301b845d112 100644
--- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
+++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -137,6 +137,13 @@ public:
         //  The starting points of the original and reverse face are identical.
         inline triFace reverseFace() const;
 
+        //- Return true if the global point label is found in face.
+        bool found(const label globalIndex) const;
+
+        //- Which local index (0,1,2) on face given a global index.
+        //  returns -1 if not found
+        label which(const label globalIndex) const;
+
         //- Return swept-volume from old-points to new-points
         inline scalar sweptVol
         (
diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
index 69d2a4033b8..ae2545a7aa2 100644
--- a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
+++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -209,6 +209,21 @@ inline Foam::triFace Foam::triFace::reverseFace() const
 }
 
 
+inline bool Foam::triFace::found(const label globalIndex) const
+{
+    return which(globalIndex) != -1;
+}
+
+
+inline Foam::label Foam::triFace::which(const label globalIndex) const
+{
+    if (operator[](0) == globalIndex) return 0;
+    if (operator[](1) == globalIndex) return 1;
+    if (operator[](2) == globalIndex) return 2;
+    return -1;
+}
+
+
 inline Foam::scalar Foam::triFace::sweptVol
 (
     const UList<point>& opts,
diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/line.H b/src/OpenFOAM/meshes/primitiveShapes/line/line.H
index 74bc680134e..778a843ba75 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/line/line.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/line/line.H
@@ -98,10 +98,10 @@ public:
 
         // Access
 
-            //- Return first vertex
+            //- Return first point
             inline PointRef start() const;
 
-            //- Return second vertex
+            //- Return second point
             inline PointRef end() const;
 
 
@@ -113,9 +113,12 @@ public:
             //- Return scalar magnitude
             inline scalar mag() const;
 
-            //- Return start-end vector
+            //- Return start-to-end vector
             inline Point vec() const;
 
+            //- Return the unit vector (start-to-end)
+            inline Point unitVec() const;
+
             //- Return nearest distance to line from a given point
             //  If the nearest point is on the line, return a hit
             PointHit<Point> nearestDist(const Point& p) const;
diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H
index 2dfca1e1c2c..c4757716cfc 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H
@@ -90,6 +90,16 @@ inline Point Foam::line<Point, PointRef>::vec() const
 }
 
 
+template<class Point, class PointRef>
+inline Point Foam::line<Point, PointRef>::unitVec() const
+{
+    Point v = b_ - a_;
+    v /= ::Foam::mag(v) + VSMALL;
+
+    return v;
+}
+
+
 template<class Point, class PointRef>
 Foam::PointHit<Point> Foam::line<Point, PointRef>::nearestDist
 (
-- 
GitLab