1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "searchableSphere.H"
28 #include "addToRunTimeSelectionTable.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 defineTypeNameAndDebug(searchableSphere, 0);
36 addToRunTimeSelectionTable(searchableSurface, searchableSphere, dict);
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
43 Foam::pointIndexHit Foam::searchableSphere::findNearest
46 const scalar nearestDistSqr
49 pointIndexHit info(false, sample, -1);
51 const vector n(sample-centre_);
54 if (nearestDistSqr > sqr(magN-radius_))
56 info.rawPoint() = centre_ + n/magN*radius_;
65 // From Graphics Gems - intersection of sphere with ray
66 void Foam::searchableSphere::findLineAll
77 vector dir(end-start);
78 scalar magSqrDir = magSqr(dir);
80 if (magSqrDir > ROOTVSMALL)
82 const vector toCentre(centre_-start);
83 scalar magSqrToCentre = magSqr(toCentre);
85 dir /= Foam::sqrt(magSqrDir);
87 scalar v = (toCentre & dir);
89 scalar disc = sqr(radius_) - (magSqrToCentre - sqr(v));
93 scalar d = Foam::sqrt(disc);
95 scalar nearParam = v-d;
97 if (nearParam >= 0 && sqr(nearParam) <= magSqrDir)
100 near.setPoint(start + nearParam*dir);
104 scalar farParam = v+d;
106 if (farParam >= 0 && sqr(farParam) <= magSqrDir)
109 far.setPoint(start + farParam*dir);
117 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
119 Foam::searchableSphere::searchableSphere
126 searchableSurface(io),
132 Foam::searchableSphere::searchableSphere
135 const dictionary& dict
138 searchableSurface(io),
139 centre_(dict.lookup("centre")),
140 radius_(readScalar(dict.lookup("radius")))
144 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
146 Foam::searchableSphere::~searchableSphere()
150 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152 const Foam::wordList& Foam::searchableSphere::regions() const
154 if (regions_.size() == 0)
157 regions_[0] = "region0";
164 void Foam::searchableSphere::findNearest
166 const pointField& samples,
167 const scalarField& nearestDistSqr,
168 List<pointIndexHit>& info
171 info.setSize(samples.size());
175 info[i] = findNearest(samples[i], nearestDistSqr[i]);
180 void Foam::searchableSphere::findLine
182 const pointField& start,
183 const pointField& end,
184 List<pointIndexHit>& info
187 info.setSize(start.size());
193 // Pick nearest intersection. If none intersected take second one.
194 findLineAll(start[i], end[i], info[i], b);
195 if (!info[i].hit() && b.hit())
203 void Foam::searchableSphere::findLineAny
205 const pointField& start,
206 const pointField& end,
207 List<pointIndexHit>& info
210 info.setSize(start.size());
216 // Discard far intersection
217 findLineAll(start[i], end[i], info[i], b);
218 if (!info[i].hit() && b.hit())
226 void Foam::searchableSphere::findLineAll
228 const pointField& start,
229 const pointField& end,
230 List<List<pointIndexHit> >& info
233 info.setSize(start.size());
237 pointIndexHit near, far;
238 findLineAll(start[i], end[i], near, far);
270 void Foam::searchableSphere::getRegion
272 const List<pointIndexHit>& info,
276 region.setSize(info.size());
281 void Foam::searchableSphere::getNormal
283 const List<pointIndexHit>& info,
287 normal.setSize(info.size());
288 normal = vector::zero;
294 normal[i] = info[i].hitPoint() - centre_;
295 normal[i] /= mag(normal[i]);
305 void Foam::searchableSphere::getVolumeType
307 const pointField& points,
308 List<volumeType>& volType
311 volType.setSize(points.size());
314 forAll(points, pointI)
316 const point& pt = points[pointI];
318 if (magSqr(pt - centre_) <= sqr(radius_))
320 volType[pointI] = INSIDE;
324 volType[pointI] = OUTSIDE;
330 // ************************************************************************* //