From 2d77f7ae26aa0166fdfb7f9125d8acf2572adfdf Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 24 Apr 2025 10:53:22 +0200
Subject: [PATCH] ENH: add face/triFace/triangle magSqr() method (as per edge,
 boundBox etc)

---
 src/OpenFOAM/meshes/meshShapes/face/face.H        |  5 ++++-
 src/OpenFOAM/meshes/meshShapes/face/faceI.H       |  8 +++++++-
 src/OpenFOAM/meshes/meshShapes/triFace/triFace.H  |  3 +++
 src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H |  8 +++++++-
 .../constraint/cyclic/cyclicPolyPatch.C           |  6 +++---
 .../constraint/cyclic/cyclicPolyPatch.H           |  2 +-
 .../constraint/oldCyclic/oldCyclicPolyPatch.C     |  6 +++---
 .../constraint/oldCyclic/oldCyclicPolyPatch.H     |  6 +++---
 src/OpenFOAM/meshes/primitiveShapes/line/line.H   |  8 +++++++-
 src/OpenFOAM/meshes/primitiveShapes/line/lineI.H  | 15 ++++++++++++++-
 .../meshes/primitiveShapes/triangle/triangle.H    |  8 +++++++-
 .../meshes/primitiveShapes/triangle/triangleI.H   | 13 +++++++++++++
 .../polyTopoChange/hexRef8/hexRef8.C              |  4 ++--
 13 files changed, 74 insertions(+), 18 deletions(-)

diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H
index 7d9a1bd170b..4905efdf9e4 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/face.H
+++ b/src/OpenFOAM/meshes/meshShapes/face/face.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2017-2023 OpenCFD Ltd.
+    Copyright (C) 2017-2025 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -207,6 +207,9 @@ public:
         //- Magnitude of face area
         inline scalar mag(const UList<point>& p) const;
 
+        //- Magnitude squared of face area
+        inline scalar magSqr(const UList<point>& p) const;
+
         //- The enclosing (bounding) box for the face
         inline Pair<point> box(const UList<point>& pts) const;
 
diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
index 36c535a9ce6..5f2e3698a64 100644
--- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H
+++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011 OpenFOAM Foundation
-    Copyright (C) 2017-2023 OpenCFD Ltd.
+    Copyright (C) 2017-2025 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -115,6 +115,12 @@ inline Foam::scalar Foam::face::mag(const UList<point>& p) const
 }
 
 
+inline Foam::scalar Foam::face::magSqr(const UList<point>& p) const
+{
+    return ::Foam::magSqr(areaNormal(p));
+}
+
+
 inline Foam::Pair<Foam::point>
 Foam::face::box(const UList<point>& pts) const
 {
diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
index 3145dca25ad..3a2ea0da44a 100644
--- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
+++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H
@@ -181,6 +181,9 @@ public:
         //- Magnitude of face area
         inline scalar mag(const UList<point>& points) const;
 
+        //- Magnitude squared of face area
+        inline scalar magSqr(const UList<point>& points) const;
+
         //- The enclosing (bounding) box for the face
         inline Pair<point> box(const UList<point>& points) const;
 
diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
index 2832c6ff2c9..3eb283a9168 100644
--- a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
+++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2013 OpenFOAM Foundation
-    Copyright (C) 2017-2023 OpenCFD Ltd.
+    Copyright (C) 2017-2025 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -201,6 +201,12 @@ inline Foam::scalar Foam::triFace::mag(const UList<point>& points) const
 }
 
 
+inline Foam::scalar Foam::triFace::magSqr(const UList<point>& points) const
+{
+    return ::Foam::magSqr(areaNormal(points));
+}
+
+
 inline Foam::Pair<Foam::point>
 Foam::triFace::box(const UList<point>& points) const
 {
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.C b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.C
index ea0bfbbc848..dfe502034f1 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.C
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.C
@@ -53,8 +53,8 @@ namespace Foam
 
 Foam::label Foam::cyclicPolyPatch::findMaxArea
 (
-    const pointField& points,
-    const faceList& faces
+    const UList<point>& points,
+    const UList<face>& faces
 )
 {
     label maxI = -1;
@@ -62,7 +62,7 @@ Foam::label Foam::cyclicPolyPatch::findMaxArea
 
     forAll(faces, facei)
     {
-        scalar areaSqr = magSqr(faces[facei].areaNormal(points));
+        scalar areaSqr = faces[facei].magSqr(points);
 
         if (maxAreaSqr < areaSqr)
         {
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.H b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.H
index 19d0c9406e2..05437a4c3ca 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.H
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/cyclic/cyclicPolyPatch.H
@@ -112,7 +112,7 @@ class cyclicPolyPatch
     // Private Member Functions
 
         //- Find amongst selected faces the one with the largest area
-        static label findMaxArea(const pointField&, const faceList&);
+        static label findMaxArea(const UList<point>&, const UList<face>&);
 
         void calcTransforms
         (
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.C b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.C
index 26f114c46bd..d1147f826c0 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.C
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.C
@@ -85,8 +85,8 @@ Foam::pointField Foam::oldCyclicPolyPatch::getAnchorPoints
 
 Foam::label Foam::oldCyclicPolyPatch::findMaxArea
 (
-    const pointField& points,
-    const faceList& faces
+    const UList<point>& points,
+    const UList<face>& faces
 )
 {
     label maxI = -1;
@@ -94,7 +94,7 @@ Foam::label Foam::oldCyclicPolyPatch::findMaxArea
 
     forAll(faces, facei)
     {
-        scalar areaSqr = magSqr(faces[facei].areaNormal(points));
+        scalar areaSqr = faces[facei].magSqr(points);
 
         if (maxAreaSqr < areaSqr)
         {
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.H b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.H
index 4d57a7cbcbb..3a04226c27e 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.H
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/oldCyclic/oldCyclicPolyPatch.H
@@ -38,8 +38,8 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef oldCyclicPolyPatch_H
-#define oldCyclicPolyPatch_H
+#ifndef Foam_oldCyclicPolyPatch_H
+#define Foam_oldCyclicPolyPatch_H
 
 #include "coupledPolyPatch.H"
 
@@ -80,7 +80,7 @@ class oldCyclicPolyPatch
     // Private member functions
 
         //- Find amongst selected faces the one with the largest area
-        static label findMaxArea(const pointField&, const faceList&);
+        static label findMaxArea(const UList<point>&, const UList<face>&);
 
         void calcTransforms();
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/line.H b/src/OpenFOAM/meshes/primitiveShapes/line/line.H
index 0120e25aa35..2ad3fe8ced6 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/line/line.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/line/line.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011 OpenFOAM Foundation
-    Copyright (C) 2018-2022 OpenCFD Ltd.
+    Copyright (C) 2018-2025 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -128,6 +128,9 @@ public:
         //- The magnitude (length) of the line
         inline scalar mag() const;
 
+        //- The magnitude squared (length squared) of the line
+        inline scalar magSqr() const;
+
         //- Return start-to-end vector
         inline vector vec() const;
 
@@ -214,6 +217,9 @@ public:
         //- The magnitude (length) of the line
         inline scalar mag() const;
 
+        //- The magnitude squared (length squared) of the line
+        inline scalar magSqr() const;
+
         //- Return start-to-end vector
         inline Point vec() const;
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H
index 132535f1192..9eb7a2ed795 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011 OpenFOAM Foundation
-    Copyright (C) 2018-2022 OpenCFD Ltd.
+    Copyright (C) 2018-2025 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -112,6 +112,19 @@ inline Foam::scalar Foam::linePoints::mag() const
 }
 
 
+template<class Point, class PointRef>
+inline Foam::scalar Foam::line<Point, PointRef>::magSqr() const
+{
+    return ::Foam::magSqr(b() - a());
+}
+
+
+inline Foam::scalar Foam::linePoints::magSqr() const
+{
+    return ::Foam::magSqr(b() - a());
+}
+
+
 template<class Point, class PointRef>
 inline Point Foam::line<Point, PointRef>::vec() const
 {
diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H
index f9767cd3dbb..0f48056c364 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2024 OpenCFD Ltd.
+    Copyright (C) 2018-2025 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -161,6 +161,9 @@ public:
         //- The magnitude of the triangle area
         inline scalar mag() const;
 
+        //- The magnitude squared of the triangle area
+        inline scalar magSqr() const;
+
         //- The enclosing (bounding) box for the triangle
         inline Pair<point> box() const;
 
@@ -378,6 +381,9 @@ public:
         //- The magnitude of the triangle area
         inline scalar mag() const;
 
+        //- The magnitude squared of the triangle area
+        inline scalar magSqr() const;
+
         //- The enclosing (bounding) box for the triangle
         inline Pair<Point> box() const;
 
diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H
index 1014b569621..1e5a7bd25df 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H
+++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H
@@ -319,6 +319,19 @@ inline Foam::scalar Foam::triangle<Point, PointRef>::mag() const
 }
 
 
+inline Foam::scalar Foam::triPoints::magSqr() const
+{
+    return ::Foam::magSqr(areaNormal());
+}
+
+
+template<class Point, class PointRef>
+inline Foam::scalar Foam::triangle<Point, PointRef>::magSqr() const
+{
+    return ::Foam::magSqr(areaNormal());
+}
+
+
 template<class Point, class PointRef>
 inline Point Foam::triangle<Point, PointRef>::circumCentre() const
 {
diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/hexRef8.C b/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/hexRef8.C
index 380bc508032..c1070f9018e 100644
--- a/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/hexRef8.C
+++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/hexRef8.C
@@ -431,7 +431,7 @@ Foam::scalar Foam::hexRef8::getLevel0EdgeLength() const
             {
                 const edge& e = mesh_.edges()[edgeI];
 
-                scalar edgeLenSqr = magSqr(e.vec(mesh_.points()));
+                scalar edgeLenSqr = e.magSqr(mesh_.points());
 
                 typEdgeLenSqr[eLevel] = min(typEdgeLenSqr[eLevel], edgeLenSqr);
             }
@@ -468,7 +468,7 @@ Foam::scalar Foam::hexRef8::getLevel0EdgeLength() const
         {
             const edge& e = mesh_.edges()[cEdges[i]];
 
-            scalar edgeLenSqr = magSqr(e.vec(mesh_.points()));
+            scalar edgeLenSqr = e.magSqr(mesh_.points());
 
             maxEdgeLenSqr[cLevel] = max(maxEdgeLenSqr[cLevel], edgeLenSqr);
         }
-- 
GitLab