Commit 822acaf6 authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent 7bb68b4d
......@@ -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<point>
(
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;
......
......@@ -68,8 +68,7 @@ const Foam::FixedList<Foam::vector, 6> Foam::boundBox::faceNormals
Foam::boundBox::boundBox(const UList<point>& points, bool doReduce)
:
min_(invertedBox.min()),
max_(invertedBox.max())
boundBox()
{
add(points);
......@@ -82,8 +81,7 @@ Foam::boundBox::boundBox(const UList<point>& points, bool doReduce)
Foam::boundBox::boundBox(const tmp<pointField>& 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<point>& points) const
}
bool Foam::boundBox::contains
(
const UList<point>& 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<point>& points) const
{
if (points.empty())
......@@ -288,29 +262,6 @@ bool Foam::boundBox::containsAny(const UList<point>& points) const
}
bool Foam::boundBox::containsAny
(
const UList<point>& 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
......
......@@ -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<pointField>& tpoints);
//- Extend to include the subset of the point field.
// The indices could be from cell/face etc.
inline void add
(
const UList<point>& points,
const labelUList& indices
);
//- Extend to include the points.
template<unsigned Size>
void add(const FixedList<point, Size>& 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<unsigned Size>
void add
......@@ -222,6 +214,17 @@ public:
const FixedList<label, Size>& indices
);
//- Extend to include a (subsetted) point field.
//
// \tparam IntContainer A container with an iterator that
// dereferences to an label
template<class IntContainer>
void add
(
const UList<point>& points,
const IntContainer& indices
);
//- Inflate box by factor*mag(span) in all dimensions
void inflate(const scalar s);
......@@ -259,43 +262,53 @@ 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<point>& points) const;
//- Contains all of the subset of points? (inside or on edge)
//- Contains all of the (subsetted) points? (inside or on edge)
template<unsigned Size>
bool contains
(
const UList<point>& points,
const labelUList& indices
const FixedList<label, Size>& indices
) const;
//- Contains all of the subset of points? (inside or on edge)
template<unsigned Size>
//- Contains all of the (subsetted) points? (inside or on edge)
//
// \tparam IntContainer A container with an iterator that
// dereferences to an label
template<class IntContainer>
bool contains
(
const UList<point>& points,
const FixedList<label, Size>& indices
const IntContainer& indices
) const;
//- Contains any of the points? (inside or on edge)
bool containsAny(const UList<point>& points) const;
//- Contains any of the subset of points? (inside or on edge)
//- Contains any of the (subsetted) points? (inside or on edge)
template<unsigned Size>
bool containsAny
(
const UList<point>& points,
const labelUList& indices
const FixedList<label, Size>& indices
) const;
//- Contains any of the subset of points? (inside or on edge)
template<unsigned Size>
//- Contains any of the (subsetted) points? (inside or on edge)
//
// \tparam IntContainer A container with an iterator that
// dereferences to an label
template<class IntContainer>
bool containsAny
(
const UList<point>& points,
const FixedList<label, Size>& indices
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.
......
......@@ -25,7 +25,6 @@ License
#include "boundBox.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::boundBox::boundBox()
......@@ -195,22 +194,6 @@ inline void Foam::boundBox::add(const tmp<pointField>& tpoints)
}
inline void Foam::boundBox::add
(
const UList<point>& 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
......
......@@ -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<point, Size>& 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<label, Size>& 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)
{
if (pointi >= 0 && pointi < len)
{
add(points[pointi]);
}
}
}
}
template<class IntContainer>
void Foam::boundBox::add
(
const UList<point>& points,
const IntContainer& indices
)
{
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<unsigned Size>
bool Foam::boundBox::contains
inline bool Foam::boundBox::contains
(
const UList<point>& points,
const FixedList<label, Size>& indices
) const
{
// points may be empty, but a FixedList is never empty
if (points.empty())
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;
}
}
}
return true;
}
template<class IntContainer>
inline bool Foam::boundBox::contains
(
const UList<point>& points,
const IntContainer& indices
) const
{
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;
}
}
}
......@@ -108,27 +167,68 @@ bool Foam::boundBox::contains
template<unsigned Size>
bool Foam::boundBox::containsAny
inline bool Foam::boundBox::containsAny
(
const UList<point>& points,
const FixedList<label, Size>& indices
) const
{
// points may be empty, but a FixedList is never empty
if (points.empty())
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;
}
template<class IntContainer>
inline bool Foam::boundBox::containsAny
(
const UList<point>& points,
const IntContainer& indices
) const
{
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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment