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-2022 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 
30 #include "indexedOctree.H"
31 #include "triSurface.H"
32 #include "PatchTools.H"
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 Foam::triSurfaceRegionSearch::triSurfaceRegionSearch(const triSurface& surface)
37 :
39  indirectRegionPatches_(),
40  treeByRegion_()
41 {}
42 
43 
44 Foam::triSurfaceRegionSearch::triSurfaceRegionSearch
45 (
46  const triSurface& surface,
47  const dictionary& dict
48 )
49 :
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  const scalar oldTol = treeType::perturbTol(tolerance());
112 
113  indirectRegionPatches_.set
114  (
115  regionI,
116  new indirectTriSurface
117  (
119  (
120  surface(),
121  regionsAddressing[regionI]
122  ),
123  surface().points()
124  )
125  );
126 
127  // Calculate bb without constructing local point numbering.
128  treeBoundBox bb(point::zero);
129 
130  if (indirectRegionPatches_[regionI].size())
131  {
132  label nPoints;
134  (
135  indirectRegionPatches_[regionI],
136  bb,
137  nPoints
138  );
139 
140  // if (nPoints != surface().points().size())
141  // {
142  // WarningInFunction
143  // << "Surface does not have compact point numbering. "
144  // << "Of " << surface().points().size()
145  // << " only " << nPoints
146  // << " are used."
147  // << " This might give problems in some routines."
148  // << endl;
149  // }
150 
151  // Random number generator. Bit dodgy since not exactly
152  // random ;-)
153  Random rndGen(65431);
154 
155  // Slightly extended bb. Slightly off-centred just so
156  // on symmetric geometry there are fewer face/edge
157  // aligned items.
158  bb.inflate(rndGen, 1e-4, ROOTVSMALL);
159  }
160 
161  treeByRegion_.set
162  (
163  regionI,
164  new treeType
165  (
166  treeDataIndirectTriSurface
167  (
168  indirectRegionPatches_[regionI],
169  tolerance()
170  ),
171  bb,
172  maxTreeDepth(), // maxLevel
173  10, // leafsize
174  3.0 // duplicity
175  )
176  );
177 
178  treeType::perturbTol(oldTol);
179  }
180  }
181 
182  return treeByRegion_;
183 }
184 
185 
187 (
188  const pointField& samples,
189  const scalarField& nearestDistSqr,
190  const labelList& regionIndices,
191  List<pointIndexHit>& info
192 ) const
193 {
194  if (regionIndices.empty())
195  {
196  triSurfaceSearch::findNearest(samples, nearestDistSqr, info);
197  }
198  else
199  {
200  const scalar oldTol = treeType::perturbTol(tolerance());
201 
202  const PtrList<treeType>& octrees = treeByRegion();
203 
204  info.setSize(samples.size());
205 
206  forAll(octrees, treeI)
207  {
208  if (!regionIndices.found(treeI))
209  {
210  continue;
211  }
212 
213  const treeType& octree = octrees[treeI];
214  const treeDataIndirectTriSurface::findNearestOp nearOp(octree);
215 
216  forAll(samples, i)
217  {
218 // if (!octree.bb().contains(samples[i]))
219 // {
220 // continue;
221 // }
222 
223  pointIndexHit currentRegionHit = octree.findNearest
224  (
225  samples[i],
226  nearestDistSqr[i],
227  nearOp
228  );
229 
230  if
231  (
232  currentRegionHit.hit()
233  &&
234  (
235  !info[i].hit()
236  ||
237  (
238  samples[i].distSqr(currentRegionHit.point())
239  < samples[i].distSqr(info[i].point())
240  )
241  )
242  )
243  {
244  info[i] = currentRegionHit;
245  }
246  }
247  }
248 
249  treeType::perturbTol(oldTol);
250  }
251 }
252 
253 
254 // ************************************************************************* //
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
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.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void inflate(const scalar factor)
Expand box by factor*mag(span) in all dimensions.
Definition: boundBoxI.H:381
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
Random rndGen
Definition: createFields.H:23
scalarField samples(nIntervals, Zero)
PointIndexHit< point > pointIndexHit
A PointIndexHit with a 3D point.
Definition: pointIndexHit.H:58
void findNearest(const pointField &samples, const scalarField &nearestDistSqr, List< pointIndexHit > &info) const
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
Helper class to search on triSurface.
static void calcBounds(const PrimitivePatch< FaceList, PointField > &p, boundBox &bb, label &nPoints)
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
void setSize(const label n)
Alias for resize()
Definition: List.H:316
const pointField & points
label nPoints
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
void clearOut()
Clear storage.
Random number generator.
Definition: Random.H:55
const wordList surface
Standard surface field types (scalar, vector, tensor, etc)
const PtrList< treeType > & treeByRegion() const
Demand driven construction of octree for each region.
vector point
Point is a vector.
Definition: point.H:37
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Standard boundBox with extra functionality for use in octree.
Definition: treeBoundBox.H:90
List< label > labelList
A List of labels.
Definition: List.H:62
Triangulated surface description with patch information.
Definition: triSurface.H:71
A List with indirect addressing.
Definition: IndirectList.H:60
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127