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

faceCoupleInfo.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 "faceCoupleInfo.H"
00028 #include "polyMesh.H"
00029 #include "matchPoints.H"
00030 #include "indirectPrimitivePatch.H"
00031 #include "meshTools.H"
00032 #include "treeDataFace.H"
00033 #include "indexedOctree.H"
00034 #include "OFstream.H"
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 defineTypeNameAndDebug(Foam::faceCoupleInfo, 0);
00039 
00040 const Foam::scalar Foam::faceCoupleInfo::angleTol_ = 1E-3;
00041 
00042 
00043 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00044 
00045 //- Write edges
00046 void Foam::faceCoupleInfo::writeOBJ
00047 (
00048     const fileName& fName,
00049     const edgeList& edges,
00050     const pointField& points,
00051     const bool compact
00052 )
00053 {
00054     OFstream str(fName);
00055 
00056     labelList pointMap(points.size(), -1);
00057 
00058     if (compact)
00059     {
00060         label newPointI = 0;
00061 
00062         forAll(edges, edgeI)
00063         {
00064             const edge& e = edges[edgeI];
00065 
00066             forAll(e, eI)
00067             {
00068                 label pointI = e[eI];
00069 
00070                 if (pointMap[pointI] == -1)
00071                 {
00072                     pointMap[pointI] = newPointI++;
00073 
00074                     meshTools::writeOBJ(str, points[pointI]);
00075                 }
00076             }
00077         }
00078     }
00079     else
00080     {
00081         forAll(points, pointI)
00082         {
00083             meshTools::writeOBJ(str, points[pointI]);
00084         }
00085 
00086         pointMap = identity(points.size());
00087     }
00088 
00089     forAll(edges, edgeI)
00090     {
00091         const edge& e = edges[edgeI];
00092 
00093         str<< "l " << pointMap[e[0]]+1 << ' ' << pointMap[e[1]]+1 << nl;
00094     }
00095 }
00096 
00097 
00098 //- Writes edges.
00099 void Foam::faceCoupleInfo::writeOBJ
00100 (
00101     const fileName& fName,
00102     const pointField& points0,
00103     const pointField& points1
00104 )
00105 {
00106     Pout<< "Writing connections as edges to " << fName << endl;
00107 
00108     OFstream str(fName);
00109 
00110     label vertI = 0;
00111 
00112     forAll(points0, i)
00113     {
00114         meshTools::writeOBJ(str, points0[i]);
00115         vertI++;
00116         meshTools::writeOBJ(str, points1[i]);
00117         vertI++;
00118         str << "l " << vertI-1 << ' ' << vertI << nl;
00119     }
00120 }
00121 
00122 
00123 //- Writes face and point connectivity as .obj files.
00124 void Foam::faceCoupleInfo::writePointsFaces() const
00125 {
00126     const indirectPrimitivePatch& m = masterPatch();
00127     const indirectPrimitivePatch& s = slavePatch();
00128     const primitiveFacePatch& c = cutFaces();
00129 
00130     // Patches
00131     {
00132         OFstream str("masterPatch.obj");
00133         Pout<< "Writing masterPatch to " << str.name() << endl;
00134         meshTools::writeOBJ(str, m.localFaces(), m.localPoints());
00135     }
00136     {
00137         OFstream str("slavePatch.obj");
00138         Pout<< "Writing slavePatch to " << str.name() << endl;
00139         meshTools::writeOBJ(str, s.localFaces(), s.localPoints());
00140     }
00141     {
00142         OFstream str("cutFaces.obj");
00143         Pout<< "Writing cutFaces to " << str.name() << endl;
00144         meshTools::writeOBJ(str, c.localFaces(), c.localPoints());
00145     }
00146 
00147     // Point connectivity
00148     {
00149         Pout<< "Writing cutToMasterPoints to cutToMasterPoints.obj" << endl;
00150 
00151         writeOBJ
00152         (
00153             "cutToMasterPoints.obj",
00154             m.localPoints(),
00155             pointField(c.localPoints(), masterToCutPoints_));
00156     }
00157     {
00158         Pout<< "Writing cutToSlavePoints to cutToSlavePoints.obj" << endl;
00159 
00160         writeOBJ
00161         (
00162             "cutToSlavePoints.obj",
00163             s.localPoints(),
00164             pointField(c.localPoints(), slaveToCutPoints_)
00165         );
00166     }
00167 
00168     // Face connectivity
00169     {
00170         Pout<< "Writing cutToMasterFaces to cutToMasterFaces.obj" << endl;
00171 
00172         pointField equivMasterFaces(c.size());
00173 
00174         forAll(cutToMasterFaces(), cutFaceI)
00175         {
00176             label masterFaceI = cutToMasterFaces()[cutFaceI];
00177 
00178             if (masterFaceI != -1)
00179             {
00180                 equivMasterFaces[cutFaceI] = m[masterFaceI].centre(m.points());
00181             }
00182             else
00183             {
00184                 WarningIn("writePointsFaces()")
00185                     << "No master face for cut face " << cutFaceI
00186                     << " at position " << c[cutFaceI].centre(c.points())
00187                     << endl;
00188 
00189                 equivMasterFaces[cutFaceI] = vector::zero;
00190             }
00191         }
00192 
00193         writeOBJ
00194         (
00195             "cutToMasterFaces.obj",
00196             calcFaceCentres<List>(c, cutPoints(), 0, c.size()),
00197             equivMasterFaces
00198         );
00199     }
00200 
00201     {
00202         Pout<< "Writing cutToSlaveFaces to cutToSlaveFaces.obj" << endl;
00203 
00204         pointField equivSlaveFaces(c.size());
00205 
00206         forAll(cutToSlaveFaces(), cutFaceI)
00207         {
00208             label slaveFaceI = cutToSlaveFaces()[cutFaceI];
00209 
00210             equivSlaveFaces[cutFaceI] = s[slaveFaceI].centre(s.points());
00211         }
00212 
00213         writeOBJ
00214         (
00215             "cutToSlaveFaces.obj",
00216             calcFaceCentres<List>(c, cutPoints(), 0, c.size()),
00217             equivSlaveFaces
00218         );
00219     }
00220 
00221     Pout<< endl;
00222 }
00223 
00224 
00225 void Foam::faceCoupleInfo::writeEdges
00226 (
00227     const labelList& cutToMasterEdges,
00228     const labelList& cutToSlaveEdges
00229 ) const
00230 {
00231     const indirectPrimitivePatch& m = masterPatch();
00232     const indirectPrimitivePatch& s = slavePatch();
00233     const primitiveFacePatch& c = cutFaces();
00234 
00235     // Edge connectivity
00236     {
00237         OFstream str("cutToMasterEdges.obj");
00238         Pout<< "Writing cutToMasterEdges to " << str.name() << endl;
00239 
00240         label vertI = 0;
00241 
00242         forAll(cutToMasterEdges, cutEdgeI)
00243         {
00244             if (cutToMasterEdges[cutEdgeI] != -1)
00245             {
00246                 const edge& masterEdge =
00247                     m.edges()[cutToMasterEdges[cutEdgeI]];
00248                 const edge& cutEdge = c.edges()[cutEdgeI];
00249 
00250                 meshTools::writeOBJ(str, m.localPoints()[masterEdge[0]]);
00251                 vertI++;
00252                 meshTools::writeOBJ(str, m.localPoints()[masterEdge[1]]);
00253                 vertI++;
00254                 meshTools::writeOBJ(str, c.localPoints()[cutEdge[0]]);
00255                 vertI++;
00256                 meshTools::writeOBJ(str, c.localPoints()[cutEdge[1]]);
00257                 vertI++;
00258                 str << "l " << vertI-3 << ' ' << vertI-2 << nl;
00259                 str << "l " << vertI-3 << ' ' << vertI-1 << nl;
00260                 str << "l " << vertI-3 << ' ' << vertI << nl;
00261                 str << "l " << vertI-2 << ' ' << vertI-1 << nl;
00262                 str << "l " << vertI-2 << ' ' << vertI << nl;
00263                 str << "l " << vertI-1 << ' ' << vertI << nl;
00264             }
00265         }
00266     }
00267     {
00268         OFstream str("cutToSlaveEdges.obj");
00269         Pout<< "Writing cutToSlaveEdges to " << str.name() << endl;
00270 
00271         label vertI = 0;
00272 
00273         labelList slaveToCut(invert(s.nEdges(), cutToSlaveEdges));
00274 
00275         forAll(slaveToCut, edgeI)
00276         {
00277             if (slaveToCut[edgeI] != -1)
00278             {
00279                 const edge& slaveEdge = s.edges()[edgeI];
00280                 const edge& cutEdge = c.edges()[slaveToCut[edgeI]];
00281 
00282                 meshTools::writeOBJ(str, s.localPoints()[slaveEdge[0]]);
00283                 vertI++;
00284                 meshTools::writeOBJ(str, s.localPoints()[slaveEdge[1]]);
00285                 vertI++;
00286                 meshTools::writeOBJ(str, c.localPoints()[cutEdge[0]]);
00287                 vertI++;
00288                 meshTools::writeOBJ(str, c.localPoints()[cutEdge[1]]);
00289                 vertI++;
00290                 str << "l " << vertI-3 << ' ' << vertI-2 << nl;
00291                 str << "l " << vertI-3 << ' ' << vertI-1 << nl;
00292                 str << "l " << vertI-3 << ' ' << vertI << nl;
00293                 str << "l " << vertI-2 << ' ' << vertI-1 << nl;
00294                 str << "l " << vertI-2 << ' ' << vertI << nl;
00295                 str << "l " << vertI-1 << ' ' << vertI << nl;
00296             }
00297         }
00298     }
00299 
00300     Pout<< endl;
00301 }
00302 
00303 
00304 // Given an edgelist and a map for the points on the edges it tries to find
00305 // the corresponding patch edges.
00306 Foam::labelList Foam::faceCoupleInfo::findMappedEdges
00307 (
00308     const edgeList& edges,
00309     const labelList& pointMap,
00310     const indirectPrimitivePatch& patch
00311 )
00312 {
00313     labelList toPatchEdges(edges.size());
00314 
00315     forAll(toPatchEdges, edgeI)
00316     {
00317         const edge& e = edges[edgeI];
00318 
00319         label v0 = pointMap[e[0]];
00320         label v1 = pointMap[e[1]];
00321 
00322         toPatchEdges[edgeI] =
00323             meshTools::findEdge
00324             (
00325                 patch.edges(),
00326                 patch.pointEdges()[v0],
00327                 v0,
00328                 v1
00329             );
00330     }
00331     return toPatchEdges;
00332 }
00333 
00334 
00335 // Detect a cut edge which originates from two boundary faces having different
00336 // polyPatches.
00337 bool Foam::faceCoupleInfo::regionEdge
00338 (
00339     const polyMesh& slaveMesh,
00340     const label slaveEdgeI
00341 ) const
00342 {
00343     const labelList& eFaces = slavePatch().edgeFaces()[slaveEdgeI];
00344 
00345     if (eFaces.size() == 1)
00346     {
00347         return true;
00348     }
00349     else
00350     {
00351         // Count how many different patches connected to this edge.
00352 
00353         label patch0 = -1;
00354 
00355         forAll(eFaces, i)
00356         {
00357             label faceI = eFaces[i];
00358 
00359             label meshFaceI = slavePatch().addressing()[faceI];
00360 
00361             label patchI = slaveMesh.boundaryMesh().whichPatch(meshFaceI);
00362 
00363             if (patch0 == -1)
00364             {
00365                 patch0 = patchI;
00366             }
00367             else if (patchI != patch0)
00368             {
00369                 // Found two different patches connected to this edge.
00370                 return true;
00371             }
00372         }
00373         return false;
00374     }
00375 }
00376 
00377 
00378 // Find edge using pointI that is most aligned with vector between
00379 // master points. Patchdivision tells us whether or not to use
00380 // patch information to match edges.
00381 Foam::label Foam::faceCoupleInfo::mostAlignedCutEdge
00382 (
00383     const bool report,
00384     const polyMesh& slaveMesh,
00385     const bool patchDivision,
00386     const labelList& cutToMasterEdges,
00387     const labelList& cutToSlaveEdges,
00388     const label pointI,
00389     const label edgeStart,
00390     const label edgeEnd
00391 ) const
00392 {
00393     const pointField& localPoints = cutFaces().localPoints();
00394 
00395     const labelList& pEdges = cutFaces().pointEdges()[pointI];
00396 
00397     if (report)
00398     {
00399         Pout<< "mostAlignedEdge : finding nearest edge among "
00400             << UIndirectList<edge>(cutFaces().edges(), pEdges)()
00401             << " connected to point " << pointI
00402             << " coord:" << localPoints[pointI]
00403             << " running between " << edgeStart << " coord:"
00404             << localPoints[edgeStart]
00405             << " and " << edgeEnd << " coord:"
00406             << localPoints[edgeEnd]
00407             << endl;
00408     }
00409 
00410     // Find the edge that gets us nearest end.
00411 
00412     label maxEdgeI = -1;
00413     scalar maxCos = -GREAT;
00414 
00415     forAll(pEdges, i)
00416     {
00417         label edgeI = pEdges[i];
00418 
00419         if
00420         (
00421            !(
00422                 patchDivision
00423              && cutToMasterEdges[edgeI] == -1
00424             )
00425          || (
00426                 patchDivision
00427              && regionEdge(slaveMesh, cutToSlaveEdges[edgeI])
00428             )
00429         )
00430         {
00431             const edge& e = cutFaces().edges()[edgeI];
00432 
00433             label otherPointI = e.otherVertex(pointI);
00434 
00435             if (otherPointI == edgeEnd)
00436             {
00437                 // Shortcut: found edge end point.
00438                 if (report)
00439                 {
00440                     Pout<< "    mostAlignedEdge : found end point " << edgeEnd
00441                         << endl;
00442                 }
00443                 return edgeI;
00444             }
00445 
00446             // Get angle between edge and edge to masterEnd
00447 
00448             vector eVec(localPoints[otherPointI] - localPoints[pointI]);
00449 
00450             scalar magEVec = mag(eVec);
00451 
00452             if (magEVec < VSMALL)
00453             {
00454                 WarningIn("faceCoupleInfo::mostAlignedEdge")
00455                     << "Crossing zero sized edge " << edgeI
00456                     << " coords:" << localPoints[otherPointI]
00457                     << localPoints[pointI]
00458                     << " when walking from " << localPoints[edgeStart]
00459                     << " to " << localPoints[edgeEnd]
00460                     << endl;
00461                 return edgeI;
00462             }
00463 
00464             eVec /= magEVec;
00465 
00466             vector eToEndPoint(localPoints[edgeEnd] - localPoints[otherPointI]);
00467             eToEndPoint /= mag(eToEndPoint);
00468 
00469             scalar cosAngle = eVec & eToEndPoint;
00470 
00471             if (report)
00472             {
00473                 Pout<< "    edge:" << e << " points:" << localPoints[pointI]
00474                     << localPoints[otherPointI]
00475                     << "  vec:" << eVec
00476                     << "  vecToEnd:" << eToEndPoint
00477                     << " cosAngle:" << cosAngle
00478                     << endl;
00479             }
00480 
00481             if (cosAngle > maxCos)
00482             {
00483                 maxCos = cosAngle;
00484                 maxEdgeI = edgeI;
00485             }
00486         }
00487     }
00488 
00489     if (maxCos > 1 - angleTol_)
00490     {
00491         return maxEdgeI;
00492     }
00493     else
00494     {
00495         return -1;
00496     }
00497 }
00498 
00499 
00500 // Construct points to split points map (in cut addressing)
00501 void Foam::faceCoupleInfo::setCutEdgeToPoints(const labelList& cutToMasterEdges)
00502 {
00503     labelListList masterToCutEdges
00504     (
00505         invertOneToMany
00506         (
00507             masterPatch().nEdges(),
00508             cutToMasterEdges
00509         )
00510     );
00511 
00512     const edgeList& cutEdges = cutFaces().edges();
00513 
00514     // Size extra big so searching is faster
00515     cutEdgeToPoints_.resize
00516     (
00517         masterPatch().nEdges()
00518       + slavePatch().nEdges()
00519       + cutEdges.size()
00520     );
00521 
00522     forAll(masterToCutEdges, masterEdgeI)
00523     {
00524         const edge& masterE = masterPatch().edges()[masterEdgeI];
00525 
00526         //Pout<< "Master:" << masterPatch().localPoints()[masterE[0]] << ' '
00527         //    << masterPatch().localPoints()[masterE[1]] << endl;
00528 
00529         const labelList& stringedEdges = masterToCutEdges[masterEdgeI];
00530 
00531         if (stringedEdges.empty())
00532         {
00533             FatalErrorIn
00534             (
00535                 "faceCoupleInfo::setCutEdgeToPoints"
00536                 "(const labelList&)"
00537             )   << "Did not match all of master edges to cutFace edges"
00538                 << nl
00539                 << "First unmatched edge:" << masterEdgeI << " endPoints:"
00540                 << masterPatch().localPoints()[masterE[0]]
00541                 << masterPatch().localPoints()[masterE[1]]
00542                 << endl
00543                 << "This usually means that the slave patch is not a"
00544                 << " subdivision of the master patch"
00545                 << abort(FatalError);
00546         }
00547         else if (stringedEdges.size() > 1)
00548         {
00549             // String up the edges between e[0] and e[1]. Store the points
00550             // inbetween e[0] and e[1] (all in cutFaces() labels)
00551 
00552             DynamicList<label> splitPoints(stringedEdges.size()-1);
00553 
00554             // Unsplit edge endpoints
00555             const edge unsplitEdge
00556             (
00557                 masterToCutPoints_[masterE[0]],
00558                 masterToCutPoints_[masterE[1]]
00559             );
00560 
00561             label startVertI = unsplitEdge[0];
00562             label startEdgeI = -1;
00563 
00564             while (startVertI != unsplitEdge[1])
00565             {
00566                 // Loop over all string of edges. Update
00567                 // - startVertI : previous vertex
00568                 // - startEdgeI : previous edge
00569                 // and insert any points into splitPoints
00570 
00571                 // For checking
00572                 label oldStart = startVertI;
00573 
00574                 forAll(stringedEdges, i)
00575                 {
00576                     label edgeI = stringedEdges[i];
00577 
00578                     if (edgeI != startEdgeI)
00579                     {
00580                         const edge& e = cutEdges[edgeI];
00581 
00582                         //Pout<< "    cut:" << e << " at:"
00583                         //    << cutFaces().localPoints()[e[0]]
00584                         //    << ' ' << cutFaces().localPoints()[e[1]] << endl;
00585 
00586                         if (e[0] == startVertI)
00587                         {
00588                             startEdgeI = edgeI;
00589                             startVertI = e[1];
00590                             if (e[1] != unsplitEdge[1])
00591                             {
00592                                 splitPoints.append(e[1]);
00593                             }
00594                             break;
00595                         }
00596                         else if (e[1] == startVertI)
00597                         {
00598                             startEdgeI = edgeI;
00599                             startVertI = e[0];
00600                             if (e[0] != unsplitEdge[1])
00601                             {
00602                                 splitPoints.append(e[0]);
00603                             }
00604                             break;
00605                         }
00606                     }
00607                 }
00608 
00609                 // Check
00610                 if (oldStart == startVertI)
00611                 {
00612                     FatalErrorIn
00613                     (
00614                         "faceCoupleInfo::setCutEdgeToPoints"
00615                         "(const labelList&)"
00616                     )   << " unsplitEdge:" << unsplitEdge
00617                         << " does not correspond to split edges "
00618                         << UIndirectList<edge>(cutEdges, stringedEdges)()
00619                         << abort(FatalError);
00620                 }
00621             }
00622 
00623             //Pout<< "For master edge:"
00624             //    << unsplitEdge
00625             //    << " Found stringed points "
00626             //    <<  UIndirectList<point>
00627             //        (
00628             //            cutFaces().localPoints(),
00629             //            splitPoints.shrink()
00630             //        )()
00631             //    << endl;
00632 
00633             cutEdgeToPoints_.insert(unsplitEdge, splitPoints.shrink());
00634         }
00635     }
00636 }
00637 
00638 
00639 // Determines rotation for f1 to match up with f0, i.e. the index in f0 of
00640 // the first point of f1.
00641 Foam::label Foam::faceCoupleInfo::matchFaces
00642 (
00643     const scalar absTol,
00644     const pointField& points0,
00645     const face& f0,
00646     const pointField& points1,
00647     const face& f1,
00648     const bool sameOrientation
00649 )
00650 {
00651     if (f0.size() != f1.size())
00652     {
00653         FatalErrorIn
00654         (
00655             "faceCoupleInfo::matchFaces"
00656             "(const scalar, const face&, const pointField&"
00657             ", const face&, const pointField&)"
00658         )   << "Different sizes for supposedly matching faces." << nl
00659             << "f0:" << f0 << " coords:" << UIndirectList<point>(points0, f0)()
00660             << nl
00661             << "f1:" << f1 << " coords:" << UIndirectList<point>(points1, f1)()
00662             << abort(FatalError);
00663     }
00664 
00665     const scalar absTolSqr = sqr(absTol);
00666 
00667 
00668     label matchFp = -1;
00669 
00670     forAll(f0, startFp)
00671     {
00672         // See -if starting from startFp on f0- the two faces match.
00673         bool fullMatch = true;
00674 
00675         label fp0 = startFp;
00676         label fp1 = 0;
00677 
00678         forAll(f1, i)
00679         {
00680             scalar distSqr = Foam::magSqr(points0[f0[fp0]] - points1[f1[fp1]]);
00681 
00682             if (distSqr > absTolSqr)
00683             {
00684                 fullMatch = false;
00685                 break;
00686             }
00687 
00688             fp0 = f0.fcIndex(fp0);  // walk forward
00689 
00690             if (sameOrientation)
00691             {
00692                 fp1 = f1.fcIndex(fp1);
00693             }
00694             else
00695             {
00696                 fp1 = f1.rcIndex(fp1);
00697             }
00698         }
00699 
00700         if (fullMatch)
00701         {
00702             matchFp = startFp;
00703             break;
00704         }
00705     }
00706 
00707     if (matchFp == -1)
00708     {
00709         FatalErrorIn
00710         (
00711             "faceCoupleInfo::matchFaces"
00712             "(const scalar, const face&, const pointField&"
00713             ", const face&, const pointField&)"
00714         )   << "No unique match between two faces" << nl
00715             << "Face " << f0 << " coords "
00716             << UIndirectList<point>(points0, f0)() << nl
00717             << "Face " << f1 << " coords "
00718             << UIndirectList<point>(points1, f1)()
00719             << "when using tolerance " << absTol
00720             << " and forwardMatching:" << sameOrientation
00721             << abort(FatalError);
00722     }
00723 
00724     return matchFp;
00725 }
00726 
00727 
00728 // Find correspondence from patch points to cut points. This might
00729 // detect shared points so the output is a patch-to-cut point list
00730 // and a compaction list for the cut points (which will always be equal or more
00731 // connected than the patch).
00732 // Returns true if there are any duplicates.
00733 bool Foam::faceCoupleInfo::matchPointsThroughFaces
00734 (
00735     const scalar absTol,
00736     const pointField& cutPoints,
00737     const faceList& cutFaces,
00738     const pointField& patchPoints,
00739     const faceList& patchFaces,
00740     const bool sameOrientation,
00741 
00742     labelList& patchToCutPoints,    // patch to (uncompacted) cut points
00743     labelList& cutToCompact,        // compaction list for cut points
00744     labelList& compactToCut         // inverse ,,
00745 )
00746 {
00747 
00748     // From slave to cut point
00749     patchToCutPoints.setSize(patchPoints.size());
00750     patchToCutPoints = -1;
00751 
00752     // Compaction list for cut points: either -1 or index into master which
00753     // gives the point to compact to.
00754     labelList cutPointRegion(cutPoints.size(), -1);
00755     DynamicList<label> cutPointRegionMaster;
00756 
00757     forAll(patchFaces, patchFaceI)
00758     {
00759         const face& patchF = patchFaces[patchFaceI];
00760 
00761         //const face& cutF = cutFaces[patchToCutFaces[patchFaceI]];
00762         const face& cutF = cutFaces[patchFaceI];
00763 
00764         // Do geometric matching to get position of cutF[0] in patchF
00765         label patchFp = matchFaces
00766         (
00767             absTol,
00768             patchPoints,
00769             patchF,
00770             cutPoints,
00771             cutF,
00772             sameOrientation        // orientation
00773         );
00774 
00775         forAll(cutF, cutFp)
00776         {
00777             label cutPointI = cutF[cutFp];
00778             label patchPointI = patchF[patchFp];
00779 
00780             //const point& cutPt = cutPoints[cutPointI];
00781             //const point& patchPt = patchPoints[patchPointI];
00782             //if (mag(cutPt - patchPt) > SMALL)
00783             //{
00784             //    FatalErrorIn("matchPointsThroughFaces")
00785             //    << "cutP:" << cutPt
00786             //    << " patchP:" << patchPt
00787             //    << abort(FatalError);
00788             //}
00789 
00790             if (patchToCutPoints[patchPointI] == -1)
00791             {
00792                 patchToCutPoints[patchPointI] = cutPointI;
00793             }
00794             else if (patchToCutPoints[patchPointI] != cutPointI)
00795             {
00796                 // Multiple cut points connecting to same patch.
00797                 // Check if already have region & region master for this set
00798                 label otherCutPointI = patchToCutPoints[patchPointI];
00799 
00800                 //Pout<< "PatchPoint:" << patchPt
00801                 //    << " matches to:" << cutPointI
00802                 //    << " coord:" << cutPoints[cutPointI]
00803                 //    << " and to:" << otherCutPointI
00804                 //    << " coord:" << cutPoints[otherCutPointI]
00805                 //    << endl;
00806 
00807                 if (cutPointRegion[otherCutPointI] != -1)
00808                 {
00809                     // Have region for this set. Copy.
00810                     label region = cutPointRegion[otherCutPointI];
00811                     cutPointRegion[cutPointI] = region;
00812 
00813                     // Update region master with min point label
00814                     cutPointRegionMaster[region] = min
00815                     (
00816                         cutPointRegionMaster[region],
00817                         cutPointI
00818                     );
00819                 }
00820                 else
00821                 {
00822                     // Create new region.
00823                     label region = cutPointRegionMaster.size();
00824                     cutPointRegionMaster.append
00825                     (
00826                         min(cutPointI, otherCutPointI)
00827                     );
00828                     cutPointRegion[cutPointI] = region;
00829                     cutPointRegion[otherCutPointI] = region;
00830                 }
00831             }
00832 
00833             if (sameOrientation)
00834             {
00835                 patchFp = patchF.fcIndex(patchFp);
00836             }
00837             else
00838             {
00839                 patchFp = patchF.rcIndex(patchFp);
00840             }
00841         }
00842     }
00843 
00844     // Rework region&master into compaction array
00845     compactToCut.setSize(cutPointRegion.size());
00846     cutToCompact.setSize(cutPointRegion.size());
00847     cutToCompact = -1;
00848     label compactPointI = 0;
00849 
00850     forAll(cutPointRegion, i)
00851     {
00852         if (cutPointRegion[i] == -1)
00853         {
00854             // Unduplicated point. Allocate new compacted point.
00855             cutToCompact[i] = compactPointI;
00856             compactToCut[compactPointI] = i;
00857             compactPointI++;
00858         }
00859         else
00860         {
00861             // Duplicate point. Get master.
00862 
00863             label masterPointI = cutPointRegionMaster[cutPointRegion[i]];
00864 
00865             if (cutToCompact[masterPointI] == -1)
00866             {
00867                 cutToCompact[masterPointI] = compactPointI;
00868                 compactToCut[compactPointI] = masterPointI;
00869                 compactPointI++;
00870             }
00871             cutToCompact[i] = cutToCompact[masterPointI];
00872         }
00873     }
00874     compactToCut.setSize(compactPointI);
00875 
00876     return compactToCut.size() != cutToCompact.size();
00877 }
00878 
00879 
00880 // Return max distance from any point on cutF to masterF
00881 Foam::scalar Foam::faceCoupleInfo::maxDistance
00882 (
00883     const face& cutF,
00884     const pointField& cutPoints,
00885     const face& masterF,
00886     const pointField& masterPoints
00887 )
00888 {
00889     scalar maxDist = -GREAT;
00890 
00891     forAll(cutF, fp)
00892     {
00893         const point& cutPt = cutPoints[cutF[fp]];
00894 
00895         pointHit pHit = masterF.nearestPoint(cutPt, masterPoints);
00896 
00897         maxDist = max(maxDist, pHit.distance());
00898     }
00899     return maxDist;
00900 }
00901 
00902 
00903 void Foam::faceCoupleInfo::findPerfectMatchingFaces
00904 (
00905     const primitiveMesh& mesh0,
00906     const primitiveMesh& mesh1,
00907     const scalar absTol,
00908 
00909     labelList& mesh0Faces,
00910     labelList& mesh1Faces
00911 )
00912 {
00913     // Face centres of external faces (without invoking
00914     // mesh.faceCentres since mesh might have been clearedOut)
00915 
00916     pointField fc0
00917     (
00918         calcFaceCentres<List>
00919         (
00920             mesh0.faces(),
00921             mesh0.points(),
00922             mesh0.nInternalFaces(),
00923             mesh0.nFaces() - mesh0.nInternalFaces()
00924         )
00925     );
00926 
00927     pointField fc1
00928     (
00929         calcFaceCentres<List>
00930         (
00931             mesh1.faces(),
00932             mesh1.points(),
00933             mesh1.nInternalFaces(),
00934             mesh1.nFaces() - mesh1.nInternalFaces()
00935         )
00936     );
00937 
00938 
00939     if (debug)
00940     {
00941         Pout<< "Face matching tolerance : " << absTol << endl;
00942     }
00943 
00944 
00945     // Match geometrically
00946     labelList from1To0;
00947     bool matchedAllFaces = matchPoints
00948     (
00949         fc1,
00950         fc0,
00951         scalarField(fc1.size(), absTol),
00952         false,
00953         from1To0
00954     );
00955 
00956     if (matchedAllFaces)
00957     {
00958         Warning
00959             << "faceCoupleInfo::faceCoupleInfo : "
00960             << "Matched ALL " << fc1.size()
00961             << " boundary faces of mesh0 to boundary faces of mesh1." << endl
00962             << "This is only valid if the mesh to add is fully"
00963             << " enclosed by the mesh it is added to." << endl;
00964     }
00965 
00966 
00967     // Collect matches.
00968     label nMatched = 0;
00969 
00970     mesh0Faces.setSize(fc0.size());
00971     mesh1Faces.setSize(fc1.size());
00972 
00973     forAll(from1To0, i)
00974     {
00975         if (from1To0[i] != -1)
00976         {
00977             mesh1Faces[nMatched] = i + mesh1.nInternalFaces();
00978             mesh0Faces[nMatched] = from1To0[i] + mesh0.nInternalFaces();
00979 
00980             nMatched++;
00981         }
00982     }
00983 
00984     mesh0Faces.setSize(nMatched);
00985     mesh1Faces.setSize(nMatched);
00986 }
00987 
00988 
00989 void Foam::faceCoupleInfo::findSlavesCoveringMaster
00990 (
00991     const primitiveMesh& mesh0,
00992     const primitiveMesh& mesh1,
00993     const scalar absTol,
00994 
00995     labelList& mesh0Faces,
00996     labelList& mesh1Faces
00997 )
00998 {
00999     // Construct octree from all mesh0 boundary faces
01000     labelList bndFaces(mesh0.nFaces()-mesh0.nInternalFaces());
01001     forAll(bndFaces, i)
01002     {
01003         bndFaces[i] = mesh0.nInternalFaces() + i;
01004     }
01005 
01006     treeBoundBox overallBb(mesh0.points());
01007 
01008     Random rndGen(123456);
01009 
01010     indexedOctree<treeDataFace> tree
01011     (
01012         treeDataFace    // all information needed to search faces
01013         (
01014             false,                      // do not cache bb
01015             mesh0,
01016             bndFaces                    // boundary faces only
01017         ),
01018         overallBb.extend(rndGen, 1E-4), // overall search domain
01019         8,                              // maxLevel
01020         10,                             // leafsize
01021         3.0                             // duplicity
01022     );
01023 
01024     if (debug)
01025     {
01026         Pout<< "findSlavesCoveringMaster :"
01027             << " constructed octree for mesh0 boundary faces" << endl;
01028     }
01029 
01030     // Search nearest face for every mesh1 boundary face.
01031 
01032     labelHashSet mesh0Set(mesh0.nFaces() - mesh0.nInternalFaces());
01033     labelHashSet mesh1Set(mesh1.nFaces() - mesh1.nInternalFaces());
01034 
01035     for
01036     (
01037         label mesh1FaceI = mesh1.nInternalFaces();
01038         mesh1FaceI < mesh1.nFaces();
01039         mesh1FaceI++
01040     )
01041     {
01042         const face& f1 = mesh1.faces()[mesh1FaceI];
01043 
01044         // Generate face centre (prevent cellCentres() reconstruction)
01045         point fc(f1.centre(mesh1.points()));
01046 
01047         pointIndexHit nearInfo = tree.findNearest(fc, Foam::sqr(absTol));
01048 
01049         if (nearInfo.hit())
01050         {
01051             label mesh0FaceI = tree.shapes().faceLabels()[nearInfo.index()];
01052 
01053             // Check if points of f1 actually lie on top of mesh0 face
01054             // This is the bit that might fail since if f0 is severely warped
01055             // and f1's points are not present on f0 (since subdivision)
01056             // then the distance of the points to f0 might be quite large
01057             // and the test will fail. This all should in fact be some kind
01058             // of walk from known corresponding points/edges/faces.
01059             scalar dist =
01060                 maxDistance
01061                 (
01062                     f1,
01063                     mesh1.points(),
01064                     mesh0.faces()[mesh0FaceI],
01065                     mesh0.points()
01066                 );
01067 
01068             if (dist < absTol)
01069             {
01070                 mesh0Set.insert(mesh0FaceI);
01071                 mesh1Set.insert(mesh1FaceI);
01072             }
01073         }
01074     }
01075 
01076     if (debug)
01077     {
01078         Pout<< "findSlavesCoveringMaster :"
01079             << " matched " << mesh1Set.size() << " mesh1 faces to "
01080             << mesh0Set.size() << " mesh0 faces" << endl;
01081     }
01082 
01083     mesh0Faces = mesh0Set.toc();
01084     mesh1Faces = mesh1Set.toc();
01085 }
01086 
01087 
01088 // Grow cutToMasterFace across 'internal' edges.
01089 Foam::label Foam::faceCoupleInfo::growCutFaces
01090 (
01091     const labelList& cutToMasterEdges,
01092     Map<labelList>& candidates
01093 )
01094 {
01095     if (debug)
01096     {
01097         Pout<< "growCutFaces :"
01098             << " growing cut faces to masterPatch" << endl;
01099     }
01100 
01101     label nTotChanged = 0;
01102 
01103     while (true)
01104     {
01105         const labelListList& cutFaceEdges = cutFaces().faceEdges();
01106         const labelListList& cutEdgeFaces = cutFaces().edgeFaces();
01107 
01108         label nChanged = 0;
01109 
01110         forAll(cutToMasterFaces_, cutFaceI)
01111         {
01112             const label masterFaceI = cutToMasterFaces_[cutFaceI];
01113 
01114             if (masterFaceI != -1)
01115             {
01116                 // We now have a cutFace for which we have already found a
01117                 // master face. Grow this masterface across any internal edge
01118                 // (internal: no corresponding master edge)
01119 
01120                 const labelList& fEdges = cutFaceEdges[cutFaceI];
01121 
01122                 forAll(fEdges, i)
01123                 {
01124                     const label cutEdgeI = fEdges[i];
01125 
01126                     if (cutToMasterEdges[cutEdgeI] == -1)
01127                     {
01128                         // So this edge:
01129                         // - internal to the cutPatch (since all region edges
01130                         //   marked before)
01131                         // - on cutFaceI which corresponds to masterFace.
01132                         // Mark all connected faces with this masterFace.
01133 
01134                         const labelList& eFaces = cutEdgeFaces[cutEdgeI];
01135 
01136                         forAll(eFaces, j)
01137                         {
01138                             const label faceI = eFaces[j];
01139 
01140                             if (cutToMasterFaces_[faceI] == -1)
01141                             {
01142                                 cutToMasterFaces_[faceI] = masterFaceI;
01143                                 candidates.erase(faceI);
01144                                 nChanged++;
01145                             }
01146                             else if (cutToMasterFaces_[faceI] != masterFaceI)
01147                             {
01148                                 const pointField& cutPoints =
01149                                     cutFaces().points();
01150                                 const pointField& masterPoints =
01151                                     masterPatch().points();
01152 
01153                                 const edge& e = cutFaces().edges()[cutEdgeI];
01154 
01155                                 label myMaster = cutToMasterFaces_[faceI];
01156                                 const face& myF = masterPatch()[myMaster];
01157 
01158                                 const face& nbrF = masterPatch()[masterFaceI];
01159 
01160                                 FatalErrorIn
01161                                 (
01162                                     "faceCoupleInfo::growCutFaces"
01163                                     "(const labelList&, Map<labelList>&)"
01164                                 )   << "Cut face "
01165                                     << cutFaces()[faceI].points(cutPoints)
01166                                     << " has master "
01167                                     << myMaster
01168                                     << " but also connects to nbr face "
01169                                     << cutFaces()[cutFaceI].points(cutPoints)
01170                                     << " with master " << masterFaceI
01171                                     << nl
01172                                     << "myMasterFace:"
01173                                     << myF.points(masterPoints)
01174                                     << "  nbrMasterFace:"
01175                                     << nbrF.points(masterPoints) << nl
01176                                     << "Across cut edge " << cutEdgeI
01177                                     << " coord:"
01178                                     << cutFaces().localPoints()[e[0]]
01179                                     << cutFaces().localPoints()[e[1]]
01180                                     << abort(FatalError);
01181                             }
01182                         }
01183                     }
01184                 }
01185             }
01186         }
01187 
01188         if (debug)
01189         {
01190             Pout<< "growCutFaces : Grown an additional " << nChanged
01191                 << " cut to master face" << " correspondence" << endl;
01192         }
01193 
01194         nTotChanged += nChanged;
01195 
01196         if (nChanged == 0)
01197         {
01198             break;
01199         }
01200     }
01201 
01202     return nTotChanged;
01203 }
01204 
01205 
01206 void Foam::faceCoupleInfo::checkMatch(const labelList& cutToMasterEdges) const
01207 {
01208     const pointField& cutLocalPoints = cutFaces().localPoints();
01209 
01210     const pointField& masterLocalPoints = masterPatch().localPoints();
01211     const faceList& masterLocalFaces = masterPatch().localFaces();
01212 
01213     forAll(cutToMasterEdges, cutEdgeI)
01214     {
01215         const edge& e = cutFaces().edges()[cutEdgeI];
01216 
01217         if (cutToMasterEdges[cutEdgeI] == -1)
01218         {
01219             // Internal edge. Check that master face is same on both sides.
01220             const labelList& cutEFaces = cutFaces().edgeFaces()[cutEdgeI];
01221 
01222             label masterFaceI = -1;
01223 
01224             forAll(cutEFaces, i)
01225             {
01226                 label cutFaceI = cutEFaces[i];
01227 
01228                 if (cutToMasterFaces_[cutFaceI] != -1)
01229                 {
01230                     if (masterFaceI == -1)
01231                     {
01232                         masterFaceI = cutToMasterFaces_[cutFaceI];
01233                     }
01234                     else if (masterFaceI != cutToMasterFaces_[cutFaceI])
01235                     {
01236                         label myMaster = cutToMasterFaces_[cutFaceI];
01237                         const face& myF = masterLocalFaces[myMaster];
01238 
01239                         const face& nbrF = masterLocalFaces[masterFaceI];
01240 
01241                         FatalErrorIn
01242                         (
01243                             "faceCoupleInfo::checkMatch(const labelList&) const"
01244                         )
01245                             << "Internal CutEdge " << e
01246                             << " coord:"
01247                             << cutLocalPoints[e[0]]
01248                             << cutLocalPoints[e[1]]
01249                             << " connects to master " << myMaster
01250                             << " and to master " << masterFaceI << nl
01251                             << "myMasterFace:"
01252                             << myF.points(masterLocalPoints)
01253                             << "  nbrMasterFace:"
01254                             << nbrF.points(masterLocalPoints)
01255                             << abort(FatalError);
01256                     }
01257                 }
01258             }
01259         }
01260     }
01261 }
01262 
01263 
01264 // Extends matching information by elimination across cutFaces using more
01265 // than one region edge. Updates cutToMasterFaces_ and sets candidates
01266 // which is for every cutface on a region edge the possible master faces.
01267 Foam::label Foam::faceCoupleInfo::matchEdgeFaces
01268 (
01269     const labelList& cutToMasterEdges,
01270     Map<labelList>& candidates
01271 )
01272 {
01273     // For every unassigned cutFaceI the possible list of master faces.
01274     candidates.clear();
01275     candidates.resize(cutFaces().size());
01276 
01277     label nChanged = 0;
01278 
01279     forAll(cutToMasterEdges, cutEdgeI)
01280     {
01281         label masterEdgeI = cutToMasterEdges[cutEdgeI];
01282 
01283         if (masterEdgeI != -1)
01284         {
01285             // So cutEdgeI is matched to masterEdgeI. For all cut faces using
01286             // the cut edge store the master face as a candidate match.
01287 
01288             const labelList& cutEFaces = cutFaces().edgeFaces()[cutEdgeI];
01289             const labelList& masterEFaces =
01290                 masterPatch().edgeFaces()[masterEdgeI];
01291 
01292             forAll(cutEFaces, i)
01293             {
01294                 label cutFaceI = cutEFaces[i];
01295 
01296                 if (cutToMasterFaces_[cutFaceI] == -1)
01297                 {
01298                     // So this face (cutFaceI) is on an edge (cutEdgeI) that
01299                     // is used by some master faces (masterEFaces). Check if
01300                     // the cutFaceI and some of these masterEFaces share more
01301                     // than one edge (which uniquely defines face)
01302 
01303                     // Combine master faces with current set of candidate
01304                     // master faces.
01305                     Map<labelList>::iterator fnd = candidates.find(cutFaceI);
01306 
01307                     if (fnd == candidates.end())
01308                     {
01309                         // No info yet for cutFaceI. Add all master faces as
01310                         // candidates
01311                         candidates.insert(cutFaceI, masterEFaces);
01312                     }
01313                     else
01314                     {
01315                         // From some other cutEdgeI there are already some
01316                         // candidate master faces. Check the overlap with
01317                         // the current set of master faces.
01318                         const labelList& masterFaces = fnd();
01319 
01320                         DynamicList<label> newCandidates(masterFaces.size());
01321 
01322                         forAll(masterEFaces, j)
01323                         {
01324                             if (findIndex(masterFaces, masterEFaces[j]) != -1)
01325                             {
01326                                 newCandidates.append(masterEFaces[j]);
01327                             }
01328                         }
01329 
01330                         if (newCandidates.size() == 1)
01331                         {
01332                             // We found a perfect match. Delete entry from
01333                             // candidates map.
01334                             cutToMasterFaces_[cutFaceI] = newCandidates[0];
01335                             candidates.erase(cutFaceI);
01336                             nChanged++;
01337                         }
01338                         else
01339                         {
01340                             // Should not happen?
01341                             //Pout<< "On edge:" << cutEdgeI
01342                             //    << " have connected masterFaces:"
01343                             //    << masterEFaces
01344                             //    << " and from previous edge we have"
01345                             //    << " connected masterFaces" << masterFaces
01346                             //    << " . Overlap:" << newCandidates << endl;
01347 
01348                             fnd() = newCandidates.shrink();
01349                         }
01350                     }
01351                 }
01352 
01353             }
01354         }
01355     }
01356 
01357     if (debug)
01358     {
01359         Pout<< "matchEdgeFaces : Found " << nChanged
01360             << " faces where there was"
01361             << " only one remaining choice for cut-master correspondence"
01362             << endl;
01363     }
01364 
01365     return nChanged;
01366 }
01367 
01368 
01369 // Gets a list of cutFaces (that use a master edge) and the candidate
01370 // master faces.
01371 // Finds most aligned master face.
01372 Foam::label Foam::faceCoupleInfo::geometricMatchEdgeFaces
01373 (
01374     Map<labelList>& candidates
01375 )
01376 {
01377     const pointField& cutPoints = cutFaces().points();
01378 
01379     label nChanged = 0;
01380 
01381     // Mark all master faces that have been matched so far.
01382 
01383     labelListList masterToCutFaces
01384     (
01385         invertOneToMany
01386         (
01387             masterPatch().size(),
01388             cutToMasterFaces_
01389         )
01390     );
01391 
01392     forAllConstIter(Map<labelList>, candidates, iter)
01393     {
01394         label cutFaceI = iter.key();
01395 
01396         const face& cutF = cutFaces()[cutFaceI];
01397 
01398         if (cutToMasterFaces_[cutFaceI] == -1)
01399         {
01400             const labelList& masterFaces = iter();
01401 
01402             // Find the best matching master face.
01403             scalar minDist = GREAT;
01404             label minMasterFaceI = -1;
01405 
01406             forAll(masterFaces, i)
01407             {
01408                 label masterFaceI = masterFaces[i];
01409 
01410                 if (masterToCutFaces[masterFaces[i]].empty())
01411                 {
01412                     scalar dist = maxDistance
01413                     (
01414                         cutF,
01415                         cutPoints,
01416                         masterPatch()[masterFaceI],
01417                         masterPatch().points()
01418                     );
01419 
01420                     if (dist < minDist)
01421                     {
01422                         minDist = dist;
01423                         minMasterFaceI = masterFaceI;
01424                     }
01425                 }
01426             }
01427 
01428             if (minMasterFaceI != -1)
01429             {
01430                 cutToMasterFaces_[cutFaceI] = minMasterFaceI;
01431                 masterToCutFaces[minMasterFaceI] = cutFaceI;
01432                 nChanged++;
01433             }
01434         }
01435     }
01436 
01437     // (inefficiently) force candidates to be uptodate.
01438     forAll(cutToMasterFaces_, cutFaceI)
01439     {
01440         if (cutToMasterFaces_[cutFaceI] != -1)
01441         {
01442             candidates.erase(cutFaceI);
01443         }
01444     }
01445 
01446 
01447     if (debug)
01448     {
01449         Pout<< "geometricMatchEdgeFaces : Found " << nChanged
01450             << " faces where there was"
01451             << " only one remaining choice for cut-master correspondence"
01452             << endl;
01453     }
01454 
01455     return nChanged;
01456 }
01457 
01458 
01459 // Calculate the set of cut faces inbetween master and slave patch
01460 // assuming perfect match (and optional face ordering on slave)
01461 void Foam::faceCoupleInfo::perfectPointMatch
01462 (
01463     const scalar absTol,
01464     const bool slaveFacesOrdered
01465 )
01466 {
01467     if (debug)
01468     {
01469         Pout<< "perfectPointMatch :"
01470             << " Matching master and slave to cut."
01471             << " Master and slave faces are identical" << nl;
01472 
01473         if (slaveFacesOrdered)
01474         {
01475             Pout<< "and master and slave faces are ordered"
01476                 << " (on coupled patches)" << endl;
01477         }
01478         else
01479         {
01480             Pout<< "and master and slave faces are not ordered"
01481                 << " (on coupled patches)" << endl;
01482         }
01483     }
01484 
01485     cutToMasterFaces_ = identity(masterPatch().size());
01486     cutPoints_ = masterPatch().localPoints();
01487     cutFacesPtr_.reset
01488     (
01489         new primitiveFacePatch
01490         (
01491             masterPatch().localFaces(),
01492             cutPoints_
01493         )
01494     );
01495     masterToCutPoints_ = identity(cutPoints_.size());
01496 
01497 
01498     // Cut faces to slave patch.
01499     bool matchedAllFaces = false;
01500 
01501     if (slaveFacesOrdered)
01502     {
01503         cutToSlaveFaces_ = identity(cutFaces().size());
01504         matchedAllFaces = (cutFaces().size() == slavePatch().size());
01505     }
01506     else
01507     {
01508         // Faces do not have to be ordered (but all have
01509         // to match). Note: Faces will be already ordered if we enter here from
01510         // construct from meshes.
01511         matchedAllFaces = matchPoints
01512         (
01513             calcFaceCentres<List>
01514             (
01515                 cutFaces(),
01516                 cutPoints_,
01517                 0,
01518                 cutFaces().size()
01519             ),
01520             calcFaceCentres<IndirectList>
01521             (
01522                 slavePatch(),
01523                 slavePatch().points(),
01524                 0,
01525                 slavePatch().size()
01526             ),
01527             scalarField(slavePatch().size(), absTol),
01528             true,
01529             cutToSlaveFaces_
01530         );
01531     }
01532 
01533 
01534     if (!matchedAllFaces)
01535     {
01536         FatalErrorIn
01537         (
01538             "faceCoupleInfo::perfectPointMatch"
01539             "(const scalar&, const bool)"
01540         )   << "Did not match all of the master faces to the slave faces"
01541             << endl
01542             << "This usually means that the slave patch and master patch"
01543             << " do not align to within " << absTol << " meter."
01544             << abort(FatalError);
01545     }
01546 
01547 
01548     // Find correspondence from slave points to cut points. This might
01549     // detect shared points so the output is a slave-to-cut point list
01550     // and a compaction list.
01551 
01552     labelList cutToCompact, compactToCut;
01553     matchPointsThroughFaces
01554     (
01555         absTol,
01556         cutFaces().localPoints(),
01557         reorder(cutToSlaveFaces_, cutFaces().localFaces()),
01558         slavePatch().localPoints(),
01559         slavePatch().localFaces(),
01560         false,                      // slave and cut have opposite orientation
01561 
01562         slaveToCutPoints_,          // slave to (uncompacted) cut points
01563         cutToCompact,               // compaction map: from cut to compacted
01564         compactToCut                // compaction map: from compacted to cut
01565     );
01566 
01567 
01568     // Use compaction lists to renumber cutPoints.
01569     cutPoints_ = UIndirectList<point>(cutPoints_, compactToCut)();
01570     {
01571         const faceList& cutLocalFaces = cutFaces().localFaces();
01572 
01573         faceList compactFaces(cutLocalFaces.size());
01574         forAll(cutLocalFaces, i)
01575         {
01576             compactFaces[i] = renumber(cutToCompact, cutLocalFaces[i]);
01577         }
01578         cutFacesPtr_.reset
01579         (
01580             new primitiveFacePatch
01581             (
01582                 compactFaces,
01583                 cutPoints_
01584             )
01585         );
01586     }
01587     inplaceRenumber(cutToCompact, slaveToCutPoints_);
01588     inplaceRenumber(cutToCompact, masterToCutPoints_);
01589 }
01590 
01591 
01592 // Calculate the set of cut faces inbetween master and slave patch
01593 // assuming that slave patch is subdivision of masterPatch.
01594 void Foam::faceCoupleInfo::subDivisionMatch
01595 (
01596     const polyMesh& slaveMesh,
01597     const bool patchDivision,
01598     const scalar absTol
01599 )
01600 {
01601     if (debug)
01602     {
01603         Pout<< "subDivisionMatch :"
01604             << " Matching master and slave to cut."
01605             << " Slave can be subdivision of master but all master edges"
01606             << " have to be covered by slave edges." << endl;
01607     }
01608 
01609     // Assume slave patch is subdivision of the master patch so cutFaces is a
01610     // copy of the slave (but reversed since orientation has to be like master).
01611     cutPoints_ = slavePatch().localPoints();
01612     {
01613         faceList cutFaces(slavePatch().size());
01614 
01615         forAll(cutFaces, i)
01616         {
01617             cutFaces[i] = slavePatch().localFaces()[i].reverseFace();
01618         }
01619         cutFacesPtr_.reset(new primitiveFacePatch(cutFaces, cutPoints_));
01620     }
01621 
01622     // Cut is copy of slave so addressing to slave is trivial.
01623     cutToSlaveFaces_ = identity(cutFaces().size());
01624     slaveToCutPoints_ = identity(slavePatch().nPoints());
01625 
01626     // Cut edges to slave patch
01627     labelList cutToSlaveEdges
01628     (
01629         findMappedEdges
01630         (
01631             cutFaces().edges(),
01632             slaveToCutPoints_,  //note:should be cutToSlavePoints but since iden
01633             slavePatch()
01634         )
01635     );
01636 
01637 
01638     if (debug)
01639     {
01640         OFstream str("regionEdges.obj");
01641 
01642         Pout<< "subDivisionMatch :"
01643             << " addressing for slave patch fully done."
01644             << " Dumping region edges to " << str.name() << endl;
01645 
01646         label vertI = 0;
01647 
01648         forAll(slavePatch().edges(), slaveEdgeI)
01649         {
01650             if (regionEdge(slaveMesh, slaveEdgeI))
01651             {
01652                 const edge& e = slavePatch().edges()[slaveEdgeI];
01653 
01654                 meshTools::writeOBJ(str, slavePatch().localPoints()[e[0]]);
01655                 vertI++;
01656                 meshTools::writeOBJ(str, slavePatch().localPoints()[e[1]]);
01657                 vertI++;
01658                 str<< "l " << vertI-1 << ' ' << vertI << nl;
01659             }
01660         }
01661     }
01662 
01663 
01664     // Addressing from cut to master.
01665 
01666     // Cut points to master patch
01667     // - determine master points to cut points. Has to be full!
01668     // - invert to get cut to master
01669     if (debug)
01670     {
01671         Pout<< "subDivisionMatch :"
01672             << " matching master points to cut points" << endl;
01673     }
01674 
01675     bool matchedAllPoints = matchPoints
01676     (
01677         masterPatch().localPoints(),
01678         cutPoints_,
01679         scalarField(masterPatch().nPoints(), absTol),
01680         true,
01681         masterToCutPoints_
01682     );
01683 
01684     if (!matchedAllPoints)
01685     {
01686         FatalErrorIn
01687         (
01688             "faceCoupleInfo::subDivisionMatch"
01689             "(const polyMesh&, const bool, const scalar)"
01690         )   << "Did not match all of the master points to the slave points"
01691             << endl
01692             << "This usually means that the slave patch is not a"
01693             << " subdivision of the master patch"
01694             << abort(FatalError);
01695     }
01696 
01697 
01698     // Do masterEdges to cutEdges :
01699     // - mark all edges between two masterEdge endpoints. (geometric test since
01700     //   this is the only distinction)
01701     // - this gives the borders inbetween which all cutfaces come from
01702     //   a single master face.
01703     if (debug)
01704     {
01705         Pout<< "subDivisionMatch :"
01706             << " matching cut edges to master edges" << endl;
01707     }
01708 
01709     const edgeList& masterEdges = masterPatch().edges();
01710     const pointField& masterPoints = masterPatch().localPoints();
01711 
01712     const edgeList& cutEdges = cutFaces().edges();
01713 
01714     labelList cutToMasterEdges(cutFaces().nEdges(), -1);
01715 
01716     forAll(masterEdges, masterEdgeI)
01717     {
01718         const edge& masterEdge = masterEdges[masterEdgeI];
01719 
01720         label cutPoint0 = masterToCutPoints_[masterEdge[0]];
01721         label cutPoint1 = masterToCutPoints_[masterEdge[1]];
01722 
01723         // Find edges between cutPoint0 and cutPoint1.
01724 
01725         label cutPointI = cutPoint0;
01726         do
01727         {
01728             // Find edge (starting at pointI on cut), aligned with master
01729             // edge.
01730             label cutEdgeI =
01731                 mostAlignedCutEdge
01732                 (
01733                     false,
01734                     slaveMesh,
01735                     patchDivision,
01736                     cutToMasterEdges,
01737                     cutToSlaveEdges,
01738                     cutPointI,
01739                     cutPoint0,
01740                     cutPoint1
01741                 );
01742 
01743             if (cutEdgeI == -1)
01744             {
01745                 // Problem. Report while matching to produce nice error message
01746                 mostAlignedCutEdge
01747                 (
01748                     true,
01749                     slaveMesh,
01750                     patchDivision,
01751                     cutToMasterEdges,
01752                     cutToSlaveEdges,
01753                     cutPointI,
01754                     cutPoint0,
01755                     cutPoint1
01756                 );
01757 
01758                 Pout<< "Dumping unmatched pointEdges to errorEdges.obj"
01759                     << endl;
01760 
01761                 writeOBJ
01762                 (
01763                     "errorEdges.obj",
01764                     UIndirectList<edge>
01765                     (
01766                         cutFaces().edges(),
01767                         cutFaces().pointEdges()[cutPointI]
01768                     ),
01769                     cutFaces().localPoints(),
01770                     false
01771                 );
01772 
01773                 FatalErrorIn
01774                 (
01775                     "faceCoupleInfo::subDivisionMatch"
01776                     "(const polyMesh&, const bool, const scalar)"
01777                 )   << "Problem in finding cut edges corresponding to"
01778                     << " master edge " << masterEdge
01779                     << " points:" << masterPoints[masterEdge[0]]
01780                     << ' ' << masterPoints[masterEdge[1]]
01781                     << " corresponding cut edge: (" << cutPoint0
01782                     << ' ' << cutPoint1
01783                     << abort(FatalError);
01784             }
01785 
01786             cutToMasterEdges[cutEdgeI] = masterEdgeI;
01787 
01788             cutPointI = cutEdges[cutEdgeI].otherVertex(cutPointI);
01789 
01790         } while(cutPointI != cutPoint1);
01791     }
01792 
01793     if (debug)
01794     {
01795         // Write all matched edges.
01796         writeEdges(cutToMasterEdges, cutToSlaveEdges);
01797     }
01798 
01799     // Rework cutToMasterEdges into list of points inbetween two endpoints
01800     // (cutEdgeToPoints_)
01801     setCutEdgeToPoints(cutToMasterEdges);
01802 
01803 
01804     // Now that we have marked the cut edges that lie on top of master edges
01805     // we can find cut faces that have two (or more) of these edges.
01806     // Note that there can be multiple faces having two or more matched edges
01807     // since the cut faces can form a non-manifold surface(?)
01808     // So the matching is done as an elimination process where for every
01809     // cutFace on a matched edge we store the possible master faces and
01810     // eliminate more and more until we only have one possible master face
01811     // left.
01812 
01813     if (debug)
01814     {
01815         Pout<< "subDivisionMatch :"
01816             << " matching (topological) cut faces to masterPatch" << endl;
01817     }
01818 
01819     // For every unassigned cutFaceI the possible list of master faces.
01820     Map<labelList> candidates(cutFaces().size());
01821 
01822     cutToMasterFaces_.setSize(cutFaces().size());
01823     cutToMasterFaces_ = -1;
01824 
01825     while (true)
01826     {
01827         // See if there are any edges left where there is only one remaining
01828         // candidate.
01829         label nChanged = matchEdgeFaces(cutToMasterEdges, candidates);
01830 
01831         checkMatch(cutToMasterEdges);
01832 
01833 
01834         // Now we should have found a face correspondence for every cutFace
01835         // that uses two or more cutEdges. Floodfill from these across
01836         // 'internal' cutedges (i.e. edges that do not have a master
01837         // equivalent) to determine some more cutToMasterFaces
01838         nChanged += growCutFaces(cutToMasterEdges, candidates);
01839 
01840         checkMatch(cutToMasterEdges);
01841 
01842         if (nChanged == 0)
01843         {
01844             break;
01845         }
01846     }
01847 
01848     if (debug)
01849     {
01850         Pout<< "subDivisionMatch :"
01851             << " matching (geometric) cut faces to masterPatch" << endl;
01852     }
01853 
01854     // Above should have matched all cases fully. Cannot prove this yet so
01855     // do any remaining unmatched edges by a geometric test.
01856     while (true)
01857     {
01858         // See if there are any edges left where there is only one remaining
01859         // candidate.
01860         label nChanged = geometricMatchEdgeFaces(candidates);
01861 
01862         checkMatch(cutToMasterEdges);
01863 
01864         nChanged += growCutFaces(cutToMasterEdges, candidates);
01865 
01866         checkMatch(cutToMasterEdges);
01867 
01868         if (nChanged == 0)
01869         {
01870             break;
01871         }
01872     }
01873 
01874 
01875     // All cut faces matched?
01876     forAll(cutToMasterFaces_, cutFaceI)
01877     {
01878         if (cutToMasterFaces_[cutFaceI] == -1)
01879         {
01880             const face& cutF = cutFaces()[cutFaceI];
01881 
01882             FatalErrorIn
01883             (
01884                 "faceCoupleInfo::subDivisionMatch"
01885                 "(const polyMesh&, const bool, const scalar)"
01886             )   << "Did not match all of cutFaces to a master face" << nl
01887                 << "First unmatched cut face:" << cutFaceI << " with points:"
01888                 << UIndirectList<point>(cutFaces().points(), cutF)()
01889                 << nl
01890                 << "This usually means that the slave patch is not a"
01891                 << " subdivision of the master patch"
01892                 << abort(FatalError);
01893         }
01894     }
01895 
01896     if (debug)
01897     {
01898         Pout<< "subDivisionMatch :"
01899             << " finished matching master and slave to cut" << endl;
01900     }
01901 
01902     if (debug)
01903     {
01904         writePointsFaces();
01905     }
01906 }
01907 
01908 
01909 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
01910 
01911 // Construct from mesh data
01912 Foam::faceCoupleInfo::faceCoupleInfo
01913 (
01914     const polyMesh& masterMesh,
01915     const polyMesh& slaveMesh,
01916     const scalar absTol,
01917     const bool perfectMatch
01918 )
01919 :
01920     masterPatchPtr_(NULL),
01921     slavePatchPtr_(NULL),
01922     cutPoints_(0),
01923     cutFacesPtr_(NULL),
01924     cutToMasterFaces_(0),
01925     masterToCutPoints_(0),
01926     cutToSlaveFaces_(0),
01927     slaveToCutPoints_(0),
01928     cutEdgeToPoints_(0)
01929 {
01930     // Get faces on both meshes that are aligned.
01931     // (not ordered i.e. masterToMesh[0] does
01932     // not couple to slaveToMesh[0]. This ordering is done later on)
01933     labelList masterToMesh;
01934     labelList slaveToMesh;
01935 
01936     if (perfectMatch)
01937     {
01938         // Identical faces so use tight face-centre comparison.
01939         findPerfectMatchingFaces
01940         (
01941             masterMesh,
01942             slaveMesh,
01943             absTol,
01944             masterToMesh,
01945             slaveToMesh
01946         );
01947     }
01948     else
01949     {
01950         // Slave subdivision of master so use 'nearest'. Bit dodgy, especially
01951         // with using absTol (which is quite small)
01952         findSlavesCoveringMaster
01953         (
01954             masterMesh,
01955             slaveMesh,
01956             absTol,
01957             masterToMesh,
01958             slaveToMesh
01959         );
01960     }
01961 
01962     // Construct addressing engines for both sides
01963     masterPatchPtr_.reset
01964     (
01965         new indirectPrimitivePatch
01966         (
01967             IndirectList<face>(masterMesh.faces(), masterToMesh),
01968             masterMesh.points()
01969         )
01970     );
01971 
01972     slavePatchPtr_.reset
01973     (
01974         new indirectPrimitivePatch
01975         (
01976             IndirectList<face>(slaveMesh.faces(), slaveToMesh),
01977             slaveMesh.points()
01978         )
01979     );
01980 
01981 
01982     if (perfectMatch)
01983     {
01984         // Faces are perfectly aligned but probably not ordered.
01985         perfectPointMatch(absTol, false);
01986     }
01987     else
01988     {
01989         // Slave faces are subdivision of master face. Faces not ordered.
01990         subDivisionMatch(slaveMesh, false, absTol);
01991     }
01992 
01993     if (debug)
01994     {
01995         writePointsFaces();
01996     }
01997 }
01998 
01999 
02000 // Slave is subdivision of master patch.
02001 // (so -both cover the same area -all of master points are present in slave)
02002 Foam::faceCoupleInfo::faceCoupleInfo
02003 (
02004     const polyMesh& masterMesh,
02005     const labelList& masterAddressing,
02006     const polyMesh& slaveMesh,
02007     const labelList& slaveAddressing,
02008     const scalar absTol,
02009     const bool perfectMatch,
02010     const bool orderedFaces,
02011     const bool patchDivision
02012 )
02013 :
02014     masterPatchPtr_
02015     (
02016         new indirectPrimitivePatch
02017         (
02018             IndirectList<face>(masterMesh.faces(), masterAddressing),
02019             masterMesh.points()
02020         )
02021     ),
02022     slavePatchPtr_
02023     (
02024         new indirectPrimitivePatch
02025         (
02026             IndirectList<face>(slaveMesh.faces(), slaveAddressing),
02027             slaveMesh.points()
02028         )
02029     ),
02030     cutPoints_(0),
02031     cutFacesPtr_(NULL),
02032     cutToMasterFaces_(0),
02033     masterToCutPoints_(0),
02034     cutToSlaveFaces_(0),
02035     slaveToCutPoints_(0),
02036     cutEdgeToPoints_(0)
02037 {
02038     if (perfectMatch && (masterAddressing.size() != slaveAddressing.size()))
02039     {
02040         FatalErrorIn
02041         (
02042             "faceCoupleInfo::faceCoupleInfo(const primitiveMesh&"
02043             ", const primitiveMesh&, const scalar, const bool"
02044         )   << "Perfect match specified but number of master and slave faces"
02045             << " differ." << endl
02046             << "master:" << masterAddressing.size()
02047             << "  slave:" << slaveAddressing.size()
02048             << abort(FatalError);
02049     }
02050 
02051     if
02052     (
02053         masterAddressing.size()
02054      && min(masterAddressing) < masterMesh.nInternalFaces()
02055     )
02056     {
02057         FatalErrorIn
02058         (
02059             "faceCoupleInfo::faceCoupleInfo(const primitiveMesh&"
02060             ", const primitiveMesh&, const scalar, const bool"
02061         )   << "Supplied internal face on master mesh to couple." << nl
02062             << "Faces to be coupled have to be boundary faces."
02063             << abort(FatalError);
02064     }
02065     if
02066     (
02067         slaveAddressing.size()
02068      && min(slaveAddressing) < slaveMesh.nInternalFaces()
02069     )
02070     {
02071         FatalErrorIn
02072         (
02073             "faceCoupleInfo::faceCoupleInfo(const primitiveMesh&"
02074             ", const primitiveMesh&, const scalar, const bool"
02075         )   << "Supplied internal face on slave mesh to couple." << nl
02076             << "Faces to be coupled have to be boundary faces."
02077             << abort(FatalError);
02078     }
02079 
02080 
02081     if (perfectMatch)
02082     {
02083         perfectPointMatch(absTol, orderedFaces);
02084     }
02085     else
02086     {
02087         // Slave faces are subdivision of master face. Faces not ordered.
02088         subDivisionMatch(slaveMesh, patchDivision, absTol);
02089     }
02090 
02091     if (debug)
02092     {
02093         writePointsFaces();
02094     }
02095 }
02096 
02097 
02098 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
02099 
02100 Foam::faceCoupleInfo::~faceCoupleInfo()
02101 {}
02102 
02103 
02104 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
02105 
02106 Foam::labelList Foam::faceCoupleInfo::faceLabels(const polyPatch& pp)
02107 {
02108     labelList faces(pp.size());
02109 
02110     label faceI = pp.start();
02111 
02112     forAll(pp, i)
02113     {
02114         faces[i] = faceI++;
02115     }
02116     return faces;
02117 }
02118 
02119 
02120 Foam::Map<Foam::label> Foam::faceCoupleInfo::makeMap(const labelList& lst)
02121 {
02122     Map<label> map(lst.size());
02123 
02124     forAll(lst, i)
02125     {
02126         if (lst[i] != -1)
02127         {
02128             map.insert(i, lst[i]);
02129         }
02130     }
02131     return map;
02132 }
02133 
02134 
02135 Foam::Map<Foam::labelList> Foam::faceCoupleInfo::makeMap
02136 (
02137     const labelListList& lst
02138 )
02139 {
02140     Map<labelList> map(lst.size());
02141 
02142     forAll(lst, i)
02143     {
02144         if (lst[i].size())
02145         {
02146             map.insert(i, lst[i]);
02147         }
02148     }
02149     return map;
02150 }
02151 
02152 
02153 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd