From dc7d0ffeaae3cca49e93ec35241b72cae3038116 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 25 Oct 2023 21:22:51 +0200 Subject: [PATCH] ENH: add face/triFace find(edge) method (#3004) - returns the edge index within the face, -1 if not found --- applications/test/faces/Test-faces.C | 40 ++++++++++++++++++- src/OpenFOAM/meshes/meshShapes/face/face.C | 37 ++++++++++++++++- src/OpenFOAM/meshes/meshShapes/face/face.H | 7 ++++ src/OpenFOAM/meshes/meshShapes/face/faceI.H | 1 + .../meshes/meshShapes/triFace/triFace.H | 7 ++++ .../meshes/meshShapes/triFace/triFaceI.H | 35 +++++++++++++--- 6 files changed, 118 insertions(+), 9 deletions(-) diff --git a/applications/test/faces/Test-faces.C b/applications/test/faces/Test-faces.C index 9c8cfcea03..9f5376acb0 100644 --- a/applications/test/faces/Test-faces.C +++ b/applications/test/faces/Test-faces.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2022 OpenCFD Ltd. + Copyright (C) 2016-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -128,6 +128,25 @@ int main(int argc, char *argv[]) Info<< "face:"; faceInfo(f1, points1); Info << nl; testSign(f1, points1, testPoints); + Info<< "find within " << f1 << nl; + for + ( + const edge& e + : edgeList + ({ + {0, 1}, + {2, 1}, + {4, 1}, + f1.edge(1), + {4, 3}, + {8, 1}, + })) + { + Info<< " edge:" << e << " = " << f1.find(e) + << " direction:" << f1.edgeDirection(e) + << " contains:" << f1.contains(e) << nl; + } + Info<< "face:"; faceInfo(f1, points2); Info << nl; testSign(f1, points2, testPoints); Info<< nl; @@ -146,6 +165,25 @@ int main(int argc, char *argv[]) Info<< "triFace:"; faceInfo(t1, points1); Info << nl; testSign(t1, points1, testPoints); + Info<< "find within " << t1 << nl; + for + ( + const edge& e + : edgeList + ({ + {0, 1}, + {2, 1}, + {4, 1}, + t1.edge(1), + {4, 3}, + {8, 1}, + })) + { + Info<< " edge:" << e << " = " << t1.find(e) + << " direction:" << t1.edgeDirection(e) + << " contains:" << t1.contains(e) << nl; + } + { scalarField fld({0, 20, 20, 30}); Info<< "avg:" << t1.average(pointField::null(), fld) << nl; diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.C b/src/OpenFOAM/meshes/meshShapes/face/face.C index 287db3ab31..7b238c6c8b 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.C +++ b/src/OpenFOAM/meshes/meshShapes/face/face.C @@ -810,14 +810,47 @@ Foam::edgeList Foam::face::rcEdges() const } +Foam::label Foam::face::find(const Foam::edge& e) const +{ + const label idx = labelList::find(e.first()); + + // NB: the point index is simultaneously the edge index. + // ie, face point 0 starts edge 0, point 1 starts edge 1, ... + + if (idx != -1) + { + if (e.second() == nextLabel(idx)) + { + // Current edge matches forward + return idx; + } + if (e.second() == prevLabel(idx)) + { + // Previous edge matches reverse + return rcIndex(idx); + } + } + + return -1; // Not found +} + + int Foam::face::edgeDirection(const Foam::edge& e) const { const label idx = labelList::find(e.first()); if (idx != -1) { - if (e.second() == nextLabel(idx)) return 1; // Forward - if (e.second() == prevLabel(idx)) return -1; // Reverse + if (e.second() == nextLabel(idx)) + { + // Current edge matches forward. Could encode: (idx+1) + return 1; + } + if (e.second() == prevLabel(idx)) + { + // Previous edge matches reverse. Could encode: -(rcIndex(idx)+1) + return -1; + } } return 0; // Not found diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index 7f37647997..b103c0256e 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -374,6 +374,13 @@ public: //- True if face contains(edge) inline bool contains(const Foam::edge& e) const; + //- Regular find pointi within face + using labelList::find; + + //- Find the edge within the face. + // \return the edge index or -1 if not found + label find(const Foam::edge& e) const; + //- Test the edge direction on the face // \return // - 0: edge not found on the face diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H index 490f5b76d2..320b05e6c7 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H +++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H @@ -234,6 +234,7 @@ inline bool Foam::face::connected(const FixedList& other) const inline bool Foam::face::contains(const Foam::edge& e) const { + // or (find(e) >= 0) return (edgeDirection(e) != 0); } diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index 300a5aa0d1..cee935febc 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -328,6 +328,13 @@ public: //- True if face contains(edge) inline bool contains(const Foam::edge& e) const; + //- Regular find pointi within face + using FixedList::find; + + //- Find the edge within the face. + // \return the edge index or -1 if not found + inline label find(const Foam::edge& e) const; + //- Test the edge direction on the face // \return // - +1: forward (counter-clockwise) on the face diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H index f2fb6446a3..2832c6ff2c 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H @@ -446,26 +446,49 @@ inline int Foam::triFace::edgeDirection(const Foam::edge& e) const { if (e.first() == a()) { - if (e.second() == b()) return 1; // edge 0 - forward - if (e.second() == c()) return -1; // edge 2 - reverse + if (e.second() == b()) return 1; // Forward edge 0 (encoded 1) + if (e.second() == c()) return -1; // Reverse edge 2 (encoded -3) } if (e.first() == b()) { - if (e.second() == c()) return 1; // edge 1 - forward - if (e.second() == a()) return -1; // edge 0 - reverse + if (e.second() == c()) return 1; // Forward edge 1 (encoded 2) + if (e.second() == a()) return -1; // Reverse edge 0 (encoded -1) } if (e.first() == c()) { - if (e.second() == a()) return 1; // edge 2 - forward - if (e.second() == b()) return -1; // edge 1 - reverse + if (e.second() == a()) return 1; // Forward edge 2 (encoded 3) + if (e.second() == b()) return -1; // Reverse edge 1 (encoded -2) } return 0; // Not found } +inline Foam::label Foam::triFace::find(const Foam::edge& e) const +{ + if (e.first() == a()) + { + if (e.second() == b()) return 0; // Forward edge 0 + if (e.second() == c()) return 2; // Reverse edge 2 + } + if (e.first() == b()) + { + if (e.second() == c()) return 1; // Forward edge 1 + if (e.second() == a()) return 0; // Reverse edge 0 + } + if (e.first() == c()) + { + if (e.second() == a()) return 2; // Forward edge 2 + if (e.second() == b()) return 1; // Reverse edge 1 + } + + return -1; // Not found +} + + inline bool Foam::triFace::contains(const Foam::edge& e) const { + // or (find(e) >= 0) return (edgeDirection(e) != 0); }