searchableDisk.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2014-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "searchableDisk.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 
34 defineTypeNameAndDebug(searchableDisk, 0);
35 addToRunTimeSelectionTable(searchableSurface, searchableDisk, dict);
36 
37 }
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
42 Foam::pointIndexHit Foam::searchableDisk::findNearest
43 (
44  const point& sample,
45  const scalar nearestDistSqr
46 ) const
47 {
48  pointIndexHit info(false, sample, -1);
49 
50  vector v(sample - origin_);
51 
52  // Decompose sample-origin into normal and parallel component
53  scalar parallel = (v & normal_);
54 
55  // Remove the parallel component and normalise
56  v -= parallel*normal_;
57  scalar magV = mag(v);
58 
59  if (magV < rootVSmall)
60  {
61  v = Zero;
62  }
63  else
64  {
65  v /= magV;
66  }
67 
68  // Clip to radius.
69  info.setPoint(origin_ + min(magV, radius_)*v);
70 
71  if (magSqr(sample - info.rawPoint()) < nearestDistSqr)
72  {
73  info.setHit();
74  info.setIndex(0);
75  }
76 
77  return info;
78 }
79 
80 
81 void Foam::searchableDisk::findLine
82 (
83  const point& start,
84  const point& end,
85  pointIndexHit& info
86 ) const
87 {
88  info = pointIndexHit(false, Zero, -1);
89 
90  vector v(start - origin_);
91 
92  // Decompose sample-origin into normal and parallel component
93  scalar parallel = (v & normal_);
94 
95  if (sign(parallel) == sign((end - origin_) & normal_))
96  {
97  return;
98  }
99 
100  // Remove the parallel component and normalise
101  v -= parallel*normal_;
102  scalar magV = mag(v);
103 
104  if (magV < rootVSmall)
105  {
106  v = Zero;
107  }
108  else
109  {
110  v /= magV;
111  }
112 
113  // Set (hit or miss) to intersection of ray and plane of disk
114  info.setPoint(origin_ + magV*v);
115 
116  if (magV <= radius_)
117  {
118  info.setHit();
119  info.setIndex(0);
120  }
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
125 
127 (
128  const IOobject& io,
129  const point& origin,
130  const point& normal,
131  const scalar radius
132 )
133 :
134  searchableSurface(io),
135  origin_(origin),
136  normal_(normal/mag(normal)),
137  radius_(radius)
138 {
139  // Rough approximation of bounding box
140  // vector span(radius_, radius_, radius_);
141 
142  // See searchableCylinder
143  vector span
144  (
145  sqrt(sqr(normal_.y()) + sqr(normal_.z())),
146  sqrt(sqr(normal_.x()) + sqr(normal_.z())),
147  sqrt(sqr(normal_.x()) + sqr(normal_.y()))
148  );
149  span *= radius_;
150 
151  bounds().min() = origin_ - span;
152  bounds().max() = origin_ + span;
153 }
154 
155 
157 (
158  const IOobject& io,
159  const dictionary& dict
160 )
161 :
162  searchableSurface(io),
163  origin_(dict.lookup("origin")),
164  normal_(dict.lookup("normal")),
165  radius_(dict.lookup<scalar>("radius"))
166 {
167  normal_ /= mag(normal_);
168 
169  // Rough approximation of bounding box
170  // vector span(radius_, radius_, radius_);
171 
172  // See searchableCylinder
173  vector span
174  (
175  sqrt(sqr(normal_.y()) + sqr(normal_.z())),
176  sqrt(sqr(normal_.x()) + sqr(normal_.z())),
177  sqrt(sqr(normal_.x()) + sqr(normal_.y()))
178  );
179  span *= radius_;
180 
181  bounds().min() = origin_ - span;
182  bounds().max() = origin_ + span;
183 }
184 
185 
186 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
187 
189 {}
190 
191 
192 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193 
195 {
196  if (regions_.empty())
197  {
198  regions_.setSize(1);
199  regions_[0] = "region0";
200  }
201  return regions_;
202 }
203 
204 
206 (
207  pointField& centres,
208  scalarField& radiusSqr
209 ) const
210 {
211  centres.setSize(1);
212  centres[0] = origin_;
213 
214  radiusSqr.setSize(1);
215  radiusSqr[0] = sqr(radius_);
216 
217  // Add a bit to make sure all points are tested inside
218  radiusSqr += Foam::sqr(small);
219 }
220 
221 
222 void Foam::searchableDisk::findNearest
223 (
224  const pointField& samples,
225  const scalarField& nearestDistSqr,
226  List<pointIndexHit>& info
227 ) const
228 {
229  info.setSize(samples.size());
230 
231  forAll(samples, i)
232  {
233  info[i] = findNearest(samples[i], nearestDistSqr[i]);
234  }
235 }
236 
237 
238 void Foam::searchableDisk::findLine
239 (
240  const pointField& start,
241  const pointField& end,
242  List<pointIndexHit>& info
243 ) const
244 {
245  info.setSize(start.size());
246 
247  forAll(start, i)
248  {
249  findLine(start[i], end[i], info[i]);
250  }
251 }
252 
253 
255 (
256  const pointField& start,
257  const pointField& end,
258  List<pointIndexHit>& info
259 ) const
260 {
261  findLine(start, end, info);
262 }
263 
264 
266 (
267  const pointField& start,
268  const pointField& end,
270 ) const
271 {
272  info.setSize(start.size());
273 
274  forAll(start, i)
275  {
276  pointIndexHit inter;
277  findLine(start[i], end[i], inter);
278 
279  if (inter.hit())
280  {
281  info[i].setSize(1);
282  info[i][0] = inter;
283  }
284  else
285  {
286  info[i].clear();
287  }
288  }
289 }
290 
291 
293 (
294  const List<pointIndexHit>& info,
295  labelList& region
296 ) const
297 {
298  region.setSize(info.size());
299  region = 0;
300 }
301 
302 
304 (
305  const List<pointIndexHit>& info,
306  vectorField& normal
307 ) const
308 {
309  normal.setSize(info.size());
310  normal = normal_;
311 }
312 
313 
315 (
316  const pointField& points,
317  List<volumeType>& volType
318 ) const
319 {
321  << "Volume type not supported for disk."
322  << exit(FatalError);
323 }
324 
325 
326 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
static const Form max
Definition: VectorSpace.H:115
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual void boundingSpheres(pointField &centres, scalarField &radiusSqr) const
Get bounding spheres (centre and radius squared), one per element.
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
virtual void getNormal(const List< pointIndexHit > &, vectorField &normal) const
From a set of points and indices get the normal.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
dimensionedScalar sqrt(const dimensionedScalar &ds)
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:53
PointIndexHit< point > pointIndexHit
Definition: pointIndexHit.H:42
Base class of (analytical or triangulated) surface. Encapsulates all the search routines. WIP.
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Macros for easy insertion into run-time selection tables.
static const Form min
Definition: VectorSpace.H:116
virtual ~searchableDisk()
Destructor.
virtual void getRegion(const List< pointIndexHit > &, labelList &region) const
From a set of points and indices get the region.
virtual void findLineAll(const pointField &start, const pointField &end, List< List< pointIndexHit >> &) const
Get all intersections in order from start to end.
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
bool hit() const
Is there a hit.
virtual void findLineAny(const pointField &start, const pointField &end, List< pointIndexHit > &) const
Return any intersection on segment from start to end.
static const zero Zero
Definition: zero.H:97
searchableDisk(const IOobject &io, const point &origin, const point &normal, const scalar radius)
Construct from components.
dimensioned< scalar > magSqr(const dimensioned< Type > &)
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
virtual const wordList & regions() const
Names of regions.
void setSize(const label)
Reset size of List.
Definition: List.C:281
virtual void getVolumeType(const pointField &, List< volumeType > &) const
Determine type (inside/outside/mixed) for point. unknown if.
vector point
Point is a vector.
Definition: point.H:41
dimensioned< scalar > mag(const dimensioned< Type > &)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844