From 2ee4e0a06d8b2ce858f9147bd7581b996901e9ee Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 20 Jun 2018 12:17:03 +0200
Subject: [PATCH] STYLE: separate areaNormal/unitNormal method for primitives
 (issue #885)

---
 src/OpenFOAM/meshes/meshShapes/face/face.C    | 13 +++++-----
 src/OpenFOAM/meshes/meshShapes/face/face.H    | 19 ++++++++++----
 .../meshShapes/face/faceContactSphere.C       |  6 ++---
 src/OpenFOAM/meshes/meshShapes/face/faceI.H   | 12 +++++++--
 .../meshes/meshShapes/triFace/triFace.H       | 18 ++++++++++---
 .../meshes/meshShapes/triFace/triFaceI.H      | 26 ++++++++++++-------
 src/OpenFOAM/meshes/primitiveShapes/cut/cut.H |  2 +-
 .../meshes/primitiveShapes/cut/cutTemplates.C |  6 +++--
 .../meshes/primitiveShapes/plane/plane.H      |  2 +-
 .../meshes/primitiveShapes/pyramid/pyramidI.H |  2 +-
 .../primitiveShapes/tetrahedron/tetrahedron.H | 10 ++++---
 .../tetrahedron/tetrahedronI.H                | 15 ++++++-----
 .../primitiveShapes/triangle/triangle.H       | 18 ++++++++++---
 .../primitiveShapes/triangle/triangleI.H      | 21 ++++++++++-----
 src/lagrangian/basic/particle/particle.H      |  5 ----
 src/lagrangian/basic/particle/particleI.H     |  6 -----
 16 files changed, 113 insertions(+), 68 deletions(-)

diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.C b/src/OpenFOAM/meshes/meshShapes/face/face.C
index c1232819b4..c5b94c3811 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/face.C
+++ b/src/OpenFOAM/meshes/meshShapes/face/face.C
@@ -81,7 +81,7 @@ Foam::label Foam::face::mostConcaveAngle
     scalar& maxAngle
 ) const
 {
-    vector n(normal(points));
+    vector n(areaNormal(points));
 
     label index = 0;
     maxAngle = -GREAT;
@@ -542,17 +542,16 @@ Foam::point Foam::face::centre(const UList<point>& points) const
 }
 
 
-Foam::vector Foam::face::normal(const UList<point>& p) const
+Foam::vector Foam::face::areaNormal(const UList<point>& p) const
 {
     const label nPoints = size();
 
-    // Calculate the normal by summing the face triangle normals.
+    // Calculate the area normal by summing the face triangle area normals.
     // Changed to deal with small concavity by using a central decomposition
-    //
 
     // If the face is a triangle, do a direct calculation to avoid round-off
     // error-related problems
-    //
+
     if (nPoints == 3)
     {
         return triPointRef
@@ -560,7 +559,7 @@ Foam::vector Foam::face::normal(const UList<point>& p) const
             p[operator[](0)],
             p[operator[](1)],
             p[operator[](2)]
-        ).normal();
+        ).areaNormal();
     }
 
     label pI;
@@ -594,7 +593,7 @@ Foam::vector Foam::face::normal(const UList<point>& p) const
             p[operator[](pI)],
             nextPoint,
             centrePoint
-        ).normal();
+        ).areaNormal();
     }
 
     return n;
diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H
index 61f2060f10..a07d5cb6c3 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  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -39,7 +39,6 @@ SourceFiles
     faceTemplates.C
 
 \*---------------------------------------------------------------------------*/
-
 #ifndef face_H
 #define face_H
 
@@ -194,12 +193,22 @@ public:
             const Field<Type>& fld
         ) const;
 
+        //- The area normal - with magnitude equal to area of face
+        vector areaNormal(const UList<point>& p) const;
+
+        //- The unit normal
+        inline vector unitNormal(const UList<point>& p) const;
+
+        //- Legacy name for areaNormal.
+        //  \deprecated Deprecated for new use (JUN-2018)
+        inline vector normal(const UList<point>& p) const
+        {
+            return areaNormal(p); // Legacy definition
+        }
+
         //- Magnitude of face area
         inline scalar mag(const UList<point>& p) const;
 
-        //- Vector normal; magnitude is equal to area of face
-        vector normal(const UList<point>& p) const;
-
         //- Return face with reverse direction
         //  The starting points of the original and reverse face are identical.
         face reverseFace() const;
diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceContactSphere.C b/src/OpenFOAM/meshes/meshShapes/face/faceContactSphere.C
index 6a5c982840..05a9e39750 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/faceContactSphere.C
+++ b/src/OpenFOAM/meshes/meshShapes/face/faceContactSphere.C
@@ -37,10 +37,8 @@ Foam::scalar Foam::face::contactSphereDiameter
     const UList<point>& meshPoints
 ) const
 {
-    scalar magN = Foam::mag(n);
-
-    vector n1 = n/(magN + SMALL);
-    vector n2 = normal(meshPoints);
+    vector n1 = n/(Foam::mag(n) + SMALL);
+    vector n2 = areaNormal(meshPoints);
 
     n2 /= Foam::mag(n2) + SMALL;
 
diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
index c5b39c4be9..1c35b20adb 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  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -105,9 +105,17 @@ inline Foam::pointField Foam::face::points
 }
 
 
+inline Foam::vector Foam::face::unitNormal(const UList<point>& p) const
+{
+    const vector n(areaNormal(p));
+    const scalar s(Foam::mag(n));
+    return s < ROOTVSMALL ? Zero : n/s;
+}
+
+
 inline Foam::scalar Foam::face::mag(const UList<point>& p) const
 {
-    return ::Foam::mag(normal(p));
+    return ::Foam::mag(areaNormal(p));
 }
 
 
diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
index 68a41feebd..370a1079d8 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  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -124,12 +124,22 @@ public:
         template<class Type>
         Type average(const UList<point>& unused, const Field<Type>& fld) const;
 
+        //- The area normal - with magnitude equal to area of face
+        inline vector areaNormal(const UList<point>& points) const;
+
+        //- The unit normal
+        inline vector unitNormal(const UList<point>& points) const;
+
+        //- Legacy name for areaNormal.
+        //  \deprecated Deprecated for new use (JUN-2018)
+        inline vector normal(const UList<point>& points) const
+        {
+            return areaNormal(points); // Legacy definition
+        }
+
         //- Magnitude of face area
         inline scalar mag(const UList<point>& points) const;
 
-        //- Vector normal; magnitude is equal to area of face
-        inline vector normal(const UList<point>& points) const;
-
         //- Number of triangles after splitting
         inline label nTriangles() const;
 
diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
index 6fc2caa116..f8afa79b6e 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  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -171,15 +171,9 @@ inline Foam::point Foam::triFace::centre(const UList<point>& points) const
 }
 
 
-inline Foam::scalar Foam::triFace::mag(const UList<point>& points) const
-{
-    return ::Foam::mag(normal(points));
-}
-
-
-// could also delegate to triPointRef(...).normal()
-inline Foam::vector Foam::triFace::normal(const UList<point>& points) const
+inline Foam::vector Foam::triFace::areaNormal(const UList<point>& points) const
 {
+    // As per triPointRef(...).areaNormal()
     return 0.5*
     (
         (points[operator[](1)] - points[operator[](0)])
@@ -188,6 +182,20 @@ inline Foam::vector Foam::triFace::normal(const UList<point>& points) const
 }
 
 
+inline Foam::vector Foam::triFace::unitNormal(const UList<point>& points) const
+{
+    const vector n(areaNormal(points));
+    const scalar s(Foam::mag(n));
+    return s < ROOTVSMALL ? Zero : n/s;
+}
+
+
+inline Foam::scalar Foam::triFace::mag(const UList<point>& points) const
+{
+    return ::Foam::mag(areaNormal(points));
+}
+
+
 inline Foam::label Foam::triFace::nTriangles() const
 {
     return 1;
diff --git a/src/OpenFOAM/meshes/primitiveShapes/cut/cut.H b/src/OpenFOAM/meshes/primitiveShapes/cut/cut.H
index 9e821c0c53..8d46b372ac 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/cut/cut.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/cut/cut.H
@@ -172,7 +172,7 @@ public:
         //- Operate on a triangle
         result operator()(const FixedList<point, 3>& p) const
         {
-            return result(triPointRef(p[0], p[1], p[2]).normal());
+            return result(triPointRef(p[0], p[1], p[2]).areaNormal());
         }
 };
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/cut/cutTemplates.C b/src/OpenFOAM/meshes/primitiveShapes/cut/cutTemplates.C
index b4dda7f4c0..1ec2438090 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/cut/cutTemplates.C
+++ b/src/OpenFOAM/meshes/primitiveShapes/cut/cutTemplates.C
@@ -106,8 +106,9 @@ typename Foam::cut::opAddResult<AboveOp, BelowOp>::type Foam::triCut
 {
     // Set the level set to the signed distance from the plane
     FixedList<scalar, 3> level;
-    for (label i = 0; i < 3; ++ i)
+    for (label i = 0; i < 3; ++i)
     {
+        // uses unit-normal
         level[i] = (tri[i] - p.refPoint()) & p.normal();
     }
 
@@ -251,8 +252,9 @@ typename Foam::cut::opAddResult<AboveOp, BelowOp>::type Foam::tetCut
 {
     // Set the level set to the signed distance from the plane
     FixedList<scalar, 4> level;
-    for (label i = 0; i < 4; ++ i)
+    for (label i = 0; i < 4; ++i)
     {
+        // uses unit-normal
         level[i] = (tet[i] - p.refPoint()) & p.normal();
     }
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H
index 2917939bf3..88a39b3162 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H
@@ -195,7 +195,7 @@ public:
 
     // Member Functions
 
-        //- Return plane normal
+        //- Return the plane unit normal
         const vector& normal() const;
 
         //- Return plane base point
diff --git a/src/OpenFOAM/meshes/primitiveShapes/pyramid/pyramidI.H b/src/OpenFOAM/meshes/primitiveShapes/pyramid/pyramidI.H
index f729ff36c3..dc369e166c 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/pyramid/pyramidI.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/pyramid/pyramidI.H
@@ -89,7 +89,7 @@ inline Foam::scalar Foam::pyramid<Point, PointRef, polygonRef>::mag
     const UList<point>& points
 ) const
 {
-    return (1.0/3.0)*(base_.normal(points)&(height(points)));
+    return (1.0/3.0)*(base_.areaNormal(points) & (height(points)));
 }
 
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H
index 1006382339..a65317847a 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.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) 2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -54,13 +54,12 @@ SourceFiles
 namespace Foam
 {
 
+// Forward declarations
 class Istream;
 class Ostream;
 class tetPoints;
 class plane;
 
-// Forward declaration of friend functions and operators
-
 template<class Point, class PointRef> class tetrahedron;
 
 template<class Point, class PointRef>
@@ -210,13 +209,16 @@ public:
 
         // Properties
 
-            //- Return face normal
+            //- Face area normal for side a
             inline vector Sa() const;
 
+            //- Face area normal for side b
             inline vector Sb() const;
 
+            //- Face area normal for side c
             inline vector Sc() const;
 
+            //- Face area normal for side d
             inline vector Sd() const;
 
             //- Return centre (centroid)
diff --git a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H
index 9ffff4ae8c..64e18c3dd7 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -133,28 +133,28 @@ inline Foam::triPointRef Foam::tetrahedron<Point, PointRef>::tri
 template<class Point, class PointRef>
 inline Foam::vector Foam::tetrahedron<Point, PointRef>::Sa() const
 {
-    return triangle<Point, PointRef>(b_, c_, d_).normal();
+    return triangle<Point, PointRef>(b_, c_, d_).areaNormal();
 }
 
 
 template<class Point, class PointRef>
 inline Foam::vector Foam::tetrahedron<Point, PointRef>::Sb() const
 {
-    return triangle<Point, PointRef>(a_, d_, c_).normal();
+    return triangle<Point, PointRef>(a_, d_, c_).areaNormal();
 }
 
 
 template<class Point, class PointRef>
 inline Foam::vector Foam::tetrahedron<Point, PointRef>::Sc() const
 {
-    return triangle<Point, PointRef>(a_, b_, d_).normal();
+    return triangle<Point, PointRef>(a_, b_, d_).areaNormal();
 }
 
 
 template<class Point, class PointRef>
 inline Foam::vector Foam::tetrahedron<Point, PointRef>::Sd() const
 {
-    return triangle<Point, PointRef>(a_, c_, b_).normal();
+    return triangle<Point, PointRef>(a_, c_, b_).areaNormal();
 }
 
 
@@ -415,7 +415,7 @@ bool Foam::tetrahedron<Point, PointRef>::inside(const point& pt) const
     // "definitively" shown otherwise by obtaining a positive dot
     // product greater than a tolerance of SMALL.
 
-    // The tet is defined: tet(Cc, tetBasePt, pA, pB) where the normal
+    // The tet is defined: tet(Cc, tetBasePt, pA, pB) where the area normal
     // vectors and base points for the half-space planes are:
     // area[0] = Sa();
     // area[1] = Sb();
@@ -575,10 +575,11 @@ inline void Foam::tetrahedron<Point, PointRef>::tetSliceWithPlane
     label nPos = 0;
     forAll(tet, i)
     {
+        // with plane unit-normal
         d[i] = ((tet[i] - pl.refPoint()) & pl.normal());
         if (d[i] > 0)
         {
-            nPos++;
+            ++nPos;
         }
     }
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H
index d83fa0d8b8..5f09ce6026 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -206,12 +206,22 @@ public:
             //- Return centre (centroid)
             inline Point centre() const;
 
+            //- The area normal - with magnitude equal to area of triangle
+            inline vector areaNormal() const;
+
+            //- Return unit normal
+            inline vector unitNormal() const;
+
+            //- Legacy name for areaNormal.
+            //  \deprecated Deprecated for new use (JUN-2018)
+            inline vector normal() const
+            {
+                return areaNormal();
+            }
+
             //- Return scalar magnitude
             inline scalar mag() const;
 
-            //- Return vector normal
-            inline vector normal() const;
-
             //- Return circum-centre
             inline Point circumCentre() const;
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H
index 571a699855..ffd9c20cab 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -106,16 +106,25 @@ inline Point Foam::triangle<Point, PointRef>::centre() const
 
 
 template<class Point, class PointRef>
-inline Foam::scalar Foam::triangle<Point, PointRef>::mag() const
+inline Foam::vector Foam::triangle<Point, PointRef>::areaNormal() const
 {
-    return Foam::mag(normal());
+    return 0.5*((b_ - a_)^(c_ - a_));
 }
 
 
 template<class Point, class PointRef>
-inline Foam::vector Foam::triangle<Point, PointRef>::normal() const
+inline Foam::vector Foam::triangle<Point, PointRef>::unitNormal() const
 {
-    return 0.5*((b_ - a_)^(c_ - a_));
+    const vector n(areaNormal());
+    const scalar s(Foam::mag(n));
+    return s < ROOTVSMALL ? Zero : n/s;
+}
+
+
+template<class Point, class PointRef>
+inline Foam::scalar Foam::triangle<Point, PointRef>::mag() const
+{
+    return ::Foam::mag(areaNormal());
 }
 
 
@@ -392,7 +401,7 @@ inline Foam::pointHit Foam::triangle<Point, PointRef>::ray
      || (alg == intersection::HALF_RAY && dist > -planarPointTol)
      || (
             alg == intersection::VISIBLE
-         && ((q1 & normal()) < -VSMALL)
+         && ((q1 & areaNormal()) < -VSMALL)
         );
 
     if (hit && eligible)
diff --git a/src/lagrangian/basic/particle/particle.H b/src/lagrangian/basic/particle/particle.H
index 04349a3994..4d6928e2b1 100644
--- a/src/lagrangian/basic/particle/particle.H
+++ b/src/lagrangian/basic/particle/particle.H
@@ -526,11 +526,6 @@ public:
             //  current tet.
             inline vector normal() const;
 
-            //- Return the normal of the tri on tetFacei_ for the
-            //  current tet at the start of the timestep, i.e. based
-            //  on oldPoints
-            inline vector oldNormal() const;
-
             //- Is the particle on a face?
             inline bool onFace() const;
 
diff --git a/src/lagrangian/basic/particle/particleI.H b/src/lagrangian/basic/particle/particleI.H
index a622ad4faa..771eeec057 100644
--- a/src/lagrangian/basic/particle/particleI.H
+++ b/src/lagrangian/basic/particle/particleI.H
@@ -280,12 +280,6 @@ inline Foam::vector Foam::particle::normal() const
 }
 
 
-inline Foam::vector Foam::particle::oldNormal() const
-{
-    return currentTetIndices().oldFaceTri(mesh_).normal();
-}
-
-
 inline bool Foam::particle::onFace() const
 {
     return facei_ >= 0;
-- 
GitLab