cleanup
[OpenFOAM-1.5.x.git] / src / meshTools / searchableSurface / searchableSphere.C
blob04f3f2f2b52c0f410487a6b68a13518682be0a80
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 * * * * * * * * * * * * * //
32 namespace Foam
35 defineTypeNameAndDebug(searchableSphere, 0);
36 addToRunTimeSelectionTable(searchableSurface, searchableSphere, dict);
41 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
43 Foam::pointIndexHit Foam::searchableSphere::findNearest
45     const point& sample,
46     const scalar nearestDistSqr
47 ) const
49     pointIndexHit info(false, sample, -1);
51     const vector n(sample-centre_);
52     scalar magN = mag(n);
54     if (nearestDistSqr > sqr(magN-radius_))
55     {
56         info.rawPoint() = centre_ + n/magN*radius_;
57         info.setHit();
58         info.setIndex(0);
59     }
61     return info;
65 // From Graphics Gems - intersection of sphere with ray
66 void Foam::searchableSphere::findLineAll
68     const point& start,
69     const point& end,
70     pointIndexHit& near,
71     pointIndexHit& far
72 ) const
74     near.setMiss();
75     far.setMiss();
77     vector dir(end-start);
78     scalar magSqrDir = magSqr(dir);
80     if (magSqrDir > ROOTVSMALL)
81     {
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));
91         if (disc >= 0)
92         {
93             scalar d = Foam::sqrt(disc);
95             scalar nearParam = v-d;
97             if (nearParam >= 0 && sqr(nearParam) <= magSqrDir)
98             {
99                 near.setHit();
100                 near.setPoint(start + nearParam*dir);
101                 near.setIndex(0);
102             }
104             scalar farParam = v+d;
106             if (farParam >= 0 && sqr(farParam) <= magSqrDir)
107             {
108                 far.setHit();
109                 far.setPoint(start + farParam*dir);
110                 far.setIndex(0);
111             }
112         }
113     }
117 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
119 Foam::searchableSphere::searchableSphere
121     const IOobject& io,
122     const point& centre,
123     const scalar radius
126     searchableSurface(io),
127     centre_(centre),
128     radius_(radius)
132 Foam::searchableSphere::searchableSphere
134     const IOobject& io,
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)
155     {
156         regions_.setSize(1);
157         regions_[0] = "region0";
158     }
159     return regions_;
164 void Foam::searchableSphere::findNearest
166     const pointField& samples,
167     const scalarField& nearestDistSqr,
168     List<pointIndexHit>& info
169 ) const
171     info.setSize(samples.size());
173     forAll(samples, i)
174     {
175         info[i] = findNearest(samples[i], nearestDistSqr[i]);
176     }
180 void Foam::searchableSphere::findLine
182     const pointField& start,
183     const pointField& end,
184     List<pointIndexHit>& info
185 ) const
187     info.setSize(start.size());
189     pointIndexHit b;
191     forAll(start, i)
192     {
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())
196         {
197             info[i] = b;
198         }
199     }
203 void Foam::searchableSphere::findLineAny
205     const pointField& start,
206     const pointField& end,
207     List<pointIndexHit>& info
208 ) const
210     info.setSize(start.size());
212     pointIndexHit b;
214     forAll(start, i)
215     {
216         // Discard far intersection
217         findLineAll(start[i], end[i], info[i], b);
218         if (!info[i].hit() && b.hit())
219         {
220             info[i] = b;
221         }
222     }
226 void Foam::searchableSphere::findLineAll
228     const pointField& start,
229     const pointField& end,
230     List<List<pointIndexHit> >& info
231 ) const
233     info.setSize(start.size());
235     forAll(start, i)
236     {
237         pointIndexHit near, far;
238         findLineAll(start[i], end[i], near, far);
240         if (near.hit())
241         {
242             if (far.hit())
243             {
244                 info[i].setSize(2);
245                 info[i][0] = near;
246                 info[i][1] = far;
247             }
248             else
249             {
250                 info[i].setSize(1);
251                 info[i][0] = near;
252             }
253         }
254         else
255         {
256             if (far.hit())
257             {
258                 info[i].setSize(1);
259                 info[i][0] = far;
260             }
261             else
262             {
263                 info[i].clear();
264             }
265         }
266     }
270 void Foam::searchableSphere::getRegion
272     const List<pointIndexHit>& info,
273     labelList& region
274 ) const
276     region.setSize(info.size());
277     region = 0;
281 void Foam::searchableSphere::getNormal
283     const List<pointIndexHit>& info,
284     vectorField& normal
285 ) const
287     normal.setSize(info.size());
288     normal = vector::zero;
290     forAll(info, i)
291     {
292         if (info[i].hit())
293         {
294             normal[i] = info[i].hitPoint() - centre_;
295             normal[i] /= mag(normal[i]);
296         }
297         else
298         {
299             // Set to what?
300         }
301     }
305 void Foam::searchableSphere::getVolumeType
307     const pointField& points,
308     List<volumeType>& volType
309 ) const
311     volType.setSize(points.size());
312     volType = INSIDE;
314     forAll(points, pointI)
315     {
316         const point& pt = points[pointI];
318         if (magSqr(pt - centre_) <= sqr(radius_))
319         {
320             volType[pointI] = INSIDE;
321         }
322         else
323         {
324             volType[pointI] = OUTSIDE;
325         }
326     }
330 // ************************************************************************* //