mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
- boundBox::nDim() to assess dimensionality - boundBox::intersect(const plane&) to test if plane intersects the bounding box
319 lines
6.6 KiB
C
319 lines
6.6 KiB
C
/*---------------------------------------------------------------------------*\
|
|
========= |
|
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
|
\\ / O peration |
|
|
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
|
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
|
|
-------------------------------------------------------------------------------
|
|
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/>.
|
|
|
|
\*---------------------------------------------------------------------------*/
|
|
|
|
#include "boundBox.H"
|
|
#include "PstreamReduceOps.H"
|
|
#include "tmp.H"
|
|
#include "plane.H"
|
|
|
|
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
|
|
|
const Foam::boundBox Foam::boundBox::greatBox
|
|
(
|
|
point::uniform(-ROOTVGREAT),
|
|
point::uniform(ROOTVGREAT)
|
|
);
|
|
|
|
const Foam::boundBox Foam::boundBox::invertedBox
|
|
(
|
|
point::uniform(ROOTVGREAT),
|
|
point::uniform(-ROOTVGREAT)
|
|
);
|
|
|
|
const Foam::faceList Foam::boundBox::faces
|
|
({
|
|
// Point and face order as per hex cellmodel
|
|
face{0, 4, 7, 3}, // x-min
|
|
face{1, 2, 6, 5}, // x-max
|
|
face{0, 1, 5, 4}, // y-min
|
|
face{3, 7, 6, 2}, // y-max
|
|
face{0, 3, 2, 1}, // z-min
|
|
face{4, 5, 6, 7} // z-max
|
|
});
|
|
|
|
|
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
|
|
|
Foam::boundBox::boundBox(const UList<point>& points, bool doReduce)
|
|
:
|
|
min_(invertedBox.min()),
|
|
max_(invertedBox.max())
|
|
{
|
|
add(points);
|
|
|
|
if (doReduce)
|
|
{
|
|
reduce();
|
|
}
|
|
}
|
|
|
|
|
|
Foam::boundBox::boundBox(const tmp<pointField>& tpoints, bool doReduce)
|
|
:
|
|
min_(invertedBox.min()),
|
|
max_(invertedBox.max())
|
|
{
|
|
add(tpoints);
|
|
|
|
if (doReduce)
|
|
{
|
|
reduce();
|
|
}
|
|
}
|
|
|
|
|
|
Foam::boundBox::boundBox
|
|
(
|
|
const UList<point>& points,
|
|
const labelUList& indices,
|
|
bool doReduce
|
|
)
|
|
:
|
|
min_(invertedBox.min()),
|
|
max_(invertedBox.max())
|
|
{
|
|
add(points, indices);
|
|
|
|
if (doReduce)
|
|
{
|
|
reduce();
|
|
}
|
|
}
|
|
|
|
|
|
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
|
|
|
|
Foam::tmp<Foam::pointField> Foam::boundBox::points() const
|
|
{
|
|
tmp<pointField> tpoints(new pointField(8));
|
|
pointField& pt = tpoints.ref();
|
|
|
|
pt[0] = min_; // min-x, min-y, min-z
|
|
pt[1] = point(max_.x(), min_.y(), min_.z()); // max-x, min-y, min-z
|
|
pt[2] = point(max_.x(), max_.y(), min_.z()); // max-x, max-y, min-z
|
|
pt[3] = point(min_.x(), max_.y(), min_.z()); // min-x, max-y, min-z
|
|
pt[4] = point(min_.x(), min_.y(), max_.z()); // min-x, min-y, max-z
|
|
pt[5] = point(max_.x(), min_.y(), max_.z()); // max-x, min-y, max-z
|
|
pt[6] = max_; // max-x, max-y, max-z
|
|
pt[7] = point(min_.x(), max_.y(), max_.z()); // min-x, max-y, max-z
|
|
|
|
return tpoints;
|
|
}
|
|
|
|
|
|
void Foam::boundBox::inflate(const scalar s)
|
|
{
|
|
const vector ext = vector::one*s*mag();
|
|
|
|
min_ -= ext;
|
|
max_ += ext;
|
|
}
|
|
|
|
|
|
void Foam::boundBox::reduce()
|
|
{
|
|
Foam::reduce(min_, minOp<point>());
|
|
Foam::reduce(max_, maxOp<point>());
|
|
}
|
|
|
|
|
|
bool Foam::boundBox::intersect(const boundBox& bb)
|
|
{
|
|
min_ = ::Foam::max(min_, bb.min_);
|
|
max_ = ::Foam::min(max_, bb.max_);
|
|
|
|
return !empty();
|
|
}
|
|
|
|
|
|
bool Foam::boundBox::intersects(const plane& pln) const
|
|
{
|
|
// Require a full 3D box
|
|
if (nDim() != 3)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool above = false;
|
|
bool below = false;
|
|
|
|
tmp<pointField> tpts(points());
|
|
const pointField& pts = tpts();
|
|
|
|
for (const point& p : pts)
|
|
{
|
|
if (pln.sideOfPlane(p) == plane::NORMAL)
|
|
{
|
|
above = true;
|
|
}
|
|
else
|
|
{
|
|
below = true;
|
|
}
|
|
}
|
|
|
|
return (above && below);
|
|
}
|
|
|
|
|
|
bool Foam::boundBox::contains(const UList<point>& points) const
|
|
{
|
|
if (points.empty())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
for (const point& p : points)
|
|
{
|
|
if (!contains(p))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Foam::boundBox::contains
|
|
(
|
|
const UList<point>& points,
|
|
const labelUList& indices
|
|
) const
|
|
{
|
|
if (points.empty() || indices.empty())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
forAll(indices, i)
|
|
{
|
|
if (!contains(points[indices[i]]))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Foam::boundBox::containsAny(const UList<point>& points) const
|
|
{
|
|
if (points.empty())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
for (const point& p : points)
|
|
{
|
|
if (contains(p))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool Foam::boundBox::containsAny
|
|
(
|
|
const UList<point>& points,
|
|
const labelUList& indices
|
|
) const
|
|
{
|
|
if (points.empty() || indices.empty())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
forAll(indices, i)
|
|
{
|
|
if (contains(points[indices[i]]))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
Foam::point Foam::boundBox::nearest(const point& pt) const
|
|
{
|
|
// Clip the point to the range of the bounding box
|
|
const scalar surfPtx = Foam::max(Foam::min(pt.x(), max_.x()), min_.x());
|
|
const scalar surfPty = Foam::max(Foam::min(pt.y(), max_.y()), min_.y());
|
|
const scalar surfPtz = Foam::max(Foam::min(pt.z(), max_.z()), min_.z());
|
|
|
|
return point(surfPtx, surfPty, surfPtz);
|
|
}
|
|
|
|
|
|
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
|
|
|
|
Foam::Ostream& Foam::operator<<(Ostream& os, const boundBox& bb)
|
|
{
|
|
if (os.format() == IOstream::ASCII)
|
|
{
|
|
os << bb.min_ << token::SPACE << bb.max_;
|
|
}
|
|
else
|
|
{
|
|
os.write
|
|
(
|
|
reinterpret_cast<const char*>(&bb.min_),
|
|
sizeof(boundBox)
|
|
);
|
|
}
|
|
|
|
os.check(FUNCTION_NAME);
|
|
return os;
|
|
}
|
|
|
|
|
|
Foam::Istream& Foam::operator>>(Istream& is, boundBox& bb)
|
|
{
|
|
if (is.format() == IOstream::ASCII)
|
|
{
|
|
is >> bb.min_ >> bb.max_;
|
|
}
|
|
else
|
|
{
|
|
is.read
|
|
(
|
|
reinterpret_cast<char*>(&bb.min_),
|
|
sizeof(boundBox)
|
|
);
|
|
}
|
|
|
|
is.check(FUNCTION_NAME);
|
|
return is;
|
|
}
|
|
|
|
|
|
// ************************************************************************* //
|