triSurfaceSearch.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-2017 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 
29 #include "triSurfaceSearch.H"
30 #include "triSurface.H"
31 #include "PatchTools.H"
32 #include "volumeType.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 bool Foam::triSurfaceSearch::checkUniqueHit
37 (
38  const pointIndexHit& currHit,
39  const UList<pointIndexHit>& hits,
40  const vector& lineVec
41 ) const
42 {
43  // Classify the hit
44  label nearType = -1;
45  label nearLabel = -1;
46 
47  const labelledTri& f = surface()[currHit.index()];
48 
49  f.nearestPointClassify
50  (
51  currHit.hitPoint(),
52  surface().points(),
53  nearType,
54  nearLabel
55  );
56 
57  if (nearType == triPointRef::POINT)
58  {
59  // near point
60 
61  const label nearPointi = f[nearLabel];
62 
63  const labelList& pointFaces =
64  surface().pointFaces()[surface().meshPointMap()[nearPointi]];
65 
66  forAll(pointFaces, pI)
67  {
68  const label pointFacei = pointFaces[pI];
69 
70  if (pointFacei != currHit.index())
71  {
72  forAll(hits, hI)
73  {
74  const pointIndexHit& hit = hits[hI];
75 
76  if (hit.index() == pointFacei)
77  {
78  return false;
79  }
80  }
81  }
82  }
83  }
84  else if (nearType == triPointRef::EDGE)
85  {
86  // near edge
87  // check if the other face of the edge is already hit
88 
89  const labelList& fEdges = surface().faceEdges()[currHit.index()];
90 
91  const label edgeI = fEdges[nearLabel];
92 
93  const labelList& edgeFaces = surface().edgeFaces()[edgeI];
94 
95  forAll(edgeFaces, fI)
96  {
97  const label edgeFacei = edgeFaces[fI];
98 
99  if (edgeFacei != currHit.index())
100  {
101  forAll(hits, hI)
102  {
103  const pointIndexHit& hit = hits[hI];
104 
105  if (hit.index() == edgeFacei)
106  {
107  // Check normals
108  const vector currHitNormal =
109  surface().faceNormals()[currHit.index()];
110 
111  const vector existingHitNormal =
112  surface().faceNormals()[edgeFacei];
113 
114  const label signCurrHit =
115  pos0(currHitNormal & lineVec);
116 
117  const label signExistingHit =
118  pos0(existingHitNormal & lineVec);
119 
120  if (signCurrHit == signExistingHit)
121  {
122  return false;
123  }
124  }
125  }
126  }
127  }
128  }
130  return true;
131 }
132 
133 
134 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
135 
136 Foam::triSurfaceSearch::triSurfaceSearch(const triSurface& surface)
137 :
138  surface_(surface),
139  tolerance_(indexedOctree<treeDataTriSurface>::perturbTol()),
140  maxTreeDepth_(10),
141  treePtr_(nullptr)
142 {}
143 
144 
145 Foam::triSurfaceSearch::triSurfaceSearch
146 (
147  const triSurface& surface,
148  const dictionary& dict
149 )
150 :
151  surface_(surface),
152  tolerance_(indexedOctree<treeDataTriSurface>::perturbTol()),
153  maxTreeDepth_(10),
154  treePtr_(nullptr)
155 {
156  // Have optional non-standard search tolerance for gappy surfaces.
157  if (dict.readIfPresent("tolerance", tolerance_) && tolerance_ > 0)
158  {
159  Info<< " using intersection tolerance " << tolerance_ << endl;
160  }
161 
162  // Have optional non-standard tree-depth to limit storage.
163  if (dict.readIfPresent("maxTreeDepth", maxTreeDepth_) && maxTreeDepth_ > 0)
164  {
165  Info<< " using maximum tree depth " << maxTreeDepth_ << endl;
166  }
167 }
168 
169 
170 Foam::triSurfaceSearch::triSurfaceSearch
171 (
172  const triSurface& surface,
173  const scalar tolerance,
174  const label maxTreeDepth
175 )
176 :
177  surface_(surface),
178  tolerance_(tolerance),
179  maxTreeDepth_(maxTreeDepth),
180  treePtr_(nullptr)
181 {
182  if (tolerance_ < 0)
183  {
185  }
186 }
187 
188 
189 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
192 {
193  clearOut();
194 }
195 
196 
198 {
199  treePtr_.clear();
200 }
201 
202 
203 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
204 
207 {
208  if (!treePtr_)
209  {
210  // Calculate bb without constructing local point numbering.
211  treeBoundBox bb(point::zero);
212 
213  if (surface().size())
214  {
215  label nPoints;
217 
218  if (nPoints != surface().points().size())
219  {
221  << "Surface does not have compact point numbering."
222  << " Of " << surface().points().size()
223  << " only " << nPoints
224  << " are used. This might give problems in some routines."
225  << endl;
226  }
227 
228  // Random number generator. Bit dodgy since not exactly random ;-)
229  Random rndGen(65431);
230 
231  // Slightly extended bb. Slightly off-centred just so on symmetric
232  // geometry there are less face/edge aligned items.
233  bb.inflate(rndGen, 1e-4, ROOTVSMALL);
234  }
235 
236  const scalar oldTol =
238 
239  treePtr_.reset
240  (
242  (
243  treeDataTriSurface(surface_, tolerance_),
244  bb,
245  maxTreeDepth_, // maxLevel
246  10, // leafsize
247  3.0 // duplicity
248  )
249  );
250 
252  }
254  return *treePtr_;
255 }
256 
257 
258 // Determine inside/outside for samples
260 (
261  const pointField& samples
262 ) const
263 {
264  boolList inside(samples.size());
265 
266  forAll(samples, sampleI)
267  {
268  const point& sample = samples[sampleI];
269 
270  if (!tree().bb().contains(sample))
271  {
272  inside[sampleI] = false;
273  }
274  else if (tree().getVolumeType(sample) == volumeType::INSIDE)
275  {
276  inside[sampleI] = true;
277  }
278  else
279  {
280  inside[sampleI] = false;
281  }
282  }
283  return inside;
284 }
285 
286 
288 (
289  const pointField& samples,
290  const scalarField& nearestDistSqr,
291  List<pointIndexHit>& info
292 ) const
293 {
294  const scalar oldTol =
296 
297  const indexedOctree<treeDataTriSurface>& octree = tree();
298 
299  const treeDataTriSurface::findNearestOp fOp(octree);
300 
301  info.setSize(samples.size());
302 
303  forAll(samples, i)
304  {
305  info[i] = octree.findNearest
306  (
307  samples[i],
308  nearestDistSqr[i],
309  fOp
310  );
311  }
312 
314 }
315 
316 
318 (
319  const point& pt,
320  const vector& span
321 )
322 const
323 {
324  const scalar nearestDistSqr = 0.25*magSqr(span);
325 
326  return tree().findNearest(pt, nearestDistSqr);
327 }
328 
329 
331 (
332  const pointField& start,
333  const pointField& end,
334  List<pointIndexHit>& info
335 ) const
336 {
337  const indexedOctree<treeDataTriSurface>& octree = tree();
338 
339  info.setSize(start.size());
340 
341  const scalar oldTol =
343 
344  forAll(start, i)
345  {
346  info[i] = octree.findLine(start[i], end[i]);
347  }
348 
350 }
351 
352 
354 (
355  const pointField& start,
356  const pointField& end,
357  List<pointIndexHit>& info
358 ) const
359 {
360  const indexedOctree<treeDataTriSurface>& octree = tree();
361 
362  info.setSize(start.size());
363 
364  const scalar oldTol =
366 
367  forAll(start, i)
368  {
369  info[i] = octree.findLineAny(start[i], end[i]);
370  }
371 
373 }
374 
375 
377 (
378  const pointField& start,
379  const pointField& end,
380  List<List<pointIndexHit>>& info
381 ) const
382 {
383  const indexedOctree<treeDataTriSurface>& octree = tree();
384 
385  info.setSize(start.size());
386 
387  const scalar oldTol =
389 
390  // Work array
391  DynamicList<pointIndexHit> hits;
392 
393  DynamicList<label> shapeMask;
394 
395  treeDataTriSurface::findAllIntersectOp allIntersectOp(octree, shapeMask);
396 
397  forAll(start, pointi)
398  {
399  hits.clear();
400  shapeMask.clear();
401 
402  while (true)
403  {
404  // See if any intersection between pt and end
405  pointIndexHit inter = octree.findLine
406  (
407  start[pointi],
408  end[pointi],
409  allIntersectOp
410  );
411 
412  if (inter.hit())
413  {
414  const vector lineVec = normalised(end[pointi] - start[pointi]);
415 
416  if
417  (
418  checkUniqueHit
419  (
420  inter,
421  hits,
422  lineVec
423  )
424  )
425  {
426  hits.append(inter);
427  }
428 
429  shapeMask.append(inter.index());
430  }
431  else
432  {
433  break;
434  }
435  }
436 
437  info[pointi].transfer(hits);
438  }
439 
441 }
442 
443 
444 // ************************************************************************* //
static scalar & perturbTol() noexcept
Get the perturbation tolerance.
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
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
Random rndGen
Definition: createFields.H:23
scalarField samples(nIntervals, Zero)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
PointIndexHit< point > pointIndexHit
A PointIndexHit with a 3D point.
Definition: pointIndexHit.H:58
treeDataPrimitivePatch< triSurface > treeDataTriSurface
This class describes the interaction of an object (often a face) and a point. It carries the info of ...
Definition: pointIndexHit.H:44
quaternion normalised(const quaternion &q)
Return the normalised (unit) quaternion of the given quaternion.
Definition: quaternionI.H:674
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
Close to point.
Definition: triangle.H:240
const Field< point_type > & faceNormals() const
Return face unit normals for patch.
~triSurfaceSearch()
Destructor.
static void calcBounds(const PrimitivePatch< FaceList, PointField > &p, boundBox &bb, label &nPoints)
const Map< label > & meshPointMap() const
Mesh point map.
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
void findLine(const pointField &start, const pointField &end, List< pointIndexHit > &info) const
Close to edge.
Definition: triangle.H:241
const pointField & points
boolList calcInside(const pointField &searchPoints) const
Calculate for each searchPoint inside/outside status.
void findLineAll(const pointField &start, const pointField &end, List< List< pointIndexHit >> &info) const
Calculate all intersections from start to end.
label nPoints
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
void clearOut()
Clear storage.
void findLineAny(const pointField &start, const pointField &end, List< pointIndexHit > &info) const
Tree tree(triangles.begin(), triangles.end())
const labelListList & edgeFaces() const
Return edge-face addressing.
Vector< scalar > vector
Definition: vector.H:57
const Field< point_type > & points() const noexcept
Return reference to global points.
A location inside the volume.
Definition: volumeType.H:65
Random number generator.
Definition: Random.H:55
const wordList surface
Standard surface field types (scalar, vector, tensor, etc)
const labelListList & pointFaces() const
Return point-face addressing.
dimensionedScalar pos0(const dimensionedScalar &ds)
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
Encapsulates data for (indexedOc)tree searches on a triSurface.
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:201
labelList f(nPoints)
const triSurface & surface() const
Return reference to the surface.
vector point
Point is a vector.
Definition: point.H:37
Non-pointer based hierarchical recursive searching.
#define WarningInFunction
Report a warning using Foam::Warning.
Standard boundBox with extra functionality for use in octree.
Definition: treeBoundBox.H:90
messageStream Info
Information stream (stdout output on master, null elsewhere)
const labelListList & faceEdges() const
Return face-edge addressing.
pointIndexHit nearest(const point &pt, const vector &span) const
Calculate nearest point on surface for single searchPoint. Returns.
List< label > labelList
A List of labels.
Definition: List.H:62
Triangulated surface description with patch information.
Definition: triSurface.H:71
const indexedOctree< treeDataTriSurface > & tree() const
Demand driven construction of the octree.
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)