Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
36Foam::triSurfaceRegionSearch::triSurfaceRegionSearch(const triSurface& surface)
38 triSurfaceSearch(surface),
39 indirectRegionPatches_(),
40 treeByRegion_()
41{}
42
43
44Foam::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 * * * * * * * * * * * * * * * //
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.
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);
180 }
181
182 return treeByRegion_;
183}
184
185
187(
188 const pointField& samples,
189 const scalarField& nearestDistSqr,
190 const labelList& regionIndices,
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 }
248
249 treeType::perturbTol(oldTol);
250 }
251}
252
253
255{
257
258 for (auto& tree : treeByRegion_)
259 {
260 PackedList<2>& nodeTypes = tree.nodeTypes();
261 forAll(nodeTypes, i)
262 {
263 if (nodeTypes[i] == volumeType::INSIDE)
264 {
265 nodeTypes[i] = volumeType::OUTSIDE;
266 }
267 else if (nodeTypes[i] == volumeType::OUTSIDE)
268 {
269 nodeTypes[i] = volumeType::INSIDE;
270 }
271 }
272 }
273}
274
275
276// ************************************************************************* //
A List with indirect addressing.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void setSize(label n)
Alias for resize().
Definition List.H:536
A dynamic list of packed unsigned integers, with the number of bits per item specified by the <Width>...
Definition PackedList.H:146
static void calcBounds(const PrimitivePatch< FaceList, PointField > &p, boundBox &bb, label &nPoints)
bool hit() const noexcept
Is there a hit?
const point_type & point() const noexcept
Return point, no checks.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
Random number generator.
Definition Random.H:56
bool found(const T &val, label pos=0) const
Same as contains().
Definition UList.H:983
bool empty() const noexcept
True if List is empty (ie, size() is zero).
Definition UList.H:701
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static const Form zero
void inflate(const scalar factor)
Expand box by factor*mag(span) in all dimensions.
Definition boundBoxI.H:381
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
static scalar & perturbTol() noexcept
Get the perturbation tolerance.
Standard boundBox with extra functionality for use in octree.
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.
const PtrList< treeType > & treeByRegion() const
Demand driven construction of octree for each region.
Helper class to search on triSurface.
void flip()
Flip orientation (if cached on octree).
scalar tolerance() const
Return tolerance to use in searches.
const triSurface & surface() const
Return reference to the surface.
const indexedOctree< treeDataTriSurface > & tree() const
Demand driven construction of the octree.
label maxTreeDepth() const
Return max tree depth of octree.
void clearOut()
Clear storage.
void findNearest(const pointField &samples, const scalarField &nearestDistSqr, List< pointIndexHit > &info) const
Triangulated surface description with patch information.
Definition triSurface.H:74
@ OUTSIDE
A location outside the volume.
Definition volumeType.H:66
@ INSIDE
A location inside the volume.
Definition volumeType.H:65
const pointField & points
label nPoints
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
List< labelList > labelListList
List of labelList.
Definition labelList.H:38
List< label > labelList
A List of labels.
Definition List.H:62
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
vectorField pointField
pointField is a vectorField.
PointIndexHit< point > pointIndexHit
A PointIndexHit with a 3D point.
dictionary dict
Tree tree(triangles.begin(), triangles.end())
volScalarField & e
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
scalarField samples(nIntervals, Zero)
Random rndGen