FreeFOAM The Cross-Platform CFD Toolkit
stitchTriangles.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 | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM 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 OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "triSurface.H"
27 #include <OpenFOAM/mergePoints.H>
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 
37 bool triSurface::stitchTriangles
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  pointField& ps = storedPoints();
52 
53  // Set the coordinates to the merged ones
54  ps.transfer(newPoints);
55 
56  if (verbose)
57  {
58  Pout<< "stitchTriangles : Merged from " << rawPoints.size()
59  << " points down to " << ps.size() << endl;
60  }
61 
62  // Reset the triangle point labels to the unique points array
63  label newTriangleI = 0;
64  forAll(*this, i)
65  {
66  const labelledTri& tri = operator[](i);
67  labelledTri newTri
68  (
69  pointMap[tri[0]],
70  pointMap[tri[1]],
71  pointMap[tri[2]],
72  tri.region()
73  );
74 
75  if ((newTri[0] != newTri[1]) && (newTri[0] != newTri[2]) && (newTri[1] != newTri[2]))
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(ps)
86  << endl;
87  }
88  }
89 
90  if (newTriangleI != size())
91  {
92  if (verbose)
93  {
94  Pout<< "stitchTriangles : "
95  << "Removed " << size() - newTriangleI
96  << " triangles" << endl;
97  }
98  setSize(newTriangleI);
99 
100  // And possibly compact out any unused points (since used only
101  // by triangles that have just been deleted)
102  // Done in two passes to save memory (pointField)
103 
104  // 1. Detect only
105  PackedBoolList pointIsUsed(ps.size());
106 
107  label nPoints = 0;
108 
109  forAll(*this, i)
110  {
111  const labelledTri& tri = operator[](i);
112 
113  forAll(tri, fp)
114  {
115  label pointI = tri[fp];
116  if (pointIsUsed.set(pointI, 1))
117  {
118  nPoints++;
119  }
120  }
121  }
122 
123  if (nPoints != ps.size())
124  {
125  // 2. Compact.
126  pointMap.setSize(ps.size());
127  label newPointI = 0;
128  forAll(pointIsUsed, pointI)
129  {
130  if (pointIsUsed[pointI])
131  {
132  ps[newPointI] = ps[pointI];
133  pointMap[pointI] = newPointI++;
134  }
135  }
136  ps.setSize(newPointI);
137 
138  newTriangleI = 0;
139  forAll(*this, i)
140  {
141  const labelledTri& tri = operator[](i);
142  operator[](newTriangleI++) = labelledTri
143  (
144  pointMap[tri[0]],
145  pointMap[tri[1]],
146  pointMap[tri[2]],
147  tri.region()
148  );
149  }
150  }
151  }
152  }
153 
154  return hasMerged;
155 }
156 
157 
158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
159 
160 } // End namespace Foam
161 
162 // ************************ vim: set sw=4 sts=4 et: ************************ //