OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

treeBoundBoxI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "treeBoundBox.H"
00028 #include "Random.H"
00029 
00030 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00031 
00032 inline Foam::treeBoundBox::treeBoundBox()
00033 :
00034     boundBox()
00035 {}
00036 
00037 
00038 inline Foam::treeBoundBox::treeBoundBox(const point& min, const point& max)
00039 :
00040     boundBox(min, max)
00041 {}
00042 
00043 
00044 inline Foam::treeBoundBox::treeBoundBox(const boundBox& bb)
00045 :
00046     boundBox(bb)
00047 {}
00048 
00049 
00050 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00051 
00052 inline Foam::scalar Foam::treeBoundBox::typDim() const
00053 {
00054     return avgDim();
00055 }
00056 
00057 
00058 inline Foam::point Foam::treeBoundBox::corner(const direction octant) const
00059 {
00060     return point
00061     (
00062         (octant & RIGHTHALF) ? max().x() : min().x(),
00063         (octant & TOPHALF)   ? max().y() : min().y(),
00064         (octant & FRONTHALF) ? max().z() : min().z()
00065     );
00066 }
00067 
00068 
00069 // Returns octant in which point resides. Reverse of subBbox.
00070 inline Foam::direction Foam::treeBoundBox::subOctant(const point& pt) const
00071 {
00072     return subOctant(midpoint(), pt);
00073 }
00074 
00075 
00076 // Returns octant in which point resides. Reverse of subBbox.
00077 // Precalculated midpoint
00078 inline Foam::direction Foam::treeBoundBox::subOctant
00079 (
00080     const point& mid,
00081     const point& pt
00082 )
00083 {
00084     direction octant = 0;
00085 
00086     if (pt.x() > mid.x())
00087     {
00088         octant |= treeBoundBox::RIGHTHALF;
00089     }
00090 
00091     if (pt.y() > mid.y())
00092     {
00093         octant |= treeBoundBox::TOPHALF;
00094     }
00095 
00096     if (pt.z() > mid.z())
00097     {
00098         octant |= treeBoundBox::FRONTHALF;
00099     }
00100 
00101     return octant;
00102 }
00103 
00104 
00105 // Returns octant in which point resides. Reverse of subBbox.
00106 // Flags point exactly on edge.
00107 inline Foam::direction Foam::treeBoundBox::subOctant
00108 (
00109     const point& pt,
00110     bool& onEdge
00111 ) const
00112 {
00113     return subOctant(midpoint(), pt, onEdge);
00114 }
00115 
00116 
00117 // Returns octant in which point resides. Reverse of subBbox.
00118 // Precalculated midpoint
00119 inline Foam::direction Foam::treeBoundBox::subOctant
00120 (
00121     const point& mid,
00122     const point& pt,
00123     bool& onEdge
00124 )
00125 {
00126     direction octant = 0;
00127     onEdge = false;
00128 
00129     if (pt.x() > mid.x())
00130     {
00131         octant |= treeBoundBox::RIGHTHALF;
00132     }
00133     else if (pt.x() == mid.x())
00134     {
00135         onEdge = true;
00136     }
00137 
00138     if (pt.y() > mid.y())
00139     {
00140         octant |= treeBoundBox::TOPHALF;
00141     }
00142     else if (pt.y() == mid.y())
00143     {
00144         onEdge = true;
00145     }
00146 
00147     if (pt.z() > mid.z())
00148     {
00149         octant |= treeBoundBox::FRONTHALF;
00150     }
00151     else if (pt.z() == mid.z())
00152     {
00153         onEdge = true;
00154     }
00155 
00156     return octant;
00157 }
00158 
00159 
00160 // Returns octant in which intersection resides.
00161 // Precalculated midpoint. If the point is on the dividing line between
00162 // the octants the direction vector determines which octant to use
00163 // (i.e. in which octant the point would be if it were moved along dir)
00164 inline Foam::direction Foam::treeBoundBox::subOctant
00165 (
00166     const point& mid,
00167     const vector& dir,
00168     const point& pt,
00169     bool& onEdge
00170 )
00171 {
00172     direction octant = 0;
00173     onEdge = false;
00174 
00175     if (pt.x() > mid.x())
00176     {
00177         octant |= treeBoundBox::RIGHTHALF;
00178     }
00179     else if (pt.x() == mid.x())
00180     {
00181         onEdge = true;
00182         if (dir.x() > 0)
00183         {
00184             octant |= treeBoundBox::RIGHTHALF;
00185         }
00186     }
00187 
00188     if (pt.y() > mid.y())
00189     {
00190         octant |= treeBoundBox::TOPHALF;
00191     }
00192     else if (pt.y() == mid.y())
00193     {
00194         onEdge = true;
00195         if (dir.y() > 0)
00196         {
00197             octant |= treeBoundBox::TOPHALF;
00198         }
00199     }
00200 
00201     if (pt.z() > mid.z())
00202     {
00203         octant |= treeBoundBox::FRONTHALF;
00204     }
00205     else if (pt.z() == mid.z())
00206     {
00207         onEdge = true;
00208         if (dir.z() > 0)
00209         {
00210             octant |= treeBoundBox::FRONTHALF;
00211         }
00212     }
00213 
00214     return octant;
00215 }
00216 
00217 
00218 // Returns reference to octantOrder which defines the
00219 // order to do the search.
00220 inline void Foam::treeBoundBox::searchOrder
00221 (
00222     const point& pt,
00223     FixedList<direction,8>& octantOrder
00224 ) const
00225 {
00226     vector dist = midpoint() - pt;
00227 
00228     direction octant = 0;
00229 
00230     if (dist.x() < 0)
00231     {
00232         octant |= treeBoundBox::RIGHTHALF;
00233         dist.x() *= -1;
00234     }
00235 
00236     if (dist.y() < 0)
00237     {
00238         octant |= treeBoundBox::TOPHALF;
00239         dist.y() *= -1;
00240     }
00241 
00242     if (dist.z() < 0)
00243     {
00244         octant |= treeBoundBox::FRONTHALF;
00245     dist.z() *= -1;
00246     }
00247 
00248     direction min = 0;
00249     direction mid = 0;
00250     direction max = 0;
00251 
00252     if (dist.x() < dist.y())
00253     {
00254         if (dist.y() < dist.z())
00255     {
00256         min = treeBoundBox::RIGHTHALF;
00257         mid = treeBoundBox::TOPHALF;
00258         max = treeBoundBox::FRONTHALF;
00259     }
00260     else if (dist.z() < dist.x())
00261     {
00262         min = treeBoundBox::FRONTHALF;
00263             mid = treeBoundBox::RIGHTHALF;
00264         max = treeBoundBox::TOPHALF;
00265     }
00266     else
00267     {
00268         min = treeBoundBox::RIGHTHALF;
00269         mid = treeBoundBox::FRONTHALF;
00270         max = treeBoundBox::TOPHALF;
00271     }
00272     }
00273     else
00274     {
00275         if (dist.z() < dist.y())
00276     {
00277         min = treeBoundBox::FRONTHALF;
00278         mid = treeBoundBox::TOPHALF;
00279         max = treeBoundBox::RIGHTHALF;
00280     }
00281     else if (dist.x() < dist.z())
00282     {
00283         min = treeBoundBox::TOPHALF;
00284         mid = treeBoundBox::RIGHTHALF;
00285         max = treeBoundBox::FRONTHALF;
00286     }
00287     else
00288     {
00289         min = treeBoundBox::TOPHALF;
00290         mid = treeBoundBox::FRONTHALF;
00291         max = treeBoundBox::RIGHTHALF;
00292     }
00293     }
00294 
00295     // Primary subOctant
00296     octantOrder[0] = octant;
00297     // subOctants joined to the primary by faces.
00298     octantOrder[1] = octant ^ min;
00299     octantOrder[2] = octant ^ mid;
00300     octantOrder[3] = octant ^ max;
00301     // subOctants joined to the primary by edges.
00302     octantOrder[4] = octantOrder[1] ^ mid;
00303     octantOrder[5] = octantOrder[1] ^ max;
00304     octantOrder[6] = octantOrder[2] ^ max;
00305     // subOctants joined to the primary by corner.
00306     octantOrder[7] = octantOrder[4] ^ max;
00307 }
00308 
00309 
00310 // true if bb's intersect or overlap.
00311 // Note: <= to make sure we catch all.
00312 inline bool Foam::treeBoundBox::overlaps(const treeBoundBox& bb) const
00313 {
00314     return boundBox::overlaps(bb);
00315 }
00316 
00317 
00318 inline bool Foam::treeBoundBox::contains(const point& pt) const
00319 {
00320     return boundBox::contains(pt);
00321 }
00322 
00323 
00324 //- Return slightly wider bounding box
00325 inline Foam::treeBoundBox Foam::treeBoundBox::extend
00326 (
00327     Random& rndGen,
00328     const scalar s
00329 ) const
00330 {
00331     treeBoundBox bb(*this);
00332 
00333     vector newSpan = bb.span();
00334 
00335     // Make 3D
00336     scalar minSpan = s * Foam::mag(newSpan);
00337 
00338     for (direction dir = 0; dir < vector::nComponents; dir++)
00339     {
00340         newSpan[dir] = Foam::max(newSpan[dir], minSpan);
00341     }
00342 
00343     bb.min() -= cmptMultiply(s * rndGen.vector01(), newSpan);
00344     bb.max() += cmptMultiply(s * rndGen.vector01(), newSpan);
00345 
00346     return bb;
00347 }
00348 
00349 
00350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00351 
00352 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd