From d3e285b48b54c8b3b72768e93985145c4e14c05a Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 1 Nov 2022 13:57:10 +0100 Subject: [PATCH] ENH: add FixedList templated get() methods - provides fast compile-time indexing for FixedList (invalid indices trigger a compiler error). This enables noexcept access, which can propagate into various other uses (eg, triFace, triPoints, ...) ENH: add triangle edge vectors --- applications/test/FixedList/Test-FixedList.C | 6 + .../test/boolVector/Test-boolVector.C | 6 +- applications/test/faces/Test-faces.C | 20 +++- applications/test/triTet/Test-triTet.C | 12 +- .../containers/Lists/FixedList/FixedList.H | 24 ++-- .../containers/Lists/FixedList/FixedListI.H | 18 +++ src/OpenFOAM/meshes/meshShapes/edge/edge.H | 35 +++--- src/OpenFOAM/meshes/meshShapes/edge/edgeI.H | 6 +- .../meshes/meshShapes/triFace/triFace.H | 34 ++---- .../meshes/meshShapes/triFace/triFaceI.H | 108 +++++++----------- .../meshShapes/triFace/triFaceTemplates.C | 8 +- .../meshes/primitiveShapes/line/line.H | 19 +-- .../meshes/primitiveShapes/line/lineI.H | 6 +- .../primitiveShapes/tetrahedron/tetrahedron.H | 16 +-- .../tetrahedron/tetrahedronI.H | 32 +++--- .../primitiveShapes/triangle/triangle.H | 78 +++++++++---- .../primitiveShapes/triangle/triangleI.H | 61 ++++++++-- .../primitives/Vector/bools/boolVector.H | 20 ++-- .../primitives/Vector/bools/boolVectorI.H | 29 ++--- src/OpenFOAM/primitives/tuples/Pair.H | 18 +-- src/OpenFOAM/primitives/tuples/PairI.H | 16 +-- src/OpenFOAM/primitives/tuples/Tuple2.H | 8 +- 22 files changed, 318 insertions(+), 262 deletions(-) diff --git a/applications/test/FixedList/Test-FixedList.C b/applications/test/FixedList/Test-FixedList.C index c8648ae05f..7e510d8c78 100644 --- a/applications/test/FixedList/Test-FixedList.C +++ b/applications/test/FixedList/Test-FixedList.C @@ -189,6 +189,12 @@ int main(int argc, char *argv[]) << " hash:" << FixedList::hasher()(list1) << nl << " hash:" << Hash>()(list1) << nl; + Info<< "get<0>: " << list1.get<0>() << nl; + Info<< "get<1>: " << list1.get<1>() << nl; + Info<< "get<2>: " << list1.get<2>() << nl; + Info<< "get<3>: " << list1.get<3>() << nl; +// Will not compile: Info<< "get<4>: " << list1.get<4>() << nl; + label a[4] = {0, 1, 2, 3}; FixedList list2(a); diff --git a/applications/test/boolVector/Test-boolVector.C b/applications/test/boolVector/Test-boolVector.C index 0c66d261f6..d345b55ddf 100644 --- a/applications/test/boolVector/Test-boolVector.C +++ b/applications/test/boolVector/Test-boolVector.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,7 +43,7 @@ void print(const boolVector& v) << " any:" << Switch::name(v.any()) << " all:" << Switch::name(v.all()) << " none:" << Switch::name(v.none()) - << " count:" << v.count() << nl; + << " on:" << v.count() << " off:" << v.count(false) << nl; } @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) Info<< nl; { - boolVector vec{1, 0, 1}; + boolVector vec(1, 0, 1); print(vec); vec.flip(); diff --git a/applications/test/faces/Test-faces.C b/applications/test/faces/Test-faces.C index fab3d04ca1..9c8cfcea03 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-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -33,6 +33,7 @@ Description #include "argList.H" #include "labelledTri.H" +#include "edge.H" #include "faceList.H" #include "triFaceList.H" #include "pointList.H" @@ -131,10 +132,25 @@ int main(int argc, char *argv[]) testSign(f1, points2, testPoints); Info<< nl; - triFace t1({1, 2, 3}); + // Initializer list + // triFace t1({1, 2, 3}); + + // Component-wise + { + edge e1(3, 2, 1); // Inadvertent sort!!! + Info<< "edge:" << e1 << nl; + } + + // Component-wise + triFace t1(1, 2, 3); Info<< "triFace:"; faceInfo(t1, points1); Info << nl; testSign(t1, points1, testPoints); + { + scalarField fld({0, 20, 20, 30}); + Info<< "avg:" << t1.average(pointField::null(), fld) << nl; + } + Info<< "triFace:"; faceInfo(t1, points2); Info << nl; testSign(t1, points2, testPoints); Info<< nl; diff --git a/applications/test/triTet/Test-triTet.C b/applications/test/triTet/Test-triTet.C index 5c850a4377..2f579d7a72 100644 --- a/applications/test/triTet/Test-triTet.C +++ b/applications/test/triTet/Test-triTet.C @@ -1,3 +1,4 @@ +#include "argList.H" #include "point.H" #include "triangle.H" #include "tetrahedron.H" @@ -5,7 +6,7 @@ using namespace Foam; -int main() +int main(int argc, char *argv[]) { triangle tri ( @@ -14,6 +15,12 @@ int main() vector(1, 1, 0) ); + Info<< "triangle: " << tri << nl + << " vecA: " << tri.vecA() << nl + << " vecB: " << tri.vecB() << nl + << " vecC: " << tri.vecC() << nl + << endl; + Info<< "tri circumCentre = " << tri.circumCentre() << endl; Info<< "tri circumRadius = " << tri.circumRadius() << endl; @@ -28,6 +35,8 @@ int main() Info<< "tet circumCentre = " << tet.circumCentre() << endl; Info<< "tet circumRadius = " << tet.circumRadius() << endl; + InfoErr<< "Enter four points: " << endl; + vector a(Sin); vector b(Sin); vector c(Sin); @@ -36,5 +45,6 @@ int main() Info<< "tet circumRadius = " << tetrahedron(a, b, c, d).circumRadius() << endl; + Info<< "\nEnd\n"; return 0; } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 0ff6b37164..2f2d7b2148 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -216,6 +216,22 @@ public: // \note Only meaningful for contiguous data inline char* data_bytes() noexcept; + //- Number of contiguous bytes for the list data, + // \note Only meaningful for contiguous data + inline static std::streamsize size_bytes() noexcept; + + //- Number of contiguous bytes for the list data, + //- runtime FatalError if type is not contiguous + static std::streamsize byteSize(); + + //- Element access using compile-time indexing + template + inline T& get() noexcept; + + //- Element access using compile-time indexing + template + inline const T& get() const noexcept; + //- Access first element of the list, position [0] inline T& front() noexcept; @@ -228,14 +244,6 @@ public: //- Access last element of the list, position [N-1] inline const T& back() const noexcept; - //- Number of contiguous bytes for the list data, - // \note Only meaningful for contiguous data - inline static std::streamsize size_bytes() noexcept; - - //- Number of contiguous bytes for the list data, - //- runtime FatalError if type is not contiguous - static std::streamsize byteSize(); - //- Return the forward circular index, i.e. next index //- which returns to the first at the end of the list inline label fcIndex(const label i) const; diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index d6e290d18e..f2617b3d4e 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -203,6 +203,24 @@ inline std::streamsize Foam::FixedList::size_bytes() noexcept } +template +template +inline T& Foam::FixedList::get() noexcept +{ + static_assert(Index < N, "Address outside FixedList range"); + return v_[Index]; +} + + +template +template +inline const T& Foam::FixedList::get() const noexcept +{ + static_assert(Index < N, "Address outside FixedList range"); + return v_[Index]; +} + + template inline T& Foam::FixedList::front() noexcept { diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edge.H b/src/OpenFOAM/meshes/meshShapes/edge/edge.H index 60feefd475..b014650ce4 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edge.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edge.H @@ -108,38 +108,28 @@ public: // Access //- The first vertex - label a() const { return labelPair::first(); } + label a() const noexcept { return labelPair::first(); } //- The second vertex - label b() const { return labelPair::second(); } + label b() const noexcept { return labelPair::second(); } //- The first vertex - label& a() { return labelPair::first(); } + label& a() noexcept { return labelPair::first(); } //- The second vertex - label& b() { return labelPair::second(); } - - - //- The first vertex label - using labelPair::first; - - //- The second (last) vertex label - using labelPair::second; - - //- The last (second) vertex label - using labelPair::last; + label& b() noexcept { return labelPair::second(); } //- The start (first) vertex label - label start() const { return labelPair::first(); } + label start() const noexcept { return labelPair::first(); } //- The end (last/second) vertex label - label end() const { return labelPair::second(); } + label end() const noexcept { return labelPair::second(); } //- The start (first) vertex label - label& start() { return labelPair::first(); } + label& start() noexcept { return labelPair::first(); } - //- The end (last/second) vertex label - label& end() { return labelPair::second(); } + //- The end (second/last) vertex label + label& end() noexcept { return labelPair::second(); } //- Return reverse edge as copy. @@ -157,8 +147,11 @@ public: // No special handling of negative point labels. inline label maxVertex() const; - //- Return true if the vertices are unique and non-negative. - inline bool valid() const; + //- True if the vertices are unique and non-negative. + inline bool good() const noexcept; + + //- True if the vertices are unique and non-negative. + bool valid() const noexcept { return good(); } //- Return true if point label is found in edge. // Always false for a negative label. diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H index 448bd43346..c87dd54285 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H @@ -58,7 +58,7 @@ inline Foam::edge::edge(const labelPair& pair) inline Foam::edge::edge(const FixedList& list) : - labelPair(list.first(), list.last()) + labelPair(list.get<0>(), list.get<1>()) {} @@ -80,7 +80,7 @@ inline Foam::edge::edge const FixedList& indices ) : - labelPair(list[indices.first()], list[indices.last()]) + labelPair(list[indices.get<0>()], list[indices.get<1>()]) {} @@ -104,7 +104,7 @@ inline Foam::label Foam::edge::maxVertex() const } -inline bool Foam::edge::valid() const +inline bool Foam::edge::good() const noexcept { return (first() != second() && first() >= 0 && second() >= 0); } diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index 94d3f1b128..9fbe3e4905 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -88,7 +88,7 @@ public: inline triFace(); //- Construct from three point labels - inline triFace(const label p0, const label p1, const label p2); + inline triFace(const label p0, const label p1, const label p2) noexcept; //- Construct from an initializer list of three point labels inline explicit triFace(std::initializer_list