triSurfaceRegionSearch.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "triSurfaceRegionSearch.H"
30 #include "indexedOctree.H"
31 #include "triSurface.H"
32 #include "PatchTools.H"
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 Foam::triSurfaceRegionSearch::triSurfaceRegionSearch(const triSurface& surface)
37 :
38  triSurfaceSearch(surface),
39  indirectRegionPatches_(),
40  treeByRegion_()
41 {}
42 
43 
44 Foam::triSurfaceRegionSearch::triSurfaceRegionSearch
45 (
46  const triSurface& surface,
47  const dictionary& dict
48 )
49 :
50  triSurfaceSearch(surface, dict),
51  indirectRegionPatches_(),
52  treeByRegion_()
53 {}
54 
55 
56 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
57 
59 {
60  clearOut();
61 }
62 
63 
65 {
67  treeByRegion_.clear();
68 }
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
75 {
76  if (treeByRegion_.empty())
77  {
78  label maxRegion = -1;
79  forAll(surface(), fI)
80  {
81  const label regionI = surface()[fI].region();
82  maxRegion = max(maxRegion, regionI);
83  }
84  const label nRegions = maxRegion+1;
85 
86  labelList nFacesInRegions(nRegions, 0);
87  forAll(surface(), fI)
88  {
89  const label regionI = surface()[fI].region();
90  nFacesInRegions[regionI]++;
91  }
92 
93  indirectRegionPatches_.setSize(nRegions);
94  treeByRegion_.setSize(nRegions);
95 
96  labelListList regionsAddressing(nRegions);
97 
98  forAll(regionsAddressing, regionI)
99  {
100  regionsAddressing[regionI].setSize(nFacesInRegions[regionI]);
101  }
102  nFacesInRegions = Zero;
103  forAll(surface(), fI)
104  {
105  const label regionI = surface()[fI].region();
106  regionsAddressing[regionI][nFacesInRegions[regionI]++] = fI;
107  }
108 
109  forAll(regionsAddressing, regionI)
110  {
111  scalar oldTol = treeType::perturbTol();
112  treeType::perturbTol() = tolerance();
113 
114  indirectRegionPatches_.set
115  (
116  regionI,
118  (
120  (
121  surface(),
122  regionsAddressing[regionI]
123  ),
124  surface().points()
125  )
126  );
127 
128  // Calculate bb without constructing local point numbering.
129  treeBoundBox bb(Zero, Zero);
130 
131  if (indirectRegionPatches_[regionI].size())
132  {
133  label nPoints;
135  (
136  indirectRegionPatches_[regionI],
137  bb,
138  nPoints
139  );
140 
141  // if (nPoints != surface().points().size())
142  // {
143  // WarningInFunction
144  // << "Surface does not have compact point numbering. "
145  // << "Of " << surface().points().size()
146  // << " only " << nPoints
147  // << " are used."
148  // << " This might give problems in some routines."
149  // << endl;
150  // }
151 
152  // Random number generator. Bit dodgy since not exactly
153  // random ;-)
154  Random rndGen(65431);
155 
156  // Slightly extended bb. Slightly off-centred just so
157  // on symmetric geometry there are fewer face/edge
158  // aligned items.
159  bb = bb.extend(rndGen, 1e-4);
160  bb.min() -= point::uniform(ROOTVSMALL);
161  bb.max() += point::uniform(ROOTVSMALL);
162  }
163 
164  treeByRegion_.set
165  (
166  regionI,
167  new treeType
168  (
170  (
171  false, //true,
172  indirectRegionPatches_[regionI],
173  tolerance()
174  ),
175  bb,
176  maxTreeDepth(), // maxLevel
177  10, // leafsize
178  3.0 // duplicity
179  )
180  );
181 
182  treeType::perturbTol() = oldTol;
183  }
184  }
185 
186  return treeByRegion_;
187 }
188 
189 
191 (
192  const pointField& samples,
193  const scalarField& nearestDistSqr,
194  const labelList& regionIndices,
195  List<pointIndexHit>& info
196 ) const
197 {
198  if (regionIndices.empty())
199  {
200  triSurfaceSearch::findNearest(samples, nearestDistSqr, info);
201  }
202  else
203  {
204  scalar oldTol = treeType::perturbTol();
205  treeType::perturbTol() = tolerance();
206 
207  const PtrList<treeType>& octrees = treeByRegion();
208 
209  info.setSize(samples.size());
210 
211  forAll(octrees, treeI)
212  {
213  if (!regionIndices.found(treeI))
214  {
215  continue;
216  }
217 
218  const treeType& octree = octrees[treeI];
219  const treeDataIndirectTriSurface::findNearestOp nearOp(octree);
220 
221  forAll(samples, i)
222  {
223 // if (!octree.bb().contains(samples[i]))
224 // {
225 // continue;
226 // }
227 
228  pointIndexHit currentRegionHit = octree.findNearest
229  (
230  samples[i],
231  nearestDistSqr[i],
232  nearOp
233  );
234 
235  if
236  (
237  currentRegionHit.hit()
238  &&
239  (
240  !info[i].hit()
241  ||
242  (
243  magSqr(currentRegionHit.hitPoint() - samples[i])
244  < magSqr(info[i].hitPoint() - samples[i])
245  )
246  )
247  )
248  {
249  info[i] = currentRegionHit;
250  }
251  }
252  }
253 
254  treeType::perturbTol() = oldTol;
255  }
256 }
257 
258 
259 // ************************************************************************* //
Foam::Random
Random number generator.
Definition: Random.H:59
Foam::treeBoundBox::extend
treeBoundBox extend(Random &rndGen, const scalar s) const
Return slightly wider bounding box.
Definition: treeBoundBoxI.H:325
Foam::triSurfaceRegionSearch::treeByRegion
const PtrList< treeType > & treeByRegion() const
Demand driven construction of octree for each region.
Definition: triSurfaceRegionSearch.C:74
Foam::VectorSpace< Vector< Cmpt >, Cmpt, 3 >::uniform
static Vector< Cmpt > uniform(const Cmpt &s)
Return a VectorSpace with all elements = s.
Definition: VectorSpaceI.H:164
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
PatchTools.H
Foam::treeDataPrimitivePatch::findNearestOp
Definition: treeDataPrimitivePatch.H:91
Foam::triSurfaceRegionSearch::findNearest
void findNearest(const pointField &samples, const scalarField &nearestDistSqr, const labelList &regionIndices, List< pointIndexHit > &info) const
Find the nearest point on the surface out of the regions.
Definition: triSurfaceRegionSearch.C:191
Foam::treeBoundBox
Standard boundBox with extra functionality for use in octree.
Definition: treeBoundBox.H:86
indexedOctree.H
Foam::triSurfaceRegionSearch::clearOut
void clearOut()
Clear storage.
Definition: triSurfaceRegionSearch.C:64
Foam::triSurfaceSearch::findNearest
void findNearest(const pointField &samples, const scalarField &nearestDistSqr, List< pointIndexHit > &info) const
Definition: triSurfaceSearch.C:290
Foam::triSurfaceRegionSearch::~triSurfaceRegionSearch
~triSurfaceRegionSearch()
Destructor.
Definition: triSurfaceRegionSearch.C:58
Foam::PatchTools::calcBounds
static void calcBounds(const PrimitivePatch< FaceList, PointField > &p, boundBox &bb, label &nPoints)
Definition: PatchToolsSearch.C:178
Foam::PointIndexHit::hitPoint
const point_type & hitPoint() const
Return hit point. Fatal if not hit.
Definition: PointIndexHit.H:154
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:97
triSurface.H
Foam::triSurfaceSearch
Helper class to search on triSurface.
Definition: triSurfaceSearch.H:58
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:91
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::PointIndexHit
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:52
Foam::PointIndexHit::hit
bool hit() const noexcept
Is there a hit?
Definition: PointIndexHit.H:130
Foam::Field< vector >
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:76
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::IndirectList
A List with indirect addressing.
Definition: IndirectList.H:56
Foam::indexedOctree
Non-pointer based hierarchical recursive searching.
Definition: treeDataEdge.H:50
samples
scalarField samples(nIntervals, Zero)
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::triSurfaceSearch::clearOut
void clearOut()
Clear storage.
Definition: triSurfaceSearch.C:197
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::treeDataPrimitivePatch
Encapsulation of data needed to search on PrimitivePatches.
Definition: treeDataPrimitivePatch.H:63
Foam::List< label >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
rndGen
Random rndGen
Definition: createFields.H:23
triSurfaceRegionSearch.H
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:79