BUGFIX: Illegal use of uninitialised value (backport)
[foam-extend-3.2.git] / src / triSurface / triSurface / stitchTriangles.C
blobf441080a13cc21bf222cfacb3501b6c9b42414cf
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "triSurface.H"
28 #include "mergePoints.H"
29 #include "PackedBoolList.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
38 bool triSurface::stitchTriangles
40     const pointField& rawPoints,
41     const scalar tol,
42     bool verbose
45     // Merge points
46     labelList pointMap;
47     pointField newPoints;
48     bool hasMerged = mergePoints(rawPoints, tol, verbose, pointMap, newPoints);
50     pointField& ps = storedPoints();
52     // Set the coordinates to the merged ones
53     ps.transfer(newPoints);
55     if (hasMerged)
56     {
57         if (verbose)
58         {
59             Pout<< "stitchTriangles : Merged from " << rawPoints.size()
60                 << " points down to " << ps.size() << endl;
61         }
63         // Reset the triangle point labels to the unique points array
64         label newTriangleI = 0;
65         forAll(*this, i)
66         {
67             const labelledTri& tri = operator[](i);
68             labelledTri newTri
69             (
70                 pointMap[tri[0]],
71                 pointMap[tri[1]],
72                 pointMap[tri[2]],
73                 tri.region()
74             );
76             if ((newTri[0] != newTri[1]) && (newTri[0] != newTri[2]) && (newTri[1] != newTri[2]))
77             {
78                 operator[](newTriangleI++) = newTri;
79             }
80             else if (verbose)
81             {
82                 Pout<< "stitchTriangles : "
83                     << "Removing triangle " << i
84                     << " with non-unique vertices." << endl
85                     << "    vertices   :" << newTri << endl
86                     << "    coordinates:" << newTri.points(ps)
87                     << endl;
88             }
89         }
91         if (newTriangleI != size())
92         {
93             if (verbose)
94             {
95                 Pout<< "stitchTriangles : "
96                     << "Removed " << size() - newTriangleI
97                     << " triangles" << endl;
98             }
99             setSize(newTriangleI);
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)
105             // 1. Detect only
106             PackedBoolList pointIsUsed(ps.size());
108             label nPoints = 0;
110             forAll(*this, i)
111             {
112                 const labelledTri& tri = operator[](i);
114                 forAll(tri, fp)
115                 {
116                     label pointI = tri[fp];
117                     if (pointIsUsed.set(pointI, 1))
118                     {
119                         nPoints++;
120                     }
121                 }
122             }
124             if (nPoints != ps.size())
125             {
126                 // 2. Compact.
127                 pointMap.setSize(ps.size());
128                 label newPointI = 0;
129                 forAll(pointIsUsed, pointI)
130                 {
131                     if (pointIsUsed[pointI])
132                     {
133                         ps[newPointI] = ps[pointI];
134                         pointMap[pointI] = newPointI++;
135                     }
136                 }
137                 ps.setSize(newPointI);
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     }
155     return hasMerged;
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 } // End namespace Foam
163 // ************************************************************************* //