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

boundaryMesh.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 "boundaryMesh.H"
00028 #include "Time.H"
00029 #include "polyMesh.H"
00030 #include "repatchPolyTopoChanger.H"
00031 #include "faceList.H"
00032 #include "octree.H"
00033 #include "octreeDataFaceList.H"
00034 #include "triSurface.H"
00035 #include "SortableList.H"
00036 #include "OFstream.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 defineTypeNameAndDebug(Foam::boundaryMesh, 0);
00041 
00042 // Normal along which to divide faces into categories (used in getNearest)
00043 const Foam::vector Foam::boundaryMesh::splitNormal_(3, 2, 1);
00044 
00045 // Distance to face tolerance for getNearest
00046 const Foam::scalar Foam::boundaryMesh::distanceTol_ = 1E-2;
00047 
00048 
00049 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00050 
00051 // Returns number of feature edges connected to pointI
00052 Foam::label Foam::boundaryMesh::nFeatureEdges(label pointI) const
00053 {
00054     label nFeats = 0;
00055 
00056     const labelList& pEdges = mesh().pointEdges()[pointI];
00057 
00058     forAll(pEdges, pEdgeI)
00059     {
00060         label edgeI = pEdges[pEdgeI];
00061 
00062         if (edgeToFeature_[edgeI] != -1)
00063         {
00064             nFeats++;
00065         }
00066     }
00067     return nFeats;
00068 }
00069 
00070 
00071 // Returns next feature edge connected to pointI
00072 Foam::label Foam::boundaryMesh::nextFeatureEdge
00073 (
00074     const label edgeI,
00075     const label vertI
00076 ) const
00077 {
00078     const labelList& pEdges = mesh().pointEdges()[vertI];
00079 
00080     forAll(pEdges, pEdgeI)
00081     {
00082         label nbrEdgeI = pEdges[pEdgeI];
00083 
00084         if (nbrEdgeI != edgeI)
00085         {
00086             label featI = edgeToFeature_[nbrEdgeI];
00087 
00088             if (featI != -1)
00089             {
00090                 return nbrEdgeI;
00091             }
00092         }
00093     }
00094 
00095     return -1;
00096 }
00097 
00098 
00099 // Finds connected feature edges, starting from startPointI and returns
00100 // feature labels (not edge labels). Marks feature edges handled in
00101 // featVisited.
00102 Foam::labelList Foam::boundaryMesh::collectSegment
00103 (
00104     const boolList& isFeaturePoint,
00105     const label startEdgeI,
00106     boolList& featVisited
00107 ) const
00108 {
00109     // Find starting feature point on edge.
00110 
00111     label edgeI = startEdgeI;
00112 
00113     const edge& e = mesh().edges()[edgeI];
00114 
00115     label vertI = e.start();
00116 
00117     while (!isFeaturePoint[vertI])
00118     {
00119         // Step to next feature edge
00120 
00121         edgeI = nextFeatureEdge(edgeI, vertI);
00122 
00123         if ((edgeI == -1) || (edgeI == startEdgeI))
00124         {
00125             break;
00126         }
00127 
00128         // Step to next vertex on edge
00129 
00130         const edge& e = mesh().edges()[edgeI];
00131 
00132         vertI = e.otherVertex(vertI);
00133     }
00134 
00135     //
00136     // Now we have:
00137     //    edgeI : first edge on this segment
00138     //    vertI : one of the endpoints of this segment
00139     //
00140     // Start walking other way and storing edges as we go along.
00141     //
00142 
00143     // Untrimmed storage for current segment
00144     labelList featLabels(featureEdges_.size());
00145 
00146     label featLabelI = 0;
00147 
00148     label initEdgeI = edgeI;
00149 
00150     do
00151     {
00152         // Mark edge as visited
00153         label featI = edgeToFeature_[edgeI];
00154 
00155         if (featI == -1)
00156         {
00157             FatalErrorIn("boundaryMesh::collectSegment")
00158                 << "Problem" << abort(FatalError);
00159         }
00160         featLabels[featLabelI++] = featI;
00161 
00162         featVisited[featI] = true;
00163 
00164         // Step to next vertex on edge
00165 
00166         const edge& e = mesh().edges()[edgeI];
00167 
00168         vertI = e.otherVertex(vertI);
00169 
00170         // Step to next feature edge
00171 
00172         edgeI = nextFeatureEdge(edgeI, vertI);
00173 
00174         if ((edgeI == -1) || (edgeI == initEdgeI))
00175         {
00176             break;
00177         }
00178     }
00179     while (!isFeaturePoint[vertI]);
00180 
00181 
00182     // Trim to size
00183     featLabels.setSize(featLabelI);
00184 
00185     return featLabels;
00186 }
00187 
00188 
00189 void Foam::boundaryMesh::markEdges
00190 (
00191     const label maxDistance,
00192     const label edgeI,
00193     const label distance,
00194     labelList& minDistance,
00195     DynamicList<label>& visited
00196 ) const
00197 {
00198     if (distance < maxDistance)
00199     {
00200         // Don't do anything if reached beyond maxDistance.
00201 
00202         if (minDistance[edgeI] == -1)
00203         {
00204             // First visit of edge. Store edge label.
00205             visited.append(edgeI);
00206         }
00207         else if (minDistance[edgeI] <= distance)
00208         {
00209             // Already done this edge
00210             return;
00211         }
00212 
00213         minDistance[edgeI] = distance;
00214 
00215         const edge& e = mesh().edges()[edgeI];
00216 
00217         // Do edges connected to e.start
00218         const labelList& startEdges = mesh().pointEdges()[e.start()];
00219 
00220         forAll(startEdges, pEdgeI)
00221         {
00222             markEdges
00223             (
00224                 maxDistance,
00225                 startEdges[pEdgeI],
00226                 distance+1,
00227                 minDistance,
00228                 visited
00229             );
00230         }
00231 
00232         // Do edges connected to e.end
00233         const labelList& endEdges = mesh().pointEdges()[e.end()];
00234 
00235         forAll(endEdges, pEdgeI)
00236         {
00237             markEdges
00238             (
00239                 maxDistance,
00240                 endEdges[pEdgeI],
00241                 distance+1,
00242                 minDistance,
00243                 visited
00244             );
00245         }
00246     }
00247 }
00248 
00249 
00250 Foam::label Foam::boundaryMesh::findPatchID
00251 (
00252     const polyPatchList& patches,
00253     const word& patchName
00254 ) const
00255 {
00256     forAll(patches, patchI)
00257     {
00258         if (patches[patchI].name() == patchName)
00259         {
00260             return patchI;
00261         }
00262     }
00263 
00264     return -1;
00265 }
00266 
00267 
00268 Foam::wordList Foam::boundaryMesh::patchNames() const
00269 {
00270     wordList names(patches_.size());
00271 
00272     forAll(patches_, patchI)
00273     {
00274         names[patchI] = patches_[patchI].name();
00275     }
00276     return names;
00277 }
00278 
00279 
00280 Foam::label Foam::boundaryMesh::whichPatch
00281 (
00282     const polyPatchList& patches,
00283     const label faceI
00284 ) const
00285 {
00286     forAll(patches, patchI)
00287     {
00288         const polyPatch& pp = patches[patchI];
00289 
00290         if ((faceI >= pp.start()) && (faceI < (pp.start() + pp.size())))
00291         {
00292             return patchI;
00293         }
00294     }
00295     return -1;
00296 }
00297 
00298 
00299 // Gets labels of changed faces and propagates them to the edges. Returns
00300 // labels of edges changed.
00301 Foam::labelList Foam::boundaryMesh::faceToEdge
00302 (
00303     const boolList& regionEdge,
00304     const label region,
00305     const labelList& changedFaces,
00306     labelList& edgeRegion
00307 ) const
00308 {
00309     labelList changedEdges(mesh().nEdges(), -1);
00310     label changedI = 0;
00311 
00312     forAll(changedFaces, i)
00313     {
00314         label faceI = changedFaces[i];
00315 
00316         const labelList& fEdges = mesh().faceEdges()[faceI];
00317 
00318         forAll(fEdges, fEdgeI)
00319         {
00320             label edgeI = fEdges[fEdgeI];
00321 
00322             if (!regionEdge[edgeI] && (edgeRegion[edgeI] == -1))
00323             {
00324                 edgeRegion[edgeI] = region;
00325 
00326                 changedEdges[changedI++] = edgeI;
00327             }
00328         }
00329     }
00330 
00331     changedEdges.setSize(changedI);
00332 
00333     return changedEdges;
00334 }
00335 
00336 
00337 // Reverse of faceToEdge: gets edges and returns faces
00338 Foam::labelList Foam::boundaryMesh::edgeToFace
00339 (
00340     const label region,
00341     const labelList& changedEdges,
00342     labelList& faceRegion
00343 ) const
00344 {
00345     labelList changedFaces(mesh().size(), -1);
00346     label changedI = 0;
00347 
00348     forAll(changedEdges, i)
00349     {
00350         label edgeI = changedEdges[i];
00351 
00352         const labelList& eFaces = mesh().edgeFaces()[edgeI];
00353 
00354         forAll(eFaces, eFaceI)
00355         {
00356             label faceI = eFaces[eFaceI];
00357 
00358             if (faceRegion[faceI] == -1)
00359             {
00360                 faceRegion[faceI] = region;
00361 
00362                 changedFaces[changedI++] = faceI;
00363             }
00364         }
00365     }
00366 
00367     changedFaces.setSize(changedI);
00368 
00369     return changedFaces;
00370 }
00371 
00372 
00373 // Finds area, starting at faceI, delimited by borderEdge
00374 void Foam::boundaryMesh::markZone
00375 (
00376     const boolList& borderEdge,
00377     label faceI,
00378     label currentZone,
00379     labelList& faceZone
00380 ) const
00381 {
00382     faceZone[faceI] = currentZone;
00383 
00384     // List of faces whose faceZone has been set.
00385     labelList changedFaces(1, faceI);
00386     // List of edges whose faceZone has been set.
00387     labelList changedEdges;
00388 
00389     // Zones on all edges.
00390     labelList edgeZone(mesh().nEdges(), -1);
00391 
00392     while(1)
00393     {
00394         changedEdges = faceToEdge
00395         (
00396             borderEdge,
00397             currentZone,
00398             changedFaces,
00399             edgeZone
00400         );
00401 
00402         if (debug)
00403         {
00404             Pout<< "From changedFaces:" << changedFaces.size()
00405                 << " to changedEdges:" << changedEdges.size()
00406                 << endl;
00407         }
00408 
00409         if (changedEdges.empty())
00410         {
00411             break;
00412         }
00413 
00414         changedFaces = edgeToFace(currentZone, changedEdges, faceZone);
00415 
00416         if (debug)
00417         {
00418             Pout<< "From changedEdges:" << changedEdges.size()
00419                 << " to changedFaces:" << changedFaces.size()
00420                 << endl;
00421         }
00422 
00423         if (changedFaces.empty())
00424         {
00425             break;
00426         }
00427     }
00428 }
00429 
00430 
00431 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00432 
00433 // Null constructor
00434 Foam::boundaryMesh::boundaryMesh()
00435 :
00436     meshPtr_(NULL),
00437     patches_(),
00438     meshFace_(),
00439     featurePoints_(),
00440     featureEdges_(),
00441     featureToEdge_(),
00442     edgeToFeature_(),
00443     featureSegments_(),
00444     extraEdges_()
00445 {}
00446 
00447 
00448 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00449 
00450 Foam::boundaryMesh::~boundaryMesh()
00451 {
00452     clearOut();
00453 }
00454 
00455 
00456 void Foam::boundaryMesh::clearOut()
00457 {
00458     if (meshPtr_)
00459     {
00460         delete meshPtr_;
00461 
00462         meshPtr_ = NULL;
00463     }
00464 }
00465 
00466 
00467 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00468 
00469 void Foam::boundaryMesh::read(const polyMesh& mesh)
00470 {
00471     patches_.clear();
00472 
00473     patches_.setSize(mesh.boundaryMesh().size());
00474 
00475     // Number of boundary faces
00476     label nBFaces = mesh.nFaces() - mesh.nInternalFaces();
00477 
00478     faceList bFaces(nBFaces);
00479 
00480     meshFace_.setSize(nBFaces);
00481 
00482     label bFaceI = 0;
00483 
00484     // Collect all boundary faces.
00485     forAll(mesh.boundaryMesh(), patchI)
00486     {
00487         const polyPatch& pp = mesh.boundaryMesh()[patchI];
00488 
00489         patches_.set
00490         (
00491             patchI,
00492             new boundaryPatch
00493             (
00494                 pp.name(),
00495                 patchI,
00496                 pp.size(),
00497                 bFaceI,
00498                 pp.type()
00499             )
00500         );
00501 
00502         // Collect all faces in global numbering.
00503         forAll(pp, patchFaceI)
00504         {
00505             meshFace_[bFaceI] = pp.start() + patchFaceI;
00506 
00507             bFaces[bFaceI] = pp[patchFaceI];
00508 
00509             bFaceI++;
00510         }
00511     }
00512 
00513 
00514     if (debug)
00515     {
00516         Pout<< "read : patches now:" << endl;
00517 
00518         forAll(patches_, patchI)
00519         {
00520             const boundaryPatch& bp = patches_[patchI];
00521 
00522             Pout<< "    name  : " << bp.name() << endl
00523                 << "    size  : " << bp.size() << endl
00524                 << "    start : " << bp.start() << endl
00525                 << "    type  : " << bp.physicalType() << endl
00526                 << endl;
00527         }
00528     }
00529 
00530     //
00531     // Construct single patch for all of boundary
00532     //
00533 
00534     // Temporary primitivePatch to calculate compact points & faces.
00535     PrimitivePatch<face, List, const pointField&> globalPatch
00536     (
00537         bFaces,
00538         mesh.points()
00539     );
00540 
00541     // Store in local(compact) addressing
00542     clearOut();
00543 
00544     meshPtr_ = new bMesh(globalPatch.localFaces(), globalPatch.localPoints());
00545 
00546 
00547     if (debug & 2)
00548     {
00549         const bMesh& msh = *meshPtr_;
00550 
00551         Pout<< "** Start of Faces **" << endl;
00552 
00553         forAll(msh, faceI)
00554         {
00555             const face& f = msh[faceI];
00556 
00557             point ctr(vector::zero);
00558 
00559             forAll(f, fp)
00560             {
00561                 ctr += msh.points()[f[fp]];
00562             }
00563             ctr /= f.size();
00564 
00565             Pout<< "    " << faceI
00566                 << " ctr:" << ctr
00567                 << " verts:" << f
00568                 << endl;
00569         }
00570 
00571         Pout<< "** End of Faces **" << endl;
00572 
00573         Pout<< "** Start of Points **" << endl;
00574 
00575         forAll(msh.points(), pointI)
00576         {
00577             Pout<< "    " << pointI
00578                 << " coord:" << msh.points()[pointI]
00579                 << endl;
00580         }
00581 
00582         Pout<< "** End of Points **" << endl;
00583     }
00584 
00585     // Clear edge storage
00586     featurePoints_.setSize(0);
00587     featureEdges_.setSize(0);
00588 
00589     featureToEdge_.setSize(0);
00590     edgeToFeature_.setSize(meshPtr_->nEdges());
00591     edgeToFeature_ = -1;
00592 
00593     featureSegments_.setSize(0);
00594 
00595     extraEdges_.setSize(0);
00596 }
00597 
00598 
00599 void Foam::boundaryMesh::readTriSurface(const fileName& fName)
00600 {
00601     triSurface surf(fName);
00602 
00603     if (surf.empty())
00604     {
00605         return;
00606     }
00607 
00608     // Sort according to region
00609     SortableList<label> regions(surf.size());
00610 
00611     forAll(surf, triI)
00612     {
00613         regions[triI] = surf[triI].region();
00614     }
00615     regions.sort();
00616 
00617     // Determine region mapping.
00618     Map<label> regionToBoundaryPatch;
00619 
00620     label oldRegion = -1111;
00621     label boundPatch = 0;
00622 
00623     forAll(regions, i)
00624     {
00625         if (regions[i] != oldRegion)
00626         {
00627             regionToBoundaryPatch.insert(regions[i], boundPatch);
00628 
00629             oldRegion = regions[i];
00630             boundPatch++;
00631         }
00632     }
00633 
00634     const geometricSurfacePatchList& surfPatches = surf.patches();
00635 
00636     patches_.clear();
00637 
00638     if (surfPatches.size() == regionToBoundaryPatch.size())
00639     {
00640         // There are as many surface patches as region numbers in triangles
00641         // so use the surface patches
00642 
00643         patches_.setSize(surfPatches.size());
00644 
00645         // Take over patches, setting size to 0 for now.
00646         forAll(surfPatches, patchI)
00647         {
00648             const geometricSurfacePatch& surfPatch = surfPatches[patchI];
00649 
00650             patches_.set
00651             (
00652                 patchI,
00653                 new boundaryPatch
00654                 (
00655                     surfPatch.name(),
00656                     patchI,
00657                     0,
00658                     0,
00659                     surfPatch.geometricType()
00660                 )
00661             );
00662         }
00663     }
00664     else
00665     {
00666         // There are not enough surface patches. Make up my own.
00667 
00668         patches_.setSize(regionToBoundaryPatch.size());
00669 
00670         forAll(patches_, patchI)
00671         {
00672             patches_.set
00673             (
00674                 patchI,
00675                 new boundaryPatch
00676                 (
00677                     "patch" + name(patchI),
00678                     patchI,
00679                     0,
00680                     0,
00681                     "empty"
00682                 )
00683             );
00684         }
00685     }
00686 
00687     //
00688     // Copy according into bFaces according to regions
00689     //
00690 
00691     const labelList& indices = regions.indices();
00692 
00693     faceList bFaces(surf.size());
00694 
00695     meshFace_.setSize(surf.size());
00696 
00697     label bFaceI = 0;
00698 
00699     // Current region number
00700     label surfRegion = regions[0];
00701     label foamRegion = regionToBoundaryPatch[surfRegion];
00702 
00703     Pout<< "Surface region " << surfRegion << " becomes boundary patch "
00704         << foamRegion << " with name " << patches_[foamRegion].name() << endl;
00705 
00706 
00707     // Index in bFaces of start of current patch
00708     label startFaceI = 0;
00709 
00710     forAll(indices, indexI)
00711     {
00712         label triI = indices[indexI];
00713 
00714         const labelledTri& tri = surf.localFaces()[triI];
00715 
00716         if (tri.region() != surfRegion)
00717         {
00718             // Change of region. We now know the size of the previous one.
00719             boundaryPatch& bp = patches_[foamRegion];
00720 
00721             bp.size() = bFaceI - startFaceI;
00722             bp.start() = startFaceI;
00723 
00724             surfRegion = tri.region();
00725             foamRegion = regionToBoundaryPatch[surfRegion];
00726 
00727             Pout<< "Surface region " << surfRegion << " becomes boundary patch "
00728                 << foamRegion << " with name " << patches_[foamRegion].name()
00729                 << endl;
00730 
00731             startFaceI = bFaceI;
00732         }
00733 
00734         meshFace_[bFaceI] = triI;
00735 
00736         bFaces[bFaceI++] = face(tri);
00737     }
00738 
00739     // Final region
00740     boundaryPatch& bp = patches_[foamRegion];
00741 
00742     bp.size() = bFaceI - startFaceI;
00743     bp.start() = startFaceI;
00744 
00745     //
00746     // Construct single primitivePatch for all of boundary
00747     //
00748 
00749     clearOut();
00750 
00751     // Store compact.
00752     meshPtr_ = new bMesh(bFaces, surf.localPoints());
00753 
00754     // Clear edge storage
00755     featurePoints_.setSize(0);
00756     featureEdges_.setSize(0);
00757 
00758     featureToEdge_.setSize(0);
00759     edgeToFeature_.setSize(meshPtr_->nEdges());
00760     edgeToFeature_ = -1;
00761 
00762     featureSegments_.setSize(0);
00763 
00764     extraEdges_.setSize(0);
00765 }
00766 
00767 
00768 void Foam::boundaryMesh::writeTriSurface(const fileName& fName) const
00769 {
00770     geometricSurfacePatchList surfPatches(patches_.size());
00771 
00772     forAll(patches_, patchI)
00773     {
00774         const boundaryPatch& bp = patches_[patchI];
00775 
00776         surfPatches[patchI] =
00777             geometricSurfacePatch
00778             (
00779                 bp.physicalType(),
00780                 bp.name(),
00781                 patchI
00782             );
00783     }
00784 
00785     //
00786     // Simple triangulation.
00787     //
00788 
00789     // Get number of triangles per face
00790     labelList nTris(mesh().size());
00791 
00792     label totalNTris = getNTris(0, mesh().size(), nTris);
00793 
00794     // Determine per face the starting triangle.
00795     labelList startTri(mesh().size());
00796 
00797     label triI = 0;
00798 
00799     forAll(mesh(), faceI)
00800     {
00801         startTri[faceI] = triI;
00802 
00803         triI += nTris[faceI];
00804     }
00805 
00806     // Triangulate
00807     labelList triVerts(3*totalNTris);
00808 
00809     triangulate(0, mesh().size(), totalNTris, triVerts);
00810 
00811     // Convert to labelledTri
00812 
00813     List<labelledTri> tris(totalNTris);
00814 
00815     triI = 0;
00816 
00817     forAll(patches_, patchI)
00818     {
00819         const boundaryPatch& bp = patches_[patchI];
00820 
00821         forAll(bp, patchFaceI)
00822         {
00823             label faceI = bp.start() + patchFaceI;
00824 
00825             label triVertI = 3*startTri[faceI];
00826 
00827             for (label faceTriI = 0; faceTriI < nTris[faceI]; faceTriI++)
00828             {
00829                 label v0 = triVerts[triVertI++];
00830                 label v1 = triVerts[triVertI++];
00831                 label v2 = triVerts[triVertI++];
00832 
00833                 tris[triI++] = labelledTri(v0, v1, v2, patchI);
00834             }
00835         }
00836     }
00837 
00838     triSurface surf(tris, surfPatches, mesh().points());
00839 
00840     OFstream surfStream(fName);
00841 
00842     surf.write(surfStream);
00843 }
00844 
00845 
00846 // Get index in this (boundaryMesh) of face nearest to each boundary face in
00847 // pMesh.
00848 // Origininally all triangles/faces of boundaryMesh would be bunged into
00849 // one big octree. Problem was that faces on top of each other, differing
00850 // only in sign of normal, could not be found separately. It would always
00851 // find only one. We could detect that it was probably finding the wrong one
00852 // (based on normal) but could not 'tell' the octree to retrieve the other
00853 // one (since they occupy exactly the same space)
00854 // So now faces get put into different octrees depending on normal.
00855 // !It still will not be possible to differentiate between two faces on top
00856 // of each other having the same normal
00857 Foam::labelList Foam::boundaryMesh::getNearest
00858 (
00859     const primitiveMesh& pMesh,
00860     const vector& searchSpan
00861 ) const
00862 {
00863 
00864     // Divide faces into two bins acc. to normal
00865     // - left of splitNormal
00866     // - right ,,
00867     DynamicList<label> leftFaces(mesh().size()/2);
00868     DynamicList<label> rightFaces(mesh().size()/2);
00869 
00870     forAll(mesh(), bFaceI)
00871     {
00872         scalar sign = mesh().faceNormals()[bFaceI] & splitNormal_;
00873 
00874         if (sign > -1E-5)
00875         {
00876             rightFaces.append(bFaceI);
00877         }
00878         if (sign < 1E-5)
00879         {
00880             leftFaces.append(bFaceI);
00881         }
00882     }
00883 
00884     leftFaces.shrink();
00885     rightFaces.shrink();
00886 
00887     if (debug)
00888     {
00889         Pout<< "getNearest :"
00890             << " rightBin:" << rightFaces.size()
00891             << " leftBin:" << leftFaces.size()
00892             << endl;
00893     }
00894 
00895 
00896     // Overall bb
00897     treeBoundBox overallBb(mesh().localPoints());
00898 
00899     // Extend domain slightly (also makes it 3D if was 2D)
00900     // Note asymmetry to avoid having faces align with octree cubes.
00901     scalar tol = 1E-6 * overallBb.avgDim();
00902 
00903     point& bbMin = overallBb.min();
00904     bbMin.x() -= tol;
00905     bbMin.y() -= tol;
00906     bbMin.z() -= tol;
00907 
00908     point& bbMax = overallBb.max();
00909     bbMax.x() += 2*tol;
00910     bbMax.y() += 2*tol;
00911     bbMax.z() += 2*tol;
00912 
00913     // Create the octrees
00914     octree<octreeDataFaceList> leftTree
00915     (
00916         overallBb,
00917         octreeDataFaceList
00918         (
00919             mesh(),
00920             leftFaces
00921         ),
00922         1,
00923         20,
00924         10
00925     );
00926     octree<octreeDataFaceList> rightTree
00927     (
00928         overallBb,
00929         octreeDataFaceList
00930         (
00931             mesh(),
00932             rightFaces
00933         ),
00934         1,
00935         20,
00936         10
00937     );
00938 
00939     if (debug)
00940     {
00941         Pout<< "getNearest : built trees" << endl;
00942     }
00943 
00944 
00945     const vectorField& ns = mesh().faceNormals();
00946 
00947 
00948     //
00949     // Search nearest triangle centre for every polyMesh boundary face
00950     //
00951 
00952     labelList nearestBFaceI(pMesh.nFaces() - pMesh.nInternalFaces());
00953 
00954     treeBoundBox tightest;
00955 
00956     const scalar searchDim = mag(searchSpan);
00957 
00958     forAll(nearestBFaceI, patchFaceI)
00959     {
00960         label meshFaceI = pMesh.nInternalFaces() + patchFaceI;
00961 
00962         const point& ctr = pMesh.faceCentres()[meshFaceI];
00963 
00964         if (debug && (patchFaceI % 1000) == 0)
00965         {
00966             Pout<< "getNearest : patchFace:" << patchFaceI
00967                 << " meshFaceI:" << meshFaceI << " ctr:" << ctr << endl;
00968         }
00969 
00970 
00971         // Get normal from area vector
00972         vector n = pMesh.faceAreas()[meshFaceI];
00973         scalar area = mag(n);
00974         n /= area;
00975 
00976         scalar typDim = -GREAT;
00977         const face& f = pMesh.faces()[meshFaceI];
00978 
00979         forAll(f, fp)
00980         {
00981             typDim = max(typDim, mag(pMesh.points()[f[fp]] - ctr));
00982         }
00983 
00984         // Search right tree
00985         tightest.min() = ctr - searchSpan;
00986         tightest.max() = ctr + searchSpan;
00987         scalar rightDist = searchDim;
00988         label rightI = rightTree.findNearest(ctr, tightest, rightDist);
00989 
00990 
00991         // Search left tree. Note: could start from rightDist bounding box
00992         // instead of starting from top.
00993         tightest.min() = ctr - searchSpan;
00994         tightest.max() = ctr + searchSpan;
00995         scalar leftDist = searchDim;
00996         label leftI = leftTree.findNearest(ctr, tightest, leftDist);
00997 
00998 
00999         if (rightI == -1)
01000         {
01001             // No face found in right tree.
01002 
01003             if (leftI == -1)
01004             {
01005                 // No face found in left tree.
01006                 nearestBFaceI[patchFaceI] = -1;
01007             }
01008             else
01009             {
01010                 // Found in left but not in right. Choose left regardless if
01011                 // correct sign. Note: do we want this?
01012                 nearestBFaceI[patchFaceI] = leftFaces[leftI];
01013             }
01014         }
01015         else
01016         {
01017             if (leftI == -1)
01018             {
01019                 // Found in right but not in left. Choose right regardless if
01020                 // correct sign. Note: do we want this?
01021                 nearestBFaceI[patchFaceI] = rightFaces[rightI];
01022             }
01023             else
01024             {
01025                 // Found in both trees. Compare normals.
01026 
01027                 scalar rightSign = n & ns[rightFaces[rightI]];
01028                 scalar leftSign = n & ns[leftFaces[leftI]];
01029 
01030                 if
01031                 (
01032                     (rightSign > 0 && leftSign > 0)
01033                  || (rightSign < 0 && leftSign < 0)
01034                 )
01035                 {
01036                     // Both same sign. Choose nearest.
01037                     if (rightDist < leftDist)
01038                     {
01039                         nearestBFaceI[patchFaceI] = rightFaces[rightI];
01040                     }
01041                     else
01042                     {
01043                         nearestBFaceI[patchFaceI] = leftFaces[leftI];
01044                     }
01045                 }
01046                 else
01047                 {
01048                     // Differing sign.
01049                     // - if both near enough choose one with correct sign
01050                     // - otherwise choose nearest.
01051 
01052                     // Get local dimension as max of distance between ctr and
01053                     // any face vertex.
01054 
01055                     typDim *= distanceTol_;
01056 
01057                     if (rightDist < typDim && leftDist < typDim)
01058                     {
01059                         // Different sign and nearby. Choosing matching normal
01060                         if (rightSign > 0)
01061                         {
01062                             nearestBFaceI[patchFaceI] = rightFaces[rightI];
01063                         }
01064                         else
01065                         {
01066                             nearestBFaceI[patchFaceI] = leftFaces[leftI];
01067                         }
01068                     }
01069                     else
01070                     {
01071                         // Different sign but faraway. Choosing nearest.
01072                         if (rightDist < leftDist)
01073                         {
01074                             nearestBFaceI[patchFaceI] = rightFaces[rightI];
01075                         }
01076                         else
01077                         {
01078                             nearestBFaceI[patchFaceI] = leftFaces[leftI];
01079                         }
01080                     }
01081                 }
01082             }
01083         }
01084     }
01085 
01086     return nearestBFaceI;
01087 }
01088 
01089 
01090 void Foam::boundaryMesh::patchify
01091 (
01092     const labelList& nearest,
01093     const polyBoundaryMesh& oldPatches,
01094     polyMesh& newMesh
01095 ) const
01096 {
01097 
01098     // 2 cases to be handled:
01099     // A- patches in boundaryMesh patches_
01100     // B- patches not in boundaryMesh patches_ but in polyMesh
01101 
01102     // Create maps from patch name to new patch index.
01103     HashTable<label> nameToIndex(2*patches_.size());
01104 
01105     Map<word> indexToName(2*patches_.size());
01106 
01107 
01108     label nNewPatches = patches_.size();
01109 
01110     forAll(oldPatches, oldPatchI)
01111     {
01112         const polyPatch& patch = oldPatches[oldPatchI];
01113 
01114         label newPatchI = findPatchID(patch.name());
01115 
01116         if (newPatchI != -1)
01117         {
01118             nameToIndex.insert(patch.name(), newPatchI);
01119 
01120             indexToName.insert(newPatchI, patch.name());
01121         }
01122     }
01123 
01124     // Include all boundaryPatches not yet in nameToIndex (i.e. not in old
01125     // patches)
01126     forAll(patches_, bPatchI)
01127     {
01128         const boundaryPatch& bp = patches_[bPatchI];
01129 
01130         if (!nameToIndex.found(bp.name()))
01131         {
01132             nameToIndex.insert(bp.name(), bPatchI);
01133 
01134             indexToName.insert(bPatchI, bp.name());
01135         }
01136     }
01137 
01138     // Pass1:
01139     // Copy names&type of patches (with zero size) from old mesh as far as
01140     // possible. First patch created gets all boundary faces; all others get
01141     // zero faces (repatched later on). Exception is coupled patches which
01142     // keep their size.
01143 
01144     List<polyPatch*> newPatchPtrList(nNewPatches);
01145 
01146     label meshFaceI = newMesh.nInternalFaces();
01147 
01148     // First patch gets all non-coupled faces
01149     label facesToBeDone = newMesh.nFaces() - newMesh.nInternalFaces();
01150 
01151     forAll(patches_, bPatchI)
01152     {
01153         const boundaryPatch& bp = patches_[bPatchI];
01154 
01155         label newPatchI = nameToIndex[bp.name()];
01156 
01157         // Find corresponding patch in polyMesh
01158         label oldPatchI = findPatchID(oldPatches, bp.name());
01159 
01160         if (oldPatchI == -1)
01161         {
01162             // Newly created patch. Gets all or zero faces.
01163             if (debug)
01164             {
01165                 Pout<< "patchify : Creating new polyPatch:" << bp.name()
01166                     << " type:" << bp.physicalType() << endl;
01167             }
01168 
01169             newPatchPtrList[newPatchI] = polyPatch::New
01170             (
01171                 bp.physicalType(),
01172                 bp.name(),
01173                 facesToBeDone,
01174                 meshFaceI,
01175                 newPatchI,
01176                 newMesh.boundaryMesh()
01177             ).ptr();
01178 
01179             meshFaceI += facesToBeDone;
01180 
01181             // first patch gets all boundary faces; all others get 0.
01182             facesToBeDone = 0;
01183         }
01184         else
01185         {
01186             // Existing patch. Gets all or zero faces.
01187             const polyPatch& oldPatch = oldPatches[oldPatchI];
01188 
01189             if (debug)
01190             {
01191                 Pout<< "patchify : Cloning existing polyPatch:"
01192                     << oldPatch.name() << endl;
01193             }
01194 
01195             newPatchPtrList[newPatchI] = oldPatch.clone
01196             (
01197                 newMesh.boundaryMesh(),
01198                 newPatchI,
01199                 facesToBeDone,
01200                 meshFaceI
01201             ).ptr();
01202 
01203             meshFaceI += facesToBeDone;
01204 
01205             // first patch gets all boundary faces; all others get 0.
01206             facesToBeDone = 0;
01207         }
01208     }
01209 
01210 
01211     if (debug)
01212     {
01213         Pout<< "Patchify : new polyPatch list:" << endl;
01214 
01215         forAll(newPatchPtrList, patchI)
01216         {
01217             const polyPatch& newPatch = *newPatchPtrList[patchI];
01218 
01219             if (debug)
01220             {
01221                 Pout<< "polyPatch:" << newPatch.name() << endl
01222                     << "    type :" << newPatch.typeName << endl
01223                     << "    size :" << newPatch.size() << endl
01224                     << "    start:" << newPatch.start() << endl
01225                     << "    index:" << patchI << endl;
01226             }
01227         }
01228     }
01229 
01230     // Actually add new list of patches
01231     repatchPolyTopoChanger polyMeshRepatcher(newMesh);
01232     polyMeshRepatcher.changePatches(newPatchPtrList);
01233 
01234 
01235     // Pass2:
01236     // Change patch type for face
01237 
01238     if (newPatchPtrList.size())
01239     {
01240         List<DynamicList<label> > patchFaces(nNewPatches);
01241 
01242         // Give reasonable estimate for size of patches
01243         label nAvgFaces =
01244             (newMesh.nFaces() - newMesh.nInternalFaces())
01245           / nNewPatches;
01246 
01247         forAll(patchFaces, newPatchI)
01248         {
01249             patchFaces[newPatchI].setCapacity(nAvgFaces);
01250         }
01251 
01252         //
01253         // Sort faces acc. to new patch index. Can loop over all old patches
01254         // since will contain all faces.
01255         //
01256 
01257         forAll(oldPatches, oldPatchI)
01258         {
01259             const polyPatch& patch = oldPatches[oldPatchI];
01260 
01261             forAll(patch, patchFaceI)
01262             {
01263                 // Put face into region given by nearest boundary face
01264 
01265                 label meshFaceI = patch.start() + patchFaceI;
01266 
01267                 label bFaceI = meshFaceI - newMesh.nInternalFaces();
01268 
01269                 patchFaces[whichPatch(nearest[bFaceI])].append(meshFaceI);
01270             }
01271         }
01272 
01273         forAll(patchFaces, newPatchI)
01274         {
01275             patchFaces[newPatchI].shrink();
01276         }
01277 
01278 
01279         // Change patch > 0. (since above we put all faces into the zeroth
01280         // patch)
01281 
01282         for (label newPatchI = 1; newPatchI < patchFaces.size(); newPatchI++)
01283         {
01284             const labelList& pFaces = patchFaces[newPatchI];
01285 
01286             forAll(pFaces, pFaceI)
01287             {
01288                 polyMeshRepatcher.changePatchID(pFaces[pFaceI], newPatchI);
01289             }
01290         }
01291 
01292         polyMeshRepatcher.repatch();
01293     }
01294 }
01295 
01296 
01297 void Foam::boundaryMesh::setFeatureEdges(const scalar minCos)
01298 {
01299     edgeToFeature_.setSize(mesh().nEdges());
01300 
01301     edgeToFeature_ = -1;
01302 
01303     // 1. Mark feature edges
01304 
01305     // Storage for edge labels that are features. Trim later.
01306     featureToEdge_.setSize(mesh().nEdges());
01307 
01308     label featureI = 0;
01309 
01310     if (minCos >= 0.9999)
01311     {
01312         // Select everything
01313         forAll(mesh().edges(), edgeI)
01314         {
01315             edgeToFeature_[edgeI] = featureI;
01316             featureToEdge_[featureI++] = edgeI;
01317         }
01318     }
01319     else
01320     {
01321         forAll(mesh().edges(), edgeI)
01322         {
01323             const labelList& eFaces = mesh().edgeFaces()[edgeI];
01324 
01325             if (eFaces.size() == 2)
01326             {
01327                 label face0I = eFaces[0];
01328 
01329                 label face1I = eFaces[1];
01330 
01333                 //if (whichPatch(face0I) != whichPatch(face1I))
01334                 //{
01335                 //    edgeToFeature_[edgeI] = featureI;
01336                 //    featureToEdge_[featureI++] = edgeI;
01337                 //}
01338                 //else
01339                 {
01340                     const vector& n0 = mesh().faceNormals()[face0I];
01341 
01342                     const vector& n1 = mesh().faceNormals()[face1I];
01343 
01344                     float cosAng = n0 & n1;
01345 
01346                     if (cosAng < minCos)
01347                     {
01348                         edgeToFeature_[edgeI] = featureI;
01349                         featureToEdge_[featureI++] = edgeI;
01350                     }
01351                 }
01352             }
01353             else
01354             {
01355                 //Should not occur: 0 or more than two faces
01356                 edgeToFeature_[edgeI] = featureI;
01357                 featureToEdge_[featureI++] = edgeI;
01358             }
01359         }
01360     }
01361 
01362     // Trim featureToEdge_ to actual number of edges.
01363     featureToEdge_.setSize(featureI);
01364 
01365     //
01366     // Compact edges i.e. relabel vertices.
01367     //
01368 
01369     featureEdges_.setSize(featureI);
01370     featurePoints_.setSize(mesh().nPoints());
01371 
01372     labelList featToMeshPoint(mesh().nPoints(), -1);
01373 
01374     label featPtI = 0;
01375 
01376     forAll(featureToEdge_, fEdgeI)
01377     {
01378         label edgeI = featureToEdge_[fEdgeI];
01379 
01380         const edge& e = mesh().edges()[edgeI];
01381 
01382         label start = featToMeshPoint[e.start()];
01383 
01384         if (start == -1)
01385         {
01386             featToMeshPoint[e.start()] = featPtI;
01387 
01388             featurePoints_[featPtI] = mesh().points()[e.start()];
01389 
01390             start = featPtI;
01391 
01392             featPtI++;
01393         }
01394 
01395         label end = featToMeshPoint[e.end()];
01396 
01397         if (end == -1)
01398         {
01399             featToMeshPoint[e.end()] = featPtI;
01400 
01401             featurePoints_[featPtI] = mesh().points()[e.end()];
01402 
01403             end = featPtI;
01404 
01405             featPtI++;
01406         }
01407 
01408         // Store with renumbered vertices.
01409         featureEdges_[fEdgeI] = edge(start, end);
01410     }
01411 
01412     // Compact points
01413     featurePoints_.setSize(featPtI);
01414 
01415 
01416     //
01417     // 2. Mark endpoints of feature segments. These are points with
01418     // != 2 feature edges connected.
01419     // Note: can add geometric constraint here as well that if 2 feature
01420     // edges the angle between them should be less than xxx.
01421     //
01422 
01423     boolList isFeaturePoint(mesh().nPoints(), false);
01424 
01425     forAll(featureToEdge_, featI)
01426     {
01427         label edgeI = featureToEdge_[featI];
01428 
01429         const edge& e = mesh().edges()[edgeI];
01430 
01431         if (nFeatureEdges(e.start()) != 2)
01432         {
01433             isFeaturePoint[e.start()] = true;
01434         }
01435 
01436         if (nFeatureEdges(e.end()) != 2)
01437         {
01438             isFeaturePoint[e.end()] = true;
01439         }
01440     }
01441 
01442 
01443     //
01444     // 3: Split feature edges into segments:
01445     // find point with not 2 feature edges -> start of feature segment
01446     //
01447 
01448     DynamicList<labelList> segments;
01449 
01450 
01451     boolList featVisited(featureToEdge_.size(), false);
01452 
01453     do
01454     {
01455         label startFeatI = -1;
01456 
01457         forAll(featVisited, featI)
01458         {
01459             if (!featVisited[featI])
01460             {
01461                 startFeatI = featI;
01462 
01463                 break;
01464             }
01465         }
01466 
01467         if (startFeatI == -1)
01468         {
01469             // No feature lines left.
01470             break;
01471         }
01472 
01473         segments.append
01474         (
01475             collectSegment
01476             (
01477                 isFeaturePoint,
01478                 featureToEdge_[startFeatI],
01479                 featVisited
01480             )
01481         );
01482     }
01483     while (true);
01484 
01485 
01486     //
01487     // Store in *this
01488     //
01489     featureSegments_.setSize(segments.size());
01490 
01491     forAll(featureSegments_, segmentI)
01492     {
01493         featureSegments_[segmentI] = segments[segmentI];
01494     }
01495 }
01496 
01497 
01498 void Foam::boundaryMesh::setExtraEdges(const label edgeI)
01499 {
01500     labelList minDistance(mesh().nEdges(), -1);
01501 
01502     // All edge labels encountered
01503     DynamicList<label> visitedEdges;
01504 
01505     // Floodfill from edgeI starting from distance 0. Stop at distance.
01506     markEdges(8, edgeI, 0, minDistance, visitedEdges);
01507 
01508     // Set edge labels to display
01509     extraEdges_.transfer(visitedEdges);
01510 }
01511 
01512 
01513 Foam::label Foam::boundaryMesh::whichPatch(const label faceI) const
01514 {
01515     forAll(patches_, patchI)
01516     {
01517         const boundaryPatch& bp = patches_[patchI];
01518 
01519         if ((faceI >= bp.start()) && (faceI < (bp.start() + bp.size())))
01520         {
01521             return patchI;
01522         }
01523     }
01524 
01525     FatalErrorIn("boundaryMesh::whichPatch(const label)")
01526         << "Cannot find face " << faceI << " in list of boundaryPatches "
01527         << patches_
01528         << abort(FatalError);
01529 
01530     return -1;
01531 }
01532 
01533 
01534 Foam::label Foam::boundaryMesh::findPatchID(const word& patchName) const
01535 {
01536     forAll(patches_, patchI)
01537     {
01538         if (patches_[patchI].name() == patchName)
01539         {
01540             return patchI;
01541         }
01542     }
01543 
01544     return -1;
01545 }
01546 
01547 
01548 void Foam::boundaryMesh::addPatch(const word& patchName)
01549 {
01550     patches_.setSize(patches_.size() + 1);
01551 
01552     // Add empty patch at end of patch list.
01553 
01554     label patchI = patches_.size()-1;
01555 
01556     boundaryPatch* bpPtr = new boundaryPatch
01557     (
01558         patchName,
01559         patchI,
01560         0,
01561         mesh().size(),
01562         "empty"
01563     );
01564 
01565     patches_.set(patchI, bpPtr);
01566 
01567     if (debug)
01568     {
01569         Pout<< "addPatch : patches now:" << endl;
01570 
01571         forAll(patches_, patchI)
01572         {
01573             const boundaryPatch& bp = patches_[patchI];
01574 
01575             Pout<< "    name  : " << bp.name() << endl
01576                 << "    size  : " << bp.size() << endl
01577                 << "    start : " << bp.start() << endl
01578                 << "    type  : " << bp.physicalType() << endl
01579                 << endl;
01580         }
01581     }
01582 }
01583 
01584 
01585 void Foam::boundaryMesh::deletePatch(const word& patchName)
01586 {
01587     label delPatchI = findPatchID(patchName);
01588 
01589     if (delPatchI == -1)
01590     {
01591         FatalErrorIn("boundaryMesh::deletePatch(const word&")
01592             << "Can't find patch named " << patchName
01593             << abort(FatalError);
01594     }
01595 
01596     if (patches_[delPatchI].size())
01597     {
01598         FatalErrorIn("boundaryMesh::deletePatch(const word&")
01599             << "Trying to delete non-empty patch " << patchName
01600             << endl << "Current size:" << patches_[delPatchI].size()
01601             << abort(FatalError);
01602     }
01603 
01604     PtrList<boundaryPatch> newPatches(patches_.size() - 1);
01605 
01606     for (label patchI = 0; patchI < delPatchI; patchI++)
01607     {
01608         newPatches.set(patchI, patches_[patchI].clone());
01609     }
01610 
01611     // Move patches down, starting from delPatchI.
01612 
01613     for (label patchI = delPatchI + 1; patchI < patches_.size(); patchI++)
01614     {
01615         newPatches.set(patchI - 1, patches_[patchI].clone());
01616     }
01617 
01618     patches_.clear();
01619 
01620     patches_ = newPatches;
01621 
01622     if (debug)
01623     {
01624         Pout<< "deletePatch : patches now:" << endl;
01625 
01626         forAll(patches_, patchI)
01627         {
01628             const boundaryPatch& bp = patches_[patchI];
01629 
01630             Pout<< "    name  : " << bp.name() << endl
01631                 << "    size  : " << bp.size() << endl
01632                 << "    start : " << bp.start() << endl
01633                 << "    type  : " << bp.physicalType() << endl
01634                 << endl;
01635         }
01636     }
01637 }
01638 
01639 
01640 void Foam::boundaryMesh::changePatchType
01641 (
01642     const word& patchName,
01643     const word& patchType
01644 )
01645 {
01646     label changeI = findPatchID(patchName);
01647 
01648     if (changeI == -1)
01649     {
01650         FatalErrorIn("boundaryMesh::changePatchType(const word&, const word&)")
01651             << "Can't find patch named " << patchName
01652             << abort(FatalError);
01653     }
01654 
01655 
01656     // Cause we can't reassign to individual PtrList elems ;-(
01657     // work on copy
01658 
01659 
01660     PtrList<boundaryPatch> newPatches(patches_.size());
01661 
01662     forAll(patches_, patchI)
01663     {
01664         if (patchI == changeI)
01665         {
01666             // Create copy but for type
01667             const boundaryPatch& oldBp = patches_[patchI];
01668 
01669             boundaryPatch* bpPtr = new boundaryPatch
01670             (
01671                 oldBp.name(),
01672                 oldBp.index(),
01673                 oldBp.size(),
01674                 oldBp.start(),
01675                 patchType
01676             );
01677 
01678             newPatches.set(patchI, bpPtr);
01679         }
01680         else
01681         {
01682             // Create copy
01683             newPatches.set(patchI, patches_[patchI].clone());
01684         }
01685     }
01686 
01687     patches_ = newPatches;
01688 }
01689 
01690 
01691 void Foam::boundaryMesh::changeFaces
01692 (
01693     const labelList& patchIDs,
01694     labelList& oldToNew
01695 )
01696 {
01697     if (patchIDs.size() != mesh().size())
01698     {
01699         FatalErrorIn("boundaryMesh::changeFaces(const labelList& patchIDs)")
01700             << "List of patchIDs not equal to number of faces." << endl
01701             << "PatchIDs size:" << patchIDs.size()
01702             << " nFaces::" << mesh().size()
01703             << abort(FatalError);
01704     }
01705 
01706     // Count number of faces for each patch
01707 
01708     labelList nFaces(patches_.size(), 0);
01709 
01710     forAll(patchIDs, faceI)
01711     {
01712         label patchID = patchIDs[faceI];
01713 
01714         if (patchID < 0 || patchID >= patches_.size())
01715         {
01716             FatalErrorIn("boundaryMesh::changeFaces(const labelList&)")
01717                 << "PatchID " << patchID << " out of range"
01718                 << abort(FatalError);
01719         }
01720         nFaces[patchID]++;
01721     }
01722 
01723 
01724     // Determine position in faces_ for each patch
01725 
01726     labelList startFace(patches_.size());
01727 
01728     startFace[0] = 0;
01729 
01730     for (label patchI = 1; patchI < patches_.size(); patchI++)
01731     {
01732         startFace[patchI] = startFace[patchI-1] + nFaces[patchI-1];
01733     }
01734 
01735     // Update patch info
01736     PtrList<boundaryPatch> newPatches(patches_.size());
01737 
01738     forAll(patches_, patchI)
01739     {
01740         const boundaryPatch& bp = patches_[patchI];
01741 
01742         newPatches.set
01743         (
01744             patchI,
01745             new boundaryPatch
01746             (
01747                 bp.name(),
01748                 patchI,
01749                 nFaces[patchI],
01750                 startFace[patchI],
01751                 bp.physicalType()
01752             )
01753         );
01754     }
01755     patches_ = newPatches;
01756 
01757     if (debug)
01758     {
01759         Pout<< "changeFaces : patches now:" << endl;
01760 
01761         forAll(patches_, patchI)
01762         {
01763             const boundaryPatch& bp = patches_[patchI];
01764 
01765             Pout<< "    name  : " << bp.name() << endl
01766                 << "    size  : " << bp.size() << endl
01767                 << "    start : " << bp.start() << endl
01768                 << "    type  : " << bp.physicalType() << endl
01769                 << endl;
01770         }
01771     }
01772 
01773 
01774     // Construct face mapping array
01775     oldToNew.setSize(patchIDs.size());
01776 
01777     forAll(patchIDs, faceI)
01778     {
01779         int patchID = patchIDs[faceI];
01780 
01781         oldToNew[faceI] = startFace[patchID]++;
01782     }
01783 
01784     // Copy faces into correct position and maintain label of original face
01785     faceList newFaces(mesh().size());
01786 
01787     labelList newMeshFace(mesh().size());
01788 
01789     forAll(oldToNew, faceI)
01790     {
01791         newFaces[oldToNew[faceI]] = mesh()[faceI];
01792         newMeshFace[oldToNew[faceI]] = meshFace_[faceI];
01793     }
01794 
01795     // Reconstruct 'mesh' from new faces and (copy of) existing points.
01796     bMesh* newMeshPtr_ = new bMesh(newFaces, mesh().points());
01797 
01798     // Reset meshFace_ to new ordering.
01799     meshFace_.transfer(newMeshFace);
01800 
01801 
01802     // Remove old PrimitivePatch on meshPtr_.
01803     clearOut();
01804 
01805     // And insert new 'mesh'.
01806     meshPtr_ = newMeshPtr_;
01807 }
01808 
01809 
01810 Foam::label Foam::boundaryMesh::getNTris(const label faceI) const
01811 {
01812     const face& f = mesh()[faceI];
01813 
01814     return f.nTriangles(mesh().points());
01815 }
01816 
01817 
01818 Foam::label Foam::boundaryMesh::getNTris
01819 (
01820     const label startFaceI,
01821     const label nFaces,
01822     labelList& nTris
01823 ) const
01824 {
01825     label totalNTris = 0;
01826 
01827     nTris.setSize(nFaces);
01828 
01829     for (label i = 0; i < nFaces; i++)
01830     {
01831         label faceNTris = getNTris(startFaceI + i);
01832 
01833         nTris[i] = faceNTris;
01834 
01835         totalNTris += faceNTris;
01836     }
01837     return totalNTris;
01838 }
01839 
01840 
01841 // Simple triangulation of face subset. Stores vertices in tris[] as three
01842 // consecutive vertices per triangle.
01843 void Foam::boundaryMesh::triangulate
01844 (
01845     const label startFaceI,
01846     const label nFaces,
01847     const label totalNTris,
01848     labelList& triVerts
01849 ) const
01850 {
01851     // Triangulate faces.
01852     triVerts.setSize(3*totalNTris);
01853 
01854     label vertI = 0;
01855 
01856     for (label i = 0; i < nFaces; i++)
01857     {
01858         label faceI = startFaceI + i;
01859 
01860         const face& f = mesh()[faceI];
01861 
01862         // Have face triangulate itself (results in faceList)
01863         faceList triFaces(f.nTriangles(mesh().points()));
01864 
01865         label nTri = 0;
01866 
01867         f.triangles(mesh().points(), nTri, triFaces);
01868 
01869         // Copy into triVerts
01870 
01871         forAll(triFaces, triFaceI)
01872         {
01873             const face& triF = triFaces[triFaceI];
01874 
01875             triVerts[vertI++] = triF[0];
01876             triVerts[vertI++] = triF[1];
01877             triVerts[vertI++] = triF[2];
01878         }
01879     }
01880 }
01881 
01882 
01883 // Number of local points in subset.
01884 Foam::label Foam::boundaryMesh::getNPoints
01885 (
01886     const label startFaceI,
01887     const label nFaces
01888 ) const
01889 {
01890     SubList<face> patchFaces(mesh(), nFaces, startFaceI);
01891 
01892     primitivePatch patch(patchFaces, mesh().points());
01893 
01894     return patch.nPoints();
01895 }
01896 
01897 
01898 // Triangulation of face subset in local coords.
01899 void Foam::boundaryMesh::triangulateLocal
01900 (
01901     const label startFaceI,
01902     const label nFaces,
01903     const label totalNTris,
01904     labelList& triVerts,
01905     labelList& localToGlobal
01906 ) const
01907 {
01908     SubList<face> patchFaces(mesh(), nFaces, startFaceI);
01909 
01910     primitivePatch patch(patchFaces, mesh().points());
01911 
01912     // Map from local to mesh().points() addressing
01913     localToGlobal = patch.meshPoints();
01914 
01915     // Triangulate patch faces.
01916     triVerts.setSize(3*totalNTris);
01917 
01918     label vertI = 0;
01919 
01920     for (label i = 0; i < nFaces; i++)
01921     {
01922         // Local face
01923         const face& f = patch.localFaces()[i];
01924 
01925         // Have face triangulate itself (results in faceList)
01926         faceList triFaces(f.nTriangles(patch.localPoints()));
01927 
01928         label nTri = 0;
01929 
01930         f.triangles(patch.localPoints(), nTri, triFaces);
01931 
01932         // Copy into triVerts
01933 
01934         forAll(triFaces, triFaceI)
01935         {
01936             const face& triF = triFaces[triFaceI];
01937 
01938             triVerts[vertI++] = triF[0];
01939             triVerts[vertI++] = triF[1];
01940             triVerts[vertI++] = triF[2];
01941         }
01942     }
01943 }
01944 
01945 
01946 void Foam::boundaryMesh::markFaces
01947 (
01948     const labelList& protectedEdges,
01949     const label seedFaceI,
01950     boolList& visited
01951 ) const
01952 {
01953     boolList protectedEdge(mesh().nEdges(), false);
01954 
01955     forAll(protectedEdges, i)
01956     {
01957         protectedEdge[protectedEdges[i]] = true;
01958     }
01959 
01960 
01961     // Initialize zone for all faces to -1
01962     labelList currentZone(mesh().size(), -1);
01963 
01964     // Mark with 0 all faces reachable from seedFaceI
01965     markZone(protectedEdge, seedFaceI, 0, currentZone);
01966 
01967     // Set in visited all reached ones.
01968     visited.setSize(mesh().size());
01969 
01970     forAll(currentZone, faceI)
01971     {
01972         if (currentZone[faceI] == 0)
01973         {
01974             visited[faceI] = true;
01975         }
01976         else
01977         {
01978             visited[faceI] = false;
01979         }
01980     }
01981 }
01982 
01983 
01984 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd