OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "error.H"
00030 #include "treeBoundBox.H"
00031 #include "point.H"
00032 #include "Random.H"
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00040 
00041 // Construct null setting points to zero
00042 inline treeBoundBox::treeBoundBox()
00043 :
00044     boundBox()
00045 {}
00046 
00047 
00048 // Construct from components
00049 inline treeBoundBox::treeBoundBox(const point& min, const point& max)
00050 :
00051     boundBox(min, max)
00052 {}
00053 
00054 
00055 // Construct from components
00056 inline treeBoundBox::treeBoundBox(const boundBox& bb)
00057 :
00058     boundBox(bb)
00059 {}
00060 
00061 
00062 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00063 
00064 // Returns octant in which sample resides. Reverse of subBbox.
00065 inline direction treeBoundBox::subOctant(const point& sample) const
00066 {
00067     scalar midx = 0.5*(max().x() + min().x());
00068     scalar midy = 0.5*(max().y() + min().y());
00069     scalar midz = 0.5*(max().z() + min().z());
00070 
00071     direction octant = 0;
00072 
00073     if (sample.x() > midx)
00074     {
00075         octant |= treeBoundBox::RIGHTHALF;
00076     }
00077 
00078     if (sample.y() > midy)
00079     {
00080         octant |= treeBoundBox::TOPHALF;
00081     }
00082 
00083     if (sample.z() > midz)
00084     {
00085         octant |= treeBoundBox::FRONTHALF;
00086     }
00087 
00088     return octant;
00089 }
00090 
00091 
00092 // Returns octant in which sample resides. Reverse of subBbox. Precalculated
00093 // midpoint
00094 inline direction treeBoundBox::subOctant
00095 (
00096     const point& mid,
00097     const point& sample
00098 )
00099 {
00100     direction octant = 0;
00101 
00102     if (sample.x() > mid.x())
00103     {
00104         octant |= treeBoundBox::RIGHTHALF;
00105     }
00106 
00107     if (sample.y() > mid.y())
00108     {
00109         octant |= treeBoundBox::TOPHALF;
00110     }
00111 
00112     if (sample.z() > mid.z())
00113     {
00114         octant |= treeBoundBox::FRONTHALF;
00115     }
00116 
00117     return octant;
00118 }
00119 
00120 
00121 // Returns octant in which sample resides. Reverse of subBbox. Flags sample
00122 // exactly on edge.
00123 inline direction treeBoundBox::subOctant(const point& sample, bool& onEdge)
00124  const
00125 {
00126     point mid = 0.5*(max() + min());
00127 
00128     direction octant = 0;
00129 
00130     onEdge = false;
00131     if (sample.x() > mid.x())
00132     {
00133         octant |= treeBoundBox::RIGHTHALF;
00134     }
00135     else if (sample.x() == mid.x())
00136     {
00137         onEdge = true;
00138     }
00139 
00140     if (sample.y() > mid.y())
00141     {
00142         octant |= treeBoundBox::TOPHALF;
00143     }
00144     else if (sample.y() == mid.y())
00145     {
00146         onEdge = true;
00147     }
00148 
00149     if (sample.z() > mid.z())
00150     {
00151         octant |= treeBoundBox::FRONTHALF;
00152     }
00153     else if (sample.z() == mid.z())
00154     {
00155         onEdge = true;
00156     }
00157 
00158     return octant;
00159 }
00160 
00161 
00162 // Returns octant in which sample resides. Reverse of subBbox. Precalculated
00163 // midpoint
00164 inline direction treeBoundBox::subOctant
00165 (
00166     const point& mid,
00167     const point& sample,
00168     bool& onEdge
00169 )
00170 {
00171     direction octant = 0;
00172 
00173     onEdge = false;
00174     if (sample.x() > mid.x())
00175     {
00176         octant |= treeBoundBox::RIGHTHALF;
00177     }
00178     else if (sample.x() == mid.x())
00179     {
00180         onEdge = true;
00181     }
00182 
00183     if (sample.y() > mid.y())
00184     {
00185         octant |= treeBoundBox::TOPHALF;
00186     }
00187     else if (sample.y() == mid.y())
00188     {
00189         onEdge = true;
00190     }
00191 
00192     if (sample.z() > mid.z())
00193     {
00194         octant |= treeBoundBox::FRONTHALF;
00195     }
00196     else if (sample.z() == mid.z())
00197     {
00198         onEdge = true;
00199     }
00200 
00201     return octant;
00202 }
00203 
00204 
00205 // Returns octant in which intersection resides.
00206 // Precalculated midpoint. If the sample is on the dividing line between
00207 // the octants the direction vector determines which octant to use
00208 // (i.e. in which octant the sample would be if it were moved along dir)
00209 inline direction treeBoundBox::subOctant
00210 (
00211     const point& mid,
00212     const vector& dir,
00213     const point& sample,
00214     bool& onEdge
00215 )
00216 {
00217     direction octant = 0;
00218 
00219     onEdge = false;
00220 
00221     if (sample.x() > mid.x())
00222     {
00223         octant |= treeBoundBox::RIGHTHALF;
00224     }
00225     else if (sample.x() == mid.x())
00226     {
00227         onEdge = true;
00228 
00229         if (dir.x() > 0)
00230         {
00231             octant |= treeBoundBox::RIGHTHALF;
00232         }
00233     }
00234 
00235     if (sample.y() > mid.y())
00236     {
00237         octant |= treeBoundBox::TOPHALF;
00238     }
00239     else if (sample.y() == mid.y())
00240     {
00241         onEdge = true;
00242 
00243         if (dir.y() > 0)
00244         {
00245             octant |= treeBoundBox::TOPHALF;
00246         }
00247     }
00248 
00249     if (sample.z() > mid.z())
00250     {
00251         octant |= treeBoundBox::FRONTHALF;
00252     }
00253     else if (sample.z() == mid.z())
00254     {
00255         onEdge = true;
00256 
00257         if (dir.z() > 0)
00258         {
00259             octant |= treeBoundBox::FRONTHALF;
00260         }
00261     }
00262 
00263     return octant;
00264 }
00265 
00266 
00267 // true if bb's intersect or overlap.
00268 // Note: <= to make sure we catch all.
00269 inline bool treeBoundBox::intersects(const treeBoundBox& bb) const
00270 {
00271     return boundBox::intersects(bb);
00272 }
00273 
00274 
00275 inline bool treeBoundBox::contains(const point& sample) const
00276 {
00277     return
00278     (
00279         (sample.x() >= min().x()) &&
00280         (sample.y() >= min().y()) &&
00281         (sample.z() >= min().z()) &&
00282         (sample.x() <= max().x()) &&
00283         (sample.y() <= max().y()) &&
00284         (sample.z() <= max().z())
00285     );
00286 }
00287 
00288 
00289 //- Return slightly wider bounding box
00290 inline treeBoundBox treeBoundBox::extend(Random& rndGen, const scalar s) const
00291 {
00292     treeBoundBox bb(*this);
00293 
00294     const vector span(bb.max() - bb.min());
00295 
00296     bb.min() -= scale(s*rndGen.vector01(), span);
00297     bb.max() += scale(s*rndGen.vector01(), span);
00298 
00299     return bb;
00300 }
00301 
00302 
00303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00304 
00305 } // End namespace Foam
00306 
00307 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd