diff --git a/applications/test/minMax1/Test-minMax1.C b/applications/test/minMax1/Test-minMax1.C index 664a3a7f22..586b6f0384 100644 --- a/applications/test/minMax1/Test-minMax1.C +++ b/applications/test/minMax1/Test-minMax1.C @@ -44,7 +44,7 @@ using namespace Foam; template Ostream& printInfo(const MinMax& range) { - Info<< range << " valid=" << range.valid() << " span=" << range.span(); + Info<< range << " good=" << range.good() << " span=" << range.span(); return Info; } diff --git a/applications/test/minMax2/Test-minMax2.C b/applications/test/minMax2/Test-minMax2.C index 0a225ff893..95a3ced198 100644 --- a/applications/test/minMax2/Test-minMax2.C +++ b/applications/test/minMax2/Test-minMax2.C @@ -44,7 +44,7 @@ using namespace Foam; template Ostream& printInfo(const MinMax& range) { - Info<< range << " valid=" << range.valid(); + Info<< range << " good=" << range.good(); return Info; } diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.C b/src/OpenFOAM/meshes/boundBox/boundBox.C index 949dc4e40a..7c45b2ad23 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.C +++ b/src/OpenFOAM/meshes/boundBox/boundBox.C @@ -197,15 +197,6 @@ Foam::boundBox Foam::boundBox::returnReduce(const boundBox& bb) } -bool Foam::boundBox::intersect(const boundBox& bb) -{ - min_ = ::Foam::max(min_, bb.min_); - max_ = ::Foam::min(max_, bb.max_); - - return valid(); -} - - bool Foam::boundBox::intersects(const plane& pln) const { // Require a full 3D box @@ -290,6 +281,15 @@ Foam::point Foam::boundBox::nearest(const point& p) const } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +void Foam::boundBox::operator&=(const boundBox& bb) +{ + min_ = ::Foam::max(min_, bb.min_); + max_ = ::Foam::min(max_, bb.max_); +} + + // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // Foam::Ostream& Foam::operator<<(Ostream& os, const boundBox& bb) diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.H b/src/OpenFOAM/meshes/boundBox/boundBox.H index c2218325e3..189e6ab2f5 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.H +++ b/src/OpenFOAM/meshes/boundBox/boundBox.H @@ -35,6 +35,9 @@ Note bounding box. Points can be added later and the bounding box will grow to include them. +SeeAlso + Foam::treeBoundBox + \*---------------------------------------------------------------------------*/ #ifndef Foam_boundBox_H @@ -164,7 +167,10 @@ public: inline bool empty() const; //- Bounding box is non-inverted. - inline bool valid() const; + inline bool good() const; + + //- Bounding box is non-inverted - same as good(). + bool valid() const { return good(); } //- Minimum describing the bounding box inline const point& min() const noexcept; @@ -227,6 +233,12 @@ public: //- Reset to an inverted box inline void reset(); + //- Reset min/max to be identical to the specified point + inline void reset(const point& pt); + + //- Reset min/max to specified values + inline void reset(const point& min, const point& max); + //- Same as reset() - reset to an inverted box void clear() { reset(); } @@ -288,10 +300,6 @@ public: // Query - //- Intersection (union) with the second box. - // The return value is true if the intersection is non-empty. - bool intersect(const boundBox& bb); - //- Does plane intersect this bounding box. // There is an intersection if the plane segments the corner points // \note the results are unreliable when plane coincides almost @@ -372,7 +380,11 @@ public: // Member Operators + //- Restrict min/max to union with other box. + void operator&=(const boundBox& bb); + //- Extend box to include the second box, as per the add() method. + // Can be used in a reduction operation. void operator+=(const boundBox& bb) { add(bb); } @@ -384,6 +396,12 @@ public: // Housekeeping + //- Deprecated(2022-10) - use 'operator&=' to avoid confusion with + //- other intersects() methods. + // \deprecated(2022-10) - use 'operator&=' + FOAM_DEPRECATED_FOR(2022-10, "boundBox::operator&=(const boundBox&)") + bool intersect(const boundBox& bb) { *this &= bb; return good(); } + //- Identical to centre() point midpoint() const { return centre(); } }; diff --git a/src/OpenFOAM/meshes/boundBox/boundBoxI.H b/src/OpenFOAM/meshes/boundBox/boundBoxI.H index c028a61762..9acad5a6ba 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBoxI.H +++ b/src/OpenFOAM/meshes/boundBox/boundBoxI.H @@ -135,30 +135,19 @@ inline Foam::boundBox::boundBox(Istream& is) inline bool Foam::boundBox::empty() const { - // Check each component for max < min - for (direction dir = 0; dir < vector::nComponents; ++dir) - { - if (max_[dir] < min_[dir]) - { - return true; - } - } - return false; + // Is empty/invalid if any component has (max < min), ie, !(min <= max) + return + ( + (max_.x() < min_.x()) + || (max_.y() < min_.y()) + || (max_.z() < min_.z()) + ); } -inline bool Foam::boundBox::valid() const +inline bool Foam::boundBox::good() const { - // Check each component for max < min - for (direction dir = 0; dir < vector::nComponents; ++dir) - { - if (max_[dir] < min_[dir]) - { - return false; - } - } - - return true; + return !empty(); } @@ -256,6 +245,20 @@ inline void Foam::boundBox::reset() } +inline void Foam::boundBox::reset(const point& pt) +{ + min_ = pt; + max_ = pt; +} + + +inline void Foam::boundBox::reset(const point& min, const point& max) +{ + min_ = min; + max_ = max; +} + + inline void Foam::boundBox::add(const boundBox& bb) { min_ = ::Foam::min(min_, bb.min_); @@ -310,9 +313,9 @@ inline bool Foam::boundBox::overlaps(const boundBox& bb) const { return ( - bb.max_.x() >= min_.x() && bb.min_.x() <= max_.x() - && bb.max_.y() >= min_.y() && bb.min_.y() <= max_.y() - && bb.max_.z() >= min_.z() && bb.min_.z() <= max_.z() + min_.x() <= bb.max_.x() && bb.min_.x() <= max_.x() + && min_.y() <= bb.max_.y() && bb.min_.y() <= max_.y() + && min_.z() <= bb.max_.z() && bb.min_.z() <= max_.z() ); } diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H index 5bdd6979be..4a05407d63 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H @@ -40,8 +40,8 @@ SourceFiles #ifndef Foam_triangle_H #define Foam_triangle_H +#include "triangleFwd.H" #include "intersection.H" -#include "point.H" #include "vector.H" #include "tensor.H" #include "pointHit.H" @@ -62,8 +62,6 @@ namespace Foam // Forward Declarations class plane; -template class triangle; - template inline Istream& operator>>(Istream&, triangle&); @@ -71,12 +69,6 @@ template inline Ostream& operator<<(Ostream&, const triangle&); -// Common Typedefs - -//- A triangle using referred points -typedef triangle triPointRef; - - /*---------------------------------------------------------------------------*\ Class triPoints Declaration \*---------------------------------------------------------------------------*/ diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleFwd.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleFwd.H new file mode 100644 index 0000000000..c3474e37e8 --- /dev/null +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleFwd.H @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2022 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 . + +Typedef + Foam::triPointRef + +Description + A triangle using referred points + +\*---------------------------------------------------------------------------*/ + +#ifndef Foam_triangleFwd_H +#define Foam_triangleFwd_H + +#include "point.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward Declarations +template class triangle; + +// Common Typedefs + +//- A triangle using referred points +typedef triangle triPointRef; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H index ee2e6c479c..403a5b22da 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H @@ -202,23 +202,31 @@ public: //- Range is empty if it is inverted inline bool empty() const; - //- Range is valid if it is not inverted - inline bool valid() const; + //- Range is non-inverted + inline bool good() const; + + //- Range is non-inverted + bool valid() const { return good(); } //- Reset to an inverted (invalid) range inline void reset(); + //- Reset min/max to be identical to the specified value + inline void reset(const T& val); + + //- Reset min/max to specified values + inline void reset(const T& minVal, const T& maxVal); + //- Same as reset() - reset to an inverted (invalid) range void clear() { reset(); } // Testing / Query - //- Intersect (union) with the second range. - // \return True if the resulting intersection is non-empty. - inline bool intersect(const MinMax& b); + //- Test if the ranges intersect (exclusive check) + inline bool intersects(const MinMax& b) const; - //- Test if the ranges overlap + //- Test if ranges overlap/touch (inclusive check) inline bool overlaps(const MinMax& b) const; //- Compares the min/max range with the specified value. @@ -228,7 +236,7 @@ public: // - +1: range (min) is greater than value inline int compare(const T& val) const; - //- True if the value is within the range + //- True if the value is within the range (inclusive check) inline bool contains(const T& val) const; //- If out of range, return the respective min/max limits, otherwise @@ -258,6 +266,9 @@ public: //- Identical to contains(), for use as a predicate. inline bool operator()(const T& val) const; + //- Restrict min/max range to union with other range + inline MinMax& operator&=(const MinMax& b); + //- Extend min/max range to include other range // Can be used in a reduction operation. inline MinMax& operator+=(const MinMax& b); diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H index c656b0328e..6f9e965a7c 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -156,14 +156,15 @@ inline Foam::scalar Foam::MinMax::mag() const template inline bool Foam::MinMax::empty() const { + // Is empty/invalid if (max < min), ie, !(min <= max) return (max() < min()); } template -inline bool Foam::MinMax::valid() const +inline bool Foam::MinMax::good() const { - return !(max() < min()); + return !empty(); } @@ -176,12 +177,26 @@ inline void Foam::MinMax::reset() template -inline bool Foam::MinMax::intersect(const MinMax& b) +inline void Foam::MinMax::reset(const T& val) { - min() = ::Foam::max(min(), b.min()); - max() = ::Foam::min(max(), b.max()); + min() = val; + max() = val; +} - return valid(); + +template +inline void Foam::MinMax::reset(const T& minVal, const T& maxVal) +{ + min() = minVal; + max() = maxVal; +} + + +template +inline bool Foam::MinMax::intersects(const MinMax& b) const +{ + const MinMax& a = *this; + return (a.min() < b.max() && b.min() < a.max()); } @@ -196,7 +211,7 @@ inline bool Foam::MinMax::overlaps(const MinMax& b) const template inline int Foam::MinMax::compare(const T& val) const { - if (valid()) + if (good()) { if (max() < val) { @@ -215,14 +230,14 @@ inline int Foam::MinMax::compare(const T& val) const template inline bool Foam::MinMax::contains(const T& val) const { - return (valid() && !compare(val)); + return good() && !(val < min() || max() < val); } template inline const T& Foam::MinMax::clip(const T& val) const { - if (valid()) + if (good()) { if (val < min()) { @@ -241,7 +256,7 @@ inline const T& Foam::MinMax::clip(const T& val) const template inline bool Foam::MinMax::inplaceClip(T& val) const { - if (valid()) + if (good()) { if (val < min()) { @@ -309,7 +324,7 @@ inline bool Foam::MinMax::operator()(const T& val) const // // // Remove overlapping portion, but cannot create a 'hole' in the middle // -// if (!a.valid() || !b.valid()) +// if (a.empty() || b.empty()) // { // // Invalid range(s): no-op // } @@ -328,6 +343,16 @@ inline bool Foam::MinMax::operator()(const T& val) const // } +template +inline Foam::MinMax& Foam::MinMax::operator&=(const MinMax& b) +{ + min() = ::Foam::max(min(), b.min()); + max() = ::Foam::min(max(), b.max()); + + return *this; +} + + template inline Foam::MinMax& Foam::MinMax::operator+=(const MinMax& b) { diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxOps.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxOps.H index 10e7a723d4..f174787309 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxOps.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxOps.H @@ -32,8 +32,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef MinMaxOps_H -#define MinMaxOps_H +#ifndef Foam_MinMaxOps_H +#define Foam_MinMaxOps_H #include "MinMax.H" #include "VectorSpace.H" diff --git a/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.C b/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.C index 8151e09754..24b0180a92 100644 --- a/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.C +++ b/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -632,10 +632,16 @@ bool Foam::sampledMeshedSurface::update() // Calculate surface and mesh overlap bounding box treeBoundBox bb(surface_.points(), surface_.meshPoints()); - // Check for overlap with (global!) mesh bb - const bool intersect = bb.intersect(mesh().bounds()); + // Restrict surface to (global!) mesh bound box + bb &= mesh().bounds(); - if (!intersect) + if (bb.good()) + { + // Extend a bit + bb.grow(0.5*bb.span()); + bb.inflate(1e-6); + } + else { // Surface and mesh do not overlap at all. Guarantee a valid // bounding box so we don't get any 'invalid bounding box' errors. @@ -645,20 +651,8 @@ bool Foam::sampledMeshedSurface::update() << " does not overlap bounding box of mesh " << mesh().bounds() << endl; - bb = treeBoundBox(mesh().bounds()); - const vector span(bb.span()); - - bb.min() += (0.5-1e-6)*span; - bb.max() -= (0.5-1e-6)*span; - } - else - { - // Extend a bit - const vector span(bb.span()); - bb.min() -= 0.5*span; - bb.max() += 0.5*span; - - bb.inflate(1e-6); + bb.reset(mesh().bounds().centre()); + bb.grow(1e-6*mesh().bounds().span()); } // Mesh search engine, no triangulation of faces.