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

searchableSphere.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 "searchableSphere.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 defineTypeNameAndDebug(searchableSphere, 0);
00036 addToRunTimeSelectionTable(searchableSurface, searchableSphere, dict);
00037 
00038 }
00039 
00040 
00041 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00042 
00043 Foam::pointIndexHit Foam::searchableSphere::findNearest
00044 (
00045     const point& sample,
00046     const scalar nearestDistSqr
00047 ) const
00048 {
00049     pointIndexHit info(false, sample, -1);
00050 
00051     const vector n(sample-centre_);
00052     scalar magN = mag(n);
00053 
00054     if (nearestDistSqr > sqr(magN-radius_))
00055     {
00056         if (magN < ROOTVSMALL)
00057         {
00058             info.rawPoint() = centre_ + vector(1,0,0)/magN*radius_;
00059         }
00060         else
00061         {
00062             info.rawPoint() = centre_ + n/magN*radius_;
00063         }
00064         info.setHit();
00065         info.setIndex(0);
00066     }
00067 
00068     return info;
00069 }
00070 
00071 
00072 // From Graphics Gems - intersection of sphere with ray
00073 void Foam::searchableSphere::findLineAll
00074 (
00075     const point& start,
00076     const point& end,
00077     pointIndexHit& near,
00078     pointIndexHit& far
00079 ) const
00080 {
00081     near.setMiss();
00082     far.setMiss();
00083 
00084     vector dir(end-start);
00085     scalar magSqrDir = magSqr(dir);
00086 
00087     if (magSqrDir > ROOTVSMALL)
00088     {
00089         const vector toCentre(centre_-start);
00090         scalar magSqrToCentre = magSqr(toCentre);
00091 
00092         dir /= Foam::sqrt(magSqrDir);
00093 
00094         scalar v = (toCentre & dir);
00095 
00096         scalar disc = sqr(radius_) - (magSqrToCentre - sqr(v));
00097 
00098         if (disc >= 0)
00099         {
00100             scalar d = Foam::sqrt(disc);
00101 
00102             scalar nearParam = v-d;
00103 
00104             if (nearParam >= 0 && sqr(nearParam) <= magSqrDir)
00105             {
00106                 near.setHit();
00107                 near.setPoint(start + nearParam*dir);
00108                 near.setIndex(0);
00109             }
00110 
00111             scalar farParam = v+d;
00112 
00113             if (farParam >= 0 && sqr(farParam) <= magSqrDir)
00114             {
00115                 far.setHit();
00116                 far.setPoint(start + farParam*dir);
00117                 far.setIndex(0);
00118             }
00119         }
00120     }
00121 }
00122 
00123 
00124 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00125 
00126 Foam::searchableSphere::searchableSphere
00127 (
00128     const IOobject& io,
00129     const point& centre,
00130     const scalar radius
00131 )
00132 :
00133     searchableSurface(io),
00134     centre_(centre),
00135     radius_(radius)
00136 {}
00137 
00138 
00139 Foam::searchableSphere::searchableSphere
00140 (
00141     const IOobject& io,
00142     const dictionary& dict
00143 )
00144 :
00145     searchableSurface(io),
00146     centre_(dict.lookup("centre")),
00147     radius_(readScalar(dict.lookup("radius")))
00148 {}
00149 
00150 
00151 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00152 
00153 Foam::searchableSphere::~searchableSphere()
00154 {}
00155 
00156 
00157 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00158 
00159 const Foam::wordList& Foam::searchableSphere::regions() const
00160 {
00161     if (regions_.empty())
00162     {
00163         regions_.setSize(1);
00164         regions_[0] = "region0";
00165     }
00166     return regions_;
00167 }
00168 
00169 
00170 
00171 void Foam::searchableSphere::findNearest
00172 (
00173     const pointField& samples,
00174     const scalarField& nearestDistSqr,
00175     List<pointIndexHit>& info
00176 ) const
00177 {
00178     info.setSize(samples.size());
00179 
00180     forAll(samples, i)
00181     {
00182         info[i] = findNearest(samples[i], nearestDistSqr[i]);
00183     }
00184 }
00185 
00186 
00187 void Foam::searchableSphere::findLine
00188 (
00189     const pointField& start,
00190     const pointField& end,
00191     List<pointIndexHit>& info
00192 ) const
00193 {
00194     info.setSize(start.size());
00195 
00196     pointIndexHit b;
00197 
00198     forAll(start, i)
00199     {
00200         // Pick nearest intersection. If none intersected take second one.
00201         findLineAll(start[i], end[i], info[i], b);
00202         if (!info[i].hit() && b.hit())
00203         {
00204             info[i] = b;
00205         }
00206     }
00207 }
00208 
00209 
00210 void Foam::searchableSphere::findLineAny
00211 (
00212     const pointField& start,
00213     const pointField& end,
00214     List<pointIndexHit>& info
00215 ) const
00216 {
00217     info.setSize(start.size());
00218 
00219     pointIndexHit b;
00220 
00221     forAll(start, i)
00222     {
00223         // Discard far intersection
00224         findLineAll(start[i], end[i], info[i], b);
00225         if (!info[i].hit() && b.hit())
00226         {
00227             info[i] = b;
00228         }
00229     }
00230 }
00231 
00232 
00233 void Foam::searchableSphere::findLineAll
00234 (
00235     const pointField& start,
00236     const pointField& end,
00237     List<List<pointIndexHit> >& info
00238 ) const
00239 {
00240     info.setSize(start.size());
00241 
00242     forAll(start, i)
00243     {
00244         pointIndexHit near, far;
00245         findLineAll(start[i], end[i], near, far);
00246 
00247         if (near.hit())
00248         {
00249             if (far.hit())
00250             {
00251                 info[i].setSize(2);
00252                 info[i][0] = near;
00253                 info[i][1] = far;
00254             }
00255             else
00256             {
00257                 info[i].setSize(1);
00258                 info[i][0] = near;
00259             }
00260         }
00261         else
00262         {
00263             if (far.hit())
00264             {
00265                 info[i].setSize(1);
00266                 info[i][0] = far;
00267             }
00268             else
00269             {
00270                 info[i].clear();
00271             }
00272         }
00273     }
00274 }
00275 
00276 
00277 void Foam::searchableSphere::getRegion
00278 (
00279     const List<pointIndexHit>& info,
00280     labelList& region
00281 ) const
00282 {
00283     region.setSize(info.size());
00284     region = 0;
00285 }
00286 
00287 
00288 void Foam::searchableSphere::getNormal
00289 (
00290     const List<pointIndexHit>& info,
00291     vectorField& normal
00292 ) const
00293 {
00294     normal.setSize(info.size());
00295     normal = vector::zero;
00296 
00297     forAll(info, i)
00298     {
00299         if (info[i].hit())
00300         {
00301             normal[i] = info[i].hitPoint() - centre_;
00302 
00303             normal[i] /= mag(normal[i])+VSMALL;
00304         }
00305         else
00306         {
00307             // Set to what?
00308         }
00309     }
00310 }
00311 
00312 
00313 void Foam::searchableSphere::getVolumeType
00314 (
00315     const pointField& points,
00316     List<volumeType>& volType
00317 ) const
00318 {
00319     volType.setSize(points.size());
00320     volType = INSIDE;
00321 
00322     forAll(points, pointI)
00323     {
00324         const point& pt = points[pointI];
00325 
00326         if (magSqr(pt - centre_) <= sqr(radius_))
00327         {
00328             volType[pointI] = INSIDE;
00329         }
00330         else
00331         {
00332             volType[pointI] = OUTSIDE;
00333         }
00334     }
00335 }
00336 
00337 
00338 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd