triSurfaceStitch.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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurface.H"
29 #include "mergePoints.H"
30 #include "bitSet.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 bool Foam::triSurface::stitchTriangles
35 (
36  const scalar tol,
37  bool verbose
38 )
39 {
40  pointField& ps = storedPoints();
41 
42  // Merge points
43  labelList pointMap;
44  pointField newPoints;
45  bool hasMerged = mergePoints(ps, tol, verbose, pointMap, newPoints);
46 
47  if (hasMerged)
48  {
49  if (verbose)
50  {
51  Pout<< "stitchTriangles : Merged from " << ps.size()
52  << " points down to " << newPoints.size() << endl;
53  }
54 
55  // Set the coordinates to the merged ones
56  ps.transfer(newPoints);
57 
58  // Reset the triangle point labels to the unique points array
59  label newTriangleI = 0;
60  forAll(*this, i)
61  {
62  const labelledTri& tri = operator[](i);
63  labelledTri newTri
64  (
65  pointMap[tri[0]],
66  pointMap[tri[1]],
67  pointMap[tri[2]],
68  tri.region()
69  );
70 
71  if
72  (
73  (newTri[0] != newTri[1])
74  && (newTri[0] != newTri[2])
75  && (newTri[1] != newTri[2])
76  )
77  {
78  operator[](newTriangleI++) = newTri;
79  }
80  else if (verbose)
81  {
82  Pout<< "stitchTriangles : "
83  << "Removing triangle " << i
84  << " with non-unique vertices." << nl
85  << " vertices :" << newTri << nl
86  << " coordinates:" << newTri.points(ps)
87  << endl;
88  }
89  }
90 
91  if (newTriangleI != size())
92  {
93  if (verbose)
94  {
95  Pout<< "stitchTriangles : "
96  << "Removed " << size() - newTriangleI
97  << " triangles" << endl;
98  }
99  setSize(newTriangleI);
100 
101  // And possibly compact out any unused points (since used only
102  // by triangles that have just been deleted)
103  // Done in two passes to save memory (pointField)
104 
105  // 1. Detect only
106  bitSet pointIsUsed(ps.size());
107 
108  label nPoints = 0;
109 
110  for (const labelledTri& f : *this)
111  {
112  for (const label pointi : f)
113  {
114  if (pointIsUsed.set(pointi))
115  {
116  ++nPoints;
117  }
118  }
119  }
120 
121  if (nPoints != ps.size())
122  {
123  // 2. Compact.
124  pointMap.setSize(ps.size());
125  label newPointi = 0;
126  forAll(pointIsUsed, pointi)
127  {
128  if (pointIsUsed[pointi])
129  {
130  ps[newPointi] = ps[pointi];
131  pointMap[pointi] = newPointi++;
132  }
133  }
134  ps.setSize(newPointi);
135 
136  newTriangleI = 0;
137  for (const labelledTri& f : *this)
138  {
139  operator[](newTriangleI++) = labelledTri
140  (
141  pointMap[f[0]],
142  pointMap[f[1]],
143  pointMap[f[2]],
144  f.region()
145  );
146  }
147  }
148  }
149  }
150 
151  return hasMerged;
152 }
153 
154 
155 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
triSurface.H
bitSet.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::List< labelledTri >::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::triSurface::storedPoints
pointField & storedPoints()
Non-const access to global points.
Definition: triSurface.H:189
newPointi
label newPointi
Definition: readKivaGrid.H:496
Foam::PrimitivePatch<::Foam::List< labelledTri >, pointField >::nPoints
label nPoints() const
Number of points supporting patch faces.
Definition: PrimitivePatch.H:316
Foam::mergePoints
label mergePoints(const PointList &points, const scalar mergeTol, const bool verbose, labelList &pointMap, typename PointList::const_reference origin=PointList::value_type::zero)
Sorts and merges points. All points closer than/equal mergeTol get merged.
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
mergePoints.H
Merge points. See below.