stitchTriangles.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration | Version: 3.2
5  \\ / A nd | Web: http://www.foam-extend.org
6  \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend 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  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "triSurface.H"
27 #include "mergePoints.H"
28 #include "PackedBoolList.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 
38 (
39  const pointField& rawPoints,
40  const scalar tol,
41  bool verbose
42 )
43 {
44  // Merge points
45  labelList pointMap;
46  pointField newPoints;
47  bool hasMerged = mergePoints(rawPoints, tol, verbose, pointMap, newPoints);
48 
49  if (hasMerged)
50  {
51  if (verbose)
52  {
53  Pout<< "stitchTriangles : Merged from " << rawPoints.size()
54  << " points down to " << newPoints.size() << endl;
55  }
56 
57  // Reset the triangle point labels to the unique points array
58  label newTriangleI = 0;
59  forAll(*this, i)
60  {
61  const labelledTri& tri = operator[](i);
62  labelledTri newTri
63  (
64  pointMap[tri[0]],
65  pointMap[tri[1]],
66  pointMap[tri[2]],
67  tri.region()
68  );
69 
70  if
71  (
72  (newTri[0] != newTri[1])
73  && (newTri[0] != newTri[2])
74  && (newTri[1] != newTri[2])
75  )
76  {
77  operator[](newTriangleI++) = newTri;
78  }
79  else if (verbose)
80  {
81  Pout<< "stitchTriangles : "
82  << "Removing triangle " << i
83  << " with non-unique vertices." << endl
84  << " vertices :" << newTri << endl
85  << " coordinates:" << newTri.points(newPoints)
86  << endl;
87  }
88  }
89 
90  // If empty triangles are detected, remove them from the list
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  // 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  PackedBoolList pointIsUsed(newPoints.size());
107 
108  label nPoints = 0;
109 
110  forAll(*this, i)
111  {
112  const labelledTri& tri = operator[](i);
113 
114  forAll(tri, fp)
115  {
116  label pointI = tri[fp];
117  if (pointIsUsed.set(pointI, 1))
118  {
119  nPoints++;
120  }
121  }
122  }
123 
124  if (nPoints != newPoints.size())
125  {
126  // 2. Compact.
127  pointMap.setSize(newPoints.size());
128  label newPointI = 0;
129  forAll(pointIsUsed, pointI)
130  {
131  if (pointIsUsed[pointI])
132  {
133  newPoints[newPointI] = newPoints[pointI];
134  pointMap[pointI] = newPointI++;
135  }
136  }
137  newPoints.setSize(newPointI);
138 
139  newTriangleI = 0;
140  forAll(*this, i)
141  {
142  const labelledTri& tri = operator[](i);
143  operator[](newTriangleI++) = labelledTri
144  (
145  pointMap[tri[0]],
146  pointMap[tri[1]],
147  pointMap[tri[2]],
148  tri.region()
149  );
150  }
151  }
152  }
153 
154  // Set the coordinates to the merged ones
155  storedPoints().transfer(newPoints);
156  }
157 
158  return hasMerged;
159 }
160 
161 
162 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
163 
164 } // End namespace Foam
165 
166 // ************************************************************************* //
setSize
points setSize(newPointi)
Foam::PackedBoolList
A bit-packed bool list.
Definition: PackedBoolList.H:63
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::PackedBoolList::set
void set(const PackedList< 1 > &)
Set specified bits.
Definition: PackedBoolList.C:169
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::triFace::points
pointField points(const pointField &) const
Return the points corresponding to this face.
Definition: triFaceI.H:128
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
PackedBoolList.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
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::triSurface::stitchTriangles
bool stitchTriangles(const pointField &rawPoints, const scalar tol=SMALL, const bool verbose=false)
Function to stitch the triangles by removing duplicate points.
Definition: stitchTriangles.C:38
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
mergePoints.H
Merge points. See below.
triSurface.H
Foam::mergePoints
label mergePoints(const UList< Type > &points, const scalar mergeTol, const bool verbose, labelList &pointMap, const Type &origin=Type::zero)
Sorts and merges points. All points closer than/equal mergeTol get merged.