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

triSurfaceSearch.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 "triSurfaceSearch.H"
00028 #include "indexedOctree.H"
00029 #include "boolList.H"
00030 #include "treeDataTriSurface.H"
00031 #include "triSurface.H"
00032 #include "line.H"
00033 #include "cpuTime.H"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039 
00040 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00041 
00042 const point triSurfaceSearch::greatPoint(GREAT, GREAT, GREAT);
00043 
00044 
00045 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00046 
00047 // Construct from surface. Holds reference!
00048 triSurfaceSearch::triSurfaceSearch(const triSurface& surface)
00049 :
00050     surface_(surface),
00051     treePtr_(NULL)
00052 {
00053     // Random number generator. Bit dodgy since not exactly random ;-)
00054     Random rndGen(65431);
00055 
00056     // Slightly extended bb. Slightly off-centred just so on symmetric
00057     // geometry there are less face/edge aligned items.
00058     treeBoundBox treeBb
00059     (
00060         treeBoundBox(surface_.points(), surface_.meshPoints()).extend
00061         (
00062             rndGen,
00063             1E-4
00064         )
00065     );
00066     treeBb.min() -= point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00067     treeBb.max() += point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00068 
00069     treePtr_.reset
00070     (
00071         new indexedOctree<treeDataTriSurface>
00072         (
00073             treeDataTriSurface(surface_),
00074             treeBb,
00075             8,      // maxLevel
00076             10,     // leafsize
00077             3.0     // duplicity
00078         )
00079     );
00080 }
00081 
00082 
00083 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00084 
00085 // Determine inside/outside for samples
00086 boolList triSurfaceSearch::calcInside(const pointField& samples) const
00087 {
00088     boolList inside(samples.size());
00089 
00090     forAll(samples, sampleI)
00091     {
00092         const point& sample = samples[sampleI];
00093 
00094         if (!tree().bb().contains(sample))
00095         {
00096             inside[sampleI] = false;
00097         }
00098         else if
00099         (
00100             tree().getVolumeType(sample)
00101          == indexedOctree<treeDataTriSurface>::INSIDE
00102         )
00103         {
00104             inside[sampleI] = true;
00105         }
00106         else
00107         {
00108             inside[sampleI] = false;
00109         }
00110     }
00111     return inside;
00112 }
00113 
00114 
00115 labelList triSurfaceSearch::calcNearestTri
00116 (
00117     const pointField& samples,
00118     const vector& span
00119 ) const
00120 {
00121     labelList nearest(samples.size());
00122 
00123     const scalar nearestDistSqr = 0.25*magSqr(span);
00124 
00125     pointIndexHit hitInfo;
00126 
00127     forAll(samples, sampleI)
00128     {
00129         hitInfo = tree().findNearest(samples[sampleI], nearestDistSqr);
00130 
00131         if (hitInfo.hit())
00132         {
00133             nearest[sampleI] = hitInfo.index();
00134         }
00135         else
00136         {
00137             nearest[sampleI] = -1;
00138         }
00139     }
00140 
00141     return nearest;
00142 }
00143 
00144 
00145 // Nearest point on surface
00146 tmp<pointField> triSurfaceSearch::calcNearest
00147 (
00148     const pointField& samples,
00149     const vector& span
00150 ) const
00151 {
00152     const scalar nearestDistSqr = 0.25*magSqr(span);
00153 
00154     tmp<pointField> tnearest(new pointField(samples.size()));
00155     pointField& nearest = tnearest();
00156 
00157     pointIndexHit hitInfo;
00158 
00159     forAll(samples, sampleI)
00160     {
00161         hitInfo = tree().findNearest(samples[sampleI], nearestDistSqr);
00162 
00163         if (hitInfo.hit())
00164         {
00165             nearest[sampleI] = hitInfo.hitPoint();
00166         }
00167         else
00168         {
00169             nearest[sampleI] = greatPoint;
00170         }
00171     }
00172 
00173     return tnearest;
00174 }
00175 
00176 
00177 pointIndexHit triSurfaceSearch::nearest(const point& pt, const vector& span)
00178  const
00179 {
00180     const scalar nearestDistSqr = 0.25*magSqr(span);
00181 
00182     return tree().findNearest(pt, nearestDistSqr);
00183 }
00184 
00185 
00186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00187 
00188 } // End namespace Foam
00189 
00190 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd