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

triSurface.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 "triSurface.H"
00028 #include "demandDrivenData.H"
00029 #include "IFstream.H"
00030 #include "OFstream.H"
00031 #include "Time.H"
00032 #include "boundBox.H"
00033 #include "SortableList.H"
00034 #include "PackedBoolList.H"
00035 
00036 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 namespace Foam
00040 {
00041     defineTypeNameAndDebug(Foam::triSurface, 0);
00042 }
00043 
00044 
00045 Foam::fileName Foam::triSurface::triSurfInstance(const Time& d)
00046 {
00047     fileName foamName(d.caseName() + ".ftr");
00048 
00049     // Search back through the time directories list to find the time
00050     // closest to and lower than current time
00051 
00052     instantList ts = d.times();
00053     label i;
00054 
00055     for (i=ts.size()-1; i>=0; i--)
00056     {
00057         if (ts[i].value() <= d.timeOutputValue())
00058         {
00059             break;
00060         }
00061     }
00062 
00063     // Noting that the current directory has already been searched
00064     // for mesh data, start searching from the previously stored time directory
00065 
00066     if (i>=0)
00067     {
00068         for (label j=i; j>=0; j--)
00069         {
00070             if (isFile(d.path()/ts[j].name()/typeName/foamName))
00071             {
00072                 if (debug)
00073                 {
00074                     Pout<< " triSurface::triSurfInstance(const Time& d)"
00075                         << "reading " << foamName
00076                         << " from " << ts[j].name()/typeName
00077                         << endl;
00078                 }
00079 
00080                 return ts[j].name();
00081             }
00082         }
00083     }
00084 
00085     if (debug)
00086     {
00087         Pout<< " triSurface::triSurfInstance(const Time& d)"
00088             << "reading " << foamName
00089             << " from constant/" << endl;
00090     }
00091     return "constant";
00092 }
00093 
00094 
00095 Foam::List<Foam::labelledTri> Foam::triSurface::convertToTri
00096 (
00097     const faceList& faces,
00098     const label defaultRegion
00099 )
00100 {
00101     List<labelledTri> triFaces(faces.size());
00102 
00103     forAll(triFaces, faceI)
00104     {
00105         const face& f = faces[faceI];
00106 
00107         if (f.size() != 3)
00108         {
00109             FatalErrorIn
00110             (
00111                 "triSurface::convertToTri"
00112                 "(const faceList&, const label)"
00113             )   << "Face at position " << faceI
00114                 << " does not have three vertices:" << f
00115                 << abort(FatalError);
00116         }
00117 
00118         labelledTri& tri = triFaces[faceI];
00119 
00120         tri[0] = f[0];
00121         tri[1] = f[1];
00122         tri[2] = f[2];
00123         tri.region() = defaultRegion;
00124     }
00125 
00126     return triFaces;
00127 }
00128 
00129 
00130 Foam::List<Foam::labelledTri> Foam::triSurface::convertToTri
00131 (
00132     const triFaceList& faces,
00133     const label defaultRegion
00134 )
00135 {
00136     List<labelledTri> triFaces(faces.size());
00137 
00138     forAll(triFaces, faceI)
00139     {
00140         const triFace& f = faces[faceI];
00141 
00142         labelledTri& tri = triFaces[faceI];
00143 
00144         tri[0] = f[0];
00145         tri[1] = f[1];
00146         tri[2] = f[2];
00147         tri.region() = defaultRegion;
00148     }
00149 
00150     return triFaces;
00151 }
00152 
00153 
00154 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00155 
00156 void Foam::triSurface::printTriangle
00157 (
00158     Ostream& os,
00159     const string& pre,
00160     const labelledTri& f,
00161     const pointField& points
00162 )
00163 {
00164     os
00165         << pre.c_str() << "vertex numbers:"
00166         << f[0] << ' ' << f[1] << ' ' << f[2] << endl
00167         << pre.c_str() << "vertex coords :"
00168         << points[f[0]] << ' ' << points[f[1]] << ' ' << points[f[2]]
00169         << pre.c_str() << "region        :" << f.region() << endl
00170         << endl;
00171 }
00172 
00173 
00174 Foam::string Foam::triSurface::getLineNoComment(IFstream& is)
00175 {
00176     string line;
00177     do
00178     {
00179         is.getLine(line);
00180     }
00181     while ((line.empty() || line[0] == '#') && is.good());
00182 
00183     return line;
00184 }
00185 
00186 
00187 // Remove non-triangles, double triangles.
00188 void Foam::triSurface::checkTriangles(const bool verbose)
00189 {
00190     // Simple check on indices ok.
00191     const label maxPointI = points().size() - 1;
00192 
00193     forAll(*this, faceI)
00194     {
00195         const labelledTri& f = (*this)[faceI];
00196 
00197         if
00198         (
00199             (f[0] < 0) || (f[0] > maxPointI)
00200          || (f[1] < 0) || (f[1] > maxPointI)
00201          || (f[2] < 0) || (f[2] > maxPointI)
00202         )
00203         {
00204             FatalErrorIn("triSurface::checkTriangles(bool)")
00205                 << "triangle " << f
00206                 << " uses point indices outside point range 0.."
00207                 << maxPointI
00208                 << exit(FatalError);
00209         }
00210     }
00211 
00212     // Two phase process
00213     //   1. mark invalid faces
00214     //   2. pack
00215     // Done to keep numbering constant in phase 1
00216 
00217     // List of valid triangles
00218     boolList valid(size(), true);
00219     bool hasInvalid = false;
00220 
00221     const labelListList& fFaces = faceFaces();
00222 
00223     forAll(*this, faceI)
00224     {
00225         const labelledTri& f = (*this)[faceI];
00226 
00227         if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2]))
00228         {
00229             // 'degenerate' triangle check
00230             valid[faceI] = false;
00231             hasInvalid = true;
00232 
00233             if (verbose)
00234             {
00235                 WarningIn
00236                 (
00237                     "triSurface::checkTriangles(bool verbose)"
00238                 )   << "triangle " << faceI
00239                     << " does not have three unique vertices:\n";
00240                 printTriangle(Warning, "    ", f, points());
00241             }
00242         }
00243         else
00244         {
00245             // duplicate triangle check
00246             const labelList& neighbours = fFaces[faceI];
00247 
00248             // Check if faceNeighbours use same points as this face.
00249             // Note: discards normal information - sides of baffle are merged.
00250             forAll(neighbours, neighbourI)
00251             {
00252                 if (neighbours[neighbourI] <= faceI)
00253                 {
00254                     // lower numbered faces already checked
00255                     continue;
00256                 }
00257 
00258                 const labelledTri& n = (*this)[neighbours[neighbourI]];
00259 
00260                 if
00261                 (
00262                     ((f[0] == n[0]) || (f[0] == n[1]) || (f[0] == n[2]))
00263                  && ((f[1] == n[0]) || (f[1] == n[1]) || (f[1] == n[2]))
00264                  && ((f[2] == n[0]) || (f[2] == n[1]) || (f[2] == n[2]))
00265                 )
00266                 {
00267                     valid[faceI] = false;
00268                     hasInvalid = true;
00269 
00270                     if (verbose)
00271                     {
00272                         WarningIn
00273                         (
00274                             "triSurface::checkTriangles(bool verbose)"
00275                         )   << "triangles share the same vertices:\n"
00276                             << "    face 1 :" << faceI << endl;
00277                         printTriangle(Warning, "    ", f, points());
00278 
00279                         Warning
00280                             << endl
00281                             << "    face 2 :"
00282                             << neighbours[neighbourI] << endl;
00283                         printTriangle(Warning, "    ", n, points());
00284                     }
00285 
00286                     break;
00287                 }
00288             }
00289         }
00290     }
00291 
00292     if (hasInvalid)
00293     {
00294         // Pack
00295         label newFaceI = 0;
00296         forAll(*this, faceI)
00297         {
00298             if (valid[faceI])
00299             {
00300                 const labelledTri& f = (*this)[faceI];
00301                 (*this)[newFaceI++] = f;
00302             }
00303         }
00304 
00305         if (verbose)
00306         {
00307             WarningIn
00308             (
00309                 "triSurface::checkTriangles(bool verbose)"
00310             )   << "Removing " << size() - newFaceI
00311                 << " illegal faces." << endl;
00312         }
00313         (*this).setSize(newFaceI);
00314 
00315         // Topology can change because of renumbering
00316         clearOut();
00317     }
00318 }
00319 
00320 
00321 // Check/fix edges with more than two triangles
00322 void Foam::triSurface::checkEdges(const bool verbose)
00323 {
00324     const labelListList& eFaces = edgeFaces();
00325 
00326     forAll(eFaces, edgeI)
00327     {
00328         const labelList& myFaces = eFaces[edgeI];
00329 
00330         if (myFaces.empty())
00331         {
00332             FatalErrorIn("triSurface::checkEdges(bool verbose)")
00333                 << "Edge " << edgeI << " with vertices " << edges()[edgeI]
00334                 << " has no edgeFaces"
00335                 << exit(FatalError);
00336         }
00337         else if (myFaces.size() > 2)
00338         {
00339             WarningIn
00340             (
00341                 "triSurface::checkEdges(bool verbose)"
00342             )   << "Edge " << edgeI << " with vertices " << edges()[edgeI]
00343                 << " has more than 2 faces connected to it : " << myFaces
00344                 << endl;
00345         }
00346     }
00347 }
00348 
00349 
00350 // Read triangles, points from Istream
00351 bool Foam::triSurface::read(Istream& is)
00352 {
00353     is  >> patches_ >> storedPoints() >> storedFaces();
00354 
00355     return true;
00356 }
00357 
00358 
00359 // Read from file in given format
00360 bool Foam::triSurface::read
00361 (
00362     const fileName& name,
00363     const word& ext,
00364     const bool check
00365 )
00366 {
00367     if (check && !exists(name))
00368     {
00369         FatalErrorIn
00370         (
00371             "triSurface::read(const fileName&, const word&, const bool)"
00372         )   << "Cannnot read " << name << exit(FatalError);
00373     }
00374 
00375     if (ext == "gz")
00376     {
00377         fileName unzipName = name.lessExt();
00378 
00379         // Do not check for existence. Let IFstream do the unzipping.
00380         return read(unzipName, unzipName.ext(), false);
00381     }
00382     else if (ext == "ftr")
00383     {
00384         return read(IFstream(name)());
00385     }
00386     else if (ext == "stl")
00387     {
00388         return readSTL(name);
00389     }
00390     else if (ext == "stlb")
00391     {
00392         return readSTL(name);
00393     }
00394     else if (ext == "gts")
00395     {
00396         return readGTS(name);
00397     }
00398     else if (ext == "obj")
00399     {
00400         return readOBJ(name);
00401     }
00402     else if (ext == "off")
00403     {
00404         return readOFF(name);
00405     }
00406     else if (ext == "tri")
00407     {
00408         return readTRI(name);
00409     }
00410     else if (ext == "ac")
00411     {
00412         return readAC(name);
00413     }
00414     else if (ext == "nas")
00415     {
00416         return readNAS(name);
00417     }
00418     else
00419     {
00420         FatalErrorIn
00421         (
00422             "triSurface::read(const fileName&, const word&)"
00423         )   << "unknown file extension " << ext
00424             << ". Supported extensions are '.ftr', '.stl', '.stlb', '.gts'"
00425             << ", '.obj', '.ac', '.off', '.nas' and '.tri'"
00426             << exit(FatalError);
00427 
00428         return false;
00429     }
00430 }
00431 
00432 
00433 // Write to file in given format
00434 void Foam::triSurface::write
00435 (
00436     const fileName& name,
00437     const word& ext,
00438     const bool sort
00439 ) const
00440 {
00441     if (ext == "ftr")
00442     {
00443         return write(OFstream(name)());
00444     }
00445     else if (ext == "stl")
00446     {
00447         return writeSTLASCII(OFstream(name)());
00448     }
00449     else if (ext == "stlb")
00450     {
00451         ofstream outFile(name.c_str(), std::ios::binary);
00452 
00453         writeSTLBINARY(outFile);
00454     }
00455     else if (ext == "gts")
00456     {
00457         return writeGTS(sort, OFstream(name)());
00458     }
00459     else if (ext == "obj")
00460     {
00461         writeOBJ(sort, OFstream(name)());
00462     }
00463     else if (ext == "off")
00464     {
00465         writeOFF(sort, OFstream(name)());
00466     }
00467     else if (ext == "vtk")
00468     {
00469         writeVTK(sort, OFstream(name)());
00470     }
00471     else if (ext == "tri")
00472     {
00473         writeTRI(sort, OFstream(name)());
00474     }
00475     else if (ext == "dx")
00476     {
00477         writeDX(sort, OFstream(name)());
00478     }
00479     else if (ext == "ac")
00480     {
00481         writeAC(OFstream(name)());
00482     }
00483     else if (ext == "smesh")
00484     {
00485         writeSMESH(sort, OFstream(name)());
00486     }
00487     else
00488     {
00489         FatalErrorIn
00490         (
00491             "triSurface::write(const fileName&, const word&, const bool)"
00492         )   << "unknown file extension " << ext
00493             << ". Supported extensions are '.ftr', '.stl', '.stlb', "
00494             << "'.gts', '.obj', '.vtk'"
00495             << ", '.off', '.dx', '.smesh', '.ac' and '.tri'"
00496             << exit(FatalError);
00497     }
00498 }
00499 
00500 
00501 // Returns patch info. Sets faceMap to the indexing according to patch
00502 // numbers. Patch numbers start at 0.
00503 Foam::surfacePatchList Foam::triSurface::calcPatches(labelList& faceMap) const
00504 {
00505     // Sort according to region numbers of labelledTri
00506     SortableList<label> sortedRegion(size());
00507 
00508     forAll(sortedRegion, faceI)
00509     {
00510         sortedRegion[faceI] = operator[](faceI).region();
00511     }
00512     sortedRegion.sort();
00513 
00514     faceMap = sortedRegion.indices();
00515 
00516     // Extend regions
00517     label maxRegion = patches_.size()-1;    // for non-compacted regions
00518 
00519     if (faceMap.size())
00520     {
00521         maxRegion = max
00522         (
00523             maxRegion,
00524             operator[](faceMap[faceMap.size() - 1]).region()
00525         );
00526     }
00527 
00528     // Get new region list
00529     surfacePatchList newPatches(maxRegion + 1);
00530 
00531     // Fill patch sizes
00532     forAll(*this, faceI)
00533     {
00534         label region = operator[](faceI).region();
00535 
00536         newPatches[region].size()++;
00537     }
00538 
00539 
00540     // Fill rest of patch info
00541 
00542     label startFaceI = 0;
00543     forAll(newPatches, newPatchI)
00544     {
00545         surfacePatch& newPatch = newPatches[newPatchI];
00546 
00547         label oldPatchI = newPatchI;
00548 
00549         // start of patch
00550         newPatch.start() = startFaceI;
00551 
00552 
00553         // Take over any information from existing patches
00554         if ((oldPatchI < patches_.size()) && (patches_[oldPatchI].name() != ""))
00555         {
00556             newPatch.name() = patches_[oldPatchI].name();
00557         }
00558         else
00559         {
00560             newPatch.name() = word("patch") + name(newPatchI);
00561         }
00562 
00563         if
00564         (
00565             (oldPatchI < patches_.size())
00566          && (patches_[oldPatchI].geometricType() != "")
00567         )
00568         {
00569             newPatch.geometricType() = patches_[oldPatchI].geometricType();
00570         }
00571         else
00572         {
00573             newPatch.geometricType() = "empty";
00574         }
00575 
00576         startFaceI += newPatch.size();
00577     }
00578 
00579     return newPatches;
00580 }
00581 
00582 
00583 void Foam::triSurface::setDefaultPatches()
00584 {
00585     labelList faceMap;
00586 
00587     // Get names, types and sizes
00588     surfacePatchList newPatches(calcPatches(faceMap));
00589 
00590     // Take over names and types (but not size)
00591     patches_.setSize(newPatches.size());
00592 
00593     forAll(newPatches, patchI)
00594     {
00595         patches_[patchI].name() = newPatches[patchI].name();
00596         patches_[patchI].geometricType() = newPatches[patchI].geometricType();
00597     }
00598 }
00599 
00600 
00601 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00602 
00603 Foam::triSurface::triSurface()
00604 :
00605     ParentType(List<Face>(), pointField()),
00606     patches_(0),
00607     sortedEdgeFacesPtr_(NULL),
00608     edgeOwnerPtr_(NULL)
00609 {}
00610 
00611 
00612 
00613 Foam::triSurface::triSurface
00614 (
00615     const List<labelledTri>& triangles,
00616     const geometricSurfacePatchList& patches,
00617     const pointField& points
00618 )
00619 :
00620     ParentType(triangles, points),
00621     patches_(patches),
00622     sortedEdgeFacesPtr_(NULL),
00623     edgeOwnerPtr_(NULL)
00624 {}
00625 
00626 
00627 Foam::triSurface::triSurface
00628 (
00629     List<labelledTri>& triangles,
00630     const geometricSurfacePatchList& patches,
00631     pointField& points,
00632     const bool reUse
00633 )
00634 :
00635     ParentType(triangles, points, reUse),
00636     patches_(patches),
00637     sortedEdgeFacesPtr_(NULL),
00638     edgeOwnerPtr_(NULL)
00639 {}
00640 
00641 
00642 Foam::triSurface::triSurface
00643 (
00644     const List<labelledTri>& triangles,
00645     const pointField& points
00646 )
00647 :
00648     ParentType(triangles, points),
00649     patches_(),
00650     sortedEdgeFacesPtr_(NULL),
00651     edgeOwnerPtr_(NULL)
00652 {
00653     setDefaultPatches();
00654 }
00655 
00656 
00657 Foam::triSurface::triSurface
00658 (
00659     const triFaceList& triangles,
00660     const pointField& points
00661 )
00662 :
00663     ParentType(convertToTri(triangles, 0), points),
00664     patches_(),
00665     sortedEdgeFacesPtr_(NULL),
00666     edgeOwnerPtr_(NULL)
00667 {
00668     setDefaultPatches();
00669 }
00670 
00671 
00672 Foam::triSurface::triSurface(const fileName& name)
00673 :
00674     ParentType(List<Face>(), pointField()),
00675     patches_(),
00676     sortedEdgeFacesPtr_(NULL),
00677     edgeOwnerPtr_(NULL)
00678 {
00679     word ext = name.ext();
00680 
00681     read(name, ext);
00682 
00683     setDefaultPatches();
00684 }
00685 
00686 
00687 Foam::triSurface::triSurface(Istream& is)
00688 :
00689     ParentType(List<Face>(), pointField()),
00690     patches_(),
00691     sortedEdgeFacesPtr_(NULL),
00692     edgeOwnerPtr_(NULL)
00693 {
00694     read(is);
00695 
00696     setDefaultPatches();
00697 }
00698 
00699 
00700 Foam::triSurface::triSurface(const Time& d)
00701 :
00702     ParentType(List<Face>(), pointField()),
00703     patches_(),
00704     sortedEdgeFacesPtr_(NULL),
00705     edgeOwnerPtr_(NULL)
00706 {
00707     fileName foamFile(d.caseName() + ".ftr");
00708 
00709     fileName foamPath(d.path()/triSurfInstance(d)/typeName/foamFile);
00710 
00711     IFstream foamStream(foamPath);
00712 
00713     read(foamStream);
00714 
00715     setDefaultPatches();
00716 }
00717 
00718 
00719 Foam::triSurface::triSurface(const triSurface& ts)
00720 :
00721     ParentType(ts, ts.points()),
00722     patches_(ts.patches()),
00723     sortedEdgeFacesPtr_(NULL),
00724     edgeOwnerPtr_(NULL)
00725 {}
00726 
00727 
00728 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00729 
00730 Foam::triSurface::~triSurface()
00731 {
00732     clearOut();
00733 }
00734 
00735 
00736 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00737 
00738 void Foam::triSurface::clearTopology()
00739 {
00740     ParentType::clearTopology();
00741     deleteDemandDrivenData(sortedEdgeFacesPtr_);
00742     deleteDemandDrivenData(edgeOwnerPtr_);
00743 }
00744 
00745 
00746 void Foam::triSurface::clearPatchMeshAddr()
00747 {
00748     ParentType::clearPatchMeshAddr();
00749 }
00750 
00751 
00752 void Foam::triSurface::clearOut()
00753 {
00754     ParentType::clearOut();
00755 
00756     clearTopology();
00757     clearPatchMeshAddr();
00758 }
00759 
00760 
00761 const Foam::labelListList& Foam::triSurface::sortedEdgeFaces() const
00762 {
00763     if (!sortedEdgeFacesPtr_)
00764     {
00765         calcSortedEdgeFaces();
00766     }
00767 
00768     return *sortedEdgeFacesPtr_;
00769 }
00770 
00771 
00772 const Foam::labelList& Foam::triSurface::edgeOwner() const
00773 {
00774     if (!edgeOwnerPtr_)
00775     {
00776         calcEdgeOwner();
00777     }
00778 
00779     return *edgeOwnerPtr_;
00780 }
00781 
00782 
00783 //- Move points
00784 void Foam::triSurface::movePoints(const pointField& newPoints)
00785 {
00786     // Remove all geometry dependent data
00787     deleteDemandDrivenData(sortedEdgeFacesPtr_);
00788 
00789     // Adapt for new point position
00790     ParentType::movePoints(newPoints);
00791 
00792     // Copy new points
00793     storedPoints() = newPoints;
00794 }
00795 
00796 
00797 // scale points
00798 void Foam::triSurface::scalePoints(const scalar& scaleFactor)
00799 {
00800     // avoid bad scaling
00801     if (scaleFactor > 0 && scaleFactor != 1.0)
00802     {
00803         // Remove all geometry dependent data
00804         clearTopology();
00805 
00806         // Adapt for new point position
00807         ParentType::movePoints(pointField());
00808 
00809         storedPoints() *= scaleFactor;
00810     }
00811 }
00812 
00813 
00814 // Remove non-triangles, double triangles.
00815 void Foam::triSurface::cleanup(const bool verbose)
00816 {
00817     // Merge points (already done for STL, TRI)
00818     stitchTriangles(pointField(points()), SMALL, verbose);
00819 
00820     // Merging points might have changed geometric factors
00821     clearOut();
00822 
00823     checkTriangles(verbose);
00824 
00825     checkEdges(verbose);
00826 }
00827 
00828 
00829 // Finds area, starting at faceI, delimited by borderEdge. Marks all visited
00830 // faces (from face-edge-face walk) with currentZone.
00831 void Foam::triSurface::markZone
00832 (
00833     const boolList& borderEdge,
00834     const label faceI,
00835     const label currentZone,
00836     labelList& faceZone
00837 ) const
00838 {
00839     // List of faces whose faceZone has been set.
00840     labelList changedFaces(1, faceI);
00841 
00842     while(true)
00843     {
00844         // Pick up neighbours of changedFaces
00845         DynamicList<label> newChangedFaces(2*changedFaces.size());
00846 
00847         forAll(changedFaces, i)
00848         {
00849             label faceI = changedFaces[i];
00850 
00851             const labelList& fEdges = faceEdges()[faceI];
00852 
00853             forAll(fEdges, i)
00854             {
00855                 label edgeI = fEdges[i];
00856 
00857                 if (!borderEdge[edgeI])
00858                 {
00859                     const labelList& eFaces = edgeFaces()[edgeI];
00860 
00861                     forAll(eFaces, j)
00862                     {
00863                         label nbrFaceI = eFaces[j];
00864 
00865                         if (faceZone[nbrFaceI] == -1)
00866                         {
00867                             faceZone[nbrFaceI] = currentZone;
00868                             newChangedFaces.append(nbrFaceI);
00869                         }
00870                         else if (faceZone[nbrFaceI] != currentZone)
00871                         {
00872                             FatalErrorIn
00873                             (
00874                                 "triSurface::markZone(const boolList&,"
00875                                 "const label, const label, labelList&) const"
00876                             )
00877                                 << "Zones " << faceZone[nbrFaceI]
00878                                 << " at face " << nbrFaceI
00879                                 << " connects to zone " << currentZone
00880                                 << " at face " << faceI
00881                                 << abort(FatalError);
00882                         }
00883                     }
00884                 }
00885             }
00886         }
00887 
00888         if (newChangedFaces.empty())
00889         {
00890             break;
00891         }
00892 
00893         changedFaces.transfer(newChangedFaces);
00894     }
00895 }
00896 
00897 
00898 // Finds areas delimited by borderEdge (or 'real' edges).
00899 // Fills faceZone accordingly
00900 Foam::label Foam::triSurface::markZones
00901 (
00902     const boolList& borderEdge,
00903     labelList& faceZone
00904 ) const
00905 {
00906     faceZone.setSize(size());
00907     faceZone = -1;
00908 
00909     if (borderEdge.size() != nEdges())
00910     {
00911         FatalErrorIn
00912         (
00913             "triSurface::markZones"
00914             "(const boolList&, labelList&)"
00915         )
00916             << "borderEdge boolList not same size as number of edges" << endl
00917             << "borderEdge:" << borderEdge.size() << endl
00918             << "nEdges    :" << nEdges()
00919             << exit(FatalError);
00920     }
00921 
00922     label zoneI = 0;
00923 
00924     for (label startFaceI = 0;; zoneI++)
00925     {
00926         // Find first non-coloured face
00927         for (; startFaceI < size(); startFaceI++)
00928         {
00929             if (faceZone[startFaceI] == -1)
00930             {
00931                 break;
00932             }
00933         }
00934 
00935         if (startFaceI >= size())
00936         {
00937             break;
00938         }
00939 
00940         faceZone[startFaceI] = zoneI;
00941 
00942         markZone(borderEdge, startFaceI, zoneI, faceZone);
00943     }
00944 
00945     return zoneI;
00946 }
00947 
00948 
00949 void Foam::triSurface::subsetMeshMap
00950 (
00951     const boolList& include,
00952     labelList& pointMap,
00953     labelList& faceMap
00954 ) const
00955 {
00956     const List<labelledTri>& locFaces = localFaces();
00957 
00958     label faceI = 0;
00959     label pointI = 0;
00960 
00961     faceMap.setSize(locFaces.size());
00962 
00963     pointMap.setSize(nPoints());
00964 
00965     boolList pointHad(nPoints(), false);
00966 
00967     forAll(include, oldFacei)
00968     {
00969         if (include[oldFacei])
00970         {
00971             // Store new faces compact
00972             faceMap[faceI++] = oldFacei;
00973 
00974             // Renumber labels for triangle
00975             const labelledTri& tri = locFaces[oldFacei];
00976 
00977             label a = tri[0];
00978             if (!pointHad[a])
00979             {
00980                 pointHad[a] = true;
00981                 pointMap[pointI++] = a;
00982             }
00983 
00984             label b = tri[1];
00985             if (!pointHad[b])
00986             {
00987                 pointHad[b] = true;
00988                 pointMap[pointI++] = b;
00989             }
00990 
00991             label c = tri[2];
00992             if (!pointHad[c])
00993             {
00994                 pointHad[c] = true;
00995                 pointMap[pointI++] = c;
00996             }
00997         }
00998     }
00999 
01000     // Trim
01001     faceMap.setSize(faceI);
01002 
01003     pointMap.setSize(pointI);
01004 }
01005 
01006 
01007 Foam::triSurface Foam::triSurface::subsetMesh
01008 (
01009     const boolList& include,
01010     labelList& pointMap,
01011     labelList& faceMap
01012 ) const
01013 {
01014     const pointField& locPoints = localPoints();
01015     const List<labelledTri>& locFaces = localFaces();
01016 
01017     // Fill pointMap, faceMap
01018     subsetMeshMap(include, pointMap, faceMap);
01019 
01020 
01021     // Create compact coordinate list and forward mapping array
01022     pointField newPoints(pointMap.size());
01023     labelList oldToNew(locPoints.size());
01024     forAll(pointMap, pointi)
01025     {
01026         newPoints[pointi] = locPoints[pointMap[pointi]];
01027         oldToNew[pointMap[pointi]] = pointi;
01028     }
01029 
01030     // Renumber triangle node labels and compact
01031     List<labelledTri> newTriangles(faceMap.size());
01032 
01033     forAll(faceMap, facei)
01034     {
01035         // Get old vertex labels
01036         const labelledTri& tri = locFaces[faceMap[facei]];
01037 
01038         newTriangles[facei][0] = oldToNew[tri[0]];
01039         newTriangles[facei][1] = oldToNew[tri[1]];
01040         newTriangles[facei][2] = oldToNew[tri[2]];
01041         newTriangles[facei].region() = tri.region();
01042     }
01043 
01044     // Construct subsurface
01045     return triSurface(newTriangles, patches(), newPoints, true);
01046 }
01047 
01048 
01049 void Foam::triSurface::write
01050 (
01051     const fileName& name,
01052     const bool sortByRegion
01053 ) const
01054 {
01055     write(name, name.ext(), sortByRegion);
01056 }
01057 
01058 
01059 void Foam::triSurface::write(Ostream& os) const
01060 {
01061     os  << patches() << endl;
01062 
01063     //Note: Write with global point numbering
01064     os  << points() << nl
01065         << static_cast<const List<labelledTri>&>(*this) << endl;
01066 
01067     // Check state of Ostream
01068     os.check("triSurface::write(Ostream&)");
01069 }
01070 
01071 
01072 void Foam::triSurface::write(const Time& d) const
01073 {
01074     fileName foamFile(d.caseName() + ".ftr");
01075 
01076     fileName foamPath(d.path()/triSurfInstance(d)/typeName/foamFile);
01077 
01078     OFstream foamStream(foamPath);
01079 
01080     write(foamStream);
01081 }
01082 
01083 
01084 void Foam::triSurface::writeStats(Ostream& os) const
01085 {
01086     // Unfortunately nPoints constructs meshPoints() so do compact version
01087     // ourselves.
01088     PackedBoolList pointIsUsed(points().size());
01089 
01090     label nPoints = 0;
01091     boundBox bb = boundBox::invertedBox;
01092 
01093     forAll(*this, triI)
01094     {
01095         const labelledTri& f = operator[](triI);
01096 
01097         forAll(f, fp)
01098         {
01099             label pointI = f[fp];
01100             if (pointIsUsed.set(pointI, 1))
01101             {
01102                 bb.min() = ::Foam::min(bb.min(), points()[pointI]);
01103                 bb.max() = ::Foam::max(bb.max(), points()[pointI]);
01104                 nPoints++;
01105             }
01106         }
01107     }
01108 
01109     os  << "Triangles    : " << size() << endl
01110         << "Vertices     : " << nPoints << endl
01111         << "Bounding Box : " << bb << endl;
01112 }
01113 
01114 
01115 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
01116 
01117 void Foam::triSurface::operator=(const triSurface& ts)
01118 {
01119     List<labelledTri>::operator=(ts);
01120     clearOut();
01121     storedPoints() = ts.points();
01122     patches_ = ts.patches();
01123 }
01124 
01125 
01126 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
01127 
01128 Foam::Ostream& Foam::operator<<(Ostream& os, const triSurface& sm)
01129 {
01130     sm.write(os);
01131     return os;
01132 }
01133 
01134 
01135 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd