triSurface2DCheck.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 
28 #include "triSurface2DCheck.H"
29 #include "triSurfModifier.H"
30 #include "boundBox.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
40 {
41  const vectorField& fNormals = surf_.facetNormals();
42 
43  //- find the normal vector of the best-fitting plane
45 
46  forAll(fNormals, tI)
47  {
48  vector fn = fNormals[tI];
49  fn /= (mag(fn) + VSMALL);
50 
51  covarianceMatrix_ += symm(fn * fn);
52  }
53 }
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
58 :
59  surf_(surface),
60  covarianceMatrix_(symmTensor::zero)
61 {
63 }
64 
66 {}
67 
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
69 
71 {
72  const pointField& points = surf_.points();
73 
74  const vector eigenVal = eigenValues(covarianceMatrix_);
75 
76  //- the smallest eigenvalue must be zero in case all face normals
77  //- lie in a plane
78  if( mag(eigenVal[0]) > SMALL )
79  {
80  WarningIn("bool triSurface2DCheck::is2DSurface() const")
81  << "Surface mesh is in 3D space!"
82  << " This may result in an invalid mesh!" << endl;
83 
84  return false;
85  }
86 
87  //- calculate the plane normal as a cross prduct of the two
88  //- eigenVectors spanning the plane
89  const vector n
90  (
91  eigenVector(covarianceMatrix_, eigenVal[1]) ^
92  eigenVector(covarianceMatrix_, eigenVal[2])
93  );
94 
95  //- check if the plane is in the x-y plane of the coordinate system
96  if( mag(n.x()) > SMALL || mag(n.y()) > SMALL )
97  {
98  //- this could be a 2D surface, but it is not in the x-y plane
99  WarningIn("bool triSurface2DCheck::is2DSurface() const")
100  << "The surface mesh IS NOT IN THE X-Y PLANE!!!!"
101  << " This will result in a mesh without any cells" << endl;
102 
103  return false;
104  }
105 
106  //- check if the points in the 2D surface have uniform z coordinates
107  boundBox bb(points);
108  forAll(points, pI)
109  {
110  const point& p = points[pI];
111 
112  if
113  (
114  mag(p.z() - bb.max().z()) > SMALL &&
115  mag(p.z() - bb.min().z()) > SMALL
116  )
117  {
118  WarningIn("bool triSurface2DCheck::is2DSurface() const")
119  << "z coordinates of the 2D surface are not uniform" << endl;
120 
121  return false;
122  }
123  }
124 
125  Info << "Detected a 2D surface in the x-y plane" << endl;
126 
127  return true;
128 }
129 
131 {
132  const pointField& points = surf_.points();
133  const vectorField& fNormals = surf_.facetNormals();
134 
135  //- create a subset containing faces having non-zero z coordinate
136  //- of the normals
137  triSurf& surf = const_cast<triSurf&>(surf_);
138  const label badFacetsId = surf.addFacetSubset("badFacets");
139  forAll(fNormals, triI)
140  {
141  vector fn = fNormals[triI];
142  fn /= (mag(fn) + VSMALL);
143 
144  if( mag(fn.z()) > SMALL )
145  surf.addFacetToSubset(badFacetsId, triI);
146  }
147 
148  //- create a subset containing points which are not
149  //- in z-min and z-max planes
150  const label badPointsId = surf.addPointSubset("badPointsId");
151  boundBox bb(points);
152  forAll(points, pI)
153  {
154  const point& p = points[pI];
155 
156  if
157  (
158  mag(p.z() - bb.max().z()) > SMALL &&
159  mag(p.z() - bb.min().z()) > SMALL
160  )
161  {
162  surf.addPointToSubset(badPointsId, pI);
163  }
164  }
165 }
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 } // End namespace Foam
170 
171 // ************************************************************************* //
Foam::symm
dimensionedSymmTensor symm(const dimensionedSymmTensor &dt)
Definition: dimensionedSymmTensor.C:82
Foam::SymmTensor< scalar >
Foam::triSurfAddressing::facetNormals
const vectorField & facetNormals() const
return normals of facets
Definition: triSurfAddressingI.H:170
p
p
Definition: pEqn.H:62
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
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
triSurfModifier.H
Foam::triSurface2DCheck::covarianceMatrix_
symmTensor covarianceMatrix_
covariance matrix
Definition: triSurface2DCheck.H:56
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::triSurfPoints::addPointSubset
label addPointSubset(const word &)
point subsets
Definition: triSurfPoints.C:54
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::triSurfFacets::addFacetToSubset
void addFacetToSubset(const label, const label)
Definition: triSurfFacetsI.H:60
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::triSurfPoints::addPointToSubset
void addPointToSubset(const label, const label)
Definition: triSurfPointsI.H:57
Foam::eigenVector
vector eigenVector(const tensor &, const scalar lambda)
Definition: tensor.C:207
Foam::eigenValues
dimensionedVector eigenValues(const dimensionedTensor &dt)
Definition: dimensionedTensor.C:146
Foam::Info
messageStream Info
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
Foam::triSurfFacets::addFacetSubset
label addFacetSubset(const word &)
point subsets
Definition: triSurfFacets.C:105
Foam::triSurface2DCheck::createCovarianceMatrix
void createCovarianceMatrix()
create covariance matrix
Definition: triSurface2DCheck.C:39
Foam::SymmTensor< scalar >::zero
static const SymmTensor zero
Definition: SymmTensor.H:77
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
triSurface2DCheck.H
boundBox.H
Foam::triSurface2DCheck::triSurface2DCheck
triSurface2DCheck(const triSurface2DCheck &)
Disallow default bitwise copy construct.
Foam::Vector< scalar >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::triSurface2DCheck::is2DSurface
bool is2DSurface() const
checks if the surface is a 2D triangulation
Definition: triSurface2DCheck.C:70
Foam::triSurface2DCheck::createSubsets
void createSubsets()
create subset containing invalid facets
Definition: triSurface2DCheck.C:130
Foam::surface
Definition: surface.H:55
Foam::boundBox
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:55
WarningIn
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Definition: messageStream.H:254
Foam::triSurf
Definition: triSurf.H:59
Foam::triSurface2DCheck::~triSurface2DCheck
~triSurface2DCheck()
Definition: triSurface2DCheck.C:65
Foam::triSurface2DCheck::surf_
const triSurf & surf_
reference to triSurf
Definition: triSurface2DCheck.H:53
Foam::zero
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:47