OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

meshTriangulation.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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "meshTriangulation.H"
00028 #include "polyMesh.H"
00029 #include "faceTriangulation.H"
00030 
00031 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00032 
00033 bool Foam::meshTriangulation::isInternalFace
00034 (
00035     const primitiveMesh& mesh,
00036     const boolList& includedCell,
00037     const label faceI
00038 )
00039 {
00040     if (mesh.isInternalFace(faceI))
00041     {
00042         label own = mesh.faceOwner()[faceI];
00043         label nei = mesh.faceNeighbour()[faceI];
00044 
00045         if (includedCell[own] && includedCell[nei])
00046         {
00047             // Neighbouring cell will get included in subset
00048             // as well so face is internal.
00049             return true;
00050         }
00051         else
00052         {
00053             return false;
00054         }
00055     }
00056     else
00057     {
00058         return false;
00059     }
00060 }
00061 
00062 
00063 void Foam::meshTriangulation::getFaces
00064 (
00065     const primitiveMesh& mesh,
00066     const boolList& includedCell,
00067     boolList& faceIsCut,
00068     label& nFaces,
00069     label& nInternalFaces
00070 )
00071 {    
00072     // All faces to be triangulated.     
00073     faceIsCut.setSize(mesh.nFaces());
00074     faceIsCut = false;
00075 
00076     nFaces = 0;
00077     nInternalFaces = 0;
00078     
00079     forAll(includedCell, cellI)
00080     {
00081         // Include faces of cut cells only.
00082         if (includedCell[cellI])
00083         {
00084             const labelList& cFaces = mesh.cells()[cellI];
00085 
00086             forAll(cFaces, i)
00087             {
00088                 label faceI = cFaces[i];
00089 
00090                 if (!faceIsCut[faceI])
00091                 {
00092                     // First visit of face.
00093                     nFaces++;
00094                     faceIsCut[faceI] = true;
00095 
00096                     // See if would become internal or external face
00097                     if (isInternalFace(mesh, includedCell, faceI))
00098                     {
00099                         nInternalFaces++;
00100                     }
00101                 }
00102             }
00103         }
00104     }
00105 
00106     Pout<< "Subset consists of " << nFaces << " faces out of " << mesh.nFaces()
00107         << " of which " << nInternalFaces << " are internal" << endl;
00108 }
00109 
00110 
00111 void Foam::meshTriangulation::insertTriangles
00112 (
00113     const triFaceList& faceTris,
00114     const label faceI,
00115     const label regionI,
00116     const bool reverse,
00117 
00118     List<labelledTri>& triangles,
00119     label& triI
00120 )
00121 {
00122     // Copy triangles. Optionally reverse them
00123     forAll(faceTris, i)
00124     {
00125         const triFace& f = faceTris[i];
00126 
00127         labelledTri& tri = triangles[triI];
00128 
00129         if (reverse)
00130         {
00131             tri[0] = f[0];
00132             tri[2] = f[1];
00133             tri[1] = f[2];
00134         }
00135         else
00136         {
00137             tri[0] = f[0];
00138             tri[1] = f[1];
00139             tri[2] = f[2];
00140         }
00141 
00142         tri.region() = regionI;
00143 
00144         faceMap_[triI] = faceI;
00145 
00146         triI++;
00147     }
00148 }
00149 
00150 
00151 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00152 
00153 // Null constructor
00154 Foam::meshTriangulation::meshTriangulation()
00155 :
00156     triSurface(),
00157     nInternalFaces_(0),
00158     faceMap_()
00159 {}
00160 
00161 
00162 // Construct from faces of cells
00163 Foam::meshTriangulation::meshTriangulation
00164 (
00165     const polyMesh& mesh,
00166     const label internalFacesPatch,
00167     const boolList& includedCell,
00168     const bool faceCentreDecomposition
00169 )
00170 :
00171     triSurface(),
00172     nInternalFaces_(0),
00173     faceMap_()
00174 {
00175     const faceList& faces = mesh.faces();
00176     const pointField& points = mesh.points();
00177     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00178 
00179     // All faces to be triangulated.     
00180     boolList faceIsCut;
00181     label nFaces, nInternalFaces;
00182 
00183     getFaces
00184     (
00185         mesh,
00186         includedCell,
00187         faceIsCut,
00188         nFaces,
00189         nInternalFaces
00190     );
00191 
00192 
00193     // Find upper limit for number of triangles
00194     // (can be less if triangulation fails)
00195     label nTotTri = 0;
00196 
00197     if (faceCentreDecomposition)
00198     {
00199         forAll(faceIsCut, faceI)
00200         {
00201             if (faceIsCut[faceI])
00202             {
00203                 nTotTri += faces[faceI].size();
00204             }
00205         }
00206     }
00207     else
00208     {
00209         forAll(faceIsCut, faceI)
00210         {
00211             if (faceIsCut[faceI])
00212             {
00213                 nTotTri += faces[faceI].nTriangles(points);
00214             }
00215         }
00216     }
00217     Pout<< "nTotTri : " << nTotTri << endl;
00218 
00219 
00220     // Storage for new and old points (only for faceCentre decomposition;
00221     // for triangulation uses only existing points)
00222     pointField newPoints;
00223 
00224     if (faceCentreDecomposition)
00225     {
00226         newPoints.setSize(mesh.nPoints() + faces.size());
00227         forAll(mesh.points(), pointI)
00228         {
00229             newPoints[pointI] = mesh.points()[pointI];
00230         }
00231         // Face centres
00232         forAll(faces, faceI)
00233         {
00234             newPoints[mesh.nPoints() + faceI] = mesh.faceCentres()[faceI];
00235         }
00236     }
00237 
00238     // Storage for all triangles
00239     List<labelledTri> triangles(nTotTri);
00240     faceMap_.setSize(nTotTri);
00241     label triI = 0;
00242 
00243 
00244     if (faceCentreDecomposition)
00245     {
00246         // Decomposition around face centre
00247         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00248 
00249         // Triangulate internal faces
00250         forAll(faceIsCut, faceI)
00251         {
00252             if (faceIsCut[faceI] && isInternalFace(mesh, includedCell, faceI))
00253             {
00254                 // Face was internal to the mesh and will be 'internal' to
00255                 // the surface.
00256 
00257                 // Triangulate face
00258                 const face& f = faces[faceI];
00259 
00260                 forAll(f, fp)
00261                 {
00262                     faceMap_[triI] = faceI;
00263 
00264                     triangles[triI++] =
00265                         labelledTri
00266                         (
00267                             f[fp],
00268                             f.nextLabel(fp),
00269                             mesh.nPoints() + faceI,     // face centre
00270                             internalFacesPatch
00271                         );
00272                 }
00273             }
00274         }
00275         nInternalFaces_ = triI;
00276 
00277 
00278         // Triangulate external faces
00279         forAll(faceIsCut, faceI)
00280         {
00281             if (faceIsCut[faceI] && !isInternalFace(mesh, includedCell, faceI))
00282             {
00283                 // Face will become outside of the surface.
00284 
00285                 label patchI = -1;
00286                 bool reverse = false;
00287 
00288                 if (mesh.isInternalFace(faceI))
00289                 {
00290                     patchI = internalFacesPatch;
00291 
00292                     // Check orientation. Check which side of the face gets
00293                     // included (note: only one side is).
00294                     if (includedCell[mesh.faceOwner()[faceI]])
00295                     {
00296                         reverse = false;
00297                     }
00298                     else
00299                     {
00300                         reverse = true;
00301                     }
00302                 }
00303                 else
00304                 {
00305                     // Face was already outside so orientation ok.
00306 
00307                     patchI = patches.whichPatch(faceI);
00308 
00309                     reverse = false;
00310                 }
00311 
00312 
00313                 // Triangulate face
00314                 const face& f = faces[faceI];
00315 
00316                 if (reverse)
00317                 {
00318                     forAll(f, fp)
00319                     {
00320                         faceMap_[triI] = faceI;
00321 
00322                         triangles[triI++] =
00323                             labelledTri
00324                             (
00325                                 f.nextLabel(fp),
00326                                 f[fp],
00327                                 mesh.nPoints() + faceI,     // face centre
00328                                 patchI
00329                             );
00330                     }
00331                 }
00332                 else
00333                 {
00334                     forAll(f, fp)
00335                     {
00336                         faceMap_[triI] = faceI;
00337 
00338                         triangles[triI++] =
00339                             labelledTri
00340                             (
00341                                 f[fp],
00342                                 f.nextLabel(fp),
00343                                 mesh.nPoints() + faceI,     // face centre
00344                                 patchI
00345                             );
00346                     }
00347                 }
00348             }
00349         }
00350     }
00351     else
00352     {
00353         // Triangulation using existing vertices
00354         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00355 
00356         // Triangulate internal faces
00357         forAll(faceIsCut, faceI)
00358         {
00359             if (faceIsCut[faceI] && isInternalFace(mesh, includedCell, faceI))
00360             {
00361                 // Face was internal to the mesh and will be 'internal' to
00362                 // the surface.
00363 
00364                 // Triangulate face. Fall back to naive triangulation if failed.
00365                 faceTriangulation faceTris(points, faces[faceI], true);
00366 
00367                 if (faceTris.empty())
00368                 {
00369                     WarningIn("meshTriangulation::meshTriangulation")
00370                         << "Could not find triangulation for face " << faceI
00371                         << " vertices " << faces[faceI] << " coords "
00372                         << IndirectList<point>(points, faces[faceI])() << endl;
00373                 }
00374                 else
00375                 {
00376                     // Copy triangles. Make them internalFacesPatch
00377                     insertTriangles
00378                     (
00379                         faceTris,
00380                         faceI,
00381                         internalFacesPatch,
00382                         false,                  // no reverse
00383 
00384                         triangles,
00385                         triI
00386                     );
00387                 }
00388             }
00389         }
00390         nInternalFaces_ = triI;
00391 
00392 
00393         // Triangulate external faces
00394         forAll(faceIsCut, faceI)
00395         {
00396             if (faceIsCut[faceI] && !isInternalFace(mesh, includedCell, faceI))
00397             {
00398                 // Face will become outside of the surface.
00399 
00400                 label patchI = -1;
00401                 bool reverse = false;
00402 
00403                 if (mesh.isInternalFace(faceI))
00404                 {
00405                     patchI = internalFacesPatch;
00406 
00407                     // Check orientation. Check which side of the face gets
00408                     // included (note: only one side is).
00409                     if (includedCell[mesh.faceOwner()[faceI]])
00410                     {
00411                         reverse = false;
00412                     }
00413                     else
00414                     {
00415                         reverse = true;
00416                     }
00417                 }
00418                 else
00419                 {
00420                     // Face was already outside so orientation ok.
00421 
00422                     patchI = patches.whichPatch(faceI);
00423 
00424                     reverse = false;
00425                 }
00426 
00427                 // Triangulate face
00428                 faceTriangulation faceTris(points, faces[faceI], true);
00429 
00430                 if (faceTris.empty())
00431                 {
00432                     WarningIn("meshTriangulation::meshTriangulation")
00433                         << "Could not find triangulation for face " << faceI
00434                         << " vertices " << faces[faceI] << " coords "
00435                         << IndirectList<point>(points, faces[faceI])() << endl;
00436                 }
00437                 else
00438                 {
00439                     // Copy triangles. Optionally reverse them
00440                     insertTriangles
00441                     (
00442                         faceTris,
00443                         faceI,
00444                         patchI,
00445                         reverse,    // whether to reverse
00446 
00447                         triangles,
00448                         triI
00449                     );
00450                 }
00451             }
00452         }
00453     }
00454 
00455     // Shrink if nessecary (because of invalid triangulations)
00456     triangles.setSize(triI);
00457     faceMap_.setSize(triI);
00458 
00459     Pout<< "nInternalFaces_:" << nInternalFaces_ << endl;
00460     Pout<< "triangles:" << triangles.size() << endl;
00461 
00462 
00463     geometricSurfacePatchList surfPatches(patches.size());
00464 
00465     forAll(patches, patchI)
00466     {
00467         surfPatches[patchI] =
00468             geometricSurfacePatch
00469             (
00470                 patches[patchI].physicalType(),
00471                 patches[patchI].name(),
00472                 patchI
00473             );
00474     }
00475 
00476     // Create globally numbered tri surface
00477     if (faceCentreDecomposition)
00478     {
00479         // Use newPoints (mesh points + face centres)
00480         triSurface globalSurf(triangles, surfPatches, newPoints);
00481 
00482         // Create locally numbered tri surface
00483         triSurface::operator=
00484         (
00485             triSurface
00486             (
00487                 globalSurf.localFaces(),
00488                 surfPatches,
00489                 globalSurf.localPoints()
00490             )
00491         );
00492     }
00493     else
00494     {
00495         // Use mesh points
00496         triSurface globalSurf(triangles, surfPatches, mesh.points());
00497 
00498         // Create locally numbered tri surface
00499         triSurface::operator=
00500         (
00501             triSurface
00502             (
00503                 globalSurf.localFaces(),
00504                 surfPatches,
00505                 globalSurf.localPoints()
00506             )
00507         );
00508     }
00509 }
00510 
00511 
00512 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd