From eac3922e280e782c0a1c78597bdd0147627ab3f4 Mon Sep 17 00:00:00 2001 From: mark <mark@opencfd> Date: Sat, 12 Nov 2016 20:57:48 +0100 Subject: [PATCH] ENH: ensure face, triFace and labelledTri all work consistently (issue #294) - triFace() now initialized with '-1', which makes it behave equivalently to face(label). - supply default region=0 for some labelledTri constructors. This allows labelledTri to work more like a triFace and makes it easier to use in templated methods and eases conversion from triFace to a labelledTri. - labelledTri(const labelUList&) can now be used when converting from a face. It can have 3 values (use default region) or 4 values (with region). - face, triFace, labelledTri now all support construction with initializer lists. This can be useful for certain types of code. Eg, triFace f1{a, b, c}; face f2{a, b, c}; labelledTri f3{a, b, c}; Work without ambiguity. Also useful for templated methods: FaceType f{remap[a], remap[b], remap[c]}; --- applications/test/faces/Make/files | 3 + applications/test/faces/Make/options | 0 applications/test/faces/Test-faces.C | 84 +++++++++++++++++++ src/OpenFOAM/meshes/meshShapes/face/face.H | 4 +- src/OpenFOAM/meshes/meshShapes/face/faceI.H | 4 +- .../meshes/meshShapes/face/faceListFwd.H | 2 - .../meshShapes/labelledTri/labelledTri.H | 38 +++++---- .../meshShapes/labelledTri/labelledTriI.H | 44 +++++++++- .../meshes/meshShapes/triFace/triFace.H | 7 +- .../meshes/meshShapes/triFace/triFaceI.H | 8 ++ 10 files changed, 168 insertions(+), 26 deletions(-) create mode 100644 applications/test/faces/Make/files create mode 100644 applications/test/faces/Make/options create mode 100644 applications/test/faces/Test-faces.C diff --git a/applications/test/faces/Make/files b/applications/test/faces/Make/files new file mode 100644 index 0000000000..8418bb2b93 --- /dev/null +++ b/applications/test/faces/Make/files @@ -0,0 +1,3 @@ +Test-faces.C + +EXE = $(FOAM_USER_APPBIN)/Test-faces diff --git a/applications/test/faces/Make/options b/applications/test/faces/Make/options new file mode 100644 index 0000000000..e69de29bb2 diff --git a/applications/test/faces/Test-faces.C b/applications/test/faces/Test-faces.C new file mode 100644 index 0000000000..823f33e3e1 --- /dev/null +++ b/applications/test/faces/Test-faces.C @@ -0,0 +1,84 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 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/>. + +Application + Test-faces + +Description + Simple tests for various faces + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "labelledTri.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + face f1{ 1, 2, 3, 4 }; + Info<< "face:" << f1 << nl; + + triFace t1{ 1, 2, 3 }; + Info<< "triFace:" << t1 << nl; + + f1 = t1; + Info<< "face:" << f1 << nl; + + f1 = t1.triFaceFace(); + Info<< "face:" << f1 << nl; + + // expect these to fail + FatalError.throwExceptions(); + try + { + labelledTri l1{ 1, 2, 3, 10, 24 }; + Info<< "labelled:" << l1 << nl; + } + catch (Foam::error& err) + { + WarningInFunction + << "Caught FatalError " << err << nl << endl; + } + FatalError.dontThrowExceptions(); + + labelledTri l2{ 1, 2, 3 }; + Info<< "labelled:" << l2 << nl; + + labelledTri l3{ 1, 2, 3, 10 }; + Info<< "labelled:" << l3 << nl; + + t1.flip(); + l3.flip(); + + Info<< "flip:" << t1 << nl; + Info<< "flip:" << l3 << nl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index 0504145ade..62ae47b3d0 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -155,8 +155,8 @@ public: //- Construct from list of labels explicit inline face(const labelUList&); - //- Construct from list of labels - explicit inline face(const labelList&); + //- Construct from an initializer list of labels + explicit inline face(std::initializer_list<label>); //- Construct by transferring the parameter contents explicit inline face(const Xfer<labelList>&); diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H index 7dfc9ef7ea..0dc5e035a6 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H +++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H @@ -57,7 +57,7 @@ inline Foam::face::face(const labelUList& lst) {} -inline Foam::face::face(const labelList& lst) +inline Foam::face::face(std::initializer_list<label> lst) : labelList(lst) {} @@ -79,7 +79,7 @@ inline Foam::face::face(Istream& is) inline Foam::pointField Foam::face::points(const pointField& meshPoints) const { - // There are as many points as there labels for them + // There are as many points as there are labels for them pointField p(size()); // For each point in list, set it to the point in 'pnts' addressed diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceListFwd.H b/src/OpenFOAM/meshes/meshShapes/face/faceListFwd.H index d268b0ccbc..0caf8a454a 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/faceListFwd.H +++ b/src/OpenFOAM/meshes/meshShapes/face/faceListFwd.H @@ -43,8 +43,6 @@ namespace Foam typedef List<face> faceList; typedef SubList<face> faceSubList; typedef List<faceList> faceListList; - // same as faceUList: - typedef UList<face> unallocFaceList; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTri.H b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTri.H index a0fd5a90dd..f9feafa610 100644 --- a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTri.H +++ b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTri.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -64,29 +64,44 @@ class labelledTri label region_; + // Private Member Functions + + //- Assign from a list of 3 or 4 labels. + // Default region is 0. + inline void assign(const labelUList&); + public: // Constructors - //- Construct null + //- Construct null with invalid point labels and region (-1). inline labelledTri(); - //- Construct from triFace and a region label + //- Construct from triFace and region label. + // Default region is 0 if not specified. inline labelledTri ( const triFace&, - const label region + const label region = 0 ); - //- Construct from three point labels and a region label + //- Construct from three point labels and a region label. + // Default region is 0 if not specified. inline labelledTri ( const label a, const label b, const label c, - const label region + const label region = 0 ); + //- Construct from a list of 3 or 4 labels. + // Default region is 0. + explicit inline labelledTri(const labelUList&); + + //- Construct from an initializer list of 3 or 4 labels. + explicit inline labelledTri(std::initializer_list<label>); + //- Construct from Istream inline labelledTri(Istream&); @@ -102,17 +117,6 @@ public: inline label& region(); - // Check - - // Edit - - // Write - - - // Friend Functions - - // Friend Operators - // IOstream Operators inline friend Istream& operator>>(Istream&, labelledTri&); diff --git a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H index 0d86d6cee9..0e0c72af77 100644 --- a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H +++ b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,10 +25,34 @@ License #include "IOstreams.H" +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +inline void Foam::labelledTri::assign(const labelUList& lst) +{ + const label sz = lst.size(); + + // checkSize + if (sz < 3 || sz > 4) + { + FatalErrorInFunction + << "size " << sz << " != (3 or 4)" + << abort(FatalError); + } + + for (label i=0; i<3; ++i) + { + operator[](i) = lst[i]; + } + + region_ = (sz > 3 ? lst[3] : 0); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // inline Foam::labelledTri::labelledTri() : + triFace(), region_(-1) {} @@ -57,6 +81,24 @@ inline Foam::labelledTri::labelledTri {} +inline Foam::labelledTri::labelledTri(const labelUList& lst) +: + triFace(), + region_(0) +{ + assign(lst); +} + + +inline Foam::labelledTri::labelledTri(std::initializer_list<label> initLst) +: + triFace(), + region_(0) +{ + assign(labelList(initLst)); +} + + inline Foam::labelledTri::labelledTri(Istream& is) { operator>>(is, *this); diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index 2e7a730228..0b2d5170ad 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -75,7 +75,7 @@ public: // Constructors - //- Construct null + //- Construct null with invalid point labels (-1) inline triFace(); //- Construct from three point labels @@ -86,9 +86,12 @@ public: const label c ); - //- Construct from a list of labels + //- Construct from a list of 3 labels. explicit inline triFace(const labelUList&); + //- Construct from an initializer list of 3 labels + explicit inline triFace(std::initializer_list<label>); + //- Construct from Istream inline triFace(Istream&); diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H index b956780df4..8d8bcaba99 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H @@ -62,6 +62,8 @@ inline int Foam::triFace::compare(const triFace& a, const triFace& b) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // inline Foam::triFace::triFace() +: + FixedList<label, 3>(-1) {} @@ -84,6 +86,12 @@ inline Foam::triFace::triFace(const labelUList& lst) {} +inline Foam::triFace::triFace(std::initializer_list<label> lst) +: + FixedList<label, 3>(lst) +{} + + inline Foam::triFace::triFace(Istream& is) : FixedList<label, 3>(is) -- GitLab