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

isoSurface.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 "isoSurface.H"
00028 #include "fvMesh.H"
00029 #include "mergePoints.H"
00030 #include "syncTools.H"
00031 #include "addToRunTimeSelectionTable.H"
00032 #include "slicedVolFields.H"
00033 #include "volFields.H"
00034 #include "surfaceFields.H"
00035 #include "OFstream.H"
00036 #include "meshTools.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042     defineTypeNameAndDebug(isoSurface, 0);
00043 }
00044 
00045 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00046 
00047 bool Foam::isoSurface::noTransform(const tensor& tt) const
00048 {
00049     return
00050         (mag(tt.xx()-1) < mergeDistance_)
00051      && (mag(tt.yy()-1) < mergeDistance_)
00052      && (mag(tt.zz()-1) < mergeDistance_)
00053      && (mag(tt.xy()) < mergeDistance_)
00054      && (mag(tt.xz()) < mergeDistance_)
00055      && (mag(tt.yx()) < mergeDistance_)
00056      && (mag(tt.yz()) < mergeDistance_)
00057      && (mag(tt.zx()) < mergeDistance_)
00058      && (mag(tt.zy()) < mergeDistance_);
00059 }
00060 
00061 
00062 bool Foam::isoSurface::collocatedPatch(const polyPatch& pp)
00063 {
00064     const coupledPolyPatch& cpp = refCast<const coupledPolyPatch>(pp);
00065 
00066     return cpp.parallel() && !cpp.separated();
00067 }
00068 
00069 
00070 // Calculates per face whether couple is collocated.
00071 Foam::PackedBoolList Foam::isoSurface::collocatedFaces
00072 (
00073     const coupledPolyPatch& pp
00074 ) const
00075 {
00076     // Initialise to false
00077     PackedBoolList collocated(pp.size());
00078 
00079     const vectorField& separation = pp.separation();
00080     const tensorField& forwardT = pp.forwardT();
00081 
00082     if (forwardT.size() == 0)
00083     {
00084         // Parallel.
00085         if (separation.size() == 0)
00086         {
00087             collocated = 1u;
00088         }
00089         else if (separation.size() == 1)
00090         {
00091             // Fully separate. Do not synchronise.
00092         }
00093         else
00094         {
00095             // Per face separation.
00096             forAll(pp, faceI)
00097             {
00098                 if (mag(separation[faceI]) < mergeDistance_)
00099                 {
00100                     collocated[faceI] = 1u;
00101                 }
00102             }
00103         }
00104     }
00105     else if (forwardT.size() == 1)
00106     {
00107         // Fully transformed.
00108     }
00109     else
00110     {
00111         // Per face transformation.
00112         forAll(pp, faceI)
00113         {
00114             if (noTransform(forwardT[faceI]))
00115             {
00116                 collocated[faceI] = 1u;
00117             }
00118         }
00119     }
00120     return collocated;
00121 }
00122 
00123 
00124 // Insert the data for local point patchPointI into patch local values
00125 // and/or into the shared values field.
00126 void Foam::isoSurface::insertPointData
00127 (
00128     const processorPolyPatch& pp,
00129     const Map<label>& meshToShared,
00130     const pointField& pointValues,
00131     const label patchPointI,
00132     pointField& patchValues,
00133     pointField& sharedValues
00134 ) const
00135 {
00136     label meshPointI = pp.meshPoints()[patchPointI];
00137 
00138     // Store in local field
00139     label nbrPointI = pp.neighbPoints()[patchPointI];
00140     if (nbrPointI >= 0 && nbrPointI < patchValues.size())
00141     {
00142         minEqOp<point>()(patchValues[nbrPointI], pointValues[meshPointI]);
00143     }
00144 
00145     // Store in shared field
00146     Map<label>::const_iterator iter = meshToShared.find(meshPointI);
00147     if (iter != meshToShared.end())
00148     {
00149         minEqOp<point>()(sharedValues[iter()], pointValues[meshPointI]);
00150     }
00151 }
00152 
00153 
00154 void Foam::isoSurface::syncUnseparatedPoints
00155 (
00156     pointField& pointValues,
00157     const point& nullValue
00158 ) const
00159 {
00160     // Until syncPointList handles separated coupled patches with multiple
00161     // transforms do our own synchronisation of non-separated patches only
00162     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00163     const globalMeshData& pd = mesh_.globalData();
00164     const labelList& sharedPtAddr = pd.sharedPointAddr();
00165     const labelList& sharedPtLabels = pd.sharedPointLabels();
00166 
00167     // Create map from meshPoint to globalShared index.
00168     Map<label> meshToShared(2*sharedPtLabels.size());
00169     forAll(sharedPtLabels, i)
00170     {
00171         meshToShared.insert(sharedPtLabels[i], sharedPtAddr[i]);
00172     }
00173 
00174     // Values on shared points.
00175     pointField sharedInfo(pd.nGlobalPoints(), nullValue);
00176 
00177 
00178     if (Pstream::parRun())
00179     {
00180         // Send
00181         forAll(patches, patchI)
00182         {
00183             if
00184             (
00185                 isA<processorPolyPatch>(patches[patchI])
00186              && patches[patchI].nPoints() > 0
00187             )
00188             {
00189                 const processorPolyPatch& pp =
00190                     refCast<const processorPolyPatch>(patches[patchI]);
00191                 const labelList& meshPts = pp.meshPoints();
00192 
00193                 pointField patchInfo(meshPts.size(), nullValue);
00194 
00195                 PackedBoolList isCollocated(collocatedFaces(pp));
00196 
00197                 forAll(isCollocated, faceI)
00198                 {
00199                     if (isCollocated[faceI])
00200                     {
00201                         const face& f = pp.localFaces()[faceI];
00202 
00203                         forAll(f, fp)
00204                         {
00205                             label pointI = f[fp];
00206 
00207                             insertPointData
00208                             (
00209                                 pp,
00210                                 meshToShared,
00211                                 pointValues,
00212                                 pointI,
00213                                 patchInfo,
00214                                 sharedInfo
00215                             );
00216                         }
00217                     }
00218                 }
00219 
00220                 OPstream toNbr(Pstream::blocking, pp.neighbProcNo());
00221                 toNbr << patchInfo;
00222             }
00223         }
00224 
00225         // Receive and combine.
00226 
00227         forAll(patches, patchI)
00228         {
00229             if
00230             (
00231                 isA<processorPolyPatch>(patches[patchI])
00232              && patches[patchI].nPoints() > 0
00233             )
00234             {
00235                 const processorPolyPatch& pp =
00236                     refCast<const processorPolyPatch>(patches[patchI]);
00237 
00238                 pointField nbrPatchInfo(pp.nPoints());
00239                 {
00240                     // We do not know the number of points on the other side
00241                     // so cannot use Pstream::read.
00242                     IPstream fromNbr(Pstream::blocking, pp.neighbProcNo());
00243                     fromNbr >> nbrPatchInfo;
00244                 }
00245 
00246                 // Null any value which is not on neighbouring processor
00247                 nbrPatchInfo.setSize(pp.nPoints(), nullValue);
00248 
00249                 const labelList& meshPts = pp.meshPoints();
00250 
00251                 forAll(meshPts, pointI)
00252                 {
00253                     label meshPointI = meshPts[pointI];
00254                     minEqOp<point>()
00255                     (
00256                         pointValues[meshPointI],
00257                         nbrPatchInfo[pointI]
00258                     );
00259                 }
00260             }
00261         }
00262     }
00263 
00264 
00265     // Don't do cyclics for now. Are almost always separated anyway.
00266 
00267 
00268     // Shared points
00269 
00270     // Combine on master.
00271     Pstream::listCombineGather(sharedInfo, minEqOp<point>());
00272     Pstream::listCombineScatter(sharedInfo);
00273 
00274     // Now we will all have the same information. Merge it back with
00275     // my local information. (Note assignment and not combine operator)
00276     forAll(sharedPtLabels, i)
00277     {
00278         label meshPointI = sharedPtLabels[i];
00279         pointValues[meshPointI] = sharedInfo[sharedPtAddr[i]];
00280     }
00281 }
00282 
00283 
00284 Foam::scalar Foam::isoSurface::isoFraction
00285 (
00286     const scalar s0,
00287     const scalar s1
00288 ) const
00289 {
00290     scalar d = s1-s0;
00291 
00292     if (mag(d) > VSMALL)
00293     {
00294         return (iso_-s0)/d;
00295     }
00296     else
00297     {
00298         return -1.0;
00299     }
00300 }
00301 
00302 
00303 bool Foam::isoSurface::isEdgeOfFaceCut
00304 (
00305     const scalarField& pVals,
00306     const face& f,
00307     const bool ownLower,
00308     const bool neiLower
00309 ) const
00310 {
00311     forAll(f, fp)
00312     {
00313         bool fpLower = (pVals[f[fp]] < iso_);
00314         if
00315         (
00316             (fpLower != ownLower)
00317          || (fpLower != neiLower)
00318          || (fpLower != (pVals[f[f.fcIndex(fp)]] < iso_))
00319         )
00320         {
00321             return true;
00322         }
00323     }
00324     return false;
00325 }
00326 
00327 
00328 // Get neighbour value and position.
00329 void Foam::isoSurface::getNeighbour
00330 (
00331     const labelList& boundaryRegion,
00332     const volVectorField& meshC,
00333     const volScalarField& cVals,
00334     const label cellI,
00335     const label faceI,
00336     scalar& nbrValue,
00337     point& nbrPoint
00338 ) const
00339 {
00340     const labelList& own = mesh_.faceOwner();
00341     const labelList& nei = mesh_.faceNeighbour();
00342 
00343     if (mesh_.isInternalFace(faceI))
00344     {
00345         label nbr = (own[faceI] == cellI ? nei[faceI] : own[faceI]);
00346         nbrValue = cVals[nbr];
00347         nbrPoint = meshC[nbr];
00348     }
00349     else
00350     {
00351         label bFaceI = faceI-mesh_.nInternalFaces();
00352         label patchI = boundaryRegion[bFaceI];
00353         const polyPatch& pp = mesh_.boundaryMesh()[patchI];
00354         label patchFaceI = faceI-pp.start();
00355 
00356         nbrValue = cVals.boundaryField()[patchI][patchFaceI];
00357         nbrPoint = meshC.boundaryField()[patchI][patchFaceI];
00358     }
00359 }
00360 
00361 
00362 // Determine for every face/cell whether it (possibly) generates triangles.
00363 void Foam::isoSurface::calcCutTypes
00364 (
00365     const labelList& boundaryRegion,
00366     const volVectorField& meshC,
00367     const volScalarField& cVals,
00368     const scalarField& pVals
00369 )
00370 {
00371     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00372     const labelList& own = mesh_.faceOwner();
00373     const labelList& nei = mesh_.faceNeighbour();
00374 
00375     faceCutType_.setSize(mesh_.nFaces());
00376     faceCutType_ = NOTCUT;
00377 
00378     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00379     {
00380         // CC edge.
00381         bool ownLower = (cVals[own[faceI]] < iso_);
00382 
00383         scalar nbrValue;
00384         point nbrPoint;
00385         getNeighbour
00386         (
00387             boundaryRegion,
00388             meshC,
00389             cVals,
00390             own[faceI],
00391             faceI,
00392             nbrValue,
00393             nbrPoint
00394         );
00395 
00396         bool neiLower = (nbrValue < iso_);
00397 
00398         if (ownLower != neiLower)
00399         {
00400             faceCutType_[faceI] = CUT;
00401         }
00402         else
00403         {
00404             // See if any mesh edge is cut by looping over all the edges of the
00405             // face.
00406             const face f = mesh_.faces()[faceI];
00407 
00408             if (isEdgeOfFaceCut(pVals, f, ownLower, neiLower))
00409             {
00410                 faceCutType_[faceI] = CUT;
00411             }
00412         }
00413     }
00414 
00415     forAll(patches, patchI)
00416     {
00417         const polyPatch& pp = patches[patchI];
00418 
00419         label faceI = pp.start();
00420 
00421         forAll(pp, i)
00422         {
00423             bool ownLower = (cVals[own[faceI]] < iso_);
00424 
00425             scalar nbrValue;
00426             point nbrPoint;
00427             getNeighbour
00428             (
00429                 boundaryRegion,
00430                 meshC,
00431                 cVals,
00432                 own[faceI],
00433                 faceI,
00434                 nbrValue,
00435                 nbrPoint
00436             );
00437 
00438             bool neiLower = (nbrValue < iso_);
00439 
00440             if (ownLower != neiLower)
00441             {
00442                 faceCutType_[faceI] = CUT;
00443             }
00444             else
00445             {
00446                 // Mesh edge.
00447                 const face f = mesh_.faces()[faceI];
00448 
00449                 if (isEdgeOfFaceCut(pVals, f, ownLower, neiLower))
00450                 {
00451                     faceCutType_[faceI] = CUT;
00452                 }
00453             }
00454 
00455             faceI++;
00456         }
00457     }
00458 
00459 
00460 
00461     nCutCells_ = 0;
00462     cellCutType_.setSize(mesh_.nCells());
00463     cellCutType_ = NOTCUT;
00464 
00465     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00466     {
00467         if (faceCutType_[faceI] != NOTCUT)
00468         {
00469             if (cellCutType_[own[faceI]] == NOTCUT)
00470             {
00471                 cellCutType_[own[faceI]] = CUT;
00472                 nCutCells_++;
00473             }
00474             if (cellCutType_[nei[faceI]] == NOTCUT)
00475             {
00476                 cellCutType_[nei[faceI]] = CUT;
00477                 nCutCells_++;
00478             }
00479         }
00480     }
00481     for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00482     {
00483         if (faceCutType_[faceI] != NOTCUT)
00484         {
00485             if (cellCutType_[own[faceI]] == NOTCUT)
00486             {
00487                 cellCutType_[own[faceI]] = CUT;
00488                 nCutCells_++;
00489             }
00490         }
00491     }
00492 
00493     if (debug)
00494     {
00495         Pout<< "isoSurface : detected " << nCutCells_
00496             << " candidate cut cells (out of " << mesh_.nCells()
00497             << ")." << endl;
00498     }
00499 }
00500 
00501 
00502 // Return the two common points between two triangles
00503 Foam::labelPair Foam::isoSurface::findCommonPoints
00504 (
00505     const labelledTri& tri0,
00506     const labelledTri& tri1
00507 )
00508 {
00509     labelPair common(-1, -1);
00510 
00511     label fp0 = 0;
00512     label fp1 = findIndex(tri1, tri0[fp0]);
00513 
00514     if (fp1 == -1)
00515     {
00516         fp0 = 1;
00517         fp1 = findIndex(tri1, tri0[fp0]);
00518     }
00519 
00520     if (fp1 != -1)
00521     {
00522         // So tri0[fp0] is tri1[fp1]
00523 
00524         // Find next common point
00525         label fp0p1 = tri0.fcIndex(fp0);
00526         label fp1p1 = tri1.fcIndex(fp1);
00527         label fp1m1 = tri1.rcIndex(fp1);
00528 
00529         if (tri0[fp0p1] == tri1[fp1p1] || tri0[fp0p1] == tri1[fp1m1])
00530         {
00531             common[0] = tri0[fp0];
00532             common[1] = tri0[fp0p1];
00533         }
00534     }
00535     return common;
00536 }
00537 
00538 
00539 // Caculate centre of surface.
00540 Foam::point Foam::isoSurface::calcCentre(const triSurface& s)
00541 {
00542     vector sum = vector::zero;
00543 
00544     forAll(s, i)
00545     {
00546         sum += s[i].centre(s.points());
00547     }
00548     return sum/s.size();
00549 }
00550 
00551 
00552 // Replace surface (localPoints, localTris) with single point. Returns
00553 // point. Destructs arguments.
00554 Foam::pointIndexHit Foam::isoSurface::collapseSurface
00555 (
00556     pointField& localPoints,
00557     DynamicList<labelledTri, 64>& localTris
00558 )
00559 {
00560     pointIndexHit info(false, vector::zero, localTris.size());
00561 
00562     if (localTris.size() == 1)
00563     {
00564         const labelledTri& tri = localTris[0];
00565         info.setPoint(tri.centre(localPoints));
00566         info.setHit();
00567     }
00568     else if (localTris.size() == 2)
00569     {
00570         // Check if the two triangles share an edge.
00571         const labelledTri& tri0 = localTris[0];
00572         const labelledTri& tri1 = localTris[0];
00573 
00574         labelPair shared = findCommonPoints(tri0, tri1);
00575 
00576         if (shared[0] != -1)
00577         {
00578             info.setPoint
00579             (
00580                 0.5
00581               * (
00582                     tri0.centre(localPoints)
00583                   + tri1.centre(localPoints)
00584                 )
00585             );
00586             info.setHit();
00587         }
00588     }
00589     else if (localTris.size())
00590     {
00591         // Check if single region. Rare situation.
00592         triSurface surf
00593         (
00594             localTris,
00595             geometricSurfacePatchList(0),
00596             localPoints,
00597             true
00598         );
00599         localTris.clearStorage();
00600 
00601         labelList faceZone;
00602         label nZones = surf.markZones
00603         (
00604             boolList(surf.nEdges(), false),
00605             faceZone
00606         );
00607 
00608         if (nZones == 1)
00609         {
00610             info.setPoint(calcCentre(surf));
00611             info.setHit();
00612         }
00613     }
00614 
00615     return info;
00616 }
00617 
00618 
00619 // Determine per cell centre whether all the intersections get collapsed
00620 // to a single point
00621 void Foam::isoSurface::calcSnappedCc
00622 (
00623     const labelList& boundaryRegion,
00624     const volVectorField& meshC,
00625     const volScalarField& cVals,
00626     const scalarField& pVals,
00627 
00628     DynamicList<point>& snappedPoints,
00629     labelList& snappedCc
00630 ) const
00631 {
00632     const pointField& pts = mesh_.points();
00633     const pointField& cc = mesh_.cellCentres();
00634 
00635     snappedCc.setSize(mesh_.nCells());
00636     snappedCc = -1;
00637 
00638     // Work arrays
00639     DynamicList<point, 64> localTriPoints(64);
00640 
00641     forAll(mesh_.cells(), cellI)
00642     {
00643         if (cellCutType_[cellI] == CUT)
00644         {
00645             scalar cVal = cVals[cellI];
00646 
00647             const cell& cFaces = mesh_.cells()[cellI];
00648 
00649             localTriPoints.clear();
00650             label nOther = 0;
00651             point otherPointSum = vector::zero;
00652 
00653             // Create points for all intersections close to cell centre
00654             // (i.e. from pyramid edges)
00655 
00656             forAll(cFaces, cFaceI)
00657             {
00658                 label faceI = cFaces[cFaceI];
00659 
00660                 scalar nbrValue;
00661                 point nbrPoint;
00662                 getNeighbour
00663                 (
00664                     boundaryRegion,
00665                     meshC,
00666                     cVals,
00667                     cellI,
00668                     faceI,
00669                     nbrValue,
00670                     nbrPoint
00671                 );
00672 
00673                 // Calculate intersection points of edges to cell centre
00674                 FixedList<scalar, 3> s;
00675                 FixedList<point, 3> pt;
00676 
00677                 // From cc to neighbour cc.
00678                 s[2] = isoFraction(cVal, nbrValue);
00679                 pt[2] = (1.0-s[2])*cc[cellI] + s[2]*nbrPoint;
00680 
00681                 const face& f = mesh_.faces()[cFaces[cFaceI]];
00682 
00683                 forAll(f, fp)
00684                 {
00685                     // From cc to fp
00686                     label p0 = f[fp];
00687                     s[0] = isoFraction(cVal, pVals[p0]);
00688                     pt[0] = (1.0-s[0])*cc[cellI] + s[0]*pts[p0];
00689 
00690                     // From cc to fp+1
00691                     label p1 = f[f.fcIndex(fp)];
00692                     s[1] = isoFraction(cVal, pVals[p1]);
00693                     pt[1] = (1.0-s[1])*cc[cellI] + s[1]*pts[p1];
00694 
00695                     if
00696                     (
00697                         (s[0] >= 0.0 && s[0] <= 0.5)
00698                      && (s[1] >= 0.0 && s[1] <= 0.5)
00699                      && (s[2] >= 0.0 && s[2] <= 0.5)
00700                     )
00701                     {
00702                         localTriPoints.append(pt[0]);
00703                         localTriPoints.append(pt[1]);
00704                         localTriPoints.append(pt[2]);
00705                     }
00706                     else
00707                     {
00708                         // Get average of all other points
00709                         forAll(s, i)
00710                         {
00711                             if (s[i] >= 0.0 && s[i] <= 0.5)
00712                             {
00713                                 otherPointSum += pt[i];
00714                                 nOther++;
00715                             }
00716                         }
00717                     }
00718                 }
00719             }
00720 
00721             if (localTriPoints.size() == 0)
00722             {
00723                 // No complete triangles. Get average of all intersection
00724                 // points.
00725                 if (nOther > 0)
00726                 {
00727                     snappedCc[cellI] = snappedPoints.size();
00728                     snappedPoints.append(otherPointSum/nOther);
00729 
00730                     //Pout<< "    point:" << pointI
00731                     //    << " replacing coord:" << mesh_.points()[pointI]
00732                     //    << " by average:" << collapsedPoint[pointI] << endl;
00733                 }
00734             }
00735             else if (localTriPoints.size() == 3)
00736             {
00737                 // Single triangle. No need for any analysis. Average points.
00738                 pointField points;
00739                 points.transfer(localTriPoints);
00740                 snappedCc[cellI] = snappedPoints.size();
00741                 snappedPoints.append(sum(points)/points.size());
00742 
00743                 //Pout<< "    point:" << pointI
00744                 //    << " replacing coord:" << mesh_.points()[pointI]
00745                 //    << " by average:" << collapsedPoint[pointI] << endl;
00746             }
00747             else
00748             {
00749                 // Convert points into triSurface.
00750 
00751                 // Merge points and compact out non-valid triangles
00752                 labelList triMap;               // merged to unmerged triangle
00753                 labelList triPointReverseMap;   // unmerged to merged point
00754                 triSurface surf
00755                 (
00756                     stitchTriPoints
00757                     (
00758                         false,              // do not check for duplicate tris
00759                         localTriPoints,
00760                         triPointReverseMap,
00761                         triMap
00762                     )
00763                 );
00764 
00765                 labelList faceZone;
00766                 label nZones = surf.markZones
00767                 (
00768                     boolList(surf.nEdges(), false),
00769                     faceZone
00770                 );
00771 
00772                 if (nZones == 1)
00773                 {
00774                     snappedCc[cellI] = snappedPoints.size();
00775                     snappedPoints.append(calcCentre(surf));
00776                     //Pout<< "    point:" << pointI << " nZones:" << nZones
00777                     //    << " replacing coord:" << mesh_.points()[pointI]
00778                     //    << " by average:" << collapsedPoint[pointI] << endl;
00779                 }
00780             }
00781         }
00782     }
00783 }
00784 
00785 
00786 // Determine per meshpoint whether all the intersections get collapsed
00787 // to a single point
00788 void Foam::isoSurface::calcSnappedPoint
00789 (
00790     const PackedBoolList& isBoundaryPoint,
00791     const labelList& boundaryRegion,
00792     const volVectorField& meshC,
00793     const volScalarField& cVals,
00794     const scalarField& pVals,
00795 
00796     DynamicList<point>& snappedPoints,
00797     labelList& snappedPoint
00798 ) const
00799 {
00800     const pointField& pts = mesh_.points();
00801     const pointField& cc = mesh_.cellCentres();
00802 
00803 
00804     const point greatPoint(VGREAT, VGREAT, VGREAT);
00805     pointField collapsedPoint(mesh_.nPoints(), greatPoint);
00806 
00807 
00808     // Work arrays
00809     DynamicList<point, 64> localTriPoints(100);
00810 
00811     forAll(mesh_.pointFaces(), pointI)
00812     {
00813         if (isBoundaryPoint.get(pointI) == 1)
00814         {
00815             continue;
00816         }
00817 
00818         const labelList& pFaces = mesh_.pointFaces()[pointI];
00819 
00820         bool anyCut = false;
00821 
00822         forAll(pFaces, i)
00823         {
00824             label faceI = pFaces[i];
00825 
00826             if (faceCutType_[faceI] == CUT)
00827             {
00828                 anyCut = true;
00829                 break;
00830             }
00831         }
00832 
00833         if (!anyCut)
00834         {
00835             continue;
00836         }
00837 
00838 
00839         localTriPoints.clear();
00840         label nOther = 0;
00841         point otherPointSum = vector::zero;
00842 
00843         forAll(pFaces, pFaceI)
00844         {
00845             // Create points for all intersections close to point
00846             // (i.e. from pyramid edges)
00847 
00848             label faceI = pFaces[pFaceI];
00849             const face& f = mesh_.faces()[faceI];
00850             label own = mesh_.faceOwner()[faceI];
00851 
00852             // Get neighbour value
00853             scalar nbrValue;
00854             point nbrPoint;
00855             getNeighbour
00856             (
00857                 boundaryRegion,
00858                 meshC,
00859                 cVals,
00860                 own,
00861                 faceI,
00862                 nbrValue,
00863                 nbrPoint
00864             );
00865 
00866             // Calculate intersection points of edges emanating from point
00867             FixedList<scalar, 4> s;
00868             FixedList<point, 4> pt;
00869 
00870             label fp = findIndex(f, pointI);
00871             s[0] = isoFraction(pVals[pointI], cVals[own]);
00872             pt[0] = (1.0-s[0])*pts[pointI] + s[0]*cc[own];
00873 
00874             s[1] = isoFraction(pVals[pointI], nbrValue);
00875             pt[1] = (1.0-s[1])*pts[pointI] + s[1]*nbrPoint;
00876 
00877             label nextPointI = f[f.fcIndex(fp)];
00878             s[2] = isoFraction(pVals[pointI], pVals[nextPointI]);
00879             pt[2] = (1.0-s[2])*pts[pointI] + s[2]*pts[nextPointI];
00880 
00881             label prevPointI = f[f.rcIndex(fp)];
00882             s[3] = isoFraction(pVals[pointI], pVals[prevPointI]);
00883             pt[3] = (1.0-s[3])*pts[pointI] + s[3]*pts[prevPointI];
00884 
00885             if
00886             (
00887                 (s[0] >= 0.0 && s[0] <= 0.5)
00888              && (s[1] >= 0.0 && s[1] <= 0.5)
00889              && (s[2] >= 0.0 && s[2] <= 0.5)
00890             )
00891             {
00892                 localTriPoints.append(pt[0]);
00893                 localTriPoints.append(pt[1]);
00894                 localTriPoints.append(pt[2]);
00895             }
00896             if
00897             (
00898                 (s[0] >= 0.0 && s[0] <= 0.5)
00899              && (s[1] >= 0.0 && s[1] <= 0.5)
00900              && (s[3] >= 0.0 && s[3] <= 0.5)
00901             )
00902             {
00903                 localTriPoints.append(pt[3]);
00904                 localTriPoints.append(pt[0]);
00905                 localTriPoints.append(pt[1]);
00906             }
00907 
00908             // Get average of points as fallback
00909             forAll(s, i)
00910             {
00911                 if (s[i] >= 0.0 && s[i] <= 0.5)
00912                 {
00913                     otherPointSum += pt[i];
00914                     nOther++;
00915                 }
00916             }
00917         }
00918 
00919         if (localTriPoints.size() == 0)
00920         {
00921             // No complete triangles. Get average of all intersection
00922             // points.
00923             if (nOther > 0)
00924             {
00925                 collapsedPoint[pointI] = otherPointSum/nOther;
00926             }
00927         }
00928         else if (localTriPoints.size() == 3)
00929         {
00930             // Single triangle. No need for any analysis. Average points.
00931             pointField points;
00932             points.transfer(localTriPoints);
00933             collapsedPoint[pointI] = sum(points)/points.size();
00934         }
00935         else
00936         {
00937             // Convert points into triSurface.
00938 
00939             // Merge points and compact out non-valid triangles
00940             labelList triMap;               // merged to unmerged triangle
00941             labelList triPointReverseMap;   // unmerged to merged point
00942             triSurface surf
00943             (
00944                 stitchTriPoints
00945                 (
00946                     false,                  // do not check for duplicate tris
00947                     localTriPoints,
00948                     triPointReverseMap,
00949                     triMap
00950                 )
00951             );
00952 
00953             labelList faceZone;
00954             label nZones = surf.markZones
00955             (
00956                 boolList(surf.nEdges(), false),
00957                 faceZone
00958             );
00959 
00960             if (nZones == 1)
00961             {
00962                 collapsedPoint[pointI] = calcCentre(surf);
00963             }
00964         }
00965     }
00966 
00967 
00968     // Synchronise snap location
00969     syncUnseparatedPoints(collapsedPoint, greatPoint);
00970 
00971 
00972     snappedPoint.setSize(mesh_.nPoints());
00973     snappedPoint = -1;
00974 
00975     forAll(collapsedPoint, pointI)
00976     {
00977         if (collapsedPoint[pointI] != greatPoint)
00978         {
00979             snappedPoint[pointI] = snappedPoints.size();
00980             snappedPoints.append(collapsedPoint[pointI]);
00981         }
00982     }
00983 }
00984 
00985 
00986 Foam::triSurface Foam::isoSurface::stitchTriPoints
00987 (
00988     const bool checkDuplicates,
00989     const List<point>& triPoints,
00990     labelList& triPointReverseMap,  // unmerged to merged point
00991     labelList& triMap               // merged to unmerged triangle
00992 ) const
00993 {
00994     label nTris = triPoints.size()/3;
00995 
00996     if ((triPoints.size() % 3) != 0)
00997     {
00998         FatalErrorIn("isoSurface::stitchTriPoints(..)")
00999             << "Problem: number of points " << triPoints.size()
01000             << " not a multiple of 3." << abort(FatalError);
01001     }
01002 
01003     pointField newPoints;
01004     mergePoints
01005     (
01006         triPoints,
01007         mergeDistance_,
01008         false,
01009         triPointReverseMap,
01010         newPoints
01011     );
01012 
01013     // Check that enough merged.
01014     if (debug)
01015     {
01016         pointField newNewPoints;
01017         labelList oldToNew;
01018         bool hasMerged = mergePoints
01019         (
01020             newPoints,
01021             mergeDistance_,
01022             true,
01023             oldToNew,
01024             newNewPoints
01025         );
01026 
01027         if (hasMerged)
01028         {
01029             FatalErrorIn("isoSurface::stitchTriPoints(..)")
01030                 << "Merged points contain duplicates"
01031                 << " when merging with distance " << mergeDistance_ << endl
01032                 << "merged:" << newPoints.size() << " re-merged:"
01033                 << newNewPoints.size()
01034                 << abort(FatalError);
01035         }
01036     }
01037 
01038 
01039     List<labelledTri> tris;
01040     {
01041         DynamicList<labelledTri> dynTris(nTris);
01042         label rawPointI = 0;
01043         DynamicList<label> newToOldTri(nTris);
01044 
01045         for (label oldTriI = 0; oldTriI < nTris; oldTriI++)
01046         {
01047             labelledTri tri
01048             (
01049                 triPointReverseMap[rawPointI],
01050                 triPointReverseMap[rawPointI+1],
01051                 triPointReverseMap[rawPointI+2],
01052                 0
01053             );
01054             rawPointI += 3;
01055 
01056             if ((tri[0] != tri[1]) && (tri[0] != tri[2]) && (tri[1] != tri[2]))
01057             {
01058                 newToOldTri.append(oldTriI);
01059                 dynTris.append(tri);
01060             }
01061         }
01062 
01063         triMap.transfer(newToOldTri);
01064         tris.transfer(dynTris);
01065     }
01066 
01067 
01068 
01069     // Determine 'flat hole' situation (see RMT paper).
01070     // Two unconnected triangles get connected because (some of) the edges
01071     // separating them get collapsed. Below only checks for duplicate triangles,
01072     // not non-manifold edge connectivity.
01073     if (checkDuplicates)
01074     {
01075         labelListList pointFaces;
01076         invertManyToMany(newPoints.size(), tris, pointFaces);
01077 
01078         // Filter out duplicates.
01079         DynamicList<label> newToOldTri(tris.size());
01080 
01081         forAll(tris, triI)
01082         {
01083             const labelledTri& tri = tris[triI];
01084             const labelList& pFaces = pointFaces[tri[0]];
01085 
01086             // Find the maximum of any duplicates. Maximum since the tris
01087             // below triI
01088             // get overwritten so we cannot use them in a comparison.
01089             label dupTriI = -1;
01090             forAll(pFaces, i)
01091             {
01092                 label nbrTriI = pFaces[i];
01093 
01094                 if (nbrTriI > triI && (tris[nbrTriI] == tri))
01095                 {
01096                     //Pout<< "Duplicate : " << triI << " verts:" << tri
01097                     //    << " to " << nbrTriI << " verts:" << tris[nbrTriI]
01098                     //    << endl;
01099                     dupTriI = nbrTriI;
01100                     break;
01101                 }
01102             }
01103 
01104             if (dupTriI == -1)
01105             {
01106                 // There is no (higher numbered) duplicate triangle
01107                 label newTriI = newToOldTri.size();
01108                 newToOldTri.append(triI);
01109                 tris[newTriI] = tris[triI];
01110             }
01111         }
01112 
01113         triMap.transfer(newToOldTri);
01114         tris.setSize(triMap.size());
01115 
01116         if (debug)
01117         {
01118             Pout<< "isoSurface : merged from " << nTris
01119                 << " down to " << tris.size() << " unique triangles." << endl;
01120         }
01121 
01122 
01123         if (debug)
01124         {
01125             triSurface surf(tris, geometricSurfacePatchList(0), newPoints);
01126 
01127             forAll(surf, faceI)
01128             {
01129                 const labelledTri& f = surf[faceI];
01130                 const labelList& fFaces = surf.faceFaces()[faceI];
01131 
01132                 forAll(fFaces, i)
01133                 {
01134                     label nbrFaceI = fFaces[i];
01135 
01136                     if (nbrFaceI <= faceI)
01137                     {
01138                         // lower numbered faces already checked
01139                         continue;
01140                     }
01141 
01142                     const labelledTri& nbrF = surf[nbrFaceI];
01143 
01144                     if (f == nbrF)
01145                     {
01146                         FatalErrorIn("validTri(const triSurface&, const label)")
01147                             << "Check : "
01148                             << " triangle " << faceI << " vertices " << f
01149                             << " fc:" << f.centre(surf.points())
01150                             << " has the same vertices as triangle " << nbrFaceI
01151                             << " vertices " << nbrF
01152                             << " fc:" << nbrF.centre(surf.points())
01153                             << abort(FatalError);
01154                     }
01155                 }
01156             }
01157         }
01158     }
01159 
01160     return triSurface(tris, geometricSurfacePatchList(0), newPoints, true);
01161 }
01162 
01163 
01164 // Does face use valid vertices?
01165 bool Foam::isoSurface::validTri(const triSurface& surf, const label faceI)
01166 {
01167     // Simple check on indices ok.
01168 
01169     const labelledTri& f = surf[faceI];
01170 
01171     if
01172     (
01173         (f[0] < 0) || (f[0] >= surf.points().size())
01174      || (f[1] < 0) || (f[1] >= surf.points().size())
01175      || (f[2] < 0) || (f[2] >= surf.points().size())
01176     )
01177     {
01178         WarningIn("validTri(const triSurface&, const label)")
01179             << "triangle " << faceI << " vertices " << f
01180             << " uses point indices outside point range 0.."
01181             << surf.points().size()-1 << endl;
01182 
01183         return false;
01184     }
01185 
01186     if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2]))
01187     {
01188         WarningIn("validTri(const triSurface&, const label)")
01189             << "triangle " << faceI
01190             << " uses non-unique vertices " << f
01191             << endl;
01192         return false;
01193     }
01194 
01195     // duplicate triangle check
01196 
01197     const labelList& fFaces = surf.faceFaces()[faceI];
01198 
01199     // Check if faceNeighbours use same points as this face.
01200     // Note: discards normal information - sides of baffle are merged.
01201     forAll(fFaces, i)
01202     {
01203         label nbrFaceI = fFaces[i];
01204 
01205         if (nbrFaceI <= faceI)
01206         {
01207             // lower numbered faces already checked
01208             continue;
01209         }
01210 
01211         const labelledTri& nbrF = surf[nbrFaceI];
01212 
01213         if
01214         (
01215             ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2]))
01216          && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2]))
01217          && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2]))
01218         )
01219         {
01220             WarningIn("validTri(const triSurface&, const label)")
01221                 << "triangle " << faceI << " vertices " << f
01222                 << " fc:" << f.centre(surf.points())
01223                 << " has the same vertices as triangle " << nbrFaceI
01224                 << " vertices " << nbrF
01225                 << " fc:" << nbrF.centre(surf.points())
01226                 << endl;
01227 
01228             return false;
01229         }
01230     }
01231     return true;
01232 }
01233 
01234 
01235 void Foam::isoSurface::calcAddressing
01236 (
01237     const triSurface& surf,
01238     List<FixedList<label, 3> >& faceEdges,
01239     labelList& edgeFace0,
01240     labelList& edgeFace1,
01241     Map<labelList>& edgeFacesRest
01242 ) const
01243 {
01244     const pointField& points = surf.points();
01245 
01246     pointField edgeCentres(3*surf.size());
01247     label edgeI = 0;
01248     forAll(surf, triI)
01249     {
01250         const labelledTri& tri = surf[triI];
01251         edgeCentres[edgeI++] = 0.5*(points[tri[0]]+points[tri[1]]);
01252         edgeCentres[edgeI++] = 0.5*(points[tri[1]]+points[tri[2]]);
01253         edgeCentres[edgeI++] = 0.5*(points[tri[2]]+points[tri[0]]);
01254     }
01255 
01256     pointField mergedCentres;
01257     labelList oldToMerged;
01258     bool hasMerged = mergePoints
01259     (
01260         edgeCentres,
01261         mergeDistance_,
01262         false,
01263         oldToMerged,
01264         mergedCentres
01265     );
01266 
01267     if (debug)
01268     {
01269         Pout<< "isoSurface : detected "
01270             << mergedCentres.size()
01271             << " geometric edges on " << surf.size() << " triangles." << endl;
01272     }
01273 
01274     if (!hasMerged)
01275     {
01276         return;
01277     }
01278 
01279 
01280     // Determine faceEdges
01281     faceEdges.setSize(surf.size());
01282     edgeI = 0;
01283     forAll(surf, triI)
01284     {
01285         faceEdges[triI][0] = oldToMerged[edgeI++];
01286         faceEdges[triI][1] = oldToMerged[edgeI++];
01287         faceEdges[triI][2] = oldToMerged[edgeI++];
01288     }
01289 
01290 
01291     // Determine edgeFaces
01292     edgeFace0.setSize(mergedCentres.size());
01293     edgeFace0 = -1;
01294     edgeFace1.setSize(mergedCentres.size());
01295     edgeFace1 = -1;
01296     edgeFacesRest.clear();
01297 
01298     // Overflow edge faces for geometric shared edges that turned
01299     // out to be different anyway.
01300     EdgeMap<labelList> extraEdgeFaces(mergedCentres.size()/100);
01301 
01302     forAll(oldToMerged, oldEdgeI)
01303     {
01304         label triI = oldEdgeI / 3;
01305         label edgeI = oldToMerged[oldEdgeI];
01306 
01307         if (edgeFace0[edgeI] == -1)
01308         {
01309             // First triangle for edge
01310             edgeFace0[edgeI] = triI;
01311         }
01312         else
01313         {
01314             //- Check that the two triangles actually topologically
01315             //  share an edge
01316             const labelledTri& prevTri = surf[edgeFace0[edgeI]];
01317             const labelledTri& tri = surf[triI];
01318 
01319             label fp = oldEdgeI % 3;
01320 
01321             edge e(tri[fp], tri[tri.fcIndex(fp)]);
01322 
01323             label prevTriIndex = -1;
01324 
01325             forAll(prevTri, i)
01326             {
01327                 if (edge(prevTri[i], prevTri[prevTri.fcIndex(i)]) == e)
01328                 {
01329                     prevTriIndex = i;
01330                     break;
01331                 }
01332             }
01333 
01334             if (prevTriIndex == -1)
01335             {
01336                 // Different edge. Store for later.
01337                 EdgeMap<labelList>::iterator iter = extraEdgeFaces.find(e);
01338 
01339                 if (iter != extraEdgeFaces.end())
01340                 {
01341                     labelList& eFaces = iter();
01342                     label sz = eFaces.size();
01343                     eFaces.setSize(sz+1);
01344                     eFaces[sz] = triI;
01345                 }
01346                 else
01347                 {
01348                     extraEdgeFaces.insert(e, labelList(1, triI));
01349                 }
01350             }
01351             else if (edgeFace1[edgeI] == -1)
01352             {
01353                 edgeFace1[edgeI] = triI;
01354             }
01355             else
01356             {
01357                 //WarningIn("orientSurface(triSurface&)")
01358                 //    << "Edge " << edgeI << " with centre "
01359                 //    << mergedCentres[edgeI]
01360                 //    << " used by more than two triangles: "
01361                 //    << edgeFace0[edgeI] << ", "
01362                 //    << edgeFace1[edgeI] << " and " << triI << endl;
01363                 Map<labelList>::iterator iter = edgeFacesRest.find(edgeI);
01364 
01365                 if (iter != edgeFacesRest.end())
01366                 {
01367                     labelList& eFaces = iter();
01368                     label sz = eFaces.size();
01369                     eFaces.setSize(sz+1);
01370                     eFaces[sz] = triI;
01371                 }
01372                 else
01373                 {
01374                     edgeFacesRest.insert(edgeI, labelList(1, triI));
01375                 }
01376             }
01377         }
01378     }
01379 
01380     // Add extraEdgeFaces
01381     edgeI = edgeFace0.size();
01382 
01383     edgeFace0.setSize(edgeI + extraEdgeFaces.size());
01384     edgeFace1.setSize(edgeI + extraEdgeFaces.size(), -1);
01385 
01386     forAllConstIter(EdgeMap<labelList>, extraEdgeFaces, iter)
01387     {
01388         const labelList& eFaces = iter();
01389 
01390         // The current edge will become edgeI. Replace all occurrences in
01391         // faceEdges
01392         forAll(eFaces, i)
01393         {
01394             label triI = eFaces[i];
01395             const labelledTri& tri = surf[triI];
01396 
01397             FixedList<label, 3>& fEdges = faceEdges[triI];
01398             forAll(tri, fp)
01399             {
01400                 edge e(tri[fp], tri[tri.fcIndex(fp)]);
01401 
01402                 if (e == iter.key())
01403                 {
01404                     fEdges[fp] = edgeI;
01405                     break;
01406                 }
01407             }
01408         }
01409 
01410 
01411         // Add face to edgeFaces
01412 
01413         edgeFace0[edgeI] = eFaces[0];
01414 
01415         if (eFaces.size() >= 2)
01416         {
01417             edgeFace1[edgeI] = eFaces[1];
01418 
01419             if (eFaces.size() > 2)
01420             {
01421                 edgeFacesRest.insert
01422                 (
01423                     edgeI,
01424                     SubList<label>(eFaces, eFaces.size()-2, 2)
01425                 );
01426             }
01427         }
01428 
01429         edgeI++;
01430     }
01431 }
01432 
01433 
01434 void Foam::isoSurface::walkOrientation
01435 (
01436     const triSurface& surf,
01437     const List<FixedList<label, 3> >& faceEdges,
01438     const labelList& edgeFace0,
01439     const labelList& edgeFace1,
01440     const label seedTriI,
01441     labelList& flipState
01442 )
01443 {
01444     // Do walk for consistent orientation.
01445     DynamicList<label> changedFaces(surf.size());
01446 
01447     changedFaces.append(seedTriI);
01448 
01449     while (changedFaces.size())
01450     {
01451         DynamicList<label> newChangedFaces(changedFaces.size());
01452 
01453         forAll(changedFaces, i)
01454         {
01455             label triI = changedFaces[i];
01456             const labelledTri& tri = surf[triI];
01457             const FixedList<label, 3>& fEdges = faceEdges[triI];
01458 
01459             forAll(fEdges, fp)
01460             {
01461                 label edgeI = fEdges[fp];
01462 
01463                 // my points:
01464                 label p0 = tri[fp];
01465                 label p1 = tri[tri.fcIndex(fp)];
01466 
01467                 label nbrI =
01468                 (
01469                     edgeFace0[edgeI] != triI
01470                   ? edgeFace0[edgeI]
01471                   : edgeFace1[edgeI]
01472                 );
01473 
01474                 if (nbrI != -1 && flipState[nbrI] == -1)
01475                 {
01476                     const labelledTri& nbrTri = surf[nbrI];
01477 
01478                     // nbr points
01479                     label nbrFp = findIndex(nbrTri, p0);
01480 
01481                     if (nbrFp == -1)
01482                     {
01483                         FatalErrorIn("isoSurface::walkOrientation(..)")
01484                             << "triI:" << triI
01485                             << " tri:" << tri
01486                             << " p0:" << p0
01487                             << " p1:" << p1
01488                             << " fEdges:" << fEdges
01489                             << " edgeI:" << edgeI
01490                             << " edgeFace0:" << edgeFace0[edgeI]
01491                             << " edgeFace1:" << edgeFace1[edgeI]
01492                             << " nbrI:" << nbrI
01493                             << " nbrTri:" << nbrTri
01494                             << abort(FatalError);
01495                     }
01496 
01497 
01498                     label nbrP1 = nbrTri[nbrTri.rcIndex(nbrFp)];
01499 
01500                     bool sameOrientation = (p1 == nbrP1);
01501 
01502                     if (flipState[triI] == 0)
01503                     {
01504                         flipState[nbrI] = (sameOrientation ? 0 : 1);
01505                     }
01506                     else
01507                     {
01508                         flipState[nbrI] = (sameOrientation ? 1 : 0);
01509                     }
01510                     newChangedFaces.append(nbrI);
01511                 }
01512             }
01513         }
01514 
01515         changedFaces.transfer(newChangedFaces);
01516     }
01517 }
01518 
01519 
01520 void Foam::isoSurface::orientSurface
01521 (
01522     triSurface& surf,
01523     const List<FixedList<label, 3> >& faceEdges,
01524     const labelList& edgeFace0,
01525     const labelList& edgeFace1,
01526     const Map<labelList>& edgeFacesRest
01527 )
01528 {
01529     // -1 : unvisited
01530     //  0 : leave as is
01531     //  1 : flip
01532     labelList flipState(surf.size(), -1);
01533 
01534     label seedTriI = 0;
01535 
01536     while (true)
01537     {
01538         // Find first unvisited triangle
01539         for
01540         (
01541             ;
01542             seedTriI < surf.size() && flipState[seedTriI] != -1;
01543             seedTriI++
01544         )
01545         {}
01546 
01547         if (seedTriI == surf.size())
01548         {
01549             break;
01550         }
01551 
01552         // Note: Determine orientation of seedTriI?
01553         // for now assume it is ok
01554         flipState[seedTriI] = 0;
01555 
01556         walkOrientation
01557         (
01558             surf,
01559             faceEdges,
01560             edgeFace0,
01561             edgeFace1,
01562             seedTriI,
01563             flipState
01564         );
01565     }
01566 
01567     // Do actual flipping
01568     surf.clearOut();
01569     forAll(surf, triI)
01570     {
01571         if (flipState[triI] == 1)
01572         {
01573             labelledTri tri(surf[triI]);
01574 
01575             surf[triI][0] = tri[0];
01576             surf[triI][1] = tri[2];
01577             surf[triI][2] = tri[1];
01578         }
01579         else if (flipState[triI] == -1)
01580         {
01581             FatalErrorIn
01582             (
01583                 "isoSurface::orientSurface(triSurface&, const label)"
01584             )   << "problem" << abort(FatalError);
01585         }
01586     }
01587 }
01588 
01589 
01590 // Checks if triangle is connected through edgeI only.
01591 bool Foam::isoSurface::danglingTriangle
01592 (
01593     const FixedList<label, 3>& fEdges,
01594     const labelList& edgeFace1
01595 )
01596 {
01597     label nOpen = 0;
01598     forAll(fEdges, i)
01599     {
01600         if (edgeFace1[fEdges[i]] == -1)
01601         {
01602             nOpen++;
01603         }
01604     }
01605 
01606     if (nOpen == 1 || nOpen == 2 || nOpen == 3)
01607     {
01608         return true;
01609     }
01610     else
01611     {
01612         return false;
01613     }
01614 }
01615 
01616 
01617 // Mark triangles to keep. Returns number of dangling triangles.
01618 Foam::label Foam::isoSurface::markDanglingTriangles
01619 (
01620     const List<FixedList<label, 3> >& faceEdges,
01621     const labelList& edgeFace0,
01622     const labelList& edgeFace1,
01623     const Map<labelList>& edgeFacesRest,
01624     boolList& keepTriangles
01625 )
01626 {
01627     keepTriangles.setSize(faceEdges.size());
01628     keepTriangles = true;
01629 
01630     label nDangling = 0;
01631 
01632     // Remove any dangling triangles
01633     forAllConstIter(Map<labelList>,  edgeFacesRest, iter)
01634     {
01635         // These are all the non-manifold edges. Filter out all triangles
01636         // with only one connected edge (= this edge)
01637 
01638         label edgeI = iter.key();
01639         const labelList& otherEdgeFaces = iter();
01640 
01641         // Remove all dangling triangles
01642         if (danglingTriangle(faceEdges[edgeFace0[edgeI]], edgeFace1))
01643         {
01644             keepTriangles[edgeFace0[edgeI]] = false;
01645             nDangling++;
01646         }
01647         if (danglingTriangle(faceEdges[edgeFace1[edgeI]], edgeFace1))
01648         {
01649             keepTriangles[edgeFace1[edgeI]] = false;
01650             nDangling++;
01651         }
01652         forAll(otherEdgeFaces, i)
01653         {
01654             label triI = otherEdgeFaces[i];
01655             if (danglingTriangle(faceEdges[triI], edgeFace1))
01656             {
01657                 keepTriangles[triI] = false;
01658                 nDangling++;
01659             }
01660         }
01661     }
01662     return nDangling;
01663 }
01664 
01665 
01666 Foam::triSurface Foam::isoSurface::subsetMesh
01667 (
01668     const triSurface& s,
01669     const labelList& newToOldFaces,
01670     labelList& oldToNewPoints,
01671     labelList& newToOldPoints
01672 )
01673 {
01674     const boolList include
01675     (
01676         createWithValues<boolList>
01677         (
01678             s.size(),
01679             false,
01680             newToOldFaces,
01681             true
01682         )
01683     );
01684 
01685     newToOldPoints.setSize(s.points().size());
01686     oldToNewPoints.setSize(s.points().size());
01687     oldToNewPoints = -1;
01688     {
01689         label pointI = 0;
01690 
01691         forAll(include, oldFacei)
01692         {
01693             if (include[oldFacei])
01694             {
01695                 // Renumber labels for triangle
01696                 const labelledTri& tri = s[oldFacei];
01697 
01698                 forAll(tri, fp)
01699                 {
01700                     label oldPointI = tri[fp];
01701 
01702                     if (oldToNewPoints[oldPointI] == -1)
01703                     {
01704                         oldToNewPoints[oldPointI] = pointI;
01705                         newToOldPoints[pointI++] = oldPointI;
01706                     }
01707                 }
01708             }
01709         }
01710         newToOldPoints.setSize(pointI);
01711     }
01712 
01713     // Extract points
01714     pointField newPoints(newToOldPoints.size());
01715     forAll(newToOldPoints, i)
01716     {
01717         newPoints[i] = s.points()[newToOldPoints[i]];
01718     }
01719     // Extract faces
01720     List<labelledTri> newTriangles(newToOldFaces.size());
01721 
01722     forAll(newToOldFaces, i)
01723     {
01724         // Get old vertex labels
01725         const labelledTri& tri = s[newToOldFaces[i]];
01726 
01727         newTriangles[i][0] = oldToNewPoints[tri[0]];
01728         newTriangles[i][1] = oldToNewPoints[tri[1]];
01729         newTriangles[i][2] = oldToNewPoints[tri[2]];
01730         newTriangles[i].region() = tri.region();
01731     }
01732 
01733     // Reuse storage.
01734     return triSurface(newTriangles, s.patches(), newPoints, true);
01735 }
01736 
01737 
01738 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
01739 
01740 Foam::isoSurface::isoSurface
01741 (
01742     const volScalarField& cVals,
01743     const scalarField& pVals,
01744     const scalar iso,
01745     const bool regularise,
01746     const scalar mergeTol
01747 )
01748 :
01749     mesh_(cVals.mesh()),
01750     pVals_(pVals),
01751     iso_(iso),
01752     regularise_(regularise),
01753     mergeDistance_(mergeTol*mesh_.bounds().mag())
01754 {
01755     if (debug)
01756     {
01757         Pout<< "isoSurface:" << nl
01758             << "    isoField      : " << cVals.name() << nl
01759             << "    cell min/max  : "
01760             << min(cVals.internalField()) << " / "
01761             << max(cVals.internalField()) << nl
01762             << "    point min/max : "
01763             << min(pVals_) << " / "
01764             << max(pVals_) << nl
01765             << "    isoValue      : " << iso << nl
01766             << "    regularise    : " << regularise_ << nl
01767             << "    mergeTol      : " << mergeTol << nl
01768             << endl;
01769     }
01770 
01771     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
01772 
01773 
01774     // Rewrite input field
01775     // ~~~~~~~~~~~~~~~~~~~
01776     // Rewrite input volScalarField to have interpolated values
01777     // on separated patches.
01778 
01779     cValsPtr_.reset(adaptPatchFields(cVals).ptr());
01780 
01781 
01782     // Construct cell centres field consistent with cVals
01783     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01784     // Generate field to interpolate. This is identical to the mesh.C()
01785     // except on separated coupled patches and on empty patches.
01786 
01787     slicedVolVectorField meshC
01788     (
01789         IOobject
01790         (
01791             "C",
01792             mesh_.pointsInstance(),
01793             mesh_.meshSubDir,
01794             mesh_,
01795             IOobject::NO_READ,
01796             IOobject::NO_WRITE,
01797             false
01798         ),
01799         mesh_,
01800         dimLength,
01801         mesh_.cellCentres(),
01802         mesh_.faceCentres()
01803     );
01804     forAll(patches, patchI)
01805     {
01806         const polyPatch& pp = patches[patchI];
01807 
01808         // Adapt separated coupled (proc and cyclic) patches
01809         if (isA<coupledPolyPatch>(pp) && !collocatedPatch(pp))
01810         {
01811             fvPatchVectorField& pfld = const_cast<fvPatchVectorField&>
01812             (
01813                 meshC.boundaryField()[patchI]
01814             );
01815 
01816             PackedBoolList isCollocated
01817             (
01818                 collocatedFaces(refCast<const coupledPolyPatch>(pp))
01819             );
01820 
01821             forAll(isCollocated, i)
01822             {
01823                 if (!isCollocated[i])
01824                 {
01825                     pfld[i] = mesh_.faceCentres()[pp.start()+i];
01826                 }
01827             }
01828         }
01829         else if (isA<emptyPolyPatch>(pp))
01830         {
01831             typedef slicedVolVectorField::GeometricBoundaryField bType;
01832 
01833             bType& bfld = const_cast<bType&>(meshC.boundaryField());
01834 
01835             // Clear old value. Cannot resize it since is a slice.
01836             bfld.set(patchI, NULL);
01837 
01838             // Set new value we can change
01839             bfld.set
01840             (
01841                 patchI,
01842                 new calculatedFvPatchField<vector>
01843                 (
01844                     mesh_.boundary()[patchI],
01845                     meshC
01846                 )
01847             );
01848 
01849             // Change to face centres
01850             bfld[patchI] = pp.patchSlice(mesh_.faceCentres());
01851         }
01852     }
01853 
01854 
01855 
01856     // Pre-calculate patch-per-face to avoid whichPatch call.
01857     labelList boundaryRegion(mesh_.nFaces()-mesh_.nInternalFaces());
01858 
01859     forAll(patches, patchI)
01860     {
01861         const polyPatch& pp = patches[patchI];
01862 
01863         label faceI = pp.start();
01864 
01865         forAll(pp, i)
01866         {
01867             boundaryRegion[faceI-mesh_.nInternalFaces()] = patchI;
01868             faceI++;
01869         }
01870     }
01871 
01872 
01873 
01874     // Determine if any cut through face/cell
01875     calcCutTypes(boundaryRegion, meshC, cValsPtr_(), pVals_);
01876 
01877 
01878     DynamicList<point> snappedPoints(nCutCells_);
01879 
01880     // Per cc -1 or a point inside snappedPoints.
01881     labelList snappedCc;
01882     if (regularise_)
01883     {
01884         calcSnappedCc
01885         (
01886             boundaryRegion,
01887             meshC,
01888             cValsPtr_(),
01889             pVals_,
01890 
01891             snappedPoints,
01892             snappedCc
01893         );
01894     }
01895     else
01896     {
01897         snappedCc.setSize(mesh_.nCells());
01898         snappedCc = -1;
01899     }
01900 
01901 
01902 
01903     if (debug)
01904     {
01905         Pout<< "isoSurface : shifted " << snappedPoints.size()
01906             << " cell centres to intersection." << endl;
01907     }
01908 
01909     label nCellSnaps = snappedPoints.size();
01910 
01911 
01912     // Per point -1 or a point inside snappedPoints.
01913     labelList snappedPoint;
01914     if (regularise_)
01915     {
01916         // Determine if point is on boundary.
01917         PackedBoolList isBoundaryPoint(mesh_.nPoints());
01918 
01919         forAll(patches, patchI)
01920         {
01921             // Mark all boundary points that are not physically coupled
01922             // (so anything but collocated coupled patches)
01923 
01924             if (patches[patchI].coupled())
01925             {
01926                 if (!collocatedPatch(patches[patchI]))
01927                 {
01928                     const coupledPolyPatch& cpp =
01929                         refCast<const coupledPolyPatch>
01930                         (
01931                             patches[patchI]
01932                         );
01933 
01934                     PackedBoolList isCollocated(collocatedFaces(cpp));
01935 
01936                     forAll(isCollocated, i)
01937                     {
01938                         if (!isCollocated[i])
01939                         {
01940                             const face& f = mesh_.faces()[cpp.start()+i];
01941 
01942                             forAll(f, fp)
01943                             {
01944                                 isBoundaryPoint.set(f[fp], 1);
01945                             }
01946                         }
01947                     }
01948                 }
01949             }
01950             else
01951             {
01952                 const polyPatch& pp = patches[patchI];
01953 
01954                 forAll(pp, i)
01955                 {
01956                     const face& f = mesh_.faces()[pp.start()+i];
01957 
01958                     forAll(f, fp)
01959                     {
01960                         isBoundaryPoint.set(f[fp], 1);
01961                     }
01962                 }
01963             }
01964         }
01965 
01966         calcSnappedPoint
01967         (
01968             isBoundaryPoint,
01969             boundaryRegion,
01970             meshC,
01971             cValsPtr_(),
01972             pVals_,
01973 
01974             snappedPoints,
01975             snappedPoint
01976         );
01977     }
01978     else
01979     {
01980         snappedPoint.setSize(mesh_.nPoints());
01981         snappedPoint = -1;
01982     }
01983 
01984     if (debug)
01985     {
01986         Pout<< "isoSurface : shifted " << snappedPoints.size()-nCellSnaps
01987             << " vertices to intersection." << endl;
01988     }
01989 
01990 
01991 
01992     DynamicList<point> triPoints(nCutCells_);
01993     DynamicList<label> triMeshCells(nCutCells_);
01994 
01995     generateTriPoints
01996     (
01997         cValsPtr_(),
01998         pVals_,
01999 
02000         meshC,
02001         mesh_.points(),
02002 
02003         snappedPoints,
02004         snappedCc,
02005         snappedPoint,
02006 
02007         triPoints,
02008         triMeshCells
02009     );
02010 
02011     if (debug)
02012     {
02013         Pout<< "isoSurface : generated " << triMeshCells.size()
02014             << " unmerged triangles from " << triPoints.size()
02015             << " unmerged points." << endl;
02016     }
02017 
02018 
02019     // Merge points and compact out non-valid triangles
02020     labelList triMap;           // merged to unmerged triangle
02021     triSurface::operator=
02022     (
02023         stitchTriPoints
02024         (
02025             true,               // check for duplicate tris
02026             triPoints,
02027             triPointMergeMap_,  // unmerged to merged point
02028             triMap
02029         )
02030     );
02031 
02032     if (debug)
02033     {
02034         Pout<< "isoSurface : generated " << triMap.size()
02035             << " merged triangles." << endl;
02036     }
02037 
02038     meshCells_.setSize(triMap.size());
02039     forAll(triMap, i)
02040     {
02041         meshCells_[i] = triMeshCells[triMap[i]];
02042     }
02043 
02044     if (debug)
02045     {
02046         Pout<< "isoSurface : checking " << size()
02047             << " triangles for validity." << endl;
02048 
02049         forAll(*this, triI)
02050         {
02051             // Copied from surfaceCheck
02052             validTri(*this, triI);
02053         }
02054     }
02055 
02056 
02057     //if (false)
02058     //{
02059     List<FixedList<label, 3> > faceEdges;
02060     labelList edgeFace0, edgeFace1;
02061     Map<labelList> edgeFacesRest;
02062 
02063 
02064     while (true)
02065     {
02066         // Calculate addressing
02067         calcAddressing(*this, faceEdges, edgeFace0, edgeFace1, edgeFacesRest);
02068 
02069         // See if any dangling triangles
02070         boolList keepTriangles;
02071         label nDangling = markDanglingTriangles
02072         (
02073             faceEdges,
02074             edgeFace0,
02075             edgeFace1,
02076             edgeFacesRest,
02077             keepTriangles
02078         );
02079 
02080         if (debug)
02081         {
02082             Pout<< "isoSurface : detected " << nDangling
02083                 << " dangling triangles." << endl;
02084         }
02085 
02086         if (nDangling == 0)
02087         {
02088             break;
02089         }
02090 
02091         // Create face map (new to old)
02092         labelList subsetTriMap(findIndices(keepTriangles, true));
02093 
02094         labelList subsetPointMap;
02095         labelList reversePointMap;
02096         triSurface::operator=
02097         (
02098             subsetMesh
02099             (
02100                 *this,
02101                 subsetTriMap,
02102                 reversePointMap,
02103                 subsetPointMap
02104             )
02105         );
02106         meshCells_ = labelField(meshCells_, subsetTriMap);
02107         inplaceRenumber(reversePointMap, triPointMergeMap_);
02108     }
02109 
02110     orientSurface(*this, faceEdges, edgeFace0, edgeFace1, edgeFacesRest);
02111     //}
02112 
02113 
02114     if (debug)
02115     {
02116         fileName stlFile = mesh_.time().path() + ".stl";
02117         Pout<< "Dumping surface to " << stlFile << endl;
02118         triSurface::write(stlFile);
02119     }
02120 }
02121 
02122 
02123 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd