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

booleanSurface.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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "booleanSurface.H"
00030 #include "intersectedSurface.H"
00031 #include "orientedSurface.H"
00032 #include "triSurfaceSearch.H"
00033 #include "OFstream.H"
00034 #include "treeBoundBox.H"
00035 #include "meshTools.H"
00036 
00037 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00038 
00039 defineTypeNameAndDebug(Foam::booleanSurface, 0);
00040 
00041 
00042 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00043 
00044 // Check whether at least one of faces connected to the intersection has been
00045 // marked.
00046 void Foam::booleanSurface::checkIncluded
00047 (
00048     const intersectedSurface& surf,
00049     const labelList& faceZone,
00050     const label includedFace
00051 )
00052 {
00053     forAll(surf.intersectionEdges(), intEdgeI)
00054     {
00055         label edgeI = surf.intersectionEdges()[intEdgeI];
00056 
00057         const labelList& myFaces = surf.edgeFaces()[edgeI];
00058 
00059         bool usesIncluded = false;
00060 
00061         forAll(myFaces, myFaceI)
00062         {
00063             if (faceZone[myFaces[myFaceI]] == faceZone[includedFace])
00064             {
00065                 usesIncluded = true;
00066 
00067                 break;
00068             }
00069         }
00070 
00071         if (!usesIncluded)
00072         {
00073             FatalErrorIn
00074             (
00075                 "booleanSurface::checkIncluded(const intersectedSurface&"
00076                 ", const labelList&, const label)"
00077             )   << "None of the faces reachable from face " << includedFace
00078                 << " connects to the intersection."
00079                 << exit(FatalError);
00080         }
00081     }
00082 }
00083 
00084 
00085 // Linear lookup
00086 Foam::label Foam::booleanSurface::index
00087 (
00088     const labelList& elems,
00089     const label elem
00090 )
00091 {
00092     forAll(elems, elemI)
00093     {
00094         if (elems[elemI] == elem)
00095         {
00096             return elemI;
00097         }
00098     }
00099     return -1;
00100 }
00101 
00102 
00103 Foam::label Foam::booleanSurface::findEdge
00104 (
00105     const edgeList& edges,
00106     const labelList& edgeLabels,
00107     const edge& e
00108 )
00109 {
00110     forAll(edgeLabels, edgeLabelI)
00111     {
00112         if (edges[edgeLabels[edgeLabelI]] == e)
00113         {
00114             return edgeLabels[edgeLabelI];
00115         }
00116     }
00117     FatalErrorIn
00118     (
00119         "booleanSurface::findEdge(const edgeList&, const labelList&"
00120         ", const edge&)"
00121     )   << "Cannot find edge " << e << " in edges " << edgeLabels
00122         << abort(FatalError);
00123 
00124     return -1;
00125 }
00126 
00127 
00128 // Generate combined patchList (returned). Sets patchMap to map from surf2
00129 // region numbers into combined patchList
00130 Foam::geometricSurfacePatchList Foam::booleanSurface::mergePatches
00131 (
00132     const triSurface& surf1,
00133     const triSurface& surf2,
00134     labelList& patchMap2
00135 )
00136 {
00137     // Size too big.
00138     geometricSurfacePatchList combinedPatches
00139     (
00140         surf1.patches().size()
00141       + surf2.patches().size()
00142     );
00143 
00144     // Copy all patches of surf1
00145     label combinedPatchI = 0;
00146     forAll(surf1.patches(), patchI)
00147     {
00148         combinedPatches[combinedPatchI++] = surf1.patches()[patchI];
00149     }
00150 
00151     // (inefficiently) add unique patches from surf2
00152     patchMap2.setSize(surf2.patches().size());
00153 
00154     forAll(surf2.patches(), patch2I)
00155     {
00156         label index = -1;
00157 
00158         forAll(surf1.patches(), patch1I)
00159         {
00160             if (surf1.patches()[patch1I] == surf2.patches()[patch2I])
00161             {
00162                 index = patch1I;
00163 
00164                 break;
00165             }
00166         }
00167 
00168         if (index == -1)
00169         {
00170             combinedPatches[combinedPatchI] = surf2.patches()[patch2I];
00171             patchMap2[patch2I] = combinedPatchI;
00172             combinedPatchI++;
00173         }
00174         else
00175         {
00176             patchMap2[patch2I] = index;
00177         }
00178     }
00179 
00180     combinedPatches.setSize(combinedPatchI);
00181 
00182     return combinedPatches;
00183 }
00184 
00185 
00186 void Foam::booleanSurface::propagateEdgeSide
00187 (
00188     const triSurface& surf,
00189     const label prevVert0,
00190     const label prevFaceI,
00191     const label prevState,
00192     const label edgeI,
00193     labelList& side
00194 )
00195 {
00196     const labelList& eFaces = surf.sortedEdgeFaces()[edgeI];
00197 
00198     // Simple case. Propagate side.
00199     if (eFaces.size() == 2)
00200     {
00201         forAll(eFaces, eFaceI)
00202         {
00203             propagateSide
00204             (
00205                 surf,
00206                 prevState,
00207                 eFaces[eFaceI],
00208                 side
00209             );
00210         }
00211     }
00212 
00213 
00214     if (((eFaces.size() % 2) == 1) && (eFaces.size() != 1))
00215     {
00216         FatalErrorIn
00217         (
00218             "booleanSurface::propagateEdgeSide(const triSurface&,"
00219             "const label, const label, const label, const label,"
00220             " labelList&)"
00221         )   << "Don't know how to handle edges with odd number of faces"
00222             << endl
00223             << "edge:" << edgeI << " vertices:" << surf.edges()[edgeI]
00224             << " coming from face:" << prevFaceI
00225             << " edgeFaces:" << eFaces << abort(FatalError);
00226     }
00227 
00228 
00229     // Get position of face in edgeFaces
00230     label ind = index(eFaces, prevFaceI);
00231 
00232     // Determine orientation of faces around edge prevVert0
00233     // (might be opposite of edge)
00234     const edge& e = surf.edges()[edgeI];
00235 
00236     // Get next face to include
00237     label nextInd;
00238     label prevInd;
00239 
00240     if (e.start() == prevVert0)
00241     {
00242         // Edge (and hence eFaces) in same order as prevVert0.
00243         // Take next face from sorted list
00244         nextInd = eFaces.fcIndex(ind);
00245         prevInd = eFaces.rcIndex(ind);
00246     }
00247     else
00248     {
00249         // Take previous face from sorted neighbours
00250         nextInd = eFaces.rcIndex(ind);
00251         prevInd = eFaces.fcIndex(ind);
00252     }
00253 
00254 
00255     if (prevState == OUTSIDE)
00256     {
00257         // Coming from outside. nextInd is outside, rest is inside.
00258 
00259         forAll(eFaces, eFaceI)
00260         {
00261             if (eFaceI != ind)
00262             {
00263                 label nextState;
00264 
00265                 if (eFaceI == nextInd)
00266                 {
00267                     nextState = OUTSIDE;
00268                 }
00269                 else
00270                 {
00271                     nextState = INSIDE;
00272                 }
00273 
00274                 propagateSide
00275                 (
00276                     surf,
00277                     nextState,
00278                     eFaces[eFaceI],
00279                     side
00280                 );
00281             }
00282         }
00283     }
00284     else
00285     {
00286         // Coming from inside. prevInd is inside as well, rest is outside.
00287 
00288         forAll(eFaces, eFaceI)
00289         {
00290             if (eFaceI != ind)
00291             {
00292                 label nextState;
00293 
00294                 if (eFaceI == prevInd)
00295                 {
00296                     nextState = INSIDE;
00297                 }
00298                 else
00299                 {
00300                     nextState = OUTSIDE;
00301                 }
00302 
00303                 propagateSide
00304                 (
00305                     surf,
00306                     nextState,
00307                     eFaces[eFaceI],
00308                     side
00309                 );
00310             }
00311         }
00312     }
00313 }
00314 
00315 
00316 // Face-edge walk. Determines inside/outside for all faces connected to an edge.
00317 void Foam::booleanSurface::propagateSide
00318 (
00319     const triSurface& surf,
00320     const label prevState,
00321     const label faceI,
00322     labelList& side
00323 )
00324 {
00325     if (side[faceI] == UNVISITED)
00326     {
00327         side[faceI] = prevState;
00328 
00329         const labelledTri& tri = surf[faceI];
00330 
00331         // Get copy of face labels
00332         label a = tri[0];
00333         label b = tri[1];
00334         label c = tri[2];
00335 
00336         // Go and visit my edges' face-neighbours.
00337 
00338         const labelList& myEdges = surf.faceEdges()[faceI];
00339 
00340         label edgeAB = findEdge(surf.edges(), myEdges, edge(a, b));
00341 
00342         propagateEdgeSide
00343         (
00344             surf,
00345             a,
00346             faceI,
00347             prevState,
00348             edgeAB,
00349             side
00350         );
00351 
00352         label edgeBC = findEdge(surf.edges(), myEdges, edge(b, c));
00353 
00354         propagateEdgeSide
00355         (
00356             surf,
00357             b,
00358             faceI,
00359             prevState,
00360             edgeBC,
00361             side
00362         );
00363 
00364         label edgeCA = findEdge(surf.edges(), myEdges, edge(c, a));
00365 
00366         propagateEdgeSide
00367         (
00368             surf,
00369             c,
00370             faceI,
00371             prevState,
00372             edgeCA,
00373             side
00374         );
00375     }
00376 }
00377 
00378 
00379 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00380 
00381 // Null constructor
00382 Foam::booleanSurface::booleanSurface()
00383 :
00384     triSurface()
00385 {}
00386 
00387 
00388 // Construct from surfaces and face to include for every surface
00389 Foam::booleanSurface::booleanSurface
00390 (
00391     const triSurface& surf1,
00392     const triSurface& surf2,
00393     const surfaceIntersection& inter,
00394     const label includeFace1,
00395     const label includeFace2
00396 )
00397 :
00398     triSurface(),
00399     faceMap_()
00400 {
00401     if (debug)
00402     {
00403         Pout<< "booleanSurface : Generating intersected surface for surf1"
00404             << endl;
00405     }
00406 
00407     // Add intersection to surface1 (retriangulates cut faces)
00408     intersectedSurface cutSurf1(surf1, true, inter);
00409 
00410 
00411     if (debug)
00412     {
00413         Pout<< "booleanSurface : Generated cutSurf1: " << endl;
00414         cutSurf1.writeStats(Pout);
00415 
00416         Pout<< "Writing to file cutSurf1.ftr" << endl;
00417         OFstream cutSurf1Stream("cutSurf1.ftr");
00418         cutSurf1.write(cutSurf1Stream);
00419     }
00420 
00421     if (debug)
00422     {
00423         Pout<< "booleanSurface : Generating intersected surface for surf2"
00424             << endl;
00425     }
00426 
00427     // Add intersection to surface2
00428     intersectedSurface cutSurf2(surf2, false, inter);
00429 
00430     if (debug)
00431     {
00432         Pout<< "booleanSurface : Generated cutSurf2: " << endl;
00433         cutSurf2.writeStats(Pout);
00434 
00435         Pout<< "Writing to file cutSurf2.ftr" << endl;
00436         OFstream cutSurf2Stream("cutSurf2.ftr");
00437         cutSurf2.write(cutSurf2Stream);
00438     }
00439 
00440 
00441     // Find (first) face of cutSurf1 that originates from includeFace1
00442     label cutSurf1FaceI = index(cutSurf1.faceMap(), includeFace1);
00443 
00444     if (debug)
00445     {
00446         Pout<< "cutSurf1 : starting to fill from face:" << cutSurf1FaceI
00447             << endl;
00448     }
00449 
00450     if (cutSurf1FaceI == -1)
00451     {
00452         FatalErrorIn
00453         (
00454            "booleanSurface(const triSurfaceSearch&"
00455             ", const label, const triSurfaceSearch&, const label)"
00456         )   << "Did not find face with label " << includeFace1
00457             << " in intersectedSurface."
00458             << exit(FatalError);
00459     }
00460 
00461     // Find (first) face of cutSurf2 that originates from includeFace1
00462     label cutSurf2FaceI = index(cutSurf2.faceMap(), includeFace2);
00463 
00464     if (debug)
00465     {
00466         Pout<< "cutSurf2 : starting to fill from face:" << cutSurf2FaceI
00467             << endl;
00468     }
00469     if (cutSurf2FaceI == -1)
00470     {
00471         FatalErrorIn
00472         (
00473            "booleanSurface(const triSurfaceSearch&"
00474             ", const label, const triSurfaceSearch&, const label)"
00475         )   << "Did not find face with label " << includeFace2
00476             << " in intersectedSurface."
00477             << exit(FatalError);
00478     }
00479 
00480 
00481     //
00482     // Mark faces of cutSurf1 that need to be kept by walking from includeFace1
00483     // without crossing any edges of the intersection.
00484     //
00485 
00486     // Mark edges on intersection
00487     const labelList& int1Edges = cutSurf1.intersectionEdges();
00488 
00489     boolList isIntersectionEdge1(cutSurf1.nEdges(), false);
00490     forAll(int1Edges, intEdgeI)
00491     {
00492         label edgeI = int1Edges[intEdgeI];
00493         isIntersectionEdge1[edgeI] = true;
00494     }
00495 
00496     labelList faceZone1;
00497     (void)cutSurf1.markZones(isIntersectionEdge1, faceZone1);
00498 
00499 
00500     // Check whether at least one of sides of intersection has been marked.
00501     checkIncluded(cutSurf1, faceZone1, cutSurf1FaceI);
00502 
00503     // Subset zone which includes cutSurf2FaceI
00504     boolList includedFaces1(cutSurf1.size(), false);
00505 
00506     forAll(faceZone1, faceI)
00507     {
00508         if (faceZone1[faceI] == faceZone1[cutSurf1FaceI])
00509         {
00510             includedFaces1[faceI] = true;
00511         }
00512     }
00513 
00514     // Subset to include only interesting part
00515     labelList pointMap1;
00516     labelList faceMap1;
00517 
00518     triSurface subSurf1
00519     (
00520         cutSurf1.subsetMesh
00521         (
00522             includedFaces1,
00523             pointMap1,
00524             faceMap1
00525         )
00526     );
00527 
00528 
00529     //
00530     // Mark faces of cutSurf2 that need to be kept by walking from includeFace2
00531     // without crossing any edges of the intersection.
00532     //
00533 
00534     // Mark edges and points on intersection
00535     const labelList& int2Edges = cutSurf2.intersectionEdges();
00536 
00537     boolList isIntersectionEdge2(cutSurf2.nEdges(), false);
00538     forAll(int2Edges, intEdgeI)
00539     {
00540         label edgeI = int2Edges[intEdgeI];
00541         isIntersectionEdge2[edgeI] = true;
00542     }
00543 
00544     labelList faceZone2;
00545     (void)cutSurf2.markZones(isIntersectionEdge2, faceZone2);
00546 
00547 
00548     // Check whether at least one of sides of intersection has been marked.
00549     checkIncluded(cutSurf2, faceZone2, cutSurf2FaceI);
00550 
00551     // Subset zone which includes cutSurf2FaceI
00552     boolList includedFaces2(cutSurf2.size(), false);
00553 
00554     forAll(faceZone2, faceI)
00555     {
00556         if (faceZone2[faceI] == faceZone2[cutSurf2FaceI])
00557         {
00558             includedFaces2[faceI] = true;
00559         }
00560     }
00561 
00562     labelList pointMap2;
00563     labelList faceMap2;
00564 
00565     triSurface subSurf2
00566     (
00567         cutSurf2.subsetMesh
00568         (
00569             includedFaces2,
00570             pointMap2,
00571             faceMap2
00572         )
00573     );
00574 
00575 
00576     //
00577     // Now match up the corresponding points on the intersection. The
00578     // intersectedSurfaces will have the points resulting from the
00579     // intersection last in their points and in the same
00580     // order so we can use the pointMaps from the subsets to find them.
00581     //
00582     // We keep the vertices on the first surface and renumber those on the
00583     // second one.
00584 
00585 
00586     //
00587     // points
00588     //
00589     pointField combinedPoints
00590     (
00591         subSurf1.nPoints()
00592       + subSurf2.nPoints()
00593       - (cutSurf2.nPoints() - cutSurf2.nSurfacePoints())
00594     );
00595 
00596     // Copy points from subSurf1 and remember the labels of the ones in
00597     // the intersection
00598     labelList intersectionLabels(cutSurf1.nPoints() - cutSurf1.nSurfacePoints());
00599 
00600     label combinedPointI = 0;
00601 
00602     forAll(subSurf1.points(), pointI)
00603     {
00604         // Label in cutSurf
00605         label cutSurfPointI = pointMap1[pointI];
00606 
00607         if (!cutSurf1.isSurfacePoint(cutSurfPointI))
00608         {
00609             // Label in original intersection is equal to the cutSurfPointI
00610 
00611             // Remember label in combinedPoints for intersection point.
00612             intersectionLabels[cutSurfPointI] = combinedPointI;
00613         }
00614 
00615         // Copy point
00616         combinedPoints[combinedPointI++] = subSurf1.points()[pointI];
00617     }
00618 
00619     // Append points from subSurf2 (if they are not intersection points)
00620     // and construct mapping
00621     labelList pointMap(subSurf2.nPoints());
00622 
00623     forAll(subSurf2.points(), pointI)
00624     {
00625         // Label in cutSurf
00626         label cutSurfPointI = pointMap2[pointI];
00627 
00628         if (!cutSurf2.isSurfacePoint(cutSurfPointI))
00629         {
00630             // Lookup its label in combined point list.
00631             pointMap[pointI] = intersectionLabels[cutSurfPointI];
00632         }
00633         else
00634         {
00635             pointMap[pointI] = combinedPointI;
00636 
00637             combinedPoints[combinedPointI++] = subSurf2.points()[pointI];
00638         }
00639     }
00640 
00641 
00642     //
00643     // patches
00644     //
00645 
00646     labelList patchMap2;
00647 
00648     geometricSurfacePatchList combinedPatches
00649     (
00650         mergePatches
00651         (
00652             surf1,
00653             surf2,
00654             patchMap2
00655         )
00656     );
00657 
00658 
00659     //
00660     // faces
00661     //
00662 
00663     List<labelledTri> combinedFaces(subSurf1.size() + subSurf2.size());
00664 
00665     faceMap_.setSize(combinedFaces.size());
00666 
00667     // Copy faces from subSurf1. No need for renumbering.
00668     label combinedFaceI = 0;
00669     forAll(subSurf1, faceI)
00670     {
00671         faceMap_[combinedFaceI] = faceMap1[faceI];
00672         combinedFaces[combinedFaceI++] = subSurf1[faceI];
00673     }
00674 
00675     // Copy and renumber faces from subSurf2.
00676     forAll(subSurf2, faceI)
00677     {
00678         const labelledTri& f = subSurf2[faceI];
00679 
00680         faceMap_[combinedFaceI] = -faceMap2[faceI]-1;
00681 
00682         combinedFaces[combinedFaceI++] =
00683             labelledTri
00684             (
00685                 pointMap[f[0]],
00686                 pointMap[f[1]],
00687                 pointMap[f[2]],
00688                 patchMap2[f.region()]
00689             );
00690     }
00691 
00692     triSurface::operator=
00693     (
00694         triSurface
00695         (
00696             combinedFaces,
00697             combinedPatches,
00698             combinedPoints
00699         )
00700     );
00701 }
00702 
00703 
00704 // Construct from surfaces and boolean operation
00705 Foam::booleanSurface::booleanSurface
00706 (
00707     const triSurface& surf1,
00708     const triSurface& surf2,
00709     const surfaceIntersection& inter,
00710     const label booleanOp
00711 )
00712 :
00713     triSurface(),
00714     faceMap_()
00715 {
00716     if (debug)
00717     {
00718         Pout<< "booleanSurface : Testing surf1 and surf2" << endl;
00719 
00720         {
00721             const labelListList& edgeFaces = surf1.edgeFaces();
00722 
00723             forAll(edgeFaces, edgeI)
00724             {
00725                 const labelList& eFaces = edgeFaces[edgeI];
00726 
00727                 if (eFaces.size() == 1)
00728                 {
00729                     WarningIn("booleanSurface::booleanSurface")
00730                         << "surf1 is open surface at edge " << edgeI
00731                         << " verts:" << surf1.edges()[edgeI]
00732                         << " connected to faces " << eFaces << endl;
00733                 }
00734             }
00735         }
00736         {
00737             const labelListList& edgeFaces = surf2.edgeFaces();
00738 
00739             forAll(edgeFaces, edgeI)
00740             {
00741                 const labelList& eFaces = edgeFaces[edgeI];
00742 
00743                 if (eFaces.size() == 1)
00744                 {
00745                     WarningIn("booleanSurface::booleanSurface")
00746                         << "surf2 is open surface at edge " << edgeI
00747                         << " verts:" << surf2.edges()[edgeI]
00748                         << " connected to faces " << eFaces << endl;
00749                 }
00750             }
00751         }
00752     }
00753 
00754 
00755     //
00756     // Surface 1
00757     //
00758 
00759     if (debug)
00760     {
00761         Pout<< "booleanSurface : Generating intersected surface for surf1"
00762             << endl;
00763     }
00764 
00765     // Add intersection to surface1 (retriangulates cut faces)
00766     intersectedSurface cutSurf1(surf1, true, inter);
00767 
00768     if (debug)
00769     {
00770         Pout<< "booleanSurface : Generated cutSurf1: " << endl;
00771         cutSurf1.writeStats(Pout);
00772 
00773         Pout<< "Writing to file cutSurf1.ftr" << endl;
00774         OFstream cutSurf1Stream("cutSurf1.ftr");
00775         cutSurf1.write(cutSurf1Stream);
00776     }
00777 
00778 
00779     //
00780     // Surface 2
00781     //
00782 
00783     if (debug)
00784     {
00785         Pout<< "booleanSurface : Generating intersected surface for surf2"
00786             << endl;
00787     }
00788 
00789     // Add intersection to surface2
00790     intersectedSurface cutSurf2(surf2, false, inter);
00791 
00792     if (debug)
00793     {
00794         Pout<< "booleanSurface : Generated cutSurf2: " << endl;
00795         cutSurf2.writeStats(Pout);
00796 
00797         Pout<< "Writing to file cutSurf2.ftr" << endl;
00798         OFstream cutSurf2Stream("cutSurf2.ftr");
00799         cutSurf2.write(cutSurf2Stream);
00800     }
00801 
00802 
00803     //
00804     // patches
00805     //
00806 
00807     labelList patchMap2;
00808 
00809     geometricSurfacePatchList combinedPatches
00810     (
00811         mergePatches
00812         (
00813             surf1,
00814             surf2,
00815             patchMap2
00816         )
00817     );
00818 
00819 
00820     //
00821     // Now match up the corresponding points on the intersection. The
00822     // intersectedSurfaces will have the points resulting from the
00823     // intersection first in their points and in the same
00824     // order
00825     //
00826     // We keep the vertices on the first surface and renumber those on the
00827     // second one.
00828 
00829     pointField combinedPoints(cutSurf1.nPoints() + cutSurf2.nSurfacePoints());
00830 
00831     // Copy all points from 1 and non-intersection ones from 2.
00832 
00833     label combinedPointI = 0;
00834 
00835     forAll(cutSurf1.points(), pointI)
00836     {
00837         combinedPoints[combinedPointI++] = cutSurf1.points()[pointI];
00838     }
00839 
00840     for
00841     (
00842         label pointI = 0;
00843         pointI < cutSurf2.nSurfacePoints();
00844         pointI++
00845     )
00846     {
00847         combinedPoints[combinedPointI++] = cutSurf2.points()[pointI];
00848     }
00849 
00850     // Point order is now
00851     // - 0.. cutSurf1.nSurfacePoints : original surf1 points
00852     // -  .. cutSurf1.nPoints        : intersection points
00853     // -  .. cutSurf2.nSurfacePoints : original surf2 points
00854 
00855     if (debug)
00856     {
00857         Pout<< "booleanSurface : generated points:" << nl
00858             << "    0 .. " << cutSurf1.nSurfacePoints()-1
00859             << " : original surface1"
00860             << nl
00861             << "    " << cutSurf1.nSurfacePoints()
00862             << " .. " << cutSurf1.nPoints()-1
00863             << " : intersection points"
00864             << nl
00865             << "    " << cutSurf1.nPoints() << " .. "
00866             << cutSurf2.nSurfacePoints()-1
00867             << " : surface2 points"
00868             << nl
00869             << endl;
00870     }
00871 
00872     // Copy faces. Faces from surface 1 keep vertex numbering and region info.
00873     // Faces from 2 get vertices and region renumbered.
00874     List<labelledTri> combinedFaces(cutSurf1.size() + cutSurf2.size());
00875 
00876     label combinedFaceI = 0;
00877 
00878     forAll(cutSurf1, faceI)
00879     {
00880         combinedFaces[combinedFaceI++] = cutSurf1[faceI];
00881     }
00882 
00883     forAll(cutSurf2, faceI)
00884     {
00885         labelledTri& combinedTri = combinedFaces[combinedFaceI++];
00886 
00887         const labelledTri& tri = cutSurf2[faceI];
00888 
00889         forAll(tri, fp)
00890         {
00891             if (cutSurf2.isSurfacePoint(tri[fp]))
00892             {
00893                 // Renumber. Surface2 points are after ones from surf 1.
00894                 combinedTri[fp] = tri[fp] + cutSurf1.nPoints();
00895             }
00896             else
00897             {
00898                 // Is intersection.
00899                 combinedTri[fp] =
00900                     tri[fp]
00901                   - cutSurf2.nSurfacePoints()
00902                   + cutSurf1.nSurfacePoints();
00903             }
00904         }
00905         combinedTri.region() = patchMap2[tri.region()];
00906     }
00907 
00908 
00909     // Now we have surface in combinedFaces and combinedPoints. Use
00910     // booleanOp to determine which part of what to keep.
00911 
00912     // Construct addressing for whole part.
00913     triSurface combinedSurf
00914     (
00915         combinedFaces,
00916         combinedPatches,
00917         combinedPoints
00918     );
00919 
00920     if (debug)
00921     {
00922         Pout<< "booleanSurface : Generated combinedSurf: " << endl;
00923         combinedSurf.writeStats(Pout);
00924 
00925         Pout<< "Writing to file combinedSurf.ftr" << endl;
00926         OFstream combinedSurfStream("combinedSurf.ftr");
00927         combinedSurf.write(combinedSurfStream);
00928     }
00929 
00930 
00931     if (booleanOp == booleanSurface::ALL)
00932     {
00933         // Special case: leave surface multiply connected
00934 
00935         faceMap_.setSize(combinedSurf.size());
00936 
00937         label combinedFaceI = 0;
00938 
00939         forAll(cutSurf1, faceI)
00940         {
00941             faceMap_[combinedFaceI++] = cutSurf1.faceMap()[faceI];
00942         }
00943         forAll(cutSurf2, faceI)
00944         {
00945             faceMap_[combinedFaceI++] = -cutSurf2.faceMap()[faceI] - 1;
00946         }
00947 
00948         triSurface::operator=(combinedSurf);
00949 
00950         return;
00951     }
00952 
00953 
00954     // Get outside point.
00955     point outsidePoint = 2 * treeBoundBox(combinedSurf.localPoints()).span();
00956 
00957     //
00958     // Linear search for nearest point on surface.
00959     //
00960 
00961     const pointField& pts = combinedSurf.points();
00962 
00963     label minFaceI = -1;
00964     pointHit minHit(false, vector::zero, GREAT, true);
00965 
00966     forAll(combinedSurf, faceI)
00967     {
00968         const labelledTri& f = combinedSurf[faceI];
00969 
00970         pointHit curHit =
00971             triPointRef
00972             (
00973                 pts[f[0]],
00974                 pts[f[1]],
00975                 pts[f[2]]
00976             ).nearestPoint(outsidePoint);
00977 
00978         if (curHit.distance() < minHit.distance())
00979         {
00980             minHit = curHit;
00981 
00982             minFaceI = faceI;
00983         }
00984     }
00985 
00986     if (debug)
00987     {
00988         Pout<< "booleanSurface : found for point:" << outsidePoint
00989             << "  nearest face:" << minFaceI
00990             << "  nearest point:" << minHit.rawPoint()
00991             << endl;
00992     }
00993 
00994     // Visibility/side of face:
00995     //      UNVISITED: unvisited
00996     //      OUTSIDE: visible from outside
00997     //      INSIDE: invisible from outside
00998     labelList side(combinedSurf.size(), UNVISITED);
00999 
01000     // Walk face-edge-face and propagate inside/outside status.
01001     propagateSide(combinedSurf, OUTSIDE, minFaceI, side);
01002 
01003 
01004     // Depending on operation include certain faces.
01005     //  AND: faces on inside of 1 and of 2
01006     //  OR: faces on outside of 1 and of 2
01007     //  XOR: faces on outside of 1 and inside of 2
01008 
01009     boolList include(combinedSurf.size(), false);
01010 
01011     forAll(side, faceI)
01012     {
01013         if (side[faceI] == UNVISITED)
01014         {
01015             FatalErrorIn
01016             (
01017                 "booleanSurface::booleanSurface"
01018                 "(const triSurfaceSearch&, const triSurfaceSearch&"
01019                 ", const label booleanOp)"
01020             )   << "Face " << faceI << " has not been reached by walking from"
01021                 << " nearest point " << minHit.rawPoint()
01022                 << " nearest face " << minFaceI << exit(FatalError);
01023         }
01024         else if (side[faceI] == OUTSIDE)
01025         {
01026             if (booleanOp == booleanSurface::OR)
01027             {
01028                 include[faceI] = true;
01029             }
01030             else if (booleanOp == booleanSurface::AND)
01031             {
01032                 include[faceI] = false;
01033             }
01034             else    // xor
01035             {
01036                 include[faceI] = (faceI < cutSurf1.size()); // face from surf1
01037             }
01038         }
01039         else    // inside
01040         {
01041             if (booleanOp == booleanSurface::OR)
01042             {
01043                 include[faceI] = false;
01044             }
01045             else if (booleanOp == booleanSurface::AND)
01046             {
01047                 include[faceI] = true;
01048             }
01049             else    // xor
01050             {
01051                 include[faceI] = (faceI >= cutSurf1.size()); // face from surf2
01052             }
01053         }
01054     }
01055 
01056     // Create subsetted surface
01057     labelList subToCombinedPoint;
01058     labelList subToCombinedFace;
01059     triSurface subSurf
01060     (
01061         combinedSurf.subsetMesh
01062         (
01063             include,
01064             subToCombinedPoint,
01065             subToCombinedFace
01066         )
01067     );
01068 
01069     // Create face map
01070     faceMap_.setSize(subSurf.size());
01071 
01072     forAll(subToCombinedFace, faceI)
01073     {
01074         // Get label in combinedSurf
01075         label combinedFaceI = subToCombinedFace[faceI];
01076 
01077         // First faces in combinedSurf come from cutSurf1.
01078 
01079         if (combinedFaceI < cutSurf1.size())
01080         {
01081             label cutSurf1Face = combinedFaceI;
01082 
01083             faceMap_[faceI] = cutSurf1.faceMap()[cutSurf1Face];
01084         }
01085         else
01086         {
01087             label cutSurf2Face = combinedFaceI - cutSurf1.size();
01088 
01089             faceMap_[faceI] = - cutSurf2.faceMap()[cutSurf2Face] - 1;
01090         }
01091     }
01092 
01093     // Orient outwards
01094     orientedSurface outSurf(subSurf);
01095 
01096     // Assign.
01097     triSurface::operator=(outSurf);
01098 }
01099 
01100 
01101 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
01102 
01103 
01104 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd