ENH: boundBox: Move overlaps function from treeBoundBox.

Update indexedOctree::overlaps to use boundBox::overlaps.
This commit is contained in:
laurence 2012-12-11 16:49:36 +00:00
parent 4be7afde0a
commit 26a9fbb6e1
6 changed files with 67 additions and 71 deletions

View File

@ -47,36 +47,9 @@ bool Foam::indexedOctree<Type>::overlaps
const point& sample const point& sample
) )
{ {
// Find out where sample is in relation to bb. boundBox bb(p0, p1);
// Find nearest point on bb.
scalar distSqr = 0;
for (direction dir = 0; dir < vector::nComponents; dir++) return bb.overlaps(sample, nearestDistSqr);
{
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;
} }

View File

@ -66,6 +66,7 @@ void Foam::boundBox::calculate(const UList<point>& points, const bool doReduce)
min_ = points[0]; min_ = points[0];
max_ = points[0]; max_ = points[0];
for (label i = 1; i < points.size(); i++) for (label i = 1; i < points.size(); i++)
{ {
min_ = ::Foam::min(min_, points[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 * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<(Ostream& os, const boundBox& bb) Foam::Ostream& Foam::operator<<(Ostream& os, const boundBox& bb)

View File

@ -176,6 +176,9 @@ public:
//- Overlaps/touches boundingBox? //- Overlaps/touches boundingBox?
inline bool overlaps(const boundBox&) const; 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) //- Contains point? (inside or on edge)
inline bool contains(const point&) const; inline bool contains(const point&) const;
@ -222,6 +225,10 @@ public:
const FixedList<label, Size>& indices const FixedList<label, Size>& indices
) const; ) const;
//- Distance of a point from the box.
// Return 0 if inside.
scalar distanceFromBoxSqr(const point&) const;
// Friend Operators // Friend Operators

View File

@ -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 inline bool Foam::boundBox::contains(const point& pt) const
{ {
return return

View File

@ -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 // line intersection. Returns true if line (start to end) inside
// bb or intersects bb. Sets pt to intersection. // bb or intersects bb. Sets pt to intersection.
// //

View File

@ -272,9 +272,6 @@ public:
//- Overlaps other bounding box? //- Overlaps other bounding box?
using boundBox::overlaps; 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, //- Intersects segment; set point to intersection position and face,
// return true if intersection found. // return true if intersection found.
// (pt argument used during calculation even if not intersecting). // (pt argument used during calculation even if not intersecting).