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

treeDataCell.C

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 "treeDataCell.H"
00028 #include "indexedOctree.H"
00029 #include "primitiveMesh.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 defineTypeNameAndDebug(Foam::treeDataCell, 0);
00034 
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 Foam::treeBoundBox Foam::treeDataCell::calcCellBb(const label cellI) const
00039 {
00040     const cellList& cells = mesh_.cells();
00041     const faceList& faces = mesh_.faces();
00042     const pointField& points = mesh_.points();
00043 
00044     treeBoundBox cellBb
00045     (
00046         vector(GREAT, GREAT, GREAT),
00047         vector(-GREAT, -GREAT, -GREAT)
00048     );
00049 
00050     const cell& cFaces = cells[cellI];
00051 
00052     forAll(cFaces, cFaceI)
00053     {
00054         const face& f = faces[cFaces[cFaceI]];
00055 
00056         forAll(f, fp)
00057         {
00058             const point& p = points[f[fp]];
00059 
00060             cellBb.min() = min(cellBb.min(), p);
00061             cellBb.max() = max(cellBb.max(), p);
00062         }
00063     }
00064     return cellBb;
00065 }
00066 
00067 
00068 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00069 
00070 // Construct from components
00071 Foam::treeDataCell::treeDataCell
00072 (
00073     const bool cacheBb,
00074     const primitiveMesh& mesh,
00075     const labelList& cellLabels
00076 )
00077 :
00078     mesh_(mesh),
00079     cellLabels_(cellLabels),
00080     cacheBb_(cacheBb)
00081 {
00082     if (cacheBb_)
00083     {
00084         bbs_.setSize(cellLabels_.size());
00085 
00086         forAll(cellLabels_, i)
00087         {
00088             bbs_[i] = calcCellBb(cellLabels_[i]);
00089         }
00090     }
00091 }
00092 
00093 
00094 Foam::treeDataCell::treeDataCell
00095 (
00096     const bool cacheBb,
00097     const primitiveMesh& mesh
00098 )
00099 :
00100     mesh_(mesh),
00101     cellLabels_(identity(mesh_.nCells())),
00102     cacheBb_(cacheBb)
00103 {
00104     if (cacheBb_)
00105     {
00106         bbs_.setSize(cellLabels_.size());
00107 
00108         forAll(cellLabels_, i)
00109         {
00110             bbs_[i] = calcCellBb(cellLabels_[i]);
00111         }
00112     }
00113 }
00114 
00115 
00116 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00117 
00118 Foam::pointField Foam::treeDataCell::points() const
00119 {
00120     pointField cc(cellLabels_.size());
00121 
00122     forAll(cellLabels_, i)
00123     {
00124         cc[i] = mesh_.cellCentres()[cellLabels_[i]];
00125     }
00126 
00127     return cc;
00128 }
00129 
00130 
00131 // Check if any point on shape is inside cubeBb.
00132 bool Foam::treeDataCell::overlaps
00133 (
00134     const label index,
00135     const treeBoundBox& cubeBb
00136 ) const
00137 {
00138     if (cacheBb_)
00139     {
00140         return cubeBb.overlaps(bbs_[index]);
00141     }
00142     else
00143     {
00144         return cubeBb.overlaps(calcCellBb(cellLabels_[index]));
00145     }
00146 }
00147 
00148 
00149 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
00150 // nearestPoint.
00151 void Foam::treeDataCell::findNearest
00152 (
00153     const labelList& indices,
00154     const point& sample,
00155 
00156     scalar& nearestDistSqr,
00157     label& minIndex,
00158     point& nearestPoint
00159 ) const
00160 {
00161     forAll(indices, i)
00162     {
00163         label index = indices[i];
00164         label cellI = cellLabels_[index];
00165         scalar distSqr = magSqr(sample - mesh_.cellCentres()[cellI]);
00166 
00167         if (distSqr < nearestDistSqr)
00168         {
00169             nearestDistSqr = distSqr;
00170             minIndex = index;
00171             nearestPoint = mesh_.cellCentres()[cellI];
00172         }
00173     }
00174 }
00175 
00176 
00177 bool Foam::treeDataCell::intersects
00178 (
00179     const label index,
00180     const point& start,
00181     const point& end,
00182     point& intersectionPoint
00183 ) const
00184 {
00185     // Do quick rejection test
00186     if (cacheBb_)
00187     {
00188         const treeBoundBox& cellBb = bbs_[index];
00189 
00190         if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0)
00191         {
00192             // start and end in same block outside of cellBb.
00193             return false;
00194         }
00195     }
00196     else
00197     {
00198         const treeBoundBox cellBb = calcCellBb(cellLabels_[index]);
00199 
00200         if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0)
00201         {
00202             // start and end in same block outside of cellBb.
00203             return false;
00204         }
00205     }
00206 
00207 
00208     // Do intersection with all faces of cell
00209     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00210 
00211     // Disable picking up intersections behind us.
00212     scalar oldTol = intersection::setPlanarTol(0.0);
00213 
00214     const cell& cFaces = mesh_.cells()[cellLabels_[index]];
00215 
00216     const vector dir(end - start);
00217     scalar minDistSqr = magSqr(dir);
00218     bool hasMin = false;
00219 
00220     forAll(cFaces, i)
00221     {
00222         const face& f = mesh_.faces()[cFaces[i]];
00223 
00224         pointHit inter = f.ray
00225         (
00226             start,
00227             dir,
00228             mesh_.points(),
00229             intersection::HALF_RAY
00230         );
00231 
00232         if (inter.hit() && sqr(inter.distance()) <= minDistSqr)
00233         {
00234             // Note: no extra test on whether intersection is in front of us
00235             // since using half_ray AND zero tolerance. (note that tolerance
00236             // is used to look behind us)
00237             minDistSqr = sqr(inter.distance());
00238             intersectionPoint = inter.hitPoint();
00239             hasMin = true;
00240         }
00241     }
00242 
00243     // Restore picking tolerance
00244     intersection::setPlanarTol(oldTol);
00245 
00246     return hasMin;
00247 }
00248 
00249 
00250 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd