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

edgeIntersections.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 "edgeIntersections.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 "treeDataTriSurface.H"
00037 #include "indexedOctree.H"
00038 #include "meshTools.H"
00039 #include "plane.H"
00040 #include "Random.H"
00041 #include "mathematicalConstants.H"
00042 #include "treeBoundBox.H"
00043 
00044 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00045 
00046 defineTypeNameAndDebug(Foam::edgeIntersections, 0);
00047 
00048 Foam::scalar Foam::edgeIntersections::alignedCos_ =
00049     Foam::cos(89.0 * Foam::mathematicalConstant::pi/180.0);
00050 
00051 
00052 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00053 
00054 void Foam::edgeIntersections::checkEdges(const triSurface& surf)
00055 {
00056     const pointField& localPoints = surf.localPoints();
00057     const edgeList& edges = surf.edges();
00058     const labelListList& edgeFaces = surf.edgeFaces();
00059 
00060     treeBoundBox bb(localPoints);
00061 
00062     scalar minSize = SMALL * bb.minDim();
00063 
00064     forAll(edges, edgeI)
00065     {
00066         const edge& e = edges[edgeI];
00067 
00068         scalar eMag = e.mag(localPoints);
00069 
00070         if (eMag < minSize)
00071         {
00072             WarningIn
00073             (
00074                 "Foam::edgeIntersections::checkEdges(const triSurface& surf)"
00075             )   << "Edge " << edgeI << " vertices " << e
00076                 << " coords:" << localPoints[e[0]] << ' '
00077                 << localPoints[e[1]] << " is very small compared to bounding"
00078                 << " box dimensions " << bb << endl
00079                 << "This might lead to problems in intersection"
00080                 << endl;
00081         }
00082 
00083         if (edgeFaces[edgeI].size() == 1)
00084         {
00085             WarningIn
00086             (
00087                 "Foam::edgeIntersections::checkEdges(const triSurface& surf)"
00088             )   << "Edge " << edgeI << " vertices " << e
00089                 << " coords:" << localPoints[e[0]] << ' '
00090                 << localPoints[e[1]] << " has only one face connected to it:"
00091                 << edgeFaces[edgeI] << endl
00092                 << "This might lead to problems in intersection"
00093                 << endl;
00094         }
00095     }
00096 }
00097 
00098 
00099 // Update intersections for selected edges.
00100 void Foam::edgeIntersections::intersectEdges
00101 (
00102     const triSurface& surf1,
00103     const pointField& points1,          // surf1 meshPoints (not localPoints!)
00104     const triSurfaceSearch& querySurf2,
00105     const scalarField& surf1PointTol,   // surf1 tolerance per point
00106     const labelList& edgeLabels
00107 )
00108 {
00109     const triSurface& surf2 = querySurf2.surface();
00110     const vectorField& normals2 = surf2.faceNormals();
00111 
00112     const labelList& meshPoints = surf1.meshPoints();
00113 
00114     if (debug)
00115     {
00116         Pout<< "Calculating intersection of " << edgeLabels.size() << " edges"
00117             << " out of " << surf1.nEdges() << " with " << surf2.size()
00118             << " triangles ..." << endl;
00119     }
00120 
00121     // Construct octree.
00122     const indexedOctree<treeDataTriSurface>& tree = querySurf2.tree();
00123 
00124 
00125     label nHits = 0;
00126 
00127 
00128     // Go through all edges, calculate intersections
00129     forAll(edgeLabels, i)
00130     {
00131         label edgeI = edgeLabels[i];
00132 
00133         if (debug && (i % 1000 == 0))
00134         {
00135             Pout<< "Intersecting edge " << edgeI << " with surface" << endl;
00136         }
00137 
00138         const edge& e = surf1.edges()[edgeI];
00139 
00140         const point& pStart = points1[meshPoints[e.start()]];
00141         const point& pEnd = points1[meshPoints[e.end()]];
00142 
00143         const vector eVec(pEnd - pStart);
00144         const scalar eMag = mag(eVec);
00145         const vector n(eVec/(eMag + VSMALL));
00146 
00147         // Smallish length for intersection calculation errors.
00148         const point tolVec = 1e-6*eVec;
00149 
00150         // Start tracking somewhat before pStart and upto somewhat after p1.
00151         // Note that tolerances here are smaller than those used to classify
00152         // hit below.
00153         // This will cause this hit to be marked as degenerate and resolved
00154         // later on.
00155         point p0 = pStart - 0.5*surf1PointTol[e[0]]*n;
00156         const point p1 = pEnd + 0.5*surf1PointTol[e[1]]*n;
00157         const scalar maxS = mag(p1 - pStart);
00158 
00159         // Get all intersections of the edge with the surface
00160 
00161         DynamicList<pointIndexHit> currentIntersections(100);
00162         DynamicList<label> currentIntersectionTypes(100);
00163 
00164         while (true)
00165         {
00166             pointIndexHit pHit = tree.findLine(p0, p1);
00167 
00168             if (pHit.hit())
00169             {
00170                 nHits++;
00171 
00172                 currentIntersections.append(pHit);
00173 
00174                 // Classify point on surface1 edge.
00175                 label edgeEnd = -1;
00176 
00177                 if (mag(pHit.hitPoint() - pStart) < surf1PointTol[e[0]])
00178                 {
00179                     edgeEnd = 0;
00180                 }
00181                 else if (mag(pHit.hitPoint() - pEnd) < surf1PointTol[e[1]])
00182                 {
00183                     edgeEnd = 1;
00184                 }
00185                 else if (mag(n & normals2[pHit.index()]) < alignedCos_)
00186                 {
00187                     Pout<< "Flat angle edge:" << edgeI
00188                         << " face:" << pHit.index()
00189                         << " cos:" << mag(n & normals2[pHit.index()])
00190                         << endl;
00191                     edgeEnd = 2;
00192                 }
00193 
00194                 currentIntersectionTypes.append(edgeEnd);
00195 
00196                 if (edgeEnd == 1)
00197                 {
00198                     // Close to end
00199                     break;
00200                 }
00201                 else
00202                 {
00203                     // Continue tracking. Shift by a small amount.
00204                     p0 = pHit.hitPoint() + tolVec;
00205 
00206                     if (((p0-pStart) & n) >= maxS)
00207                     {
00208                         break;
00209                     }
00210                 }
00211             }
00212             else
00213             {
00214                 // No hit.
00215                 break;
00216             }
00217         }
00218 
00219 
00220         // Done current edge. Transfer all data into *this
00221         operator[](edgeI).transfer(currentIntersections);
00222         classification_[edgeI].transfer(currentIntersectionTypes);
00223     }
00224 
00225     if (debug)
00226     {
00227         Pout<< "Found " << nHits << " intersections of edges with surface ..."
00228             << endl;
00229     }
00230 
00231 }
00232 
00233 
00234 // If edgeI intersections are close to endpoint of edge shift endpoints
00235 // slightly along edge
00236 // Updates
00237 // - points1 with new endpoint position
00238 // - affectedEdges with all edges affected by moving the point
00239 // Returns true if changed anything.
00240 bool Foam::edgeIntersections::inlinePerturb
00241 (
00242     const triSurface& surf1,
00243     const scalarField& surf1PointTol,   // surf1 tolerance per point
00244     const label edgeI,
00245     Random& rndGen,
00246     pointField& points1,
00247     boolList& affectedEdges
00248 ) const
00249 {
00250     bool hasPerturbed = false;
00251 
00252     // Check if edge close to endpoint. Note that we only have to check
00253     // the intersection closest to the edge endpoints (i.e. first and last in
00254     // edgeEdgs)
00255 
00256     const labelList& edgeEnds = classification_[edgeI];
00257 
00258     if (edgeEnds.size())
00259     {
00260         bool perturbStart = false;
00261         bool perturbEnd = false;
00262 
00263         // Check first intersection.
00264         if (edgeEnds[0] == 0)
00265         {
00266             perturbStart = true;
00267         }
00268 
00269         if (edgeEnds[edgeEnds.size()-1] == 1)
00270         {
00271             perturbEnd = true;
00272         }
00273 
00274 
00275         if (perturbStart || perturbEnd)
00276         {
00277             const edge& e = surf1.edges()[edgeI];
00278 
00279             label v0 = surf1.meshPoints()[e[0]];
00280             label v1 = surf1.meshPoints()[e[1]];
00281 
00282             vector eVec(points1[v1] - points1[v0]);
00283             vector n = eVec/mag(eVec);
00284 
00285             if (perturbStart)
00286             {
00287                 // Perturb with something (hopefully) larger than tolerance.
00288                 scalar t = 4.0*(rndGen.scalar01() - 0.5);
00289                 points1[v0] += t*surf1PointTol[e[0]]*n;
00290 
00291                 const labelList& pEdges = surf1.pointEdges()[e[0]];
00292 
00293                 forAll(pEdges, i)
00294                 {
00295                     affectedEdges[pEdges[i]] = true;
00296                 }
00297             }
00298             if (perturbEnd)
00299             {
00300                 // Perturb with something larger than tolerance.
00301                 scalar t = 4.0*(rndGen.scalar01() - 0.5);
00302                 points1[v1] += t*surf1PointTol[e[1]]*n;
00303 
00304                 const labelList& pEdges = surf1.pointEdges()[e[1]];
00305 
00306                 forAll(pEdges, i)
00307                 {
00308                     affectedEdges[pEdges[i]] = true;
00309                 }
00310             }
00311             hasPerturbed = true;
00312         }
00313     }
00314 
00315     return hasPerturbed;
00316 }
00317 
00318 
00319 // Perturb single edge endpoint when perpendicular to face
00320 bool Foam::edgeIntersections::rotatePerturb
00321 (
00322     const triSurface& surf1,
00323     const scalarField& surf1PointTol,   // surf1 tolerance per point
00324     const label edgeI,
00325 
00326     Random& rndGen,
00327     pointField& points1,
00328     boolList& affectedEdges
00329 ) const
00330 {
00331     const labelList& meshPoints = surf1.meshPoints();
00332 
00333     const labelList& edgeEnds = classification_[edgeI];
00334 
00335     bool hasPerturbed = false;
00336 
00337     forAll(edgeEnds, i)
00338     {
00339         if (edgeEnds[i] == 2)
00340         {
00341             const edge& e = surf1.edges()[edgeI];
00342 
00343             // Endpoint to modify. Choose either start or end.
00344             label pointI = e[rndGen.bit()];
00345             //label pointI = e[0];
00346 
00347             // Generate random vector slightly larger than tolerance.
00348             vector rndVec = rndGen.vector01() - vector(0.5, 0.5, 0.5);
00349 
00350             // Make sure rndVec only perp to edge
00351             vector n(points1[meshPoints[e[1]]] - points1[meshPoints[e[0]]]);
00352             scalar magN = mag(n) + VSMALL;
00353             n /= magN;
00354 
00355             rndVec -= n*(n & rndVec);
00356 
00357             // Normalize
00358             rndVec /= mag(rndVec) + VSMALL;
00359 
00360             // Scale to be moved by tolerance.
00361             rndVec *= 0.01*magN;
00362 
00363             Pout<< "rotating: shifting endpoint " << meshPoints[pointI]
00364                 << " of edge:" << edgeI << " verts:"
00365                 << points1[meshPoints[e[0]]] << ' '
00366                 << points1[meshPoints[e[1]]]
00367                 << " by " << rndVec
00368                 << " tol:" << surf1PointTol[pointI] << endl;
00369 
00370             points1[meshPoints[pointI]] += rndVec;
00371 
00372             // Mark edges affected by change to point
00373             const labelList& pEdges = surf1.pointEdges()[pointI];
00374 
00375             forAll(pEdges, i)
00376             {
00377                 affectedEdges[pEdges[i]] = true;
00378             }
00379 
00380             hasPerturbed = true;
00381 
00382             // Enough done for current edge; no need to test other intersections
00383             // of this edge.
00384             break;
00385         }
00386     }
00387 
00388     return hasPerturbed;
00389 }
00390 
00391 
00392 // Perturb edge when close to face
00393 bool Foam::edgeIntersections::offsetPerturb
00394 (
00395     const triSurface& surf1,
00396     const triSurface& surf2,
00397     const label edgeI,
00398 
00399     Random& rndGen,
00400     pointField& points1,
00401     boolList& affectedEdges
00402 ) const
00403 {
00404     const labelList& meshPoints = surf1.meshPoints();
00405 
00406     const edge& e = surf1.edges()[edgeI];
00407 
00408     const List<pointIndexHit>& hits = operator[](edgeI);
00409 
00410 
00411     bool hasPerturbed = false;
00412 
00413     // For all hits on edge
00414     forAll(hits, i)
00415     {
00416         const pointIndexHit& pHit = hits[i];
00417 
00418         // Classify point on face of surface2
00419         label surf2FaceI = pHit.index();
00420 
00421         const labelledTri& f2 = surf2.localFaces()[surf2FaceI];
00422 
00423         const pointField& surf2Pts = surf2.localPoints();
00424 
00425         label nearType;
00426         label nearLabel;
00427 
00428         triPointRef tri
00429         (
00430             surf2Pts[f2[0]],
00431             surf2Pts[f2[1]],
00432             surf2Pts[f2[2]]
00433         );
00434 
00435         point ctr = tri.centre();
00436 
00437         // Get measure for tolerance.
00438         scalar tolDim = 0.001*mag(tri.a() - ctr);
00439 
00440         tri.classify(pHit.hitPoint(), tolDim, nearType, nearLabel);
00441 
00442         if (nearType == triPointRef::POINT || nearType == triPointRef::EDGE)
00443         {
00444             // Shift edge towards tri centre
00445             vector offset = 0.01*rndGen.scalar01()*(ctr - pHit.hitPoint());
00446 
00447             // shift e[0]
00448             points1[meshPoints[e[0]]] += offset;
00449 
00450             // Mark edges affected by change to e0
00451             const labelList& pEdges0 = surf1.pointEdges()[e[0]];
00452 
00453             forAll(pEdges0, i)
00454             {
00455                 affectedEdges[pEdges0[i]] = true;
00456             }
00457 
00458             // shift e[1]
00459             points1[meshPoints[e[1]]] += offset;
00460 
00461             // Mark edges affected by change to e1
00462             const labelList& pEdges1 = surf1.pointEdges()[e[1]];
00463 
00464             forAll(pEdges1, i)
00465             {
00466                 affectedEdges[pEdges1[i]] = true;
00467             }
00468 
00469             hasPerturbed = true;
00470 
00471             // No need to test any other hits on this edge
00472             break;
00473         }
00474     }
00475 
00476     return hasPerturbed;
00477 }
00478 
00479 
00480 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00481 
00482 // Construct null
00483 Foam::edgeIntersections::edgeIntersections()
00484 :
00485     List<List<pointIndexHit> >(),
00486     classification_()
00487 {}
00488 
00489 
00490 // Construct from surface and tolerance
00491 Foam::edgeIntersections::edgeIntersections
00492 (
00493     const triSurface& surf1,
00494     const triSurfaceSearch& query2,
00495     const scalarField& surf1PointTol
00496 )
00497 :
00498     List<List<pointIndexHit> >(surf1.nEdges()),
00499     classification_(surf1.nEdges())
00500 {
00501     checkEdges(surf1);
00502     checkEdges(query2.surface());
00503 
00504     // Current set of edges to test
00505     labelList edgesToTest(surf1.nEdges());
00506 
00507     // Start off with all edges
00508     forAll(edgesToTest, i)
00509     {
00510         edgesToTest[i] = i;
00511     }
00512 
00513     // Determine intersections for edgesToTest
00514     intersectEdges
00515     (
00516         surf1,
00517         surf1.points(), // surf1 meshPoints (not localPoints!)
00518         query2,
00519         surf1PointTol,
00520         edgesToTest
00521     );
00522 }
00523 
00524 
00525 // Construct from components
00526 Foam::edgeIntersections::edgeIntersections
00527 (
00528     const List<List<pointIndexHit> >& intersections,
00529     const labelListList& classification
00530 )
00531 :
00532     List<List<pointIndexHit> >(intersections),
00533     classification_(classification)
00534 {}
00535 
00536 
00537 // * * * * * * * * * * * * * * * Static Functions  * * * * * * * * * * * * * //
00538 
00539 Foam::scalarField Foam::edgeIntersections::minEdgeLength(const triSurface& surf)
00540 {
00541     const pointField& localPoints = surf.localPoints();
00542     const labelListList& pointEdges = surf.pointEdges();
00543     const edgeList& edges = surf.edges();
00544 
00545     scalarField minLen(localPoints.size());
00546 
00547     forAll(minLen, pointI)
00548     {
00549         const labelList& pEdges = pointEdges[pointI];
00550 
00551         scalar minDist = GREAT;
00552 
00553         forAll(pEdges, i)
00554         {
00555             minDist = min(minDist, edges[pEdges[i]].mag(localPoints));
00556         }
00557 
00558         minLen[pointI] = minDist;
00559     }
00560     return minLen;
00561 }
00562 
00563 
00564 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00565 
00566 Foam::label Foam::edgeIntersections::removeDegenerates
00567 (
00568     const label nIters,
00569     const triSurface& surf1,
00570     const triSurfaceSearch& query2,
00571     const scalarField& surf1PointTol,
00572     pointField& points1
00573 )
00574 {
00575     const triSurface& surf2 = query2.surface();
00576 
00577     Random rndGen(356574);
00578 
00579     // Current set of edges to (re)test
00580     labelList edgesToTest(surf1.nEdges());
00581 
00582     // Start off with all edges
00583     forAll(edgesToTest, i)
00584     {
00585         edgesToTest[i] = i;
00586     }
00587 
00588 
00589     label iter = 0;
00590 
00591     for (; iter < nIters; iter++)
00592     {
00593         // Go through all edges to (re)test and perturb points if they are
00594         // degenerate hits. Mark off edges that need to be recalculated.
00595 
00596         boolList affectedEdges(surf1.nEdges(), false);
00597         label nShifted = 0;
00598         label nRotated = 0;
00599         label nOffset = 0;
00600 
00601         forAll(edgesToTest, i)
00602         {
00603             label edgeI = edgesToTest[i];
00604 
00605             // If edge not already marked for retesting
00606             if (!affectedEdges[edgeI])
00607             {
00608                 // 1. Check edges close to endpoint and perturb if nessecary.
00609 
00610                 bool shiftedEdgeEndPoints =
00611                     inlinePerturb
00612                     (
00613                         surf1,
00614                         surf1PointTol,
00615                         edgeI,
00616                         rndGen,
00617                         points1,
00618                         affectedEdges
00619                     );
00620 
00621                 nShifted += (shiftedEdgeEndPoints ? 1 : 0);
00622 
00623                 if (!shiftedEdgeEndPoints)
00624                 {
00625                     bool rotatedEdge =
00626                         rotatePerturb
00627                         (
00628                             surf1,
00629                             surf1PointTol,
00630                             edgeI,
00631                             rndGen,
00632                             points1,
00633                             affectedEdges
00634                         );
00635 
00636                     nRotated += (rotatedEdge ? 1 : 0);
00637 
00638                     if (!rotatedEdge)
00639                     {
00640                         // 2. we're sure now that the edge actually pierces the
00641                         // face. Now check the face for intersections close its
00642                         // points/edges
00643 
00644                         bool offsetEdgePoints =
00645                             offsetPerturb
00646                             (
00647                                 surf1,
00648                                 surf2,
00649                                 edgeI,
00650                                 rndGen,
00651                                 points1,
00652                                 affectedEdges
00653                             );
00654 
00655                         nOffset += (offsetEdgePoints ? 1 : 0);
00656                     }
00657                 }
00658             }
00659         }
00660 
00661         if (debug)
00662         {
00663             Pout<< "Edges to test : " << nl
00664                 << "    total:" << edgesToTest.size() << nl
00665                 << "    resolved by:" << nl
00666                 << "        shifting   : " << nShifted << nl
00667                 << "        rotating   : " << nRotated << nl
00668                 << "        offsetting : " << nOffset << nl
00669                 << endl;
00670         }
00671 
00672 
00673         if (nShifted == 0 && nRotated == 0 && nOffset == 0)
00674         {
00675             // Nothing changed in current iteration. Current hit pattern is
00676             // without any degenerates.
00677             break;
00678         }
00679 
00680         // Repack affected edges
00681         labelList newEdgesToTest(surf1.nEdges());
00682         label newEdgeI = 0;
00683 
00684         forAll(affectedEdges, edgeI)
00685         {
00686             if (affectedEdges[edgeI])
00687             {
00688                 newEdgesToTest[newEdgeI++] = edgeI;
00689             }
00690         }
00691         newEdgesToTest.setSize(newEdgeI);
00692 
00693         if (debug)
00694         {
00695             Pout<< "Edges to test:" << nl
00696                 << "    was : " << edgesToTest.size() << nl
00697                 << "    is  : " << newEdgesToTest.size() << nl
00698                 << endl;
00699         }
00700 
00701         // Transfer and test.
00702         edgesToTest.transfer(newEdgesToTest);
00703 
00704         if (edgesToTest.empty())
00705         {
00706             FatalErrorIn("perturb") << "oops" << abort(FatalError);
00707         }
00708 
00709         // Re intersect moved edges.
00710         intersectEdges
00711         (
00712             surf1,
00713             points1,          // surf1 meshPoints (not localPoints!)
00714             query2,
00715             surf1PointTol,
00716             edgesToTest
00717         );
00718     }
00719 
00720     return iter;
00721 }
00722 
00723 
00724 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00725 
00726 
00727 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00728 
00729 
00730 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00731 
00732 
00733 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd