OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00039 
00040 // Construct null setting points to zero
00041 inline treeBoundBox::treeBoundBox()
00042 :
00043     boundBox()
00044 {}
00045 
00046 
00047 // Construct from components
00048 inline treeBoundBox::treeBoundBox(const point& min, const point& max)
00049 :
00050     boundBox(min, max)
00051 {}
00052 
00053 
00054 // Construct from components
00055 inline treeBoundBox::treeBoundBox(const boundBox& bb)
00056 :
00057     boundBox(bb)
00058 {}
00059 
00060 
00061 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00062 
00063 // Returns octant in which sample resides. Reverse of subBbox.
00064 inline label treeBoundBox::subOctant(const point& sample) const
00065 {
00066     scalar midx = 0.5*(max().x() + min().x());
00067     scalar midy = 0.5*(max().y() + min().y());
00068     scalar midz = 0.5*(max().z() + min().z());
00069 
00070     label octant = 0;
00071 
00072     if (sample.x() > midx)
00073     {
00074         octant |= treeBoundBox::RIGHTHALF;
00075     }
00076 
00077     if (sample.y() > midy)
00078     {
00079         octant |= treeBoundBox::TOPHALF;
00080     }
00081 
00082     if (sample.z() > midz)
00083     {
00084         octant |= treeBoundBox::FRONTHALF;
00085     }
00086 
00087     return octant;
00088 }
00089 
00090 
00091 // Returns octant in which sample resides. Reverse of subBbox. Flags sample
00092 // exactly on edge.
00093 inline label treeBoundBox::subOctant(const point& sample, bool onEdge) const
00094 {
00095     point mid = 0.5*(max() + min());
00096 
00097     label octant = 0;
00098 
00099     onEdge = false;
00100     if (sample.x() > mid.x())
00101     {
00102         octant |= treeBoundBox::RIGHTHALF;
00103     }
00104     else if (sample.x() == mid.x())
00105     {
00106         onEdge = true;
00107     }
00108 
00109     if (sample.y() > mid.y())
00110     {
00111         octant |= treeBoundBox::TOPHALF;
00112     }
00113     else if (sample.y() == mid.y())
00114     {
00115         onEdge = true;
00116     }
00117 
00118     if (sample.z() > mid.z())
00119     {
00120         octant |= treeBoundBox::FRONTHALF;
00121     }
00122     else if (sample.z() == mid.z())
00123     {
00124         onEdge = true;
00125     }
00126 
00127     return octant;
00128 }
00129 
00130 
00131 // Returns octant in which sample resides. Reverse of subBbox. Precalculated
00132 // midpoint
00133 inline label treeBoundBox::subOctant
00134 (
00135     const point& mid,
00136     const point& sample,
00137     bool onEdge
00138 ) const
00139 {
00140     label octant = 0;
00141 
00142     onEdge = false;
00143     if (sample.x() > mid.x())
00144     {
00145         octant |= treeBoundBox::RIGHTHALF;
00146     }
00147     else if (sample.x() == mid.x())
00148     {
00149         onEdge = true;
00150     }
00151 
00152     if (sample.y() > mid.y())
00153     {
00154         octant |= treeBoundBox::TOPHALF;
00155     }
00156     else if (sample.y() == mid.y())
00157     {
00158         onEdge = true;
00159     }
00160 
00161     if (sample.z() > mid.z())
00162     {
00163         octant |= treeBoundBox::FRONTHALF;
00164     }
00165     else if (sample.z() == mid.z())
00166     {
00167         onEdge = true;
00168     }
00169 
00170     return octant;
00171 }
00172 
00173 
00174 // Returns octant in which intersection resides.
00175 // Precalculated midpoint. If the sample is on the dividing line between
00176 // the octants the direction vector determines which octant to use
00177 // (i.e. in which octant the sample would be if it were moved along dir)
00178 inline label treeBoundBox::subOctant
00179 (
00180     const point& mid,
00181     const vector& dir,
00182     const point& sample,
00183     bool onEdge
00184 ) const
00185 {
00186     label octant = 0;
00187 
00188     onEdge = false;
00189 
00190     if (sample.x() > mid.x())
00191     {
00192         octant |= treeBoundBox::RIGHTHALF;
00193     }
00194     else if (sample.x() == mid.x())
00195     {
00196         onEdge = true;
00197 
00198         if (dir.x() > 0)
00199         {
00200             octant |= treeBoundBox::RIGHTHALF;
00201         }
00202     }
00203 
00204     if (sample.y() > mid.y())
00205     {
00206         octant |= treeBoundBox::TOPHALF;
00207     }
00208     else if (sample.y() == mid.y())
00209     {
00210         onEdge = true;
00211 
00212         if (dir.y() > 0)
00213         {
00214             octant |= treeBoundBox::TOPHALF;
00215         }
00216     }
00217 
00218     if (sample.z() > mid.z())
00219     {
00220         octant |= treeBoundBox::FRONTHALF;
00221     }
00222     else if (sample.z() == mid.z())
00223     {
00224         onEdge = true;
00225 
00226         if (dir.z() > 0)
00227         {
00228             octant |= treeBoundBox::FRONTHALF;
00229         }
00230     }
00231 
00232     return octant;
00233 }
00234 
00235 
00236 // true if bb's intersect or overlap.
00237 // Note: <= to make sure we catch all.
00238 inline bool treeBoundBox::intersects(const treeBoundBox& bb) const
00239 {
00240     return boundBox::intersects(bb);
00241 }
00242 
00243 
00244 inline bool treeBoundBox::contains(const point& sample) const
00245 {
00246     return
00247     (
00248         (sample.x() >= min().x()) &&
00249         (sample.y() >= min().y()) &&
00250         (sample.z() >= min().z()) &&
00251         (sample.x() <= max().x()) &&
00252         (sample.y() <= max().y()) &&
00253         (sample.z() <= max().z())
00254     );
00255 }
00256 
00257 
00258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00259 
00260 } // End namespace Foam
00261 
00262 // ************************************************************************* //
For further information go to www.openfoam.org