FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

stitchTriangles.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "triSurface.H"
00027 #include <OpenFOAM/mergePoints.H>
00028 #include <OpenFOAM/PackedBoolList.H>
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00036 
00037 bool triSurface::stitchTriangles
00038 (
00039     const pointField& rawPoints,
00040     const scalar tol,
00041     bool verbose
00042 )
00043 {
00044     // Merge points
00045     labelList pointMap;
00046     pointField newPoints;
00047     bool hasMerged = mergePoints(rawPoints, tol, verbose, pointMap, newPoints);
00048 
00049     if (hasMerged)
00050     {
00051         pointField& ps = storedPoints();
00052 
00053         // Set the coordinates to the merged ones
00054         ps.transfer(newPoints);
00055 
00056         if (verbose)
00057         {
00058             Pout<< "stitchTriangles : Merged from " << rawPoints.size()
00059                 << " points down to " << ps.size() << endl;
00060         }
00061 
00062         // Reset the triangle point labels to the unique points array
00063         label newTriangleI = 0;
00064         forAll(*this, i)
00065         {
00066             const labelledTri& tri = operator[](i);
00067             labelledTri newTri
00068             (
00069                 pointMap[tri[0]],
00070                 pointMap[tri[1]],
00071                 pointMap[tri[2]],
00072                 tri.region()
00073             );
00074 
00075             if ((newTri[0] != newTri[1]) && (newTri[0] != newTri[2]) && (newTri[1] != newTri[2]))
00076             {
00077                 operator[](newTriangleI++) = newTri;
00078             }
00079             else if (verbose)
00080             {
00081                 Pout<< "stitchTriangles : "
00082                     << "Removing triangle " << i
00083                     << " with non-unique vertices." << endl
00084                     << "    vertices   :" << newTri << endl
00085                     << "    coordinates:" << newTri.points(ps)
00086                     << endl;
00087             }
00088         }
00089 
00090         if (newTriangleI != size())
00091         {
00092             if (verbose)
00093             {
00094                 Pout<< "stitchTriangles : "
00095                     << "Removed " << size() - newTriangleI
00096                     << " triangles" << endl;
00097             }
00098             setSize(newTriangleI);
00099 
00100             // And possibly compact out any unused points (since used only
00101             // by triangles that have just been deleted)
00102             // Done in two passes to save memory (pointField)
00103 
00104             // 1. Detect only
00105             PackedBoolList pointIsUsed(ps.size());
00106 
00107             label nPoints = 0;
00108 
00109             forAll(*this, i)
00110             {
00111                 const labelledTri& tri = operator[](i);
00112 
00113                 forAll(tri, fp)
00114                 {
00115                     label pointI = tri[fp];
00116                     if (pointIsUsed.set(pointI, 1))
00117                     {
00118                         nPoints++;
00119                     }
00120                 }
00121             }
00122 
00123             if (nPoints != ps.size())
00124             {
00125                 // 2. Compact.
00126                 pointMap.setSize(ps.size());
00127                 label newPointI = 0;
00128                 forAll(pointIsUsed, pointI)
00129                 {
00130                     if (pointIsUsed[pointI])
00131                     {
00132                         ps[newPointI] = ps[pointI];
00133                         pointMap[pointI] = newPointI++;
00134                     }
00135                 }
00136                 ps.setSize(newPointI);
00137 
00138                 newTriangleI = 0;
00139                 forAll(*this, i)
00140                 {
00141                     const labelledTri& tri = operator[](i);
00142                     operator[](newTriangleI++) = labelledTri
00143                     (
00144                         pointMap[tri[0]],
00145                         pointMap[tri[1]],
00146                         pointMap[tri[2]],
00147                         tri.region()
00148                     );
00149                 }
00150             }
00151         }
00152     }
00153 
00154     return hasMerged;
00155 }
00156 
00157 
00158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00159 
00160 } // End namespace Foam
00161 
00162 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines