findCellsIntersectingSurface.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
29 #include "polyMeshGen.H"
30 #include "polyMeshGenAddressing.H"
31 #include "triSurf.H"
32 #include "boundBox.H"
33 #include "helperFunctions.H"
34 #include "meshOctree.H"
35 #include "meshOctreeCreator.H"
36 #include "HashSet.H"
37 
38 # ifdef USE_OMP
39 #include <omp.h>
40 # endif
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
50 {
51  octreePtr_ = new meshOctree(surf);
52 
54 }
55 
57 {
58  const pointFieldPMG& points = mesh_.points();
59  const faceListPMG& faces = mesh_.faces();
60  const cellListPMG& cells = mesh_.cells();
61  const labelList& owner = mesh_.owner();
62 
63  const vectorField& faceCentres = mesh_.addressingData().faceCentres();
64  const vectorField& cellCentres = mesh_.addressingData().cellCentres();
65 
66  meshOctreeModifier octreeModifier(*octreePtr_);
67  const LongList<meshOctreeCube*>& leaves = octreeModifier.leavesAccess();
68 
71 
72  const triSurf& surf = octreePtr_->surface();
73  const VRWGraph& pointFacets = surf.pointFacets();
74  const pointField& sp = surf.points();
75 
76  # ifdef USE_OMP
77  # pragma omp parallel for schedule(dynamic, 40)
78  # endif
79  forAll(cells, cellI)
80  {
81  bool intersected(false);
82 
83  const cell& c = cells[cellI];
84 
85  //- find the bounding box of the cell
86  boundBox bb(cellCentres[cellI], cellCentres[cellI]);
87 
88  forAll(c, fI)
89  {
90  const face& f = faces[c[fI]];
91 
92  forAll(f, pI)
93  {
94  bb.min() = Foam::min(bb.min(), points[f[pI]]);
95  bb.max() = Foam::max(bb.max(), points[f[pI]]);
96  }
97  }
98 
99  const vector spanCorr = 0.01 * bb.span();
100  bb.max() += spanCorr;
101  bb.min() -= spanCorr;
102 
103  //- find surface triangles within the bounding box
104  DynList<label> leavesInBox;
105  octreePtr_->findLeavesContainedInBox(bb, leavesInBox);
106  labelHashSet triangles;
107  forAll(leavesInBox, boxI)
108  {
109  const meshOctreeCube& oc = *leaves[leavesInBox[boxI]];
110 
111  if( oc.hasContainedElements() )
112  {
113  const meshOctreeSlot& slot = *oc.slotPtr();
114  const label ceI = oc.containedElements();
115 
116  forAllRow(slot.containedTriangles_, ceI, tI)
117  triangles.insert(slot.containedTriangles_(ceI, tI));
118  }
119  }
120 
121  //- remove triangles which do not intersect the bounding box
122  labelHashSet reasonableCandidates;
123 
124  forAllConstIter(labelHashSet, triangles, tIter)
125  {
126  const labelledTri& tri = surf[tIter.key()];
127 
128  boundBox obb(sp[tri[0]], sp[tri[0]]);
129  for(label i=1;i<3;++i)
130  {
131  const point& v = sp[tri[i]];
132  obb.min() = Foam::min(obb.min(), v);
133  obb.max() = Foam::max(obb.max(), v);
134  }
135 
136  const vector spanTriCorr = SMALL * obb.span();
137  obb.min() -= spanTriCorr;
138  obb.max() += spanTriCorr;
139 
140  if( obb.overlaps(bb) )
141  reasonableCandidates.insert(tIter.key());
142  }
143 
144  triangles.transfer(reasonableCandidates);
145 
146  //- check if any of the surface vertices is contained within the cell
147  labelHashSet nodes, facetsInCell;
148  forAllConstIter(labelHashSet, triangles, tIter)
149  {
150  const labelledTri& tri = surf[tIter.key()];
151 
152  forAll(tri, i)
153  nodes.insert(tri[i]);
154  }
155 
156  //- check which surface nodes are within the cell
157  forAllConstIter(labelHashSet, nodes, nIter)
158  {
159  const point& p = sp[nIter.key()];
160 
161  if( !bb.contains(p) )
162  continue;
163 
164  bool foundIntersection(false);
165  forAll(c, fI)
166  {
167  const face& f = faces[c[fI]];
168 
169  if( owner[c[fI]] == cellI )
170  {
171  forAll(f, pI)
172  {
174  (
175  points[f[pI]],
176  points[f.prevLabel(pI)],
177  faceCentres[c[fI]],
178  cellCentres[cellI]
179  );
180 
181  if( help::pointInTetrahedron(p, tet) )
182  {
183  intersected = true;
184  forAllRow(pointFacets, nIter.key(), ptI)
185  {
186  facetsInCell.insert
187  (
188  pointFacets(nIter.key(), ptI)
189  );
190  }
191 
192  foundIntersection = true;
193  break;
194  }
195  }
196 
197  if( foundIntersection )
198  break;
199  }
200  else
201  {
202  forAll(f, pI)
203  {
205  (
206  points[f[pI]],
207  points[f.nextLabel(pI)],
208  faceCentres[c[fI]],
209  cellCentres[cellI]
210  );
211 
212  if( help::pointInTetrahedron(p, tet) )
213  {
214  intersected = true;
215  forAllRow(pointFacets, nIter.key(), ptI)
216  {
217  facetsInCell.insert
218  (
219  pointFacets(nIter.key(), ptI)
220  );
221  }
222 
223  foundIntersection = true;
224  break;
225  }
226  }
227 
228  if( foundIntersection )
229  break;
230  }
231  }
232  }
233 
234  //- check if any triangle in the surface mesh
235  //- intersects any of the cell's faces
236  forAllConstIter(labelHashSet, triangles, tIter)
237  {
238  if( facetsInCell.found(tIter.key()) )
239  continue;
240 
241  forAll(c, fI)
242  {
243  const face& f = faces[c[fI]];
244 
245  const bool intersect =
247  (
248  surf,
249  tIter.key(),
250  f,
251  points
252  );
253 
254  if( intersect )
255  {
256  intersected = true;
257  facetsInCell.insert(tIter.key());
258  break;
259  }
260  }
261  }
262 
263  //- store the results for this cell
264  intersectedCells_[cellI] = intersected;
265  if( intersected )
266  {
267  # ifdef USE_OMP
268  # pragma omp critical
269  # endif
270  {
271  facetsIntersectingCell_.setRow(cellI, facetsInCell.toc());
272  }
273  }
274  }
275 }
276 
277 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
278 
280 (
281  polyMeshGen& mesh,
282  const meshOctree& octree
283 )
284 :
285  mesh_(mesh),
286  octreePtr_(const_cast<meshOctree*>(&octree)),
287  octreeGenerated_(false),
288  intersectedCells_(),
289  facetsIntersectingCell_()
290 {
291  findIntersectedCells();
292 }
293 
295 (
296  polyMeshGen& mesh,
297  const triSurf& surface
298 )
299 :
300  mesh_(mesh),
301  octreePtr_(NULL),
302  octreeGenerated_(true),
303  intersectedCells_(),
304  facetsIntersectingCell_()
305 {
306  generateOctree(surface);
307 
308  findIntersectedCells();
309 }
310 
311 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
312 
314 {
315  if( octreeGenerated_ )
317 }
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
322 {
323  return intersectedCells_;
324 }
325 
327 {
329 }
330 
332 (
333  const word subsetName
334 )
335 {
336  const label setId = mesh_.addCellSubset(subsetName);
337 
338  forAll(intersectedCells_, cellI)
339  if( intersectedCells_[cellI] )
340  mesh_.addCellToSubset(setId, cellI);
341 }
342 
343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344 
345 } // End namespace Foam
346 
347 // ************************************************************************* //
Foam::polyMeshGenFaces::owner
const labelList & owner() const
owner and neighbour cells for faces
Definition: polyMeshGenFacesI.H:67
Foam::findCellsIntersectingSurface::facetsIntersectingCells
const VRWGraph & facetsIntersectingCells() const
return the graph of facets intersecting each cell
Definition: findCellsIntersectingSurface.C:326
Foam::polyMeshGenCells::addressingData
const polyMeshGenAddressing & addressingData() const
addressing which may be needed
Definition: polyMeshGenCells.C:327
Foam::help::doFaceAndTriangleIntersect
bool doFaceAndTriangleIntersect(const triSurf &surface, const label triI, const face &f, const pointField &facePoints)
check if the surface triangle and the face intersect
Definition: helperFunctionsGeometryQueriesI.H:805
triSurf.H
p
p
Definition: pEqn.H:62
Foam::polyMeshGenAddressing::faceCentres
const vectorField & faceCentres() const
Definition: polyMeshGenAddressingCentresAndAreas.C:123
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
Foam::meshOctreeCreator
Definition: meshOctreeCreator.H:57
Foam::findCellsIntersectingSurface::octreeGenerated_
const bool octreeGenerated_
check whether the octree was generated or not
Definition: findCellsIntersectingSurface.H:63
Foam::HashTable::toc
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:201
Foam::VRWGraph::setRow
void setRow(const label rowI, const ListType &l)
Set row with the list.
Definition: VRWGraphI.H:354
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::findCellsIntersectingSurface::generateOctree
void generateOctree(const triSurf &)
generate the octree
Definition: findCellsIntersectingSurface.C:49
Foam::HashTable::transfer
void transfer(HashTable< T, Key, Hash > &)
Transfer the contents of the argument table into this table.
Definition: HashTable.C:518
Foam::findCellsIntersectingSurface::intersectedCells_
boolList intersectedCells_
stores information about intersected cells
Definition: findCellsIntersectingSurface.H:66
Foam::polyMeshGenAddressing::cellCentres
const vectorField & cellCentres() const
Definition: polyMeshGenAddressingCentresAndVols.C:125
Foam::triSurfPoints::points
const pointField & points() const
access to points
Definition: triSurfPointsI.H:44
findCellsIntersectingSurface.H
Foam::meshOctreeCube::hasContainedElements
bool hasContainedElements() const
return true if the box contains some triangles
Definition: meshOctreeCubeI.H:81
Foam::polyMeshGen
Definition: polyMeshGen.H:46
Foam::polyMeshGenPoints::points
const pointFieldPMG & points() const
access to points
Definition: polyMeshGenPointsI.H:44
Foam::findCellsIntersectingSurface::mesh_
polyMeshGen & mesh_
Reference to the mesh.
Definition: findCellsIntersectingSurface.H:57
Foam::cellListPMG
Definition: cellListPMG.H:49
Foam::HashSet< label, Hash< label > >
Foam::meshOctree::findLeavesContainedInBox
void findLeavesContainedInBox(const boundBox &, DynList< const meshOctreeCube *, 256 > &) const
find leaves contained in the box
Definition: meshOctreeInsideCalculations.C:131
meshOctree.H
Foam::meshOctreeModifier
Definition: meshOctreeModifier.H:48
Foam::polyMeshGenFaces::faces
const faceListPMG & faces() const
access to faces
Definition: polyMeshGenFacesI.H:43
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::meshOctreeSlot
Definition: meshOctreeSlot.H:50
Foam::boundBox::span
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:84
Foam::LongList
Definition: LongList.H:55
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::boundBox::overlaps
bool overlaps(const boundBox &) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:120
polyMeshGen.H
Foam::polyMeshGenCells::cells
const cellListPMG & cells() const
access to cells
Definition: polyMeshGenCellsI.H:39
Foam::meshOctreeCreator::createOctreeWithRefinedBoundary
void createOctreeWithRefinedBoundary(const direction maxLevel, const label nTrianglesInLeaf=15)
Definition: meshOctreeCreatorCreateOctreeBoxes.C:498
Foam::VRWGraph::setSize
void setSize(const label)
Reset the number of rows.
Definition: VRWGraphI.H:132
forAllRow
#define forAllRow(graph, rowI, index)
Definition: VRWGraph.H:277
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
Foam::triSurfAddressing::pointFacets
const VRWGraph & pointFacets() const
return point-facets addressing
Definition: triSurfAddressingI.H:43
intersect
Raster intersect(const Raster &rast1, const Raster &rast2)
Definition: Raster.C:666
Foam::meshOctreeCube::slotPtr
const meshOctreeSlot * slotPtr() const
return the pointer to the slot containing the cube
Definition: meshOctreeCubeI.H:58
HashSet.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::HashTable::found
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:109
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynList< label >
Foam::help::pointInTetrahedron
bool pointInTetrahedron(const point &p, const tetrahedron< point, point > &tet)
check if a vertex lies inside the tetrahedron
Definition: helperFunctionsGeometryQueriesI.H:365
Foam::findCellsIntersectingSurface::findCellsIntersectingSurface
findCellsIntersectingSurface(polyMeshGen &mesh, const meshOctree &octree)
Construct from mesh and octree.
Definition: findCellsIntersectingSurface.C:280
Foam::findCellsIntersectingSurface::~findCellsIntersectingSurface
~findCellsIntersectingSurface()
Definition: findCellsIntersectingSurface.C:313
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
boundBox.H
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::findCellsIntersectingSurface::octreePtr_
meshOctree * octreePtr_
Pointer to the octree.
Definition: findCellsIntersectingSurface.H:60
Foam::meshOctreeSlot::containedTriangles_
VRWGraph containedTriangles_
surface triangles contained in an octree cube
Definition: meshOctreeSlot.H:62
Foam::findCellsIntersectingSurface::intersectedCells
const boolList & intersectedCells() const
return the list of intersected cells;
Definition: findCellsIntersectingSurface.C:321
helperFunctions.H
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::boundBox::contains
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:170
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::findCellsIntersectingSurface::facetsIntersectingCell_
VRWGraph facetsIntersectingCell_
stores information which surface facets intersect each cell
Definition: findCellsIntersectingSurface.H:69
Foam::meshOctree
Definition: meshOctree.H:55
Foam::meshOctreeCube
Definition: meshOctreeCube.H:56
Foam::findCellsIntersectingSurface::addIntersectedCellsToSubset
void addIntersectedCellsToSubset(const word subsetName="intersectedCells")
create a cell subset containing intersected cells
Definition: findCellsIntersectingSurface.C:332
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::surface
Definition: surface.H:55
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
Foam::boundBox
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:55
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
Foam::meshOctreeCube::containedElements
label containedElements() const
Definition: meshOctreeCubeI.H:89
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
meshOctreeCreator.H
Foam::meshOctree::surface
const triSurf & surface() const
return a reference to the surface
Definition: meshOctreeI.H:130
Foam::faceListPMG
Definition: faceListPMG.H:50
Foam::pointFieldPMG
Definition: pointFieldPMG.H:50
Foam::VRWGraph
Definition: VRWGraph.H:101
Foam::triSurf
Definition: triSurf.H:59
polyMeshGenAddressing.H
Foam::findCellsIntersectingSurface::findIntersectedCells
void findIntersectedCells()
check for the intersected cells
Definition: findCellsIntersectingSurface.C:56
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::cell
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:56
Foam::meshOctreeModifier::leavesAccess
LongList< meshOctreeCube * > & leavesAccess()
return leaves
Definition: meshOctreeModifierI.H:95
Foam::tetrahedron
A tetrahedron primitive.
Definition: tetrahedron.H:62