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

MeshedSurface.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 "MeshedSurface.H"
00028 #include "UnsortedMeshedSurface.H"
00029 #include "MeshedSurfaceProxy.H"
00030 #include "mergePoints.H"
00031 #include "Time.H"
00032 #include "ListOps.H"
00033 #include "polyBoundaryMesh.H"
00034 #include "polyMesh.H"
00035 #include "surfMesh.H"
00036 #include "primitivePatch.H"
00037 #include "addToRunTimeSelectionTable.H"
00038 
00039 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00040 
00041 template<class Face>
00042 inline bool Foam::MeshedSurface<Face>::isTri()
00043 {
00044     return false;
00045 }
00046 
00047 
00048 template<class Face>
00049 Foam::wordHashSet Foam::MeshedSurface<Face>::readTypes()
00050 {
00051     return wordHashSet(*fileExtensionConstructorTablePtr_);
00052 }
00053 
00054 
00055 template<class Face>
00056 Foam::wordHashSet Foam::MeshedSurface<Face>::writeTypes()
00057 {
00058     return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
00059 }
00060 
00061 
00062 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
00063 
00064 template<class Face>
00065 bool Foam::MeshedSurface<Face>::canReadType
00066 (
00067     const word& ext,
00068     const bool verbose
00069 )
00070 {
00071     return checkSupport
00072     (
00073         readTypes() | FriendType::readTypes(),
00074         ext,
00075         verbose,
00076         "reading"
00077    );
00078 }
00079 
00080 
00081 template<class Face>
00082 bool Foam::MeshedSurface<Face>::canWriteType
00083 (
00084     const word& ext,
00085     const bool verbose
00086 )
00087 {
00088     return checkSupport
00089     (
00090         writeTypes() | ProxyType::writeTypes(),
00091         ext,
00092         verbose,
00093         "writing"
00094     );
00095 }
00096 
00097 
00098 template<class Face>
00099 bool Foam::MeshedSurface<Face>::canRead
00100 (
00101     const fileName& name,
00102     const bool verbose
00103 )
00104 {
00105     word ext = name.ext();
00106     if (ext == "gz")
00107     {
00108         ext = name.lessExt().ext();
00109     }
00110     return canReadType(ext, verbose);
00111 }
00112 
00113 
00114 template<class Face>
00115 void Foam::MeshedSurface<Face>::write
00116 (
00117     const fileName& name,
00118     const MeshedSurface<Face>& surf
00119 )
00120 {
00121     if (debug)
00122     {
00123         Info<< "MeshedSurface::write"
00124             "(const fileName&, const MeshedSurface&) : "
00125             "writing to " << name
00126             << endl;
00127     }
00128 
00129     const word ext = name.ext();
00130 
00131     typename writefileExtensionMemberFunctionTable::iterator mfIter =
00132         writefileExtensionMemberFunctionTablePtr_->find(ext);
00133 
00134     if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
00135     {
00136         // no direct writer, delegate to proxy if possible
00137         wordHashSet supported = ProxyType::writeTypes();
00138 
00139         if (supported.found(ext))
00140         {
00141             MeshedSurfaceProxy<Face>(surf).write(name);
00142         }
00143         else
00144         {
00145             FatalErrorIn
00146             (
00147                 "MeshedSurface::write"
00148                 "(const fileName&, const MeshedSurface&)"
00149             )   << "Unknown file extension " << ext << nl << nl
00150                 << "Valid types are :" << endl
00151                 << (supported | writeTypes())
00152                 << exit(FatalError);
00153         }
00154     }
00155     else
00156     {
00157         mfIter()(name, surf);
00158     }
00159 }
00160 
00161 
00162 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00163 
00164 template<class Face>
00165 Foam::MeshedSurface<Face>::MeshedSurface()
00166 :
00167     ParentType(List<Face>(), pointField())
00168 {}
00169 
00170 
00171 template<class Face>
00172 Foam::MeshedSurface<Face>::MeshedSurface
00173 (
00174     const Xfer< pointField >& pointLst,
00175     const Xfer< List<Face> >& faceLst,
00176     const Xfer< surfZoneList >& zoneLst
00177 )
00178 :
00179     ParentType(List<Face>(), pointField()),
00180     zones_()
00181 {
00182     reset(pointLst, faceLst, zoneLst);
00183 }
00184 
00185 
00186 template<class Face>
00187 Foam::MeshedSurface<Face>::MeshedSurface
00188 (
00189     const Xfer< pointField >& pointLst,
00190     const Xfer< List<Face> >& faceLst,
00191     const UList<label>& zoneSizes,
00192     const UList<word>& zoneNames
00193 )
00194 :
00195     ParentType(List<Face>(), pointField())
00196 {
00197     reset(pointLst, faceLst, Xfer<surfZoneList>());
00198 
00199     if (zoneSizes.size())
00200     {
00201         if (zoneNames.size())
00202         {
00203             addZones(zoneSizes, zoneNames);
00204         }
00205         else
00206         {
00207             addZones(zoneSizes);
00208         }
00209     }
00210 }
00211 
00212 
00213 template<class Face>
00214 Foam::MeshedSurface<Face>::MeshedSurface
00215 (
00216     const MeshedSurface<Face>& surf
00217 )
00218 :
00219     ParentType(surf.faces(), surf.points()),
00220     zones_(surf.surfZones())
00221 {}
00222 
00223 
00224 template<class Face>
00225 Foam::MeshedSurface<Face>::MeshedSurface
00226 (
00227     const UnsortedMeshedSurface<Face>& surf
00228 )
00229 :
00230     ParentType(List<Face>(), surf.points())
00231 {
00232     labelList faceMap;
00233     this->storedZones().transfer(surf.sortedZones(faceMap));
00234 
00235     const List<Face>& origFaces = surf.faces();
00236     List<Face> newFaces(origFaces.size());
00237 
00238     // this is somewhat like ListOps reorder and/or IndirectList
00239     forAll(newFaces, faceI)
00240     {
00241         newFaces[faceI] = origFaces[faceMap[faceI]];
00242     }
00243 
00244     this->storedFaces().transfer(newFaces);
00245 }
00246 
00247 
00248 template<class Face>
00249 Foam::MeshedSurface<Face>::MeshedSurface(const surfMesh& mesh)
00250 :
00251     ParentType(List<Face>(), pointField())
00252 {
00253     // same face type as surfMesh
00254     MeshedSurface<face> surf
00255     (
00256         xferCopy(mesh.points()),
00257         xferCopy(mesh.faces()),
00258         xferCopy(mesh.surfZones())
00259     );
00260 
00261     this->transcribe(surf);
00262 }
00263 
00264 
00265 template<class Face>
00266 Foam::MeshedSurface<Face>::MeshedSurface
00267 (
00268     const polyBoundaryMesh& bMesh,
00269     const bool useGlobalPoints
00270 )
00271 :
00272     ParentType(List<Face>(), pointField())
00273 {
00274     const polyMesh& mesh = bMesh.mesh();
00275     const polyPatchList& bPatches = bMesh;
00276 
00277     // Get a single patch for all boundaries
00278     primitivePatch allBoundary
00279     (
00280         SubList<face>
00281         (
00282             mesh.faces(),
00283             mesh.nFaces() - mesh.nInternalFaces(),
00284             mesh.nInternalFaces()
00285         ),
00286         mesh.points()
00287     );
00288 
00289     // use global/local points:
00290     const pointField& bPoints =
00291     (
00292         useGlobalPoints ? mesh.points() : allBoundary.localPoints()
00293     );
00294 
00295     // global/local face addressing:
00296     const List<Face>& bFaces =
00297     (
00298         useGlobalPoints ? allBoundary : allBoundary.localFaces()
00299     );
00300 
00301 
00302     // create zone list
00303     surfZoneList newZones(bPatches.size());
00304 
00305     label startFaceI = 0;
00306     label nZone = 0;
00307     forAll(bPatches, patchI)
00308     {
00309         const polyPatch& p = bPatches[patchI];
00310 
00311         if (p.size())
00312         {
00313             newZones[nZone] = surfZone
00314             (
00315                 p.name(),
00316                 p.size(),
00317                 startFaceI,
00318                 nZone
00319             );
00320 
00321             nZone++;
00322             startFaceI += p.size();
00323         }
00324     }
00325 
00326     newZones.setSize(nZone);
00327 
00328     // same face type as the polyBoundaryMesh
00329     MeshedSurface<face> surf
00330     (
00331         xferCopy(bPoints),
00332         xferCopy(bFaces),
00333         xferMove(newZones)
00334     );
00335 
00336     this->transcribe(surf);
00337 }
00338 
00339 
00340 template<class Face>
00341 Foam::MeshedSurface<Face>::MeshedSurface
00342 (
00343     const fileName& name,
00344     const word& ext
00345 )
00346 :
00347     ParentType(List<Face>(), pointField())
00348 {
00349     read(name, ext);
00350 }
00351 
00352 
00353 template<class Face>
00354 Foam::MeshedSurface<Face>::MeshedSurface(const fileName& name)
00355 :
00356     ParentType(List<Face>(), pointField())
00357 {
00358     read(name);
00359 }
00360 
00361 
00362 template<class Face>
00363 Foam::MeshedSurface<Face>::MeshedSurface
00364 (
00365     const Time& t,
00366     const word& surfName
00367 )
00368 :
00369     ParentType(List<Face>(), pointField())
00370 {
00371     surfMesh mesh
00372     (
00373         IOobject
00374         (
00375             "dummyName",
00376             t.timeName(),
00377             t,
00378             IOobject::MUST_READ,
00379             IOobject::NO_WRITE,
00380             false
00381         ),
00382         surfName
00383     );
00384 
00385     // same face type as surfMesh
00386     MeshedSurface<face> surf
00387     (
00388         xferMove(mesh.storedPoints()),
00389         xferMove(mesh.storedFaces()),
00390         xferMove(mesh.storedZones())
00391     );
00392 
00393     this->transcribe(surf);
00394 }
00395 
00396 
00397 template<class Face>
00398 Foam::MeshedSurface<Face>::MeshedSurface
00399 (
00400     const Xfer< UnsortedMeshedSurface<Face> >& surf
00401 )
00402 :
00403     ParentType(List<Face>(), pointField())
00404 {
00405     transfer(surf());
00406 }
00407 
00408 
00409 template<class Face>
00410 Foam::MeshedSurface<Face>::MeshedSurface
00411 (
00412     const Xfer< MeshedSurface<Face> >& surf
00413 )
00414 :
00415     ParentType(List<Face>(), pointField())
00416 {
00417     transfer(surf());
00418 }
00419 
00420 
00421 
00422 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00423 
00424 template<class Face>
00425 Foam::MeshedSurface<Face>::~MeshedSurface()
00426 {}
00427 
00428 
00429 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00430 
00431 
00432 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
00433 
00434 template<class Face>
00435 void Foam::MeshedSurface<Face>::remapFaces
00436 (
00437     const UList<label>& faceMap
00438 )
00439 {
00440     // recalculate the zone start/size
00441     if (&faceMap && faceMap.size())
00442     {
00443         surfZoneList& zones = storedZones();
00444 
00445         if (zones.size() == 1)
00446         {
00447             // optimized for single zone case
00448             zones[0].size() = faceMap.size();
00449         }
00450         else if (zones.size())
00451         {
00452             label newFaceI = 0;
00453             label origEndI = 0;
00454             forAll(zones, zoneI)
00455             {
00456                 surfZone& zone = zones[zoneI];
00457 
00458                 // adjust zone start
00459                 zone.start() = newFaceI;
00460                 origEndI += zone.size();
00461 
00462                 for (label faceI = newFaceI; faceI < faceMap.size(); ++faceI)
00463                 {
00464                     if (faceMap[faceI] < origEndI)
00465                     {
00466                         ++newFaceI;
00467                     }
00468                     else
00469                     {
00470                         break;
00471                     }
00472                 }
00473 
00474                 // adjust zone size
00475                 zone.size() = newFaceI - zone.start();
00476             }
00477         }
00478     }
00479 }
00480 
00481 
00482 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00483 
00484 template<class Face>
00485 void Foam::MeshedSurface<Face>::clear()
00486 {
00487     ParentType::clearOut();
00488 
00489     storedPoints().clear();
00490     storedFaces().clear();
00491     storedZones().clear();
00492 }
00493 
00494 
00495 template<class Face>
00496 void Foam::MeshedSurface<Face>::movePoints(const pointField& newPoints)
00497 {
00498     // Remove all geometry dependent data
00499     ParentType::clearTopology();
00500 
00501     // Adapt for new point position
00502     ParentType::movePoints(newPoints);
00503 
00504     // Copy new points
00505     storedPoints() = newPoints;
00506 }
00507 
00508 
00509 template<class Face>
00510 void Foam::MeshedSurface<Face>::scalePoints(const scalar& scaleFactor)
00511 {
00512     // avoid bad scaling
00513     if (scaleFactor > 0 && scaleFactor != 1.0)
00514     {
00515         // Remove all geometry dependent data
00516         ParentType::clearTopology();
00517 
00518         // Adapt for new point position
00519         ParentType::movePoints(pointField());
00520 
00521         storedPoints() *= scaleFactor;
00522     }
00523 }
00524 
00525 
00526 template<class Face>
00527 void Foam::MeshedSurface<Face>::reset
00528 (
00529     const Xfer< pointField >& pointLst,
00530     const Xfer< List<Face> >& faceLst,
00531     const Xfer< surfZoneList >& zoneLst
00532 )
00533 {
00534     ParentType::clearOut();
00535 
00536     // Take over new primitive data.
00537     // Optimized to avoid overwriting data at all
00538     if (&pointLst)
00539     {
00540         storedPoints().transfer(pointLst());
00541     }
00542 
00543     if (&faceLst)
00544     {
00545         storedFaces().transfer(faceLst());
00546     }
00547 
00548     if (&zoneLst)
00549     {
00550         storedZones().transfer(zoneLst());
00551     }
00552 }
00553 
00554 
00555 template<class Face>
00556 void Foam::MeshedSurface<Face>::reset
00557 (
00558     const Xfer< List<point> >& pointLst,
00559     const Xfer< List<Face> >& faceLst,
00560     const Xfer< surfZoneList >& zoneLst
00561 )
00562 {
00563     ParentType::clearOut();
00564 
00565     // Take over new primitive data.
00566     // Optimized to avoid overwriting data at all
00567     if (&pointLst)
00568     {
00569         storedPoints().transfer(pointLst());
00570     }
00571 
00572     if (&faceLst)
00573     {
00574         storedFaces().transfer(faceLst());
00575     }
00576 
00577     if (&zoneLst)
00578     {
00579         storedZones().transfer(zoneLst());
00580     }
00581 }
00582 
00583 
00584 // Remove badly degenerate faces, double faces.
00585 template<class Face>
00586 void Foam::MeshedSurface<Face>::cleanup(const bool verbose)
00587 {
00588     // merge points (already done for STL, TRI)
00589     stitchFaces(SMALL, verbose);
00590 
00591     checkFaces(verbose);
00592     this->checkTopology(verbose);
00593 }
00594 
00595 
00596 template<class Face>
00597 bool Foam::MeshedSurface<Face>::stitchFaces
00598 (
00599     const scalar tol,
00600     const bool verbose
00601 )
00602 {
00603     pointField& pointLst = this->storedPoints();
00604 
00605     // Merge points
00606     labelList  pointMap(pointLst.size());
00607     pointField newPoints(pointLst.size());
00608 
00609     bool hasMerged = mergePoints(pointLst, tol, verbose, pointMap, newPoints);
00610 
00611     if (!hasMerged)
00612     {
00613         return false;
00614     }
00615 
00616     if (verbose)
00617     {
00618         Info<< "MeshedSurface::stitchFaces : Renumbering all faces"
00619             << endl;
00620     }
00621 
00622     // Set the coordinates to the merged ones
00623     pointLst.transfer(newPoints);
00624 
00625     List<Face>& faceLst = this->storedFaces();
00626 
00627     List<label> faceMap(faceLst.size());
00628 
00629     // Reset the point labels to the unique points array
00630     label newFaceI = 0;
00631     forAll(faceLst, faceI)
00632     {
00633         Face& f = faceLst[faceI];
00634         forAll(f, fp)
00635         {
00636             f[fp] = pointMap[f[fp]];
00637         }
00638 
00639         // for extra safety: collapse face as well
00640         if (f.collapse() >= 3)
00641         {
00642             if (newFaceI != faceI)
00643             {
00644                 faceLst[newFaceI] = f;
00645             }
00646             faceMap[newFaceI] = faceI;
00647             newFaceI++;
00648         }
00649         else if (verbose)
00650         {
00651             Pout<< "MeshedSurface::stitchFaces : "
00652                 << "Removing collapsed face " << faceI << endl
00653                 << "    vertices   :" << f << endl;
00654         }
00655     }
00656     pointMap.clear();
00657 
00658     if (newFaceI != faceLst.size())
00659     {
00660         if (verbose)
00661         {
00662             Pout<< "MeshedSurface::stitchFaces : "
00663                 << "Removed " << faceLst.size() - newFaceI
00664                 << " faces" << endl;
00665         }
00666         faceLst.setSize(newFaceI);
00667         remapFaces(faceMap);
00668     }
00669     faceMap.clear();
00670 
00671     // Merging points might have changed geometric factors
00672     ParentType::clearOut();
00673     return true;
00674 }
00675 
00676 
00677 // Remove badly degenerate faces and double faces.
00678 template<class Face>
00679 bool Foam::MeshedSurface<Face>::checkFaces
00680 (
00681     const bool verbose
00682 )
00683 {
00684     bool changed = false;
00685     List<Face>& faceLst = this->storedFaces();
00686 
00687     List<label> faceMap(faceLst.size());
00688 
00689     label newFaceI = 0;
00690     // Detect badly labelled faces and mark degenerate faces
00691     const label maxPointI = this->points().size() - 1;
00692     forAll(faceLst, faceI)
00693     {
00694         Face& f = faceLst[faceI];
00695 
00696         // avoid degenerate faces
00697         if (f.collapse() >= 3)
00698         {
00699             forAll(f, fp)
00700             {
00701                 if (f[fp] < 0 || f[fp] > maxPointI)
00702                 {
00703                     FatalErrorIn("MeshedSurface::checkFaces(bool)")
00704                         << "face " << f
00705                         << " uses point indices outside point range 0.."
00706                     << maxPointI
00707                         << exit(FatalError);
00708                 }
00709             }
00710 
00711             faceMap[faceI] = faceI;
00712             newFaceI++;
00713         }
00714         else
00715         {
00716             // mark as bad face
00717             faceMap[faceI] = -1;
00718 
00719             changed = true;
00720             if (verbose)
00721             {
00722                 WarningIn
00723                 (
00724                     "MeshedSurface::checkFaces(bool verbose)"
00725                 )   << "face[" << faceI << "] = " << f
00726                     << " does not have three unique vertices" << endl;
00727             }
00728         }
00729     }
00730 
00731     // Detect doubled faces
00732     // do not touch the faces
00733     const labelListList& fFaces = this->faceFaces();
00734     newFaceI = 0;
00735     forAll(faceLst, faceI)
00736     {
00737         // skip already collapsed faces:
00738         if (faceMap[faceI] < 0)
00739         {
00740             continue;
00741         }
00742 
00743         const Face& f = faceLst[faceI];
00744 
00745         // duplicate face check
00746         bool okay = true;
00747         const labelList& neighbours = fFaces[faceI];
00748 
00749         // Check if faceNeighbours use same points as this face.
00750         // Note: discards normal information - sides of baffle are merged.
00751         forAll(neighbours, neighI)
00752         {
00753             const label neiFaceI = neighbours[neighI];
00754 
00755             if (neiFaceI <= faceI || faceMap[neiFaceI] < 0)
00756             {
00757                 // lower numbered faces already checked
00758                 // skip neighbours that are themselves collapsed
00759                 continue;
00760             }
00761 
00762             const Face& nei = faceLst[neiFaceI];
00763 
00764             if (f == nei)
00765             {
00766                 okay = false;
00767 
00768                 if (verbose)
00769                 {
00770                     WarningIn
00771                     (
00772                         "MeshedSurface::checkFaces(bool verbose)"
00773                     )   << "faces share the same vertices:" << nl
00774                         << "    face[" << faceI << "] : " << f << nl
00775                         << "    face[" << neiFaceI << "] : " << nei << endl;
00776                     // printFace(Warning, "    ", f, points());
00777                     // printFace(Warning, "    ", nei, points());
00778                 }
00779 
00780                 break;
00781             }
00782         }
00783 
00784         if (okay)
00785         {
00786             faceMap[faceI] = faceI;
00787             newFaceI++;
00788         }
00789         else
00790         {
00791             faceMap[faceI] = -1;
00792         }
00793     }
00794 
00795     // Phase 1: pack
00796     // Done to keep numbering constant in phase 1
00797 
00798     if (changed || newFaceI < faceLst.size())
00799     {
00800         changed = true;
00801 
00802         if (verbose)
00803         {
00804             WarningIn
00805             (
00806                 "MeshedSurface::checkFaces(bool verbose)"
00807             )   << "Removed " << faceLst.size() - newFaceI
00808                 << " illegal faces." << endl;
00809         }
00810 
00811         // compress the face list
00812         newFaceI = 0;
00813         forAll(faceLst, faceI)
00814         {
00815             if (faceMap[faceI] >= 0)
00816             {
00817                 if (newFaceI != faceI)
00818                 {
00819                     faceLst[newFaceI] = faceLst[faceI];
00820                 }
00821                 faceMap[newFaceI] = faceI;
00822                 newFaceI++;
00823             }
00824         }
00825 
00826         faceLst.setSize(newFaceI);
00827         remapFaces(faceMap);
00828     }
00829     faceMap.clear();
00830 
00831     // Topology can change because of renumbering
00832     ParentType::clearOut();
00833     return changed;
00834 }
00835 
00836 
00837 template<class Face>
00838 Foam::label Foam::MeshedSurface<Face>::triangulate()
00839 {
00840     return triangulate
00841     (
00842         const_cast<List<label>&>(List<label>::null())
00843     );
00844 }
00845 
00846 
00847 template<class Face>
00848 Foam::label Foam::MeshedSurface<Face>::triangulate
00849 (
00850     List<label>& faceMapOut
00851 )
00852 {
00853     label nTri = 0;
00854     label maxTri = 0;  // the maximum number of triangles for any single face
00855     List<Face>& faceLst = this->storedFaces();
00856 
00857     // determine how many triangles will be needed
00858     forAll(faceLst, faceI)
00859     {
00860         const label n = faceLst[faceI].nTriangles();
00861         if (maxTri < n)
00862         {
00863             maxTri = n;
00864         }
00865         nTri += n;
00866     }
00867 
00868     // nothing to do
00869     if (nTri <= faceLst.size())
00870     {
00871         if (&faceMapOut)
00872         {
00873             faceMapOut.clear();
00874         }
00875         return 0;
00876     }
00877 
00878     List<Face>  newFaces(nTri);
00879     List<label> faceMap;
00880 
00881     // reuse storage from optional faceMap
00882     if (&faceMapOut)
00883     {
00884         faceMap.transfer(faceMapOut);
00885     }
00886     faceMap.setSize(nTri);
00887 
00888     // remember the number of *additional* faces
00889     nTri -= faceLst.size();
00890 
00891     if (this->points().empty())
00892     {
00893         // triangulate without points
00894         // simple face triangulation around f[0]
00895         label newFaceI = 0;
00896         forAll(faceLst, faceI)
00897         {
00898             const Face& f = faceLst[faceI];
00899 
00900             for (label fp = 1; fp < f.size() - 1; ++fp)
00901             {
00902                 label fp1 = f.fcIndex(fp);
00903 
00904                 newFaces[newFaceI] = triFace(f[0], f[fp], f[fp1]);
00905                 faceMap[newFaceI] = faceI;
00906                 newFaceI++;
00907             }
00908         }
00909     }
00910     else
00911     {
00912         // triangulate with points
00913         List<face> tmpTri(maxTri);
00914 
00915         label newFaceI = 0;
00916         forAll(faceLst, faceI)
00917         {
00918             // 'face' not '<Face>'
00919             const face& f = faceLst[faceI];
00920 
00921             label nTmp;
00922             f.triangles(this->points(), nTmp, tmpTri);
00923             for (label triI = 0; triI < nTmp; triI++)
00924             {
00925                 newFaces[newFaceI] = Face
00926                 (
00927                     static_cast<UList<label>&>(tmpTri[triI])
00928                 );
00929                 faceMap[newFaceI] = faceI;
00930                 newFaceI++;
00931             }
00932         }
00933     }
00934 
00935     faceLst.transfer(newFaces);
00936     remapFaces(faceMap);
00937 
00938     // optionally return the faceMap
00939     if (&faceMapOut)
00940     {
00941         faceMapOut.transfer(faceMap);
00942     }
00943     faceMap.clear();
00944 
00945     // Topology can change because of renumbering
00946     ParentType::clearOut();
00947     return nTri;
00948 }
00949 
00950 
00951 
00952 
00953 template<class Face>
00954 Foam::MeshedSurface<Face> Foam::MeshedSurface<Face>::subsetMesh
00955 (
00956     const labelHashSet& include,
00957     labelList& pointMap,
00958     labelList& faceMap
00959 ) const
00960 {
00961     const pointField& locPoints = this->localPoints();
00962     const List<Face>& locFaces  = this->localFaces();
00963 
00964 
00965     // Fill pointMap, faceMap
00966     PatchTools::subsetMap(*this, include, pointMap, faceMap);
00967 
00968     // Create compact coordinate list and forward mapping array
00969     pointField newPoints(pointMap.size());
00970     labelList oldToNew(locPoints.size());
00971     forAll(pointMap, pointI)
00972     {
00973         newPoints[pointI] = locPoints[pointMap[pointI]];
00974         oldToNew[pointMap[pointI]] = pointI;
00975     }
00976 
00977     // create/copy a new zones list, each zone with zero size
00978     surfZoneList newZones(this->surfZones());
00979     forAll(newZones, zoneI)
00980     {
00981         newZones[zoneI].size() = 0;
00982     }
00983 
00984     // Renumber face node labels
00985     List<Face> newFaces(faceMap.size());
00986     forAll(faceMap, faceI)
00987     {
00988         const label origFaceI = faceMap[faceI];
00989         newFaces[faceI] = Face(locFaces[origFaceI]);
00990 
00991         // Renumber labels for face
00992         Face& f = newFaces[faceI];
00993         forAll(f, fp)
00994         {
00995             f[fp] = oldToNew[f[fp]];
00996         }
00997     }
00998     oldToNew.clear();
00999 
01000     // recalculate the zones start/size
01001     label newFaceI = 0;
01002     label origEndI = 0;
01003 
01004     // adjust zone sizes
01005     forAll(newZones, zoneI)
01006     {
01007         surfZone& zone = newZones[zoneI];
01008 
01009         // adjust zone start
01010         zone.start() = newFaceI;
01011         origEndI += zone.size();
01012 
01013         for (label faceI = newFaceI; faceI < faceMap.size(); ++faceI)
01014         {
01015             if (faceMap[faceI] < origEndI)
01016             {
01017                 ++newFaceI;
01018             }
01019             else
01020             {
01021                 break;
01022             }
01023         }
01024 
01025         // adjust zone size
01026         zone.size() = newFaceI - zone.start();
01027     }
01028 
01029 
01030     // construct a sub-surface
01031     return MeshedSurface
01032     (
01033         xferMove(newPoints),
01034         xferMove(newFaces),
01035         xferMove(newZones)
01036     );
01037 }
01038 
01039 
01040 template<class Face>
01041 Foam::MeshedSurface<Face>
01042 Foam::MeshedSurface<Face>::subsetMesh
01043 (
01044     const labelHashSet& include
01045 ) const
01046 {
01047     labelList pointMap, faceMap;
01048     return subsetMesh(include, pointMap, faceMap);
01049 }
01050 
01051 
01052 
01053 template<class Face>
01054 void Foam::MeshedSurface<Face>::transfer
01055 (
01056     MeshedSurface<Face>& surf
01057 )
01058 {
01059     reset
01060     (
01061         xferMove(surf.storedPoints()),
01062         xferMove(surf.storedFaces()),
01063         xferMove(surf.storedZones())
01064     );
01065 }
01066 
01067 
01068 template<class Face>
01069 void Foam::MeshedSurface<Face>::transfer
01070 (
01071     UnsortedMeshedSurface<Face>& surf
01072 )
01073 {
01074     clear();
01075 
01076     labelList faceMap;
01077     surfZoneList zoneLst = surf.sortedZones(faceMap);
01078 
01079     if (zoneLst.size() <= 1)
01080     {
01081         reset
01082         (
01083             xferMove(surf.storedPoints()),
01084             xferMove(surf.storedFaces()),
01085             Xfer<surfZoneList>()
01086         );
01087     }
01088     else
01089     {
01090         List<Face>& oldFaces = surf.storedFaces();
01091         List<Face> newFaces(faceMap.size());
01092 
01093         forAll(faceMap, faceI)
01094         {
01095             newFaces[faceI].transfer(oldFaces[faceMap[faceI]]);
01096         }
01097 
01098         reset
01099         (
01100             xferMove(surf.storedPoints()),
01101             xferMove(newFaces),
01102             xferMove(zoneLst)
01103         );
01104     }
01105 
01106     faceMap.clear();
01107     surf.clear();
01108 }
01109 
01110 
01111 template<class Face>
01112 Foam::Xfer< Foam::MeshedSurface<Face> >
01113 Foam::MeshedSurface<Face>::xfer()
01114 {
01115     return xferMove(*this);
01116 }
01117 
01118 
01119 // Read from file, determine format from extension
01120 template<class Face>
01121 bool Foam::MeshedSurface<Face>::read(const fileName& name)
01122 {
01123     word ext = name.ext();
01124     if (ext == "gz")
01125     {
01126         fileName unzipName = name.lessExt();
01127         return read(unzipName, unzipName.ext());
01128     }
01129     else
01130     {
01131         return read(name, ext);
01132     }
01133 }
01134 
01135 
01136 // Read from file in given format
01137 template<class Face>
01138 bool Foam::MeshedSurface<Face>::read
01139 (
01140     const fileName& name,
01141     const word& ext
01142 )
01143 {
01144     clear();
01145 
01146     // read via selector mechanism
01147     transfer(New(name, ext)());
01148     return true;
01149 }
01150 
01151 
01152 template<class Face>
01153 void Foam::MeshedSurface<Face>::write
01154 (
01155     const Time& t,
01156     const word& surfName
01157 ) const
01158 {
01159     MeshedSurfaceProxy<Face>(*this).write(t, surfName);
01160 }
01161 
01162 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
01163 
01164 template<class Face>
01165 void Foam::MeshedSurface<Face>::operator=(const MeshedSurface& surf)
01166 {
01167     clear();
01168 
01169     this->storedPoints() = surf.points();
01170     this->storedFaces()  = surf.faces();
01171     this->storedZones()  = surf.surfZones();
01172 }
01173 
01174 
01175 template<class Face>
01176 Foam::MeshedSurface<Face>::operator
01177 Foam::MeshedSurfaceProxy<Face>() const
01178 {
01179     return MeshedSurfaceProxy<Face>
01180     (
01181         this->points(),
01182         this->faces(),
01183         this->surfZones()
01184     );
01185 }
01186 
01187 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
01188 
01189 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
01190 
01191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
01192 
01193 #include "MeshedSurfaceZones.C"
01194 #include "MeshedSurfaceIO.C"
01195 #include "MeshedSurfaceNew.C"
01196 
01197 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd