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

intersectedSurface.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 "intersectedSurface.H"
00028 #include "surfaceIntersection.H"
00029 #include "faceList.H"
00030 #include "faceTriangulation.H"
00031 #include "treeBoundBox.H"
00032 #include "OFstream.H"
00033 #include "error.H"
00034 #include "meshTools.H"
00035 #include "edgeSurface.H"
00036 #include "DynamicList.H"
00037 #include "transform.H"
00038 
00039 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00040 
00041 defineTypeNameAndDebug(Foam::intersectedSurface, 0);
00042 
00043 const Foam::label Foam::intersectedSurface::UNVISITED = 0;
00044 const Foam::label Foam::intersectedSurface::STARTTOEND = 1;
00045 const Foam::label Foam::intersectedSurface::ENDTOSTART = 2;
00046 const Foam::label Foam::intersectedSurface::BOTH = STARTTOEND | ENDTOSTART;
00047 
00048 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00049 
00050 // Write whole pointField and edges to stream
00051 void Foam::intersectedSurface::writeOBJ
00052 (
00053     const pointField& points,
00054     const edgeList& edges,
00055     Ostream& os
00056 )
00057 {
00058     forAll(points, pointI)
00059     {
00060         const point& pt = points[pointI];
00061 
00062         os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00063     }
00064     forAll(edges, edgeI)
00065     {
00066         const edge& e = edges[edgeI];
00067 
00068         os << "l " << e.start()+1 << ' ' << e.end()+1 << nl;
00069     }
00070 }
00071 
00072 
00073 // Write whole pointField and selected edges to stream
00074 void Foam::intersectedSurface::writeOBJ
00075 (
00076     const pointField& points,
00077     const edgeList& edges,
00078     const labelList& faceEdges,
00079     Ostream& os
00080 )
00081 {
00082     forAll(points, pointI)
00083     {
00084         const point& pt = points[pointI];
00085 
00086         os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00087     }
00088     forAll(faceEdges, i)
00089     {
00090         const edge& e = edges[faceEdges[i]];
00091 
00092         os << "l " << e.start()+1 << ' ' << e.end()+1 << nl;
00093     }
00094 }
00095 
00096 
00097 // write local points and edges to stream
00098 void Foam::intersectedSurface::writeLocalOBJ
00099 (
00100     const pointField& points,
00101     const edgeList& edges,
00102     const labelList& faceEdges,
00103     const fileName& fName
00104 )
00105 {
00106     OFstream os(fName);
00107 
00108     labelList pointMap(points.size(), -1);
00109 
00110     label maxVertI = 0;
00111 
00112     forAll(faceEdges, i)
00113     {
00114         const edge& e = edges[faceEdges[i]];
00115 
00116         forAll(e, i)
00117         {
00118             label pointI = e[i];
00119 
00120             if (pointMap[pointI] == -1)
00121             {
00122                 const point& pt = points[pointI];
00123 
00124                 os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00125 
00126                 pointMap[pointI] = maxVertI++;
00127             }
00128         }
00129     }
00130 
00131     forAll(faceEdges, i)
00132     {
00133         const edge& e = edges[faceEdges[i]];
00134 
00135         os << "l " << pointMap[e.start()]+1 << ' ' << pointMap[e.end()]+1
00136             << nl;
00137     }
00138 }
00139 
00140 
00141 // Write whole pointField and face to stream
00142 void Foam::intersectedSurface::writeOBJ
00143 (
00144     const pointField& points,
00145     const face& f,
00146     Ostream& os
00147 )
00148 {
00149     forAll(points, pointI)
00150     {
00151         const point& pt = points[pointI];
00152 
00153         os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00154     }
00155 
00156     os << 'f';
00157 
00158     forAll(f, fp)
00159     {
00160         os << ' ' << f[fp]+1;
00161     }
00162     os << nl;
00163 }
00164 
00165 
00166 // Print current visited state.
00167 void Foam::intersectedSurface::printVisit
00168 (
00169     const edgeList& edges,
00170     const labelList& edgeLabels,
00171     const Map<label>& visited
00172 )
00173 {
00174     Pout<< "Visited:" << nl;
00175     forAll(edgeLabels, i)
00176     {
00177         label edgeI = edgeLabels[i];
00178 
00179         const edge& e = edges[edgeI];
00180 
00181         label stat = visited[edgeI];
00182 
00183         if (stat == UNVISITED)
00184         {
00185             Pout<< "    edge:" << edgeI << "  verts:" << e
00186                 << "  unvisited" << nl;
00187         }
00188         else if (stat == STARTTOEND)
00189         {
00190             Pout<< "    edge:" << edgeI << "  from " << e[0]
00191                 << " to " << e[1] << nl;
00192         }
00193         else if (stat == ENDTOSTART)
00194         {
00195             Pout<< "    edge:" << edgeI << "  from " << e[1]
00196                 << " to " << e[0] << nl;
00197         }
00198         else
00199         {
00200             Pout<< "    edge:" << edgeI << "  both " << e
00201                 << nl;
00202         }
00203     }
00204     Pout<< endl;
00205 }
00206 
00207 
00208 
00209 // Check if the two vertices that f0 and f1 share are in the same order on
00210 // both faces.
00211 bool Foam::intersectedSurface::sameEdgeOrder
00212 (
00213     const labelledTri& fA,
00214     const labelledTri& fB
00215 )
00216 {
00217     forAll(fA, fpA)
00218     {
00219         label fpB = findIndex(fB, fA[fpA]);
00220 
00221         if (fpB != -1)
00222         {
00223             // Get prev/next vertex on fA
00224             label vA1 = fA[(fpA + 1) % 3];
00225             label vAMin1 = fA[fpA ? fpA-1 : 2];
00226 
00227             // Get prev/next vertex on fB
00228             label vB1 = fB[(fpB + 1) % 3];
00229             label vBMin1 = fB[fpB ? fpB-1 : 2];
00230 
00231             if (vA1 == vB1 || vAMin1 == vBMin1)
00232             {
00233                 return true;
00234             }
00235             else if (vA1 == vBMin1 || vAMin1 == vB1)
00236             {
00237                 // shared vertices in opposite order.
00238                 return false;
00239             }
00240             else
00241             {
00242                 FatalErrorIn("intersectedSurface::sameEdgeOrder")
00243                     << "Triangle:" << fA << " and triangle:" << fB
00244                     << " share a point but not an edge"
00245                     << abort(FatalError);
00246             }
00247         }
00248     }
00249 
00250     FatalErrorIn("intersectedSurface::sameEdgeOrder")
00251         << "Triangle:" << fA << " and triangle:" << fB
00252         << " do not share an edge"
00253         << abort(FatalError);
00254 
00255     return false;
00256 }
00257 
00258 
00259 void Foam::intersectedSurface::incCount
00260 (
00261     Map<label>& visited,
00262     const label key,
00263     const label offset
00264 )
00265 {
00266     Map<label>::iterator iter = visited.find(key);
00267 
00268     if (iter == visited.end())
00269     {
00270         visited.insert(key, offset);
00271     }
00272     else
00273     {
00274         iter() += offset;
00275     }
00276 }
00277 
00278 
00279 // Calculate point to edge addressing for the face given by the edge
00280 // subset faceEdges. Constructs facePointEdges which for every point
00281 // gives a list of edge labels connected to it.
00282 Foam::Map<Foam::DynamicList<Foam::label> >
00283 Foam::intersectedSurface::calcPointEdgeAddressing
00284 (
00285     const edgeSurface& eSurf,
00286     const label faceI
00287 )
00288 {
00289     const pointField& points = eSurf.points();
00290     const edgeList& edges = eSurf.edges();
00291 
00292     const labelList& fEdges = eSurf.faceEdges()[faceI];
00293 
00294     Map<DynamicList<label> > facePointEdges(4*fEdges.size());
00295 
00296     forAll(fEdges, i)
00297     {
00298         label edgeI = fEdges[i];
00299 
00300         const edge& e = edges[edgeI];
00301 
00302         // Add e.start to point-edges
00303         Map<DynamicList<label> >::iterator iter =
00304             facePointEdges.find(e.start());
00305 
00306         if (iter == facePointEdges.end())
00307         {
00308             DynamicList<label> oneEdge;
00309             oneEdge.append(edgeI);
00310             facePointEdges.insert(e.start(), oneEdge);
00311         }
00312         else
00313         {
00314             iter().append(edgeI);
00315         }
00316 
00317         // Add e.end to point-edges
00318         Map<DynamicList<label> >::iterator iter2 =
00319             facePointEdges.find(e.end());
00320 
00321         if (iter2 == facePointEdges.end())
00322         {
00323             DynamicList<label> oneEdge;
00324             oneEdge.append(edgeI);
00325             facePointEdges.insert(e.end(), oneEdge);
00326         }
00327         else
00328         {
00329             iter2().append(edgeI);
00330         }
00331     }
00332 
00333     // Shrink it
00334     for
00335     (
00336         Map<DynamicList<label> >::iterator iter = facePointEdges.begin();
00337         iter != facePointEdges.end();
00338         ++iter
00339     )
00340     {
00341         iter().shrink();
00342 
00343         // Check on dangling points.
00344         if (iter().empty())
00345         {
00346             FatalErrorIn
00347             (
00348                 "intersectedSurface::calcPointEdgeAddressing"
00349                 "(const edgeSurface&, const label)"
00350             )   << "Point:" << iter.key() << " used by too few edges:"
00351                 << iter() << abort(FatalError);
00352         }
00353     }
00354 
00355     if (debug & 2)
00356     {
00357         // Print facePointEdges
00358         Pout<< "calcPointEdgeAddressing: face consisting of edges:" << endl;
00359         forAll(fEdges, i)
00360         {
00361             label edgeI = fEdges[i];
00362             const edge& e = edges[edgeI];
00363             Pout<< "    " << edgeI << ' ' << e << points[e.start()]
00364                 << points[e.end()] << endl;
00365         }
00366 
00367         Pout<< "    Constructed point-edge adressing:" << endl;
00368         for
00369         (
00370             Map<DynamicList<label> >::iterator iter = facePointEdges.begin();
00371             iter != facePointEdges.end();
00372             ++iter
00373         )
00374         {
00375             Pout<< "    vertex " << iter.key() << " is connected to edges "
00376                 << iter() << endl;
00377         }
00378         Pout<<endl;
00379     }
00380 
00381     return facePointEdges;
00382 }
00383 
00384 
00385 // Find next (triangle or cut) edge label coming from point prevVertI on
00386 // prevEdgeI doing a right handed walk (i.e. following right wall).
00387 // Note: normal is provided externally. Could be deducted from angle between
00388 // two triangle edges but these could be in line.
00389 Foam::label Foam::intersectedSurface::nextEdge
00390 (
00391     const edgeSurface& eSurf,
00392     const Map<label>& visited,
00393     const label faceI,
00394     const vector& n,
00395     const Map<DynamicList<label> >& facePointEdges,
00396     const label prevEdgeI,
00397     const label prevVertI
00398 )
00399 {
00400     const pointField& points = eSurf.points();
00401     const edgeList& edges = eSurf.edges();
00402     const labelList& fEdges = eSurf.faceEdges()[faceI];
00403 
00404 
00405     // Edges connected to prevVertI
00406     const DynamicList<label>& connectedEdges = facePointEdges[prevVertI];
00407 
00408     if (connectedEdges.size() <= 1)
00409     {
00410         // Problem. Point not connected.
00411         {
00412             Pout<< "Writing face:" << faceI << " to face.obj" << endl;
00413             OFstream str("face.obj");
00414             writeOBJ(points, edges, fEdges, str);
00415 
00416             Pout<< "Writing connectedEdges edges to faceEdges.obj" << endl;
00417             writeLocalOBJ(points, edges, connectedEdges, "faceEdges.obj");
00418         }
00419 
00420         FatalErrorIn
00421         (
00422             "intersectedSurface::nextEdge(const pointField&, const edgeList&"
00423             ", const vector&, Map<DynamicList<label> >, const label"
00424             ", const label)"
00425         )   << "Problem: prevVertI:" << prevVertI << " on edge " << prevEdgeI
00426             << " has less than 2 connected edges."
00427             << " connectedEdges:" << connectedEdges << abort(FatalError);
00428 
00429         return -1;
00430     }
00431 
00432     if (connectedEdges.size() == 2)
00433     {
00434         // Simple case. Take other edge
00435         if (connectedEdges[0] == prevEdgeI)
00436         {
00437             if (debug & 2)
00438             {
00439                 Pout<< "Stepped from edge:" << edges[prevEdgeI]
00440                     << " to edge:" << edges[connectedEdges[1]]
00441                     << " chosen from candidates:" << connectedEdges << endl;
00442             }
00443             return connectedEdges[1];
00444         }
00445         else
00446         {
00447             if (debug & 2)
00448             {
00449                Pout<< "Stepped from edge:" << edges[prevEdgeI]
00450                    << " to edge:" << edges[connectedEdges[0]]
00451                    << " chosen from candidates:" << connectedEdges << endl;
00452             }
00453             return connectedEdges[0];
00454         }
00455     }
00456 
00457 
00458     // Multiple choices. Look at angle between edges.
00459 
00460     const edge& prevE = edges[prevEdgeI];
00461 
00462     // x-axis of coordinate system
00463     vector e0 = n ^ (points[prevE.otherVertex(prevVertI)] - points[prevVertI]);
00464     e0 /= mag(e0) + VSMALL;
00465 
00466     // Get y-axis of coordinate system
00467     vector e1 = n ^ e0;
00468 
00469     if (mag(mag(e1) - 1) > SMALL)
00470     {
00471         {
00472             Pout<< "Writing face:" << faceI << " to face.obj" << endl;
00473             OFstream str("face.obj");
00474             writeOBJ(points, edges, fEdges, str);
00475 
00476             Pout<< "Writing connectedEdges edges to faceEdges.obj" << endl;
00477             writeLocalOBJ(points, edges, connectedEdges, "faceEdges.obj");
00478         }
00479 
00480         FatalErrorIn("intersectedSurface::nextEdge")
00481             << "Unnormalized normal e1:" << e1
00482             << " formed from cross product of e0:" << e0 << " n:" << n
00483             << abort(FatalError);
00484     }
00485 
00486 
00487     //
00488     // Determine maximum angle over all connected (unvisited) edges.
00489     //
00490 
00491     scalar maxAngle = -GREAT;
00492     label maxEdgeI = -1;
00493 
00494     forAll(connectedEdges, connI)
00495     {
00496         label edgeI = connectedEdges[connI];
00497 
00498         if (edgeI != prevEdgeI)
00499         {
00500             label stat = visited[edgeI];
00501 
00502             const edge& e = edges[edgeI];
00503 
00504             // Find out whether walk of edge from prevVert would be acceptible.
00505             if
00506             (
00507                 stat == UNVISITED
00508              || (
00509                     stat == STARTTOEND
00510                  && prevVertI == e[1]
00511                 )
00512              || (
00513                     stat == ENDTOSTART
00514                  && prevVertI == e[0]
00515                 )
00516             )
00517             {
00518                 // Calculate angle of edge with respect to base e0, e1
00519                 vector vec =
00520                     n ^ (points[e.otherVertex(prevVertI)] - points[prevVertI]);
00521 
00522                 vec /= mag(vec) + VSMALL;
00523 
00524                 scalar angle = pseudoAngle(e0, e1, vec);
00525 
00526                 if (angle > maxAngle)
00527                 {
00528                     maxAngle = angle;
00529                     maxEdgeI = edgeI;
00530                 }
00531             }
00532         }
00533     }
00534 
00535 
00536     if (maxEdgeI == -1)
00537     {
00538         // No unvisited edge found
00539         {
00540             Pout<< "Writing face:" << faceI << " to face.obj" << endl;
00541             OFstream str("face.obj");
00542             writeOBJ(points, edges, fEdges, str);
00543 
00544             Pout<< "Writing connectedEdges edges to faceEdges.obj" << endl;
00545             writeLocalOBJ(points, edges, connectedEdges, "faceEdges.obj");
00546         }
00547 
00548         FatalErrorIn
00549         (
00550             "intersectedSurface::nextEdge(const pointField&, const edgeList&"
00551             ", const Map<label>&, const vector&"
00552             ", const Map<DynamicList<label> >&"
00553             ", const label, const label"
00554         )   << "Trying to step from edge " << edges[prevEdgeI]
00555             << ", vertex " << prevVertI
00556             << " but cannot find 'unvisited' edges among candidates:"
00557             << connectedEdges
00558             << abort(FatalError);
00559     }
00560 
00561     if (debug & 2)
00562     {
00563         Pout<< "Stepped from edge:" << edges[prevEdgeI]
00564             << " to edge:" << maxEdgeI << " angle:" << edges[maxEdgeI]
00565             << " with angle:" << maxAngle
00566             << endl;
00567     }
00568 
00569     return maxEdgeI;
00570 }
00571 
00572 
00573 // Create (polygonal) face by walking full circle starting from startVertI on
00574 // startEdgeI.
00575 // Uses nextEdge(..) to do the walking. Returns face. Updates visited with
00576 // the labels of visited edges.
00577 Foam::face Foam::intersectedSurface::walkFace
00578 (
00579     const edgeSurface& eSurf,
00580     const label faceI,
00581     const vector& n,
00582     const Map<DynamicList<label> >& facePointEdges,
00583 
00584     const label startEdgeI,
00585     const label startVertI,
00586 
00587     Map<label>& visited
00588 )
00589 {
00590     const pointField& points = eSurf.points();
00591     const edgeList& edges = eSurf.edges();
00592 
00593     // Overestimate size of face
00594     face f(eSurf.faceEdges()[faceI].size());
00595 
00596     label fp = 0;
00597 
00598     label vertI = startVertI;
00599     label edgeI = startEdgeI;
00600 
00601     while(true)
00602     {
00603         const edge& e = edges[edgeI];
00604 
00605         if (debug & 2)
00606         {
00607             Pout<< "Now at:" << endl
00608                 << "    edge:" << edgeI << " vertices:" << e
00609                 << " positions:" << points[e.start()] << ' ' << points[e.end()]
00610                 << "    vertex:" << vertI << endl;
00611         }
00612 
00613         // Mark edge as visited
00614         if (e[0] == vertI)
00615         {
00616             visited[edgeI] |= STARTTOEND;
00617         }
00618         else
00619         {
00620             visited[edgeI] |= ENDTOSTART;
00621         }
00622 
00623 
00624         // Store face vertex
00625         f[fp++] = vertI;
00626 
00627         // step to other vertex
00628         vertI = e.otherVertex(vertI);
00629 
00630         if (vertI == startVertI)
00631         {
00632             break;
00633         }
00634 
00635         // step from vertex to next edge
00636         edgeI = nextEdge
00637         (
00638             eSurf,
00639             visited,
00640             faceI,
00641             n,
00642             facePointEdges,
00643             edgeI,
00644             vertI
00645         );
00646     }
00647 
00648     f.setSize(fp);
00649 
00650     return f;
00651 }
00652 
00653 
00654 void Foam::intersectedSurface::findNearestVisited
00655 (
00656     const edgeSurface& eSurf,
00657     const label faceI,
00658     const Map<DynamicList<label> >& facePointEdges,
00659     const Map<label>& pointVisited,
00660     const point& pt,
00661     const label excludePointI,
00662 
00663     label& minVertI,
00664     scalar& minDist
00665 )
00666 {
00667     minVertI = -1;
00668     minDist = GREAT;
00669 
00670     forAllConstIter(Map<label>, pointVisited, iter)
00671     {
00672         label pointI = iter.key();
00673 
00674         if (pointI != excludePointI)
00675         {
00676             label nVisits = iter();
00677 
00678             if (nVisits == 2*facePointEdges[pointI].size())
00679             {
00680                 // Fully visited (i.e. both sides of all edges)
00681                 scalar dist = mag(eSurf.points()[pointI] - pt);
00682 
00683                 if (dist < minDist)
00684                 {
00685                     minDist = dist;
00686                     minVertI = pointI;
00687                 }
00688             }
00689         }
00690     }
00691 
00692     if (minVertI == -1)
00693     {
00694         const labelList& fEdges = eSurf.faceEdges()[faceI];
00695 
00696         SeriousErrorIn("intersectedSurface::findNearestVisited")
00697             << "Dumping face edges to faceEdges.obj" << endl;
00698 
00699         writeLocalOBJ(eSurf.points(), eSurf.edges(), fEdges, "faceEdges.obj");
00700 
00701         FatalErrorIn("intersectedSurface::findNearestVisited")
00702             << "No fully visited edge found for pt " << pt
00703             << abort(FatalError);
00704     }
00705 }
00706 
00707 
00708 // Sometimes there are bunches of edges that are not connected to the
00709 // other edges in the face. This routine tries to connect the loose
00710 // edges up to the 'proper' edges by adding two extra edges between a
00711 // properly connected edge and an unconnected one. Since at this level the
00712 // adressing is purely in form of points and a cloud of edges this can
00713 // be easily done.
00714 // (edges are to existing points. Don't want to introduce new vertices here
00715 // since then also neighbouring face would have to be split)
00716 Foam::faceList Foam::intersectedSurface::resplitFace
00717 (
00718     const triSurface& surf,
00719     const label faceI,
00720     const Map<DynamicList<label> >& facePointEdges,
00721     const Map<label>& visited,
00722     edgeSurface& eSurf
00723 )
00724 {
00725     {
00726         // Dump face for debugging.
00727         Pout<< "Writing face:" << faceI << " to face.obj" << endl;
00728         OFstream str("face.obj");
00729         writeOBJ(eSurf.points(), eSurf.edges(), eSurf.faceEdges()[faceI], str);
00730     }
00731 
00732 
00733     // Count the number of times point has been visited so we
00734     // can compare number to facePointEdges.
00735     Map<label> pointVisited(2*facePointEdges.size());
00736 
00737 
00738     {
00739         OFstream str0("visitedNone.obj");
00740         OFstream str1("visitedOnce.obj");
00741         OFstream str2("visitedTwice.obj");
00742         forAll(eSurf.points(), pointI)
00743         {
00744             const point& pt = eSurf.points()[pointI];
00745 
00746             str0 << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00747             str1 << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00748             str2 << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00749         }
00750 
00751 
00752     forAllConstIter(Map<label>, visited, iter)
00753     {
00754         label edgeI = iter.key();
00755 
00756         const edge& e = eSurf.edges()[edgeI];
00757 
00758         label stat = iter();
00759 
00760         if (stat == STARTTOEND || stat == ENDTOSTART)
00761         {
00762             incCount(pointVisited, e[0], 1);
00763             incCount(pointVisited, e[1], 1);
00764 
00765             str1 << "l " << e[0]+1 << ' ' << e[1]+1 << nl;
00766         }
00767         else if (stat == BOTH)
00768         {
00769             incCount(pointVisited, e[0], 2);
00770             incCount(pointVisited, e[1], 2);
00771 
00772             str2 << "l " << e[0]+1 << ' ' << e[1]+1 << nl;
00773         }
00774         else if (stat == UNVISITED)
00775         {
00776             incCount(pointVisited, e[0], 0);
00777             incCount(pointVisited, e[1], 0);
00778 
00779             str0 << "l " << e[0]+1 << ' ' << e[1]+1 << nl;
00780         }
00781     }
00782     }
00783 
00784 
00785     {
00786         forAllConstIter(Map<label>, pointVisited, iter)
00787         {
00788             label pointI = iter.key();
00789 
00790             label nVisits = iter();
00791 
00792             Pout<< "point:" << pointI << "  nVisited:" << nVisits
00793                 << "  pointEdges:" << facePointEdges[pointI].size() << endl;
00794         }
00795     }
00796 
00797 
00798     // Find nearest point pair where one is not fully visited and
00799     // the other is.
00800 
00801     label visitedVert0 = -1;
00802     label unvisitedVert0 = -1;
00803 
00804     {
00805         scalar minDist = GREAT;
00806 
00807         forAllConstIter(Map<DynamicList<label> >, facePointEdges, iter)
00808         {
00809             label pointI = iter.key();
00810 
00811             label nVisits = pointVisited[pointI];
00812 
00813             const DynamicList<label>& pEdges = iter();
00814 
00815             if (nVisits < 2*pEdges.size())
00816             {
00817                 // Not fully visited. Find nearest fully visited.
00818 
00819                 scalar nearDist;
00820                 label nearVertI;
00821 
00822                 findNearestVisited
00823                 (
00824                     eSurf,
00825                     faceI,
00826                     facePointEdges,
00827                     pointVisited,
00828                     eSurf.points()[pointI],
00829                     -1,                         // Do not exclude vertex
00830                     nearVertI,
00831                     nearDist
00832                 );
00833 
00834 
00835                 if (nearDist < minDist)
00836                 {
00837                     minDist = nearDist;
00838                     visitedVert0 = nearVertI;
00839                     unvisitedVert0 = pointI;
00840                 }
00841             }
00842         }
00843     }
00844 
00845 
00846     // Find second intersection.
00847     label visitedVert1 = -1;
00848     label unvisitedVert1 = -1;
00849     {
00850         scalar minDist = GREAT;
00851 
00852         forAllConstIter(Map<DynamicList<label> >, facePointEdges, iter)
00853         {
00854             label pointI = iter.key();
00855 
00856             if (pointI != unvisitedVert0)
00857             {
00858                 label nVisits = pointVisited[pointI];
00859 
00860                 const DynamicList<label>& pEdges = iter();
00861 
00862                 if (nVisits < 2*pEdges.size())
00863                 {
00864                     // Not fully visited. Find nearest fully visited.
00865 
00866                     scalar nearDist;
00867                     label nearVertI;
00868 
00869                     findNearestVisited
00870                     (
00871                         eSurf,
00872                         faceI,
00873                         facePointEdges,
00874                         pointVisited,
00875                         eSurf.points()[pointI],
00876                         visitedVert0,           // vertex to exclude
00877                         nearVertI,
00878                         nearDist
00879                     );
00880 
00881 
00882                     if (nearDist < minDist)
00883                     {
00884                         minDist = nearDist;
00885                         visitedVert1 = nearVertI;
00886                         unvisitedVert1 = pointI;
00887                     }
00888                 }
00889             }
00890         }
00891     }
00892 
00893 
00894     Pout<< "resplitFace : adding intersection from " << visitedVert0
00895         << " to " << unvisitedVert0 << endl
00896         << " and from " << visitedVert1
00897         << " to " << unvisitedVert1 << endl;
00898 
00899 
00900 //    // Add the new intersection edges to the edgeSurface
00901 //    edgeList additionalEdges(2);
00902 //    additionalEdges[0] = edge(visitedVert0, unvisitedVert0);
00903 //    additionalEdges[1] = edge(visitedVert1, unvisitedVert1);
00904 
00905     // Add the new intersection edges to the edgeSurface
00906     edgeList additionalEdges(1);
00907     additionalEdges[0] = edge(visitedVert0, unvisitedVert0);
00908 
00909     eSurf.addIntersectionEdges(faceI, additionalEdges);
00910 
00911     fileName newFName("face_" + Foam::name(faceI) + "_newEdges.obj");
00912     Pout<< "Dumping face:" << faceI << " to " << newFName << endl;
00913     writeLocalOBJ
00914     (
00915         eSurf.points(),
00916         eSurf.edges(),
00917         eSurf.faceEdges()[faceI],
00918         newFName
00919     );
00920 
00921     // Retry splitFace. Use recursion since is rare situation.
00922     return splitFace(surf, faceI, eSurf);
00923 }
00924 
00925 
00926 Foam::faceList Foam::intersectedSurface::splitFace
00927 (
00928     const triSurface& surf,
00929     const label faceI,
00930     edgeSurface& eSurf
00931 )
00932 {
00933     // Alias
00934     const pointField& points = eSurf.points();
00935     const edgeList& edges = eSurf.edges();
00936     const labelList& fEdges = eSurf.faceEdges()[faceI];
00937 
00938     // Create local (for the face only) point-edge connectivity.
00939     Map<DynamicList<label> > facePointEdges
00940     (
00941         calcPointEdgeAddressing
00942         (
00943             eSurf,
00944             faceI
00945         )
00946     );
00947 
00948     // Order in which edges have been walked. Initialize outside edges.
00949     Map<label> visited(fEdges.size()*2);
00950 
00951     forAll(fEdges, i)
00952     {
00953         label edgeI = fEdges[i];
00954 
00955         if (eSurf.isSurfaceEdge(edgeI))
00956         {
00957             // Edge is edge from original surface so an outside edge for
00958             // the current face.
00959             label surfEdgeI = eSurf.parentEdge(edgeI);
00960 
00961             label owner = surf.edgeOwner()[surfEdgeI];
00962 
00963             if
00964             (
00965                 owner == faceI
00966              || sameEdgeOrder
00967                 (
00968                     surf.localFaces()[owner],
00969                     surf.localFaces()[faceI]
00970                 )
00971             )
00972             {
00973                 // Edge is in same order as current face.
00974                 // Mark off the opposite order.
00975                 visited.insert(edgeI, ENDTOSTART);
00976             }
00977             else
00978             {
00979                 // Edge is in same order as current face.
00980                 // Mark off the opposite order.
00981                 visited.insert(edgeI, STARTTOEND);
00982             }
00983         }
00984         else
00985         {
00986             visited.insert(edgeI, UNVISITED);
00987         }
00988     }
00989 
00990 
00991 
00992     // Storage for faces
00993     DynamicList<face> faces(fEdges.size());
00994 
00995     while (true)
00996     {
00997         // Find starting edge:
00998         // - unvisted triangle edge
00999         // - once visited intersection edge
01000         // Give priority to triangle edges.
01001         label startEdgeI = -1;
01002         label startVertI = -1;
01003 
01004         forAll(fEdges, i)
01005         {
01006             label edgeI = fEdges[i];
01007 
01008             const edge& e = edges[edgeI];
01009 
01010             label stat = visited[edgeI];
01011 
01012             if (stat == STARTTOEND)
01013             {
01014                 startEdgeI = edgeI;
01015                 startVertI = e[1];
01016 
01017                 if (eSurf.isSurfaceEdge(edgeI))
01018                 {
01019                     break;
01020                 }
01021             }
01022             else if (stat == ENDTOSTART)
01023             {
01024                 startEdgeI = edgeI;
01025                 startVertI = e[0];
01026 
01027                 if (eSurf.isSurfaceEdge(edgeI))
01028                 {
01029                     break;
01030                 }
01031             }
01032         }
01033 
01034         if (startEdgeI == -1)
01035         {
01036             break;
01037         }
01038 
01039         //Pout<< "splitFace: starting walk from edge:" << startEdgeI
01040         //    << ' ' << edges[startEdgeI] << " vertex:" << startVertI << endl;
01041 
01043         //printVisit(eSurf.edges(), fEdges, visited);
01044 
01045         //{
01046         //    Pout<< "Writing face:" << faceI << " to face.obj" << endl;
01047         //    OFstream str("face.obj");
01048         //    writeOBJ(eSurf.points(), eSurf.edges(), fEdges, str);
01049         //}
01050 
01051         faces.append
01052         (
01053             walkFace
01054             (
01055                 eSurf,
01056                 faceI,
01057                 surf.faceNormals()[faceI],
01058                 facePointEdges,
01059 
01060                 startEdgeI,
01061                 startVertI,
01062 
01063                 visited
01064             )
01065         );
01066     }
01067 
01068 
01069     // Check if any unvisited edges left.
01070     forAll(fEdges, i)
01071     {
01072         label edgeI = fEdges[i];
01073 
01074         label stat = visited[edgeI];
01075 
01076         if (eSurf.isSurfaceEdge(edgeI) && stat != BOTH)
01077         {
01078             SeriousErrorIn("Foam::intersectedSurface::splitFace")
01079                 << "Dumping face edges to faceEdges.obj" << endl;
01080 
01081             writeLocalOBJ(points, edges, fEdges, "faceEdges.obj");
01082 
01083             FatalErrorIn("intersectedSurface::splitFace")
01084                << "Problem: edge " << edgeI << " vertices "
01085                 << edges[edgeI] << " on face " << faceI
01086                 << " has visited status " << stat << " from a "
01087                  << "righthanded walk along all"
01088                 << " of the triangle edges. Are the original surfaces"
01089                 << " closed and non-intersecting?"
01090                 << abort(FatalError);
01091         }
01092         else if (stat != BOTH)
01093         {
01094             //{
01095             //    Pout<< "Dumping faces so far to faces.obj" << nl
01096             //        << faces << endl;
01097             //
01098             //    OFstream str("faces.obj");
01099             //
01100             //    forAll(faces, i)
01101             //    {
01102             //        writeOBJ(points, faces[i], str);
01103             //    }
01104             //}
01105 
01106             Pout<< "** Resplitting **" << endl;
01107 
01108             // Redo face after introducing extra edge. Edge introduced
01109             // should be one nearest to any fully visited edge.
01110             return resplitFace
01111             (
01112                 surf,
01113                 faceI,
01114                 facePointEdges,
01115                 visited,
01116                 eSurf
01117             );
01118         }
01119     }
01120 
01121 
01122     // See if normal needs flipping.
01123     faces.shrink();
01124 
01125     vector n = faces[0].normal(eSurf.points());
01126 
01127     if ((n & surf.faceNormals()[faceI]) < 0)
01128     {
01129         forAll(faces, i)
01130         {
01131             reverse(faces[i]);
01132         }
01133     }
01134 
01135     return faces;
01136 }
01137 
01138 
01139 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
01140 
01141 // Null constructor
01142 Foam::intersectedSurface::intersectedSurface()
01143 :
01144     triSurface(),
01145     intersectionEdges_(0),
01146     faceMap_(0),
01147     nSurfacePoints_(0)
01148 {}
01149 
01150 
01151 // Construct from components
01152 Foam::intersectedSurface::intersectedSurface(const triSurface& surf)
01153 :
01154     triSurface(surf),
01155     intersectionEdges_(0),
01156     faceMap_(0),
01157     nSurfacePoints_(0)
01158 {}
01159 
01160 
01161 // Construct from surface and intersection
01162 Foam::intersectedSurface::intersectedSurface
01163 (
01164     const triSurface& surf,
01165     const bool isFirstSurface,
01166     const surfaceIntersection& inter
01167 )
01168 :
01169     triSurface(),
01170     intersectionEdges_(0),
01171     faceMap_(0),
01172     nSurfacePoints_(surf.nPoints())
01173 {
01174     if (inter.cutPoints().empty() && inter.cutEdges().empty())
01175     {
01176         // No intersection. Make straight copy.
01177         triSurface::operator=(surf);
01178 
01179         // Identity for face map
01180         faceMap_.setSize(size());
01181 
01182         forAll(faceMap_, faceI)
01183         {
01184             faceMap_[faceI] = faceI;
01185         }
01186         return;
01187     }
01188 
01189 
01190     // Calculate face-edge addressing for surface + intersection.
01191     edgeSurface eSurf(surf, isFirstSurface, inter);
01192 
01193     // Update point information for any removed points. (when are they removed?
01194     // - but better make sure)
01195     nSurfacePoints_ = eSurf.nSurfacePoints();
01196 
01197     // Now we have full points, edges and edge addressing for surf. Start
01198     // extracting faces and triangulate them.
01199 
01200     // Storage for new triangles of surface.
01201     DynamicList<labelledTri> newTris(eSurf.edges().size()/2);
01202 
01203     // Start in newTris for decomposed face.
01204     labelList startTriI(surf.size(), 0);
01205 
01206     forAll(surf, faceI)
01207     {
01208         startTriI[faceI] = newTris.size();
01209 
01210         if (eSurf.faceEdges()[faceI].size() != surf.faceEdges()[faceI].size())
01211         {
01212             // Face has been cut by intersection.
01213             // Cut face into multiple subfaces. Use faceEdge information
01214             // from edgeSurface only. (original triSurface 'surf' is used for
01215             // faceNormals and regionnumber only)
01216             faceList newFaces
01217             (
01218                 splitFace
01219                 (
01220                     surf,
01221                     faceI,              // current triangle
01222                     eSurf               // face-edge description of surface
01223                                         // + intersection
01224                 )
01225             );
01226             forAll(newFaces, newFaceI)
01227             {
01228                 const face& newF = newFaces[newFaceI];
01229 
01230 //                {
01231 //                    fileName fName
01232 //                    (
01233 //                        "face_"
01234 //                      + Foam::name(faceI)
01235 //                      + "_subFace_"
01236 //                      + Foam::name(newFaceI)
01237 //                      + ".obj"
01238 //                    );
01239 //                    Pout<< "Writing original face:" << faceI << " subFace:"
01240 //                        << newFaceI << " to " << fName << endl;
01241 //
01242 //                    OFstream str(fName);
01243 //
01244 //                    forAll(newF, fp)
01245 //                    {
01246 //                        meshTools::writeOBJ(str, eSurf.points()[newF[fp]]);
01247 //                    }
01248 //                    str << 'l';
01249 //                    forAll(newF, fp)
01250 //                    {
01251 //                        str << ' ' << fp+1;
01252 //                    }
01253 //                    str<< " 1" << nl;
01254 //                }
01255 
01256 
01257                 const vector& n = surf.faceNormals()[faceI];
01258                 const label region = surf[faceI].region();
01259 
01260                 faceTriangulation tris(eSurf.points(), newF, n);
01261 
01262                 forAll(tris, triI)
01263                 {
01264                     const triFace& t = tris[triI];
01265 
01266                     forAll(t, i)
01267                     {
01268                         if (t[i] < 0 || t[i] >= eSurf.points().size())
01269                         {
01270                             FatalErrorIn
01271                             (
01272                                 "intersectedSurface::intersectedSurface"
01273                             )   << "Face triangulation of face " << faceI
01274                                 << " uses points outside range 0.."
01275                                 << eSurf.points().size()-1 << endl
01276                                 << "Triangulation:"
01277                                 << tris << abort(FatalError);
01278                         }
01279                     }
01280 
01281                     newTris.append(labelledTri(t[0], t[1], t[2], region));
01282                 }
01283             }
01284         }
01285         else
01286         {
01287             // Face has not been cut at all. No need to renumber vertices since
01288             // eSurf keeps surface vertices first.
01289             newTris.append(surf.localFaces()[faceI]);
01290         }
01291     }
01292 
01293     newTris.shrink();
01294 
01295 
01296     // Construct triSurface. Note that addressing will be compact since
01297     // edgeSurface is compact.
01298     triSurface::operator=
01299     (
01300         triSurface
01301         (
01302             newTris,
01303             surf.patches(),
01304             eSurf.points()
01305         )
01306     );
01307 
01308     // Construct mapping back into original surface
01309     faceMap_.setSize(size());
01310 
01311     for (label faceI = 0; faceI < surf.size()-1; faceI++)
01312     {
01313         for (label triI = startTriI[faceI]; triI < startTriI[faceI+1]; triI++)
01314         {
01315             faceMap_[triI] = faceI;
01316         }
01317     }
01318     for (label triI = startTriI[surf.size()-1]; triI < size(); triI++)
01319     {
01320         faceMap_[triI] = surf.size()-1;
01321     }
01322 
01323 
01324     // Find edges on *this which originate from 'cuts'. (i.e. newEdgeI >=
01325     // nSurfaceEdges) Renumber edges into local triSurface numbering.
01326 
01327     intersectionEdges_.setSize(eSurf.edges().size() - eSurf.nSurfaceEdges());
01328 
01329     label intersectionEdgeI = 0;
01330 
01331     for
01332     (
01333         label edgeI = eSurf.nSurfaceEdges();
01334         edgeI < eSurf.edges().size();
01335         edgeI++
01336     )
01337     {
01338         // Get edge vertices in triSurface local numbering
01339         const edge& e = eSurf.edges()[edgeI];
01340         label surfStartI = meshPointMap()[e.start()];
01341         label surfEndI = meshPointMap()[e.end()];
01342 
01343         // Find edge connected to surfStartI which also uses surfEndI.
01344         label surfEdgeI = -1;
01345 
01346         const labelList& pEdges = pointEdges()[surfStartI];
01347 
01348         forAll(pEdges, i)
01349         {
01350             const edge& surfE = edges()[pEdges[i]];
01351 
01352             // Edge already connected to surfStart for sure. See if also
01353             // connects to surfEnd
01354             if (surfE.start() == surfEndI || surfE.end() == surfEndI)
01355             {
01356                 surfEdgeI = pEdges[i];
01357 
01358                 break;
01359             }
01360         }
01361 
01362         if (surfEdgeI != -1)
01363         {
01364             intersectionEdges_[intersectionEdgeI++] = surfEdgeI;
01365         }
01366         else
01367         {
01368             FatalErrorIn("intersectedSurface::intersectedSurface")
01369                 << "Cannot find edge among candidates " << pEdges
01370                 << " which uses points " << surfStartI
01371                 << " and " << surfEndI
01372                 << abort(FatalError);
01373         }
01374     }
01375 }
01376 
01377 
01378 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd