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

surfaceIntersection.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 "surfaceIntersection.H"
00030 #include "triSurfaceSearch.H"
00031 #include "labelPairLookup.H"
00032 #include "OFstream.H"
00033 #include "HashSet.H"
00034 #include "triSurface.H"
00035 #include "pointIndexHit.H"
00036 #include "octreeDataTriSurface.H"
00037 #include "octree.H"
00038 #include "mergePoints.H"
00039 #include "plane.H"
00040 #include "edgeIntersections.H"
00041 
00042 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00043 
00044 defineTypeNameAndDebug(Foam::surfaceIntersection, 0);
00045 
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 // Checks if there exists a special topological situation that causes
00050 // edge and the face it hit not to be recognized.
00051 //
00052 // For now if the face shares a point with the edge
00053 bool Foam::surfaceIntersection::excludeEdgeHit
00054 (
00055     const triSurface& surf,
00056     const label edgeI,
00057     const label faceI,
00058     const scalar
00059 )
00060 {
00061     const labelledTri& f = surf.localFaces()[faceI];
00062 
00063     const edge& e = surf.edges()[edgeI];
00064 
00065     if
00066     (
00067         (f[0] == e.start())
00068      || (f[0] == e.end())
00069      || (f[1] == e.start())
00070      || (f[1] == e.end())
00071      || (f[2] == e.start())
00072      || (f[2] == e.end())
00073     )
00074     {
00075         return true;
00076 
00077 //        // Get edge vector
00078 //        vector eVec = e.vec(surf.localPoints());
00079 //        eVec /= mag(eVec) + VSMALL;
00080 //
00081 //        const labelList& eLabels = surf.faceEdges()[faceI];
00082 //
00083 //        // Get edge vector of 0th edge of face
00084 //        vector e0Vec = surf.edges()[eLabels[0]].vec(surf.localPoints());
00085 //        e0Vec /= mag(e0Vec) + VSMALL;
00086 //
00087 //        vector n = e0Vec ^ eVec;
00088 //
00089 //        if (mag(n) < SMALL)
00090 //        {
00091 //            // e0 is aligned with e. Choose next edge of face.
00092 //            vector e1Vec = surf.edges()[eLabels[1]].vec(surf.localPoints());
00093 //            e1Vec /= mag(e1Vec) + VSMALL;
00094 //
00095 //            n = e1Vec ^ eVec;
00096 //
00097 //            if (mag(n) < SMALL)
00098 //            {
00099 //                // Problematic triangle. Two edges aligned with edgeI. Give
00100 //                // up.
00101 //                return true;
00102 //            }
00103 //        }
00104 //
00105 //        // Check if same as faceNormal
00106 //        if (mag(n & surf.faceNormals()[faceI]) > 1-tol)
00107 //        {
00108 //
00109 //            Pout<< "edge:" << e << "  face:" << faceI
00110 //                << "  e0Vec:" << e0Vec << "  n:" << n
00111 //                << "  normalComponent:" << (n & surf.faceNormals()[faceI])
00112 //                << "  tol:" << tol << endl;
00113 //
00114 //            return true;
00115 //        }
00116 //        else
00117 //        {
00118 //            return false;
00119 //        }
00120     }
00121     else
00122     {
00123         return false;
00124     }
00125 }
00126 
00127 
00131 //Foam::pointIndexHit Foam::surfaceIntersection::faceEdgeIntersection
00132 //(
00133 //    const triSurface& surf,
00134 //    const label hitFaceI,
00135 //
00136 //    const vector& n,
00137 //    const point& eStart,
00138 //    const point& eEnd
00139 //)
00140 //{
00141 //    pointIndexHit pInter;
00142 //
00143 //    const pointField& points = surf.points();
00144 //
00145 //    const labelledTri& f = surf.localFaces()[hitFaceI];
00146 //
00147 //    // Plane for intersect test.
00148 //    plane pl(eStart, n);
00149 //
00150 //    forAll(f, fp)
00151 //    {
00152 //        label fp1 = (fp + 1) % 3;
00153 //
00154 //        const point& start = points[f[fp]];
00155 //        const point& end = points[f[fp1]];
00156 //
00157 //        vector eVec(end - start);
00158 //
00159 //        scalar s = pl.normalIntersect(start, eVec);
00160 //
00161 //        if (s < 0 || s > 1)
00162 //        {
00163 //            pInter.setPoint(start + s*eVec);
00164 //
00165 //            // Check if is correct one: orientation walking
00166 //            //  eStart - eEnd - hitPoint should be opposite n
00167 //            vector n2(triPointRef(start, end, pInter.hitPoint()).normal());
00168 //
00169 //            Pout<< "plane normal:" << n
00170 //                << "  start:" << start << "  end:" << end
00171 //                << "  hit at:" << pInter.hitPoint()
00172 //                << "  resulting normal:" << n2 << endl;
00173 //
00174 //            if ((n2 & n) < 0)
00175 //            {
00176 //                pInter.setHit();
00177 //
00178 //                // Find corresponding edge between f[fp] f[fp1]
00179 //                label edgeI =
00180 //                    meshTools::findEdge
00181 //                    (
00182 //                        surf.edges(),
00183 //                        surf.faceEdges()[hitFaceI],
00184 //                        f[fp],
00185 //                        f[fp1]
00186 //                    );
00187 //
00188 //                pInter.setIndex(edgeI);
00189 //
00190 //                return pInter;
00191 //            }
00192 //        }
00193 //    }
00194 //
00195 //    FatalErrorIn("surfaceIntersection::borderEdgeIntersection")
00196 //        << "Did not find intersection of plane " << pl
00197 //        << " with edges of face " << hitFaceI << " verts:" << f
00198 //        << abort(FatalError);
00199 //
00200 //    return pInter;
00201 //}
00202 
00203 
00204 
00205 void Foam::surfaceIntersection::storeIntersection
00206 (
00207     const bool isFirstSurf,
00208     const labelList& facesA,
00209     const label faceB,
00210     DynamicList<edge>& allCutEdges,
00211     DynamicList<point>& allCutPoints
00212 )
00213 {
00214 
00215     forAll(facesA, facesAI)
00216     {
00217         label faceA = facesA[facesAI];
00218 
00219         // Combine two faces. Always make sure the face from the first surface
00220         // is element 0.
00221         FixedList<label, 2> twoFaces;
00222         if (isFirstSurf)
00223         {
00224             twoFaces[0] = faceA;
00225             twoFaces[1] = faceB;
00226         }
00227         else
00228         {
00229             twoFaces[0] = faceB;
00230             twoFaces[1] = faceA;
00231         }
00232 
00233         labelPairLookup::const_iterator iter = facePairToVertex_.find(twoFaces);
00234 
00235         if (iter == facePairToVertex_.end())
00236         {
00237             // New intersection. Store face-face intersection.
00238             facePairToVertex_.insert(twoFaces, allCutPoints.size()-1);
00239         }
00240         else
00241         {
00242             // Second occurrence of surf1-surf2 intersection.
00243             // Or rather the face on surf1 intersects a face on
00244             // surface2 twice -> we found edge.
00245 
00246             // Check whether perhaps degenerate
00247             const point& prevHit = allCutPoints[*iter];
00248 
00249             const point& thisHit = allCutPoints[allCutPoints.size()-1];
00250 
00251             if (mag(prevHit - thisHit) < SMALL)
00252             {
00253                 WarningIn
00254                 (
00255                     "Foam::surfaceIntersection::storeIntersection"
00256                     "(const bool isFirstSurf, const labelList& facesA,"
00257                     "const label faceB, DynamicList<edge>& allCutEdges,"
00258                     "DynamicList<point>& allCutPoints)"
00259                 )   << "Encountered degenerate edge between face "
00260                     << twoFaces[0] << " on first surface"
00261                     << " and face " << twoFaces[1] << " on second surface"
00262                     << endl
00263                     << "Point on first surface:" << prevHit << endl
00264                     << "Point on second surface:" << thisHit << endl
00265                     << endl;
00266             }
00267             else
00268             {
00269                 allCutEdges.append(edge(*iter, allCutPoints.size()-1));
00270 
00271                 // Remember face on surf
00272                 facePairToEdge_.insert(twoFaces, allCutEdges.size()-1);
00273             }
00274         }
00275     }
00276 }
00277 
00278 
00279 // Classify cut of edge of surface1 with surface2:
00280 // 1- point of edge hits point on surface2
00281 // 2- edge pierces point on surface2
00282 // 3- point of edge hits edge on surface2
00283 // 4- edge pierces edge on surface2
00284 // 5- point of edge hits face on surface2
00285 // 6- edge pierces face on surface2
00286 //
00287 // Note that handling of 2 and 4 should be the same but with surface1 and
00288 // surface2 reversed.
00289 void Foam::surfaceIntersection::classifyHit
00290 (
00291     const triSurface& surf1,
00292     const scalarField& surf1PointTol,
00293     const triSurface& surf2,
00294     const bool isFirstSurf,
00295     const label edgeI,
00296     const scalar tolDim,
00297     const pointIndexHit& pHit,
00298 
00299     DynamicList<edge>& allCutEdges,
00300     DynamicList<point>& allCutPoints,
00301     List<DynamicList<label> >& surfEdgeCuts
00302 )
00303 {
00304     const edge& e = surf1.edges()[edgeI];
00305 
00306     const labelList& facesA = surf1.edgeFaces()[edgeI];
00307 
00308     // Label of face on surface2 edgeI intersected
00309     label surf2FaceI = pHit.index();
00310 
00311     // Classify point on surface2
00312 
00313     const labelledTri& f2 = surf2.localFaces()[surf2FaceI];
00314 
00315     const pointField& surf2Pts = surf2.localPoints();
00316 
00317     label nearType;
00318     label nearLabel;
00319 
00320     (void)triPointRef
00321     (
00322         surf2Pts[f2[0]],
00323         surf2Pts[f2[1]],
00324         surf2Pts[f2[2]]
00325     ).classify(pHit.hitPoint(), tolDim, nearType, nearLabel);
00326 
00327     // Classify points on edge of surface1
00328     label edgeEnd =
00329         classify
00330         (
00331             surf1PointTol[e.start()],
00332             surf1PointTol[e.end()],
00333             pHit.hitPoint(),
00334             e,
00335             surf1.localPoints()
00336         );
00337 
00338     if (nearType == triPointRef::POINT)
00339     {
00340         if (edgeEnd >= 0)
00341         {
00342             // 1. Point hits point. Do nothing.
00343             if (debug&2)
00344             {
00345                 Pout<< pHit.hitPoint() << " is surf1:"
00346                     << " end point of edge " << e
00347                     << " surf2: vertex " << f2[nearLabel]
00348                     << " coord:" << surf2Pts[f2[nearLabel]] << endl;
00349             }
00350         }
00351         else
00352         {
00353             // 2. Edge hits point. Cut edge with new point.
00354             if (debug&2)
00355             {
00356                 Pout<< pHit.hitPoint() << " is surf1:"
00357                     << " somewhere on edge " << e
00358                     << " surf2: vertex " << f2[nearLabel]
00359                     << " coord:" << surf2Pts[f2[nearLabel]] << endl;
00360             }
00361 
00362             allCutPoints.append(pHit.hitPoint());
00363             surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
00364 
00365             const labelList& facesB = surf2.pointFaces()[f2[nearLabel]];
00366 
00367             forAll(facesB, faceBI)
00368             {
00369                 storeIntersection
00370                 (
00371                     isFirstSurf,
00372                     facesA,
00373                     facesB[faceBI],
00374                     allCutEdges,
00375                     allCutPoints
00376                 );
00377             }
00378         }
00379     }
00380     else if (nearType == triPointRef::EDGE)
00381     {
00382         if (edgeEnd >= 0)
00383         {
00384             // 3. Point hits edge. Do nothing on this side. Reverse
00385             // is handled by 2 (edge hits point)
00386             label edge2I = getEdge(surf2, surf2FaceI, nearLabel);
00387             const edge& e2 = surf2.edges()[edge2I];
00388 
00389             if (debug&2)
00390             {
00391                 Pout<< pHit.hitPoint() << " is surf1:"
00392                     << " end point of edge " << e
00393                     << " surf2: edge " << e2
00394                     << " coords:" << surf2Pts[e2.start()]
00395                     << surf2Pts[e2.end()] << endl;
00396             }
00397         }
00398         else
00399         {
00400             // 4. Edge hits edge.
00401 
00402             // Cut edge with new point (creates duplicates when
00403             // doing the surf2 with surf1 intersection but these
00404             // are merged later on)
00405 
00406             label edge2I = getEdge(surf2, surf2FaceI, nearLabel);
00407             const edge& e2 = surf2.edges()[edge2I];
00408 
00409             if (debug&2)
00410             {
00411                 Pout<< pHit.hitPoint() << " is surf1:"
00412                     << " somewhere on edge " << e
00413                     << " surf2: edge " << e2
00414                     << " coords:" << surf2Pts[e2.start()]
00415                     << surf2Pts[e2.end()] << endl;
00416             }
00417 
00418             allCutPoints.append(pHit.hitPoint());
00419             surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
00420 
00421             // edge hits all faces on surf2 connected to the edge
00422 
00423             if (isFirstSurf)
00424             {
00425                 // edge-edge intersection is symmetric, store only
00426                 // once.
00427                 // edge hits all faces on surf2 connected to the
00428                 // edge
00429 
00430                 const labelList& facesB = surf2.edgeFaces()[edge2I];
00431 
00432                 forAll(facesB, faceBI)
00433                 {
00434                     storeIntersection
00435                     (
00436                         isFirstSurf,
00437                         facesA,
00438                         facesB[faceBI],
00439                         allCutEdges,
00440                         allCutPoints
00441                     );
00442                 }
00443             }
00444         }
00445     }
00446     else
00447     {
00448         if (edgeEnd >= 0)
00449         {
00450             // 5. Point hits face. Do what? Introduce
00451             // point & triangulation in face?
00452             if (debug&2)
00453             {
00454                 Pout<< pHit.hitPoint() << " is surf1:"
00455                     << " end point of edge " << e
00456                     << " surf2: face " << surf2FaceI
00457                     << endl;
00458             }
00459 
00460             //
00461             // Look exactly at what side (of surf2) edge is. Leave out ones on
00462             // inside of surf2 (i.e. on opposite side of normal)
00463             //
00464 
00465             // Vertex on/near surf2
00466             label nearVert = -1;
00467 
00468             if (edgeEnd == 0)
00469             {
00470                 nearVert = e.start();
00471             }
00472             else
00473             {
00474                 nearVert = e.end();
00475             }
00476 
00477             const point& nearPt = surf1.localPoints()[nearVert];
00478 
00479             // Vertex away from surf2
00480             label otherVert = e.otherVertex(nearVert);
00481 
00482             const point& otherPt = surf1.localPoints()[otherVert];
00483 
00484 
00485             if (debug)
00486             {
00487                 Pout
00488                     << pHit.hitPoint() << " is surf1:"
00489                     << " end point of edge " << e << " coord:"
00490                     << surf1.localPoints()[nearVert]
00491                     << " surf2: face " << surf2FaceI << endl;
00492             }
00493 
00494             vector eVec = otherPt - nearPt;
00495 
00496             if ((surf2.faceNormals()[surf2FaceI] & eVec) > 0)
00497             {
00498                 // otherVert on outside of surf2
00499 
00500                 // Shift hitPoint a bit along edge.
00501                 //point hitPt = nearPt + 0.1*eVec;
00502                 point hitPt = nearPt;
00503 
00504                 if (debug&2)
00505                 {
00506                     Pout<< "Shifted " << pHit.hitPoint()
00507                         << " to " << hitPt
00508                         << " along edge:" << e
00509                         << " coords:" << surf1.localPoints()[e.start()]
00510                         << surf1.localPoints()[e.end()] << endl;
00511                 }
00512 
00513                 // Reclassify as normal edge-face pierce (see below)
00514 
00515                 allCutPoints.append(hitPt);
00516                 surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
00517 
00518                 // edge hits single face only
00519                 storeIntersection
00520                 (
00521                     isFirstSurf,
00522                     facesA,
00523                     surf2FaceI,
00524                     allCutEdges,
00525                     allCutPoints
00526                 );
00527             }
00528             else
00529             {
00530                 if (debug&2)
00531                 {
00532                     Pout<< "Discarding " << pHit.hitPoint()
00533                         << " since edge " << e << " on inside of surf2."
00534                         << " surf2 normal:" << surf2.faceNormals()[surf2FaceI]
00535                         << endl;
00536                 }
00537             }
00538         }
00539         else
00540         {
00541             // 6. Edge pierces face. 'Normal' situation.
00542             if (debug&2)
00543             {
00544                 Pout<< pHit.hitPoint() << " is surf1:"
00545                     << " somewhere on edge " << e
00546                     << " surf2: face " << surf2FaceI
00547                     << endl;
00548             }
00549 
00550             // edgeI intersects surf2. Store point.
00551             allCutPoints.append(pHit.hitPoint());
00552             surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
00553 
00554             // edge hits single face only
00555             storeIntersection
00556             (
00557                 isFirstSurf,
00558                 facesA,
00559                 surf2FaceI,
00560                 allCutEdges,
00561                 allCutPoints
00562             );
00563         }
00564     }
00565     if (debug&2)
00566     {
00567         Pout<< endl;
00568     }
00569 }
00570 
00571 
00572 // Cut all edges of surf1 with surf2. Sets
00573 // - cutPoints          : coordinates of cutPoints
00574 // - cutEdges           : newly created edges between cutPoints
00575 // - facePairToVertex   : hash from face1I and face2I to cutPoint
00576 // - facePairToEdge     : hash from face1I and face2I to cutEdge
00577 // - surfEdgeCuts       : gives for each edge the cutPoints
00578 //                        (in order from start to end)
00579 //
00580 void Foam::surfaceIntersection::doCutEdges
00581 (
00582     const triSurface& surf1,
00583     const triSurfaceSearch& querySurf2,
00584     const bool isFirstSurf,
00585     const bool isSelfIntersection,
00586 
00587     DynamicList<edge>& allCutEdges,
00588     DynamicList<point>& allCutPoints,
00589     List<DynamicList<label> >& surfEdgeCuts
00590 )
00591 {
00592     scalar oldTol = intersection::setPlanarTol(1E-3);
00593 
00594     const pointField& surf1Pts = surf1.localPoints();
00595 
00596     // Calculate local (to point) tolerance based on min edge length.
00597     scalarField surf1PointTol(surf1Pts.size());
00598 
00599     forAll(surf1PointTol, pointI)
00600     {
00601         surf1PointTol[pointI] =
00602             intersection::planarTol()
00603           * minEdgeLen(surf1, pointI);
00604     }
00605 
00606     const triSurface& surf2 = querySurf2.surface();
00607 
00608     forAll(surf1.edges(), edgeI)
00609     {
00610         const edge& e = surf1.edges()[edgeI];
00611 
00612         point pStart = surf1Pts[e.start()];
00613         const point& pEnd = surf1Pts[e.end()];
00614 
00615         const point tolVec = intersection::planarTol()*(pEnd-pStart);
00616         const scalar tolDim = mag(tolVec);
00617 
00618         bool doTrack = false;
00619         do
00620         {
00621             pointIndexHit pHit = querySurf2.tree().findLine(pStart, pEnd);
00622 
00623             if (pHit.hit())
00624             {
00625                 if (isSelfIntersection)
00626                 {
00627                     // Skip all intersections which are hit at endpoints of
00628                     // edge.
00629                     // Problem is that if faces are almost coincident the
00630                     // intersection point will be calculated quite incorrectly
00631                     // The error might easily be larger than 1% of the edge
00632                     // length.
00633                     // So what we do here is to exclude hit faces if our edge
00634                     // is in their plane and they share a point with the edge.
00635 
00636                     // Label of face on surface2 edgeI intersected
00637                     label hitFaceI = pHit.index();
00638 
00639                     if
00640                     (
00641                         !excludeEdgeHit
00642                         (
00643                             surf1,
00644                             edgeI,
00645                             hitFaceI,
00646                             0.1         // 1-cos of angle between normals
00647                         )
00648                     )
00649                     {
00650                         // Classify point on surface1
00651                         label edgeEnd = classify
00652                         (
00653                             surf1PointTol[e.start()],
00654                             surf1PointTol[e.end()],
00655                             pHit.hitPoint(),
00656                             e,
00657                             surf1Pts
00658                         );
00659 
00660                         if (edgeEnd < 0)
00661                         {
00662                             if (debug)
00663                             {
00664                                 Pout<< "edge:" << edgeI << " vertices:" << e
00665                                     << "  start:" << surf1Pts[e.start()]
00666                                     << "  end:" << surf1Pts[e.end()]
00667                                     << "  hit:" << pHit.hitPoint()
00668                                     << "  tolDim:" << tolDim
00669                                     << "  planarTol:"
00670                                     << intersection::planarTol()
00671                                     << endl;
00672                             }
00673                             allCutPoints.append(pHit.hitPoint());
00674                             surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
00675                         }
00676                     }
00677                 }
00678                 else
00679                 {
00680                     classifyHit
00681                     (
00682                         surf1,
00683                         surf1PointTol,
00684                         surf2,
00685                         isFirstSurf,
00686                         edgeI,
00687                         tolDim,
00688                         pHit,
00689 
00690                         allCutEdges,
00691                         allCutPoints,
00692                         surfEdgeCuts
00693                     );
00694                 }
00695 
00696                 if (mag(pHit.hitPoint() - pEnd) < tolDim)
00697                 {
00698                     doTrack = false;
00699                 }
00700                 else
00701                 {
00702                     pStart = pHit.hitPoint() + tolVec;
00703 
00704                     doTrack = true;
00705                 }
00706             }
00707             else
00708             {
00709                 doTrack = false;
00710             }
00711         }
00712         while(doTrack);
00713     }
00714     intersection::setPlanarTol(oldTol);
00715 }
00716 
00717 
00718 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00719 
00720 // Null constructor
00721 Foam::surfaceIntersection::surfaceIntersection()
00722 :
00723     cutPoints_(0),
00724     cutEdges_(0),
00725     facePairToVertex_(0),
00726     facePairToEdge_(0),
00727     surf1EdgeCuts_(0),
00728     surf2EdgeCuts_(0)
00729 {}
00730 
00731 
00732 // Construct from two surfaces
00733 Foam::surfaceIntersection::surfaceIntersection
00734 (
00735     const triSurfaceSearch& query1,
00736     const triSurfaceSearch& query2
00737 )
00738 :
00739     cutPoints_(0),
00740     cutEdges_(0),
00741     facePairToVertex_(2*max(query1.surface().size(), query2.surface().size())),
00742     facePairToEdge_(2*max(query1.surface().size(), query2.surface().size())),
00743     surf1EdgeCuts_(0),
00744     surf2EdgeCuts_(0)
00745 {
00746     const triSurface& surf1 = query1.surface();
00747     const triSurface& surf2 = query2.surface();
00748 
00749     //
00750     // Cut all edges of surf1 with surf2.
00751     //
00752     if (debug)
00753     {
00754         Pout<< "Cutting surf1 edges" << endl;
00755     }
00756 
00757 
00758     DynamicList<edge> allCutEdges(surf1.nEdges()/20);
00759     DynamicList<point> allCutPoints(surf1.nPoints()/20);
00760 
00761 
00762     // From edge to cut index on surface1
00763     List<DynamicList<label> > edgeCuts1(query1.surface().nEdges());
00764 
00765     doCutEdges
00766     (
00767         surf1,
00768         query2,
00769         true,               // is first surface; construct labelPair in correct
00770                             // order
00771         false,              // not self intersection
00772 
00773         allCutEdges,
00774         allCutPoints,
00775         edgeCuts1
00776     );
00777     // Transfer to straight labelListList
00778     transfer(edgeCuts1, surf1EdgeCuts_);
00779 
00780 
00781     //
00782     // Cut all edges of surf2 with surf1.
00783     //
00784     if (debug)
00785     {
00786         Pout<< "Cutting surf2 edges" << endl;
00787     }
00788 
00789     // From edge to cut index
00790     List<DynamicList<label> > edgeCuts2(query2.surface().nEdges());
00791 
00792     doCutEdges
00793     (
00794         surf2,
00795         query1,
00796         false,              // is second surface
00797         false,              // not self intersection
00798 
00799         allCutEdges,
00800         allCutPoints,
00801         edgeCuts2
00802     );
00803 
00804     // Transfer to straight label(List)List
00805     transfer(edgeCuts2, surf2EdgeCuts_);
00806     cutEdges_.transfer(allCutEdges);
00807     cutPoints_.transfer(allCutPoints);
00808 
00809 
00810     if (debug)
00811     {
00812         Pout<< "surfaceIntersection : Intersection generated:"
00813             << endl
00814             << "    points:" << cutPoints_.size() << endl
00815             << "    edges :" << cutEdges_.size() << endl;
00816 
00817         Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
00818             << endl;
00819 
00820         OFstream intStream("intEdges.obj");
00821         writeOBJ(cutPoints_, cutEdges_, intStream);
00822 
00823         // Dump all cut edges to files
00824         Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
00825         OFstream edge1Stream("surf1EdgeCuts.obj");
00826         writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
00827 
00828         Pout<< "Dumping cut edges of surface2 to surf2EdgeCuts.obj" << endl;
00829         OFstream edge2Stream("surf2EdgeCuts.obj");
00830         writeIntersectedEdges(surf2, surf2EdgeCuts_, edge2Stream);
00831     }
00832 }
00833 
00834 
00835 // Construct from full intersection Poutrmation
00836 Foam::surfaceIntersection::surfaceIntersection
00837 (
00838     const triSurface& surf1,
00839     const edgeIntersections& intersections1,
00840     const triSurface& surf2,
00841     const edgeIntersections& intersections2
00842 )
00843 :
00844     cutPoints_(0),
00845     cutEdges_(0),
00846     facePairToVertex_(2*max(surf1.size(), surf2.size())),
00847     facePairToEdge_(2*max(surf1.size(), surf2.size())),
00848     surf1EdgeCuts_(0),
00849     surf2EdgeCuts_(0)
00850 {
00851 
00852     // All intersection Pout (so for both surfaces)
00853     DynamicList<edge> allCutEdges((surf1.nEdges() + surf2.nEdges())/20);
00854     DynamicList<point> allCutPoints((surf1.nPoints() + surf2.nPoints())/20);
00855 
00856 
00857     // Cut all edges of surf1 with surf2
00858     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00859 
00860     if (debug)
00861     {
00862         Pout<< "Storing surf1 intersections" << endl;
00863     }
00864 
00865     {
00866         // From edge to cut index on surface1
00867         List<DynamicList<label> > edgeCuts1(surf1.nEdges());
00868 
00869         forAll(intersections1, edgeI)
00870         {
00871             const List<pointIndexHit>& intersections = intersections1[edgeI];
00872 
00873             forAll(intersections, i)
00874             {
00875                 const pointIndexHit& pHit = intersections[i];
00876 
00877                 // edgeI intersects surf2. Store point.
00878                 allCutPoints.append(pHit.hitPoint());
00879                 edgeCuts1[edgeI].append(allCutPoints.size()-1);
00880 
00881                 storeIntersection
00882                 (
00883                     true,                       // is first surface
00884                     surf1.edgeFaces()[edgeI],
00885                     pHit.index(),               // surf2FaceI
00886                     allCutEdges,
00887                     allCutPoints
00888                 );
00889             }
00890         }
00891 
00892         // Transfer to straight labelListList
00893         transfer(edgeCuts1, surf1EdgeCuts_);
00894     }
00895 
00896 
00897 
00898     // Cut all edges of surf2 with surf1
00899     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00900 
00901     if (debug)
00902     {
00903         Pout<< "Storing surf2 intersections" << endl;
00904     }
00905 
00906     {
00907         // From edge to cut index on surface2
00908         List<DynamicList<label> > edgeCuts2(surf2.nEdges());
00909 
00910         forAll(intersections2, edgeI)
00911         {
00912             const List<pointIndexHit>& intersections = intersections2[edgeI];
00913 
00914             forAll(intersections, i)
00915             {
00916                 const pointIndexHit& pHit = intersections[i];
00917 
00918                 // edgeI intersects surf1. Store point.
00919                 allCutPoints.append(pHit.hitPoint());
00920                 edgeCuts2[edgeI].append(allCutPoints.size()-1);
00921 
00922                 storeIntersection
00923                 (
00924                     false,                      // is second surface
00925                     surf2.edgeFaces()[edgeI],
00926                     pHit.index(),               // surf2FaceI
00927                     allCutEdges,
00928                     allCutPoints
00929                 );
00930             }
00931         }
00932 
00933         // Transfer to surf2EdgeCuts_ (straight labelListList)
00934         transfer(edgeCuts2, surf2EdgeCuts_);
00935     }
00936 
00937 
00938     // Transfer to straight label(List)List
00939     cutEdges_.transfer(allCutEdges);
00940     cutPoints_.transfer(allCutPoints);
00941 
00942 
00943     if (debug)
00944     {
00945         Pout<< "surfaceIntersection : Intersection generated:"
00946             << endl
00947             << "    points:" << cutPoints_.size() << endl
00948             << "    edges :" << cutEdges_.size() << endl;
00949 
00950         Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
00951             << endl;
00952 
00953         OFstream intStream("intEdges.obj");
00954         writeOBJ(cutPoints_, cutEdges_, intStream);
00955 
00956         // Dump all cut edges to files
00957         Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
00958         OFstream edge1Stream("surf1EdgeCuts.obj");
00959         writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
00960 
00961         Pout<< "Dumping cut edges of surface2 to surf2EdgeCuts.obj" << endl;
00962         OFstream edge2Stream("surf2EdgeCuts.obj");
00963         writeIntersectedEdges(surf2, surf2EdgeCuts_, edge2Stream);
00964     }
00965 
00966 
00967     // Debugging stuff
00968     {
00969         // Check all facePairToVertex is used.
00970         labelHashSet usedPoints;
00971 
00972         forAllConstIter(labelPairLookup, facePairToEdge_, iter)
00973         {
00974             label edgeI = iter();
00975 
00976             const edge& e = cutEdges_[edgeI];
00977 
00978             usedPoints.insert(e[0]);
00979             usedPoints.insert(e[1]);
00980         }
00981 
00982         forAllConstIter(labelPairLookup, facePairToVertex_, iter)
00983         {
00984             label pointI = iter();
00985 
00986             if (!usedPoints.found(pointI))
00987             {
00988                 FatalErrorIn("surfaceIntersection::surfaceIntersection")
00989                     << "Problem: cut point:" << pointI
00990                     << " coord:" << cutPoints_[pointI]
00991                     << " not used by any edge" << abort(FatalError);
00992             }
00993         }
00994     }
00995 }
00996 
00997 
00998 // Construct from single surface. Used to test for self-intersection.
00999 Foam::surfaceIntersection::surfaceIntersection
01000 (
01001     const triSurfaceSearch& query1
01002 )
01003 :
01004     cutPoints_(0),
01005     cutEdges_(0),
01006     facePairToVertex_(2*query1.surface().size()),
01007     facePairToEdge_(2*query1.surface().size()),
01008     surf1EdgeCuts_(0),
01009     surf2EdgeCuts_(0)
01010 {
01011     const triSurface& surf1 = query1.surface();
01012 
01013     //
01014     // Cut all edges of surf1 with surf1 itself.
01015     //
01016     if (debug)
01017     {
01018         Pout<< "Cutting surf1 edges" << endl;
01019     }
01020 
01021     DynamicList<edge> allCutEdges;
01022     DynamicList<point> allCutPoints;
01023 
01024     // From edge to cut index on surface1
01025     List<DynamicList<label> > edgeCuts1(query1.surface().nEdges());
01026 
01027     doCutEdges
01028     (
01029         surf1,
01030         query1,
01031         true,               // is first surface; construct labelPair in correct
01032                             // order
01033         true,               // self intersection
01034 
01035 
01036         allCutEdges,
01037         allCutPoints,
01038         edgeCuts1
01039     );
01040 
01041     // Transfer to straight label(List)List
01042     transfer(edgeCuts1, surf1EdgeCuts_);
01043     cutEdges_.transfer(allCutEdges);
01044     cutPoints_.transfer(allCutPoints);
01045 
01046     // Shortcut.
01047     if (cutPoints_.empty() && cutEdges_.empty())
01048     {
01049         if (debug)
01050         {
01051             Pout<< "Empty intersection" << endl;
01052         }
01053         return;
01054     }
01055 
01056     //
01057     // Remove duplicate points (from edge-point or edge-edge cutting)
01058     //
01059 
01060     // Get typical dimension.
01061     scalar minEdgeLen = GREAT;
01062     forAll(surf1.edges(), edgeI)
01063     {
01064         minEdgeLen = min
01065         (
01066             minEdgeLen,
01067             surf1.edges()[edgeI].mag(surf1.localPoints())
01068         );
01069     }
01070 
01071     // Merge points
01072     labelList pointMap;
01073     pointField newPoints;
01074 
01075     bool hasMerged = mergePoints
01076     (
01077         cutPoints_,
01078         minEdgeLen*intersection::planarTol(),
01079         false,
01080         pointMap,
01081         newPoints
01082     );
01083 
01084     if (hasMerged)
01085     {
01086         if (debug)
01087         {
01088             Pout<< "Merged:" << hasMerged
01089                 << "  mergeDist:" << minEdgeLen*intersection::planarTol()
01090                 << "  cutPoints:" << cutPoints_.size()
01091                 << "  newPoints:" << newPoints.size()
01092                 << endl;
01093         }
01094 
01095         // Copy points
01096         cutPoints_.transfer(newPoints);
01097 
01098         // Renumber vertices referenced by edges
01099         forAll(cutEdges_, edgeI)
01100         {
01101             edge& e = cutEdges_[edgeI];
01102 
01103             e.start() = pointMap[e.start()];
01104             e.end() = pointMap[e.end()];
01105 
01106             if (e.mag(cutPoints_) < minEdgeLen*intersection::planarTol())
01107             {
01108                 if (debug)
01109                 {
01110                     Pout<< "Degenerate cut:" << edgeI << " vertices:" << e
01111                         << " coords:" << cutPoints_[e.start()] << ' '
01112                         << cutPoints_[e.end()] << endl;
01113                 }
01114             }
01115         }
01116 
01117         // Renumber vertices referenced by edgeCut lists. Remove duplicates.
01118         forAll(surf1EdgeCuts_, edgeI)
01119         {
01120             // Get indices of cutPoints this edge is cut by
01121             labelList& cutVerts = surf1EdgeCuts_[edgeI];
01122 
01123             removeDuplicates(pointMap, cutVerts);
01124         }
01125     }
01126 
01127     if (debug)
01128     {
01129         Pout<< "surfaceIntersection : Intersection generated and compressed:"
01130             << endl
01131             << "    points:" << cutPoints_.size() << endl
01132             << "    edges :" << cutEdges_.size() << endl;
01133 
01134 
01135         Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
01136             << endl;
01137 
01138         OFstream intStream("intEdges.obj");
01139         writeOBJ(cutPoints_, cutEdges_, intStream);
01140     }
01141 
01142     if (debug)
01143     {
01144         // Dump all cut edges to files
01145         Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
01146         OFstream edge1Stream("surf1EdgeCuts.obj");
01147         writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
01148     }
01149 }
01150 
01151 
01152 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
01153 
01154 const Foam::pointField& Foam::surfaceIntersection::cutPoints() const
01155 {
01156     return cutPoints_;
01157 }
01158 
01159 
01160 const Foam::edgeList& Foam::surfaceIntersection::cutEdges() const
01161 {
01162     return cutEdges_;
01163 }
01164 
01165 
01166 const Foam::labelPairLookup& Foam::surfaceIntersection::facePairToEdge() const
01167 {
01168     return facePairToEdge_;
01169 }
01170 
01171 
01172 const Foam::labelListList& Foam::surfaceIntersection::edgeCuts
01173 (
01174     const bool isFirstSurf
01175 ) const
01176 {
01177     if (isFirstSurf)
01178     {
01179         return surf1EdgeCuts_;
01180     }
01181     else
01182     {
01183         return surf2EdgeCuts_;
01184     }
01185 }
01186 
01187 
01188 const Foam::labelListList& Foam::surfaceIntersection::surf1EdgeCuts() const
01189 {
01190     return surf1EdgeCuts_;
01191 }
01192 
01193 
01194 const Foam::labelListList& Foam::surfaceIntersection::surf2EdgeCuts() const
01195 {
01196     return surf2EdgeCuts_;
01197 }
01198 
01199 
01200 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd