From 26a9fbb6e15902d58dac28fd1ffb4717af23cf67 Mon Sep 17 00:00:00 2001 From: laurence Date: Tue, 11 Dec 2012 16:49:36 +0000 Subject: [PATCH] ENH: boundBox: Move overlaps function from treeBoundBox. Update indexedOctree::overlaps to use boundBox::overlaps. --- .../algorithms/indexedOctree/indexedOctree.C | 31 +-------------- src/OpenFOAM/meshes/boundBox/boundBox.C | 19 +++++++++ src/OpenFOAM/meshes/boundBox/boundBox.H | 7 ++++ src/OpenFOAM/meshes/boundBox/boundBoxI.H | 39 +++++++++++++++++++ .../meshes/treeBoundBox/treeBoundBox.C | 39 ------------------- .../meshes/treeBoundBox/treeBoundBox.H | 3 -- 6 files changed, 67 insertions(+), 71 deletions(-) diff --git a/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C b/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C index f16dd2595c..6295d6f195 100644 --- a/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C +++ b/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C @@ -47,36 +47,9 @@ bool Foam::indexedOctree::overlaps const point& sample ) { - // Find out where sample is in relation to bb. - // Find nearest point on bb. - scalar distSqr = 0; + boundBox bb(p0, p1); - for (direction dir = 0; dir < vector::nComponents; dir++) - { - scalar d0 = p0[dir] - sample[dir]; - scalar d1 = p1[dir] - sample[dir]; - - if ((d0 > 0) != (d1 > 0)) - { - // sample inside both extrema. This component does not add any - // distance. - } - else if (mag(d0) < mag(d1)) - { - distSqr += d0*d0; - } - else - { - distSqr += d1*d1; - } - - if (distSqr > nearestDistSqr) - { - return false; - } - } - - return true; + return bb.overlaps(sample, nearestDistSqr); } diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.C b/src/OpenFOAM/meshes/boundBox/boundBox.C index f1c297837d..db628c32a1 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.C +++ b/src/OpenFOAM/meshes/boundBox/boundBox.C @@ -66,6 +66,7 @@ void Foam::boundBox::calculate(const UList& points, const bool doReduce) min_ = points[0]; max_ = points[0]; + for (label i = 1; i < points.size(); i++) { min_ = ::Foam::min(min_, points[i]); @@ -299,6 +300,24 @@ bool Foam::boundBox::containsAny } +Foam::scalar Foam::boundBox::distanceFromBoxSqr(const point& pt) const +{ + if (contains(pt)) + { + return 0; + } + + // 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()); + + const point surfacePt(surfPtx, surfPty, surfPtz); + + return magSqr(pt - surfacePt); +} + + // * * * * * * * * * * * * * * * 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 c59281bdf0..857f41e2bf 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.H +++ b/src/OpenFOAM/meshes/boundBox/boundBox.H @@ -176,6 +176,9 @@ public: //- Overlaps/touches boundingBox? inline bool overlaps(const boundBox&) const; + //- Overlaps boundingSphere (centre + sqr(radius))? + inline bool overlaps(const point&, const scalar radiusSqr) const; + //- Contains point? (inside or on edge) inline bool contains(const point&) const; @@ -222,6 +225,10 @@ public: const FixedList& indices ) const; + //- Distance of a point from the box. + // Return 0 if inside. + scalar distanceFromBoxSqr(const point&) const; + // Friend Operators diff --git a/src/OpenFOAM/meshes/boundBox/boundBoxI.H b/src/OpenFOAM/meshes/boundBox/boundBoxI.H index efb8d3d9d0..ce0d1017c7 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBoxI.H +++ b/src/OpenFOAM/meshes/boundBox/boundBoxI.H @@ -128,6 +128,45 @@ inline bool Foam::boundBox::overlaps(const boundBox& bb) const } +inline bool Foam::boundBox::overlaps +( + const point& centre, + const scalar radiusSqr +) const +{ + // Find out where centre is in relation to bb. + // Find nearest point on bb. + scalar distSqr = 0; + + for (direction dir = 0; dir < vector::nComponents; dir++) + { + scalar d0 = min_[dir] - centre[dir]; + scalar d1 = max_[dir] - centre[dir]; + + if ((d0 > 0) != (d1 > 0)) + { + // centre inside both extrema. This component does not add any + // distance. + } + else if (Foam::mag(d0) < Foam::mag(d1)) + { + distSqr += d0*d0; + } + else + { + distSqr += d1*d1; + } + + if (distSqr > radiusSqr) + { + return false; + } + } + + return true; +} + + inline bool Foam::boundBox::contains(const point& pt) const { return diff --git a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C index 956997974f..4c13182f14 100644 --- a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C +++ b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C @@ -238,45 +238,6 @@ Foam::treeBoundBox Foam::treeBoundBox::subBbox } -bool Foam::treeBoundBox::overlaps -( - const point& centre, - const scalar radiusSqr -) const -{ - // Find out where centre is in relation to bb. - // Find nearest point on bb. - scalar distSqr = 0; - - for (direction dir = 0; dir < vector::nComponents; dir++) - { - scalar d0 = min()[dir] - centre[dir]; - scalar d1 = max()[dir] - centre[dir]; - - if ((d0 > 0) != (d1 > 0)) - { - // centre inside both extrema. This component does not add any - // distance. - } - else if (Foam::mag(d0) < Foam::mag(d1)) - { - distSqr += d0*d0; - } - else - { - distSqr += d1*d1; - } - - if (distSqr > radiusSqr) - { - return false; - } - } - - return true; -} - - // line intersection. Returns true if line (start to end) inside // bb or intersects bb. Sets pt to intersection. // diff --git a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H index aecdd905b3..d740c96143 100644 --- a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H +++ b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H @@ -272,9 +272,6 @@ public: //- Overlaps other bounding box? using boundBox::overlaps; - //- Overlaps boundingSphere (centre + sqr(radius))? - bool overlaps(const point&, const scalar radiusSqr) const; - //- Intersects segment; set point to intersection position and face, // return true if intersection found. // (pt argument used during calculation even if not intersecting).