triSurfaceImportSurfaceAsSubset.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 "meshOctree.H"
30 #include "meshOctreeCreator.H"
31 #include "helperFunctions.H"
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
41 (
42  const triSurf& /*surf*/,
43  meshOctree& /*octree*/
44 )
45 {
46 }
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
51 :
52  surf_(surface),
53  octreePtr_(NULL)
54 {}
55 
57 {
59 }
60 
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 
64 (
65  const triSurf& importSurf,
66  const word& subsetName,
67  const scalar angleTol
68 )
69 {
70  if( !octreePtr_ )
71  {
72  octreePtr_ = new meshOctree(surf_);
74  (
75  direction(20),
76  15
77  );
78  }
79 
80  const pointField& points = surf_.points();
81  const vectorField& fNornals = surf_.facetNormals();
82  const vectorField& fCentres = surf_.facetCentres();
83 
84  labelList nearestTriangle(importSurf.size(), -1);
85 
86  //- check which triangles in the surface fit best to the centres of the
87  //- triangles in the import surface
88  const pointField& importSurfPoints = importSurf.points();
89  const vectorField& importFaceCentres = importSurf.facetCentres();
90  const vectorField& importFaceNormals = importSurf.facetNormals();
91  # ifdef USE_OMP
92  # pragma omp parallel for schedule(dynamic, 40)
93  # endif
94  forAll(nearestTriangle, triI)
95  {
96  point np;
97  scalar dSq;
98  label nt, patch;
99 
100  octreePtr_->findNearestSurfacePoint
101  (
102  np,
103  dSq,
104  nt,
105  patch,
106  importFaceCentres[triI]
107  );
108 
109  //- find the longest edge distance
110  scalar maxEdgeDSq(0.);
111  const labelledTri& tri = importSurf[triI];
112  forAll(tri, pI)
113  {
114  const point& s = importSurfPoints[tri[pI]];
115  const point& e = importSurfPoints[tri[(pI+1)%3]];
116 
117  maxEdgeDSq = max(maxEdgeDSq, magSqr(e - s));
118  }
119 
120  //- check if the triangle has been found
121  if( (nt < 0) || (dSq > 0.09 * maxEdgeDSq) )
122  {
123  Warning << "Could not find a matching triangle " << endl;
124  Warning << "It seems that your surface meshes do not overlap" << endl;
125  continue;
126  }
127 
128  vector nTri = importFaceNormals[triI];
129  const scalar magSqrTri = magSqr(nTri);
130 
131  //- skip sliver triangles
132  if( magSqrTri < VSMALL )
133  continue;
134 
135  vector normal = fNornals[nt];
136  const scalar dSqNormal = magSqr(normal);
137 
138  //- skip sliver triangles
139  if( dSqNormal < VSMALL )
140  continue;
141 
142  if( ((nTri & normal) / (magSqrTri * dSqNormal)) > angleTol )
143  nearestTriangle[triI] = nt;
144  }
145 
146  meshOctree otherSurfOctree(importSurf);
147  meshOctreeCreator(otherSurfOctree).createOctreeWithRefinedBoundary(20, 15);
148 
149  //- search for nearest facets in the import surface
150  DynList<label> containedTriangles;
151  # ifdef USE_OMP
152  # pragma omp parallel for schedule(dynamic, 40) private(containedTriangles)
153  # endif
154  forAll(surf_, triI)
155  {
156  //- find the bounding box and the ize of the triangle
157  boundBox bb(fCentres[triI], fCentres[triI]);
158 
159  scalar maxEdgeDSq(0.);
160  const labelledTri& tri = surf_[triI];
161  forAll(tri, pI)
162  {
163  //- bounding box of the surface triangle
164  bb.min() = min(bb.min(), points[tri[pI]]);
165  bb.max() = max(bb.max(), points[tri[pI]]);
166 
167  const point& s = points[tri[pI]];
168  const point& e = points[tri[(pI+1)%3]];
169 
170  maxEdgeDSq = max(maxEdgeDSq, magSqr(e - s));
171  }
172 
173  //- find the nearest triangle in the surface which shall be imported
174  otherSurfOctree.findTrianglesInBox(bb, containedTriangles);
175 
176  label nt(-1);
177  scalar dSq(VGREAT);
178  forAll(containedTriangles, ctI)
179  {
180  const point p =
182  (
183  containedTriangles[ctI],
184  importSurf,
185  fCentres[triI]
186  );
187 
188  const scalar distSq = magSqr(p - fCentres[triI]);
189 
190  if( distSq < dSq )
191  {
192  nt = containedTriangles[ctI];
193  dSq = distSq;
194  }
195  }
196 
197  //- check if the triangle has been found
198  if( (nt < 0) || (dSq > 0.09 * maxEdgeDSq) )
199  continue;
200 
201  //- skip firther checkes f it has found the same triangle
202  if( nearestTriangle[nt] == triI )
203  continue;
204 
205  vector nTri = fNornals[triI];
206  const scalar magSqrTri = magSqr(nTri);
207 
208  //- skip sliver triangles
209  if( magSqrTri < VSMALL )
210  continue;
211 
212  vector normal = importFaceNormals[nt];
213  const scalar dSqNormal = magSqr(normal);
214 
215  //- skip sliver triangles
216  if( dSqNormal < VSMALL )
217  continue;
218 
219  if( ((nTri & normal) / (magSqrTri * dSqNormal)) > angleTol )
220  nearestTriangle[nt] = triI;
221  }
222 
223  //- create a facet subset in the surface mesh and add the facets into it
224  const label subsetId = surf_.addFacetSubset(subsetName);
225 
226  forAll(nearestTriangle, triI)
227  {
228  if( nearestTriangle[triI] < 0 )
229  continue;
230 
231  surf_.addFacetToSubset(subsetId, nearestTriangle[triI]);
232  }
233 }
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 } // End namespace Foam
238 
239 // ************************************************************************* //
Foam::triSurfAddressing::facetNormals
const vectorField & facetNormals() const
return normals of facets
Definition: triSurfAddressingI.H:170
p
p
Definition: pEqn.H:62
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
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::triSurfPoints::points
const pointField & points() const
access to points
Definition: triSurfPointsI.H:44
Foam::help::nearestPointOnTheTriangle
point nearestPointOnTheTriangle(const triangle< point, point > &tri, const point &)
find the nearest point on the triangle to the given point
Definition: helperFunctionsGeometryQueriesI.H:475
Foam::Warning
messageStream Warning
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::triSurfAddressing::facetCentres
const vectorField & facetCentres() const
return centres of facets
Definition: triSurfAddressingI.H:189
triSurfaceImportSurfaceAsSubset.H
meshOctree.H
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::triSurfaceImportSurfaceAsSubset::octreePtr_
meshOctree * octreePtr_
pointer to meshOctree, needed for searching on the master surface
Definition: triSurfaceImportSurfaceAsSubset.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::meshOctreeCreator::createOctreeWithRefinedBoundary
void createOctreeWithRefinedBoundary(const direction maxLevel, const label nTrianglesInLeaf=15)
Definition: meshOctreeCreatorCreateOctreeBoxes.C:498
Foam::triSurfaceImportSurfaceAsSubset::addSurfaceAsSubset
void addSurfaceAsSubset(const triSurf &importSurf, const word &subsetName, const scalar angleTol=5.*M_PI/180.)
Definition: triSurfaceImportSurfaceAsSubset.C:64
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
Foam::triSurfaceImportSurfaceAsSubset::~triSurfaceImportSurfaceAsSubset
~triSurfaceImportSurfaceAsSubset()
Definition: triSurfaceImportSurfaceAsSubset.C:56
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::meshOctree::findTrianglesInBox
void findTrianglesInBox(const boundBox &, DynList< label > &) const
Definition: meshOctreeInsideCalculations.C:98
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::DynList< label >
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::triSurfaceImportSurfaceAsSubset::triSurfaceImportSurfaceAsSubset
triSurfaceImportSurfaceAsSubset(const triSurfaceImportSurfaceAsSubset &)
Disallow default bitwise copy construct.
helperFunctions.H
Foam::triSurfFacets::size
label size() const
return the number of triangles
Definition: triSurfFacetsI.H:39
Foam::Vector< scalar >
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::meshOctree
Definition: meshOctree.H:55
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::surface
Definition: surface.H:55
Foam::direction
unsigned char direction
Definition: direction.H:43
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
meshOctreeCreator.H
Foam::triSurf
Definition: triSurf.H:59
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::magSqr
dimensioned< scalar > magSqr(const dimensioned< Type > &)
Foam::triSurfaceImportSurfaceAsSubset::createOctree
void createOctree(const triSurf &, meshOctree &)
Definition: triSurfaceImportSurfaceAsSubset.C:41
normal
A normal distribution model.