From 822acaf6efe0e3bc52dc7971dec727e92c884ca8 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 7 Aug 2018 22:23:16 +0200 Subject: [PATCH] ENH: make indices for boundBox::add() a templated parameter - allows use with any container with begin(), end() and where the "*iterator" dereference returns a label, which is used for indexing into the list of points. This container could be labelUList, bitSet, labelHashSet, etc --- applications/test/boundBox/Test-boundBox.C | 28 +++- src/OpenFOAM/meshes/boundBox/boundBox.C | 55 +------ src/OpenFOAM/meshes/boundBox/boundBox.H | 67 +++++---- src/OpenFOAM/meshes/boundBox/boundBoxI.H | 17 --- .../meshes/boundBox/boundBoxTemplates.C | 142 +++++++++++++++--- 5 files changed, 191 insertions(+), 118 deletions(-) diff --git a/applications/test/boundBox/Test-boundBox.C b/applications/test/boundBox/Test-boundBox.C index 037adb4c7f..e2cb5b56d4 100644 --- a/applications/test/boundBox/Test-boundBox.C +++ b/applications/test/boundBox/Test-boundBox.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -32,6 +32,9 @@ Description #include "boundBox.H" #include "treeBoundBox.H" #include "cellModel.H" +#include "bitSet.H" +#include "HashSet.H" +#include "ListOps.H" using namespace Foam; @@ -117,6 +120,29 @@ int main(int argc, char *argv[]) box1.add(box4); Info<<"union with " << box4 << " => " << box1 << endl; + + labelRange range(10, 25); + auto variousPoints = ListOps::create + ( + range.begin(), + range.end(), + [](const label val) { return vector(val, val, val); } + ); + + Info<< nl << nl; + + labelHashSet select1{4, 5, 55}; + bitSet select2(15, {1, 5, 20, 24, 34}); + + Info<< "From points: size=" << variousPoints.size() << " from " + << variousPoints.first() << " ... " << variousPoints.last() << nl; + Info<< "add points @ " << flatOutput(select1.sortedToc()) << nl; + + box1.add(variousPoints, select1); + Info<< "box is now => " << box1 << endl; + + box1.add(variousPoints, select2); + Info<< "box is now => " << box1 << endl; } return 0; diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.C b/src/OpenFOAM/meshes/boundBox/boundBox.C index ad44f505e6..0ce92a29cb 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.C +++ b/src/OpenFOAM/meshes/boundBox/boundBox.C @@ -68,8 +68,7 @@ const Foam::FixedList Foam::boundBox::faceNormals Foam::boundBox::boundBox(const UList& points, bool doReduce) : - min_(invertedBox.min()), - max_(invertedBox.max()) + boundBox() { add(points); @@ -82,8 +81,7 @@ Foam::boundBox::boundBox(const UList& points, bool doReduce) Foam::boundBox::boundBox(const tmp& tpoints, bool doReduce) : - min_(invertedBox.min()), - max_(invertedBox.max()) + boundBox() { add(tpoints); @@ -101,8 +99,7 @@ Foam::boundBox::boundBox bool doReduce ) : - min_(invertedBox.min()), - max_(invertedBox.max()) + boundBox() { add(points, indices); @@ -246,29 +243,6 @@ bool Foam::boundBox::contains(const UList& points) const } -bool Foam::boundBox::contains -( - const UList& points, - const labelUList& indices -) const -{ - if (points.empty() || indices.empty()) - { - return true; - } - - for (const label pointi : indices) - { - if (!contains(points[pointi])) - { - return false; - } - } - - return true; -} - - bool Foam::boundBox::containsAny(const UList& points) const { if (points.empty()) @@ -288,29 +262,6 @@ bool Foam::boundBox::containsAny(const UList& points) const } -bool Foam::boundBox::containsAny -( - const UList& points, - const labelUList& indices -) const -{ - if (points.empty() || indices.empty()) - { - return true; - } - - for (const label pointi : indices) - { - if (contains(points[pointi])) - { - return true; - } - } - - return false; -} - - Foam::point Foam::boundBox::nearest(const point& pt) const { // Clip the point to the range of the bounding box diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.H b/src/OpenFOAM/meshes/boundBox/boundBox.H index 5d0692dbdb..1c8182ad52 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.H +++ b/src/OpenFOAM/meshes/boundBox/boundBox.H @@ -126,7 +126,7 @@ public: inline boundBox(Istream& is); - // Member functions + // Member Functions // Access @@ -201,19 +201,11 @@ public: //- Extend to include the points from the temporary point field. inline void add(const tmp& tpoints); - //- Extend to include the subset of the point field. - // The indices could be from cell/face etc. - inline void add - ( - const UList& points, - const labelUList& indices - ); - //- Extend to include the points. template void add(const FixedList& points); - //- Extend to include the subset of the point field. + //- Extend to include a (subsetted) point field. // The indices could be from edge/triFace etc. template void add @@ -222,6 +214,17 @@ public: const FixedList& indices ); + //- Extend to include a (subsetted) point field. + // + // \tparam IntContainer A container with an iterator that + // dereferences to an label + template + void add + ( + const UList& points, + const IntContainer& indices + ); + //- Inflate box by factor*mag(span) in all dimensions void inflate(const scalar s); @@ -259,17 +262,10 @@ public: //- Contains point? (inside only) inline bool containsInside(const point& pt) const; - //- Contains all of the points? (inside or on edge) + //- Contains all points? (inside or on edge) bool contains(const UList& points) const; - //- Contains all of the subset of points? (inside or on edge) - bool contains - ( - const UList& points, - const labelUList& indices - ) const; - - //- Contains all of the subset of points? (inside or on edge) + //- Contains all of the (subsetted) points? (inside or on edge) template bool contains ( @@ -278,17 +274,22 @@ public: ) const; + //- Contains all of the (subsetted) points? (inside or on edge) + // + // \tparam IntContainer A container with an iterator that + // dereferences to an label + template + bool contains + ( + const UList& points, + const IntContainer& indices + ) const; + + //- Contains any of the points? (inside or on edge) bool containsAny(const UList& points) const; - //- Contains any of the subset of points? (inside or on edge) - bool containsAny - ( - const UList& points, - const labelUList& indices - ) const; - - //- Contains any of the subset of points? (inside or on edge) + //- Contains any of the (subsetted) points? (inside or on edge) template bool containsAny ( @@ -296,6 +297,18 @@ public: const FixedList& indices ) const; + //- Contains any of the (subsetted) points? (inside or on edge) + // + // \tparam IntContainer A container with an iterator that + // dereferences to an label + template + bool containsAny + ( + const UList& points, + const IntContainer& indices + ) const; + + //- Return the nearest point on the boundBox to the supplied point. // If point is inside the boundBox then the point is returned // unchanged. diff --git a/src/OpenFOAM/meshes/boundBox/boundBoxI.H b/src/OpenFOAM/meshes/boundBox/boundBoxI.H index 0a117c16ca..aa95e05411 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBoxI.H +++ b/src/OpenFOAM/meshes/boundBox/boundBoxI.H @@ -25,7 +25,6 @@ License #include "boundBox.H" - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // inline Foam::boundBox::boundBox() @@ -195,22 +194,6 @@ inline void Foam::boundBox::add(const tmp& tpoints) } -inline void Foam::boundBox::add -( - const UList& points, - const labelUList& indices -) -{ - if (!points.empty()) - { - for (const label pointi : indices) - { - add(points[pointi]); - } - } -} - - inline bool Foam::boundBox::overlaps(const boundBox& bb) const { return diff --git a/src/OpenFOAM/meshes/boundBox/boundBoxTemplates.C b/src/OpenFOAM/meshes/boundBox/boundBoxTemplates.C index 561a22fd25..9451c31975 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBoxTemplates.C +++ b/src/OpenFOAM/meshes/boundBox/boundBoxTemplates.C @@ -36,8 +36,7 @@ Foam::boundBox::boundBox bool doReduce ) : - min_(invertedBox.min()), - max_(invertedBox.max()) + boundBox() { add(points, indices); @@ -56,10 +55,9 @@ void Foam::boundBox::add const FixedList& points ) { - // a FixedList is never empty - for (unsigned i=0; i < Size; ++i) + for (const point& p : points) { - add(points[i]); + add(p); } } @@ -71,35 +69,96 @@ void Foam::boundBox::add const FixedList& indices ) { - // points may be empty, but a FixedList is never empty - if (!points.empty()) + const label len = points.size(); + + // Skip if points is empty + if (len) { for (const label pointi : indices) { - add(points[pointi]); + if (pointi >= 0 && pointi < len) + { + add(points[pointi]); + } + } + } +} + + +template +void Foam::boundBox::add +( + const UList& points, + const IntContainer& indices +) +{ + const label len = points.size(); + + // Skip if points is empty + if (len) + { + for (const label pointi : indices) + { + if (pointi >= 0 && pointi < len) + { + add(points[pointi]); + } } } } template -bool Foam::boundBox::contains +inline bool Foam::boundBox::contains ( const UList& points, const FixedList& indices ) const { - // points may be empty, but a FixedList is never empty - if (points.empty()) + const label len = points.size(); + + if (!len) { - return false; + return true; } for (const label pointi : indices) { - if (!contains(points[pointi])) + if (pointi >= 0 && pointi < len) { - return false; + if (!contains(points[pointi])) + { + return false; + } + } + } + + return true; +} + + +template +inline bool Foam::boundBox::contains +( + const UList& points, + const IntContainer& indices +) const +{ + const label len = points.size(); + + if (!len) + { + return true; + } + + for (const label pointi : indices) + { + if (pointi >= 0 && pointi < len) + { + if (!contains(points[pointi])) + { + return false; + } } } @@ -108,27 +167,68 @@ bool Foam::boundBox::contains template -bool Foam::boundBox::containsAny +inline bool Foam::boundBox::containsAny ( const UList& points, const FixedList& indices ) const { - // points may be empty, but a FixedList is never empty - if (points.empty()) + const label len = points.size(); + + if (!len) { - return false; + return true; } + label failed = 0; + for (const label pointi : indices) { - if (contains(points[pointi])) + if (pointi >= 0 && pointi < len) { - return true; + if (contains(points[pointi])) + { + return true; + } + + ++failed; } } - return false; + return !failed; +} + + +template +inline bool Foam::boundBox::containsAny +( + const UList& points, + const IntContainer& indices +) const +{ + const label len = points.size(); + + if (!len) + { + return true; + } + + label failed = 0; + + for (const label pointi : indices) + { + if (pointi >= 0 && pointi < len) + { + if (contains(points[pointi])) + { + return true; + } + + ++failed; + } + } + + return !failed; }