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

orientedSurface.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 "orientedSurface.H"
00030 #include "triSurfaceTools.H"
00031 #include "treeBoundBox.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 defineTypeNameAndDebug(Foam::orientedSurface, 0);
00036 
00037 
00038 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00039 
00040 // Return true if face uses edge from start to end.
00041 bool Foam::orientedSurface::edgeOrder
00042 (
00043     const labelledTri& f,
00044     const edge& e
00045 )
00046 {
00047     if
00048     (
00049         (f[0] == e[0] && f[1] == e[1])
00050      || (f[1] == e[0] && f[2] == e[1])
00051      || (f[2] == e[0] && f[0] == e[1])
00052     )
00053     {
00054         return true;
00055     }
00056     else
00057     {
00058         return false;
00059     }
00060 }
00061 
00062 
00063 // Return true if edge is used in opposite order in faces
00064 bool Foam::orientedSurface::consistentEdge
00065 (
00066     const edge& e,
00067     const labelledTri& f0,
00068     const labelledTri& f1
00069 )
00070 {
00071     return edgeOrder(f0, e) ^ edgeOrder(f1, e);
00072 }
00073 
00074 
00075 Foam::labelList Foam::orientedSurface::faceToEdge
00076 (
00077     const triSurface& s,
00078     const labelList& changedFaces
00079 )
00080 {
00081     labelList changedEdges(3*changedFaces.size());
00082     label changedI = 0;
00083 
00084     forAll(changedFaces, i)
00085     {
00086         const labelList& fEdges = s.faceEdges()[changedFaces[i]];
00087 
00088         forAll(fEdges, j)
00089         {
00090             changedEdges[changedI++] = fEdges[j];
00091         }
00092     }
00093     changedEdges.setSize(changedI);
00094 
00095     return changedEdges;
00096 }
00097 
00098 
00099 Foam::labelList Foam::orientedSurface::edgeToFace
00100 (
00101     const triSurface& s,
00102     const labelList& changedEdges,
00103     labelList& flip
00104 )
00105 {
00106     labelList changedFaces(2*changedEdges.size());
00107     label changedI = 0;
00108 
00109     forAll(changedEdges, i)
00110     {
00111         label edgeI = changedEdges[i];
00112 
00113         const labelList& eFaces = s.edgeFaces()[edgeI];
00114 
00115         if (eFaces.size() < 2)
00116         {
00117             // Do nothing, faces was already visited.
00118         }
00119         else if (eFaces.size() == 2)
00120         {
00121             label face0 = eFaces[0];
00122             label face1 = eFaces[1];
00123 
00124             const labelledTri& f0 = s.localFaces()[face0];
00125             const labelledTri& f1 = s.localFaces()[face1];
00126 
00127             if (flip[face0] == UNVISITED)
00128             {
00129                 if (flip[face1] == UNVISITED)
00130                 {
00131                     FatalErrorIn("orientedSurface::edgeToFace") << "Problem"
00132                         << abort(FatalError);
00133                 }
00134                 else
00135                 {
00136                     // Face1 has a flip state, face0 hasn't
00137                     if (consistentEdge(s.edges()[edgeI], f0, f1))
00138                     {
00139                         // Take over flip status
00140                         flip[face0] = (flip[face1] == FLIP ? FLIP : NOFLIP);
00141                     }
00142                     else
00143                     {
00144                         // Invert
00145                         flip[face0] = (flip[face1] == FLIP ? NOFLIP : FLIP);
00146                     }
00147                     changedFaces[changedI++] = face0;
00148                 }
00149             }
00150             else
00151             {
00152                 if (flip[face1] == UNVISITED)
00153                 {
00154                     // Face0 has a flip state, face1 hasn't
00155                     if (consistentEdge(s.edges()[edgeI], f0, f1))
00156                     {
00157                         flip[face1] = (flip[face0] == FLIP ? FLIP : NOFLIP);
00158                     }
00159                     else
00160                     {
00161                         flip[face1] = (flip[face0] == FLIP ? NOFLIP : FLIP);
00162                     }
00163                     changedFaces[changedI++] = face1;
00164                 }
00165             }
00166         }
00167         else
00168         {
00169             // Multiply connected. Do what?
00170         }
00171     }
00172     changedFaces.setSize(changedI);
00173 
00174     return changedFaces;
00175 }
00176 
00177 
00178 void Foam::orientedSurface::walkSurface
00179 (
00180     const triSurface& s,
00181     const label startFaceI,
00182     labelList& flipState
00183 )
00184 {
00185     // List of faces that were changed in the last iteration.
00186     labelList changedFaces(1, startFaceI);
00187     // List of edges that were changed in the last iteration.
00188     labelList changedEdges;
00189 
00190     while(true)
00191     {
00192         changedEdges = faceToEdge(s, changedFaces);
00193 
00194         if (debug)
00195         {
00196             Pout<< "From changedFaces:" << changedFaces.size()
00197                 << " to changedEdges:" << changedEdges.size()
00198                 << endl;
00199         }
00200 
00201         if (changedEdges.empty())
00202         {
00203             break;
00204         }
00205 
00206         changedFaces = edgeToFace(s, changedEdges, flipState);
00207 
00208         if (debug)
00209         {
00210             Pout<< "From changedEdges:" << changedEdges.size()
00211                 << " to changedFaces:" << changedFaces.size()
00212                 << endl;
00213         }
00214 
00215         if (changedFaces.empty())
00216         {
00217             break;
00218         }
00219     }
00220 }
00221 
00222 
00223 void Foam::orientedSurface::propagateOrientation
00224 (
00225     const triSurface& s,
00226     const point& samplePoint,
00227     const bool orientOutside,
00228     const label nearestFaceI,
00229     const point& nearestPt,
00230     labelList& flipState
00231 )
00232 {
00233     //
00234     // Determine orientation to normal on nearest face
00235     //
00236     triSurfaceTools::sideType side = triSurfaceTools::surfaceSide
00237     (
00238         s,
00239         samplePoint,
00240         nearestFaceI,
00241         nearestPt,
00242         10*SMALL
00243     );
00244 
00245     if (side == triSurfaceTools::UNKNOWN)
00246     {
00247         // Non-closed surface. Do what? For now behave as if no flipping
00248         // nessecary
00249         flipState[nearestFaceI] = NOFLIP;
00250     }
00251     else if ((side == triSurfaceTools::OUTSIDE) == orientOutside)
00252     {
00253         // outside & orientOutside or inside & !orientOutside
00254         // Normals on surface pointing correctly. No need to flip normals
00255         flipState[nearestFaceI] = NOFLIP;
00256     }
00257     else
00258     {
00259         // Need to flip normals.
00260         flipState[nearestFaceI] = FLIP;
00261     }
00262 
00263     if (debug)
00264     {
00265         vector n = triSurfaceTools::surfaceNormal(s, nearestFaceI, nearestPt);
00266 
00267         Pout<< "orientedSurface::propagateOrientation : starting face"
00268             << " orientation:" << nl
00269             << "     for samplePoint:" << samplePoint << nl
00270             << "     starting from point:" << nearestPt << nl
00271             << "     on face:" << nearestFaceI << nl
00272             << "     with normal:" << n << nl
00273             << "     decided side:" << label(side)
00274             << endl;
00275     }
00276 
00277     // Walk the surface from nearestFaceI, changing the flipstate.
00278     walkSurface(s, nearestFaceI, flipState);
00279 }
00280 
00281 
00282 bool Foam::orientedSurface::flipSurface
00283 (
00284     triSurface& s,
00285     const labelList& flipState
00286 )
00287 {
00288     bool hasFlipped = false;
00289 
00290     // Flip tris in s
00291     forAll(flipState, faceI)
00292     {
00293         if (flipState[faceI] == UNVISITED)
00294         {
00295             FatalErrorIn
00296             (
00297                 "orientSurface(const point&, const label, const point&)"
00298             )   << "unvisited face " << faceI
00299                 << abort(FatalError);
00300         }
00301         else if (flipState[faceI] == FLIP)
00302         {
00303             labelledTri& tri = s[faceI];
00304 
00305             label tmp = tri[0];
00306 
00307             tri[0] = tri[1];
00308             tri[1] = tmp;
00309 
00310             hasFlipped = true;
00311         }
00312     }
00313     // Recalculate normals
00314     s.clearOut();
00315 
00316     return hasFlipped;
00317 }
00318 
00319 
00320 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00321 
00322 // Null constructor
00323 Foam::orientedSurface::orientedSurface()
00324 :
00325     triSurface()
00326 {}
00327 
00328 
00329 // Construct from surface and point which defines outside
00330 Foam::orientedSurface::orientedSurface
00331 (
00332     const triSurface& surf,
00333     const point& samplePoint,
00334     const bool orientOutside
00335 )
00336 :
00337     triSurface(surf)
00338 {
00339     orient(*this, samplePoint, orientOutside);
00340 }
00341 
00342 
00343 // Construct from surface. Calculate outside point.
00344 Foam::orientedSurface::orientedSurface
00345 (
00346     const triSurface& surf,
00347     const bool orientOutside
00348 )
00349 :
00350     triSurface(surf)
00351 {
00352     // BoundBox calculation without localPoints
00353     treeBoundBox bb(surf.points(), surf.meshPoints());
00354 
00355     point outsidePoint = bb.max() + bb.span();
00356 
00357     orient(*this, outsidePoint, orientOutside);
00358 }
00359 
00360 
00361 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00362 
00363 bool Foam::orientedSurface::orient
00364 (
00365     triSurface& s,
00366     const point& samplePoint,
00367     const bool orientOutside
00368 )
00369 {
00370     bool anyFlipped = false;
00371 
00372     // Do initial flipping to make triangles consistent. Otherwise if the
00373     // nearest is e.g. on an edge inbetween inconsistent triangles it might
00374     // make the wrong decision.
00375     if (s.size() > 0)
00376     {
00377         // Whether face has to be flipped.
00378         //      UNVISITED: unvisited
00379         //      NOFLIP: no need to flip
00380         //      FLIP: need to flip
00381         labelList flipState(s.size(), UNVISITED);
00382 
00383         flipState[0] = NOFLIP;
00384         walkSurface(s, 0, flipState);
00385 
00386         anyFlipped = flipSurface(s, flipState);
00387     }
00388 
00389 
00390     // Whether face has to be flipped.
00391     //      UNVISITED: unvisited
00392     //      NOFLIP: no need to flip
00393     //      FLIP: need to flip
00394     labelList flipState(s.size(), UNVISITED);
00395 
00396 
00397     while (true)
00398     {
00399         // Linear search for nearest unvisited point on surface.
00400 
00401         scalar minDist = GREAT;
00402         point minPoint;
00403         label minFaceI = -1;
00404 
00405         forAll(s, faceI)
00406         {
00407             if (flipState[faceI] == UNVISITED)
00408             {
00409                 const labelledTri& f = s[faceI];
00410 
00411                 pointHit curHit =
00412                     triPointRef
00413                     (
00414                         s.points()[f[0]],
00415                         s.points()[f[1]],
00416                         s.points()[f[2]]
00417                     ).nearestPoint(samplePoint);
00418 
00419                 if (curHit.distance() < minDist)
00420                 {
00421                     minDist = curHit.distance();
00422                     minPoint = curHit.rawPoint();
00423                     minFaceI = faceI;
00424                 }
00425             }
00426         }
00427 
00428         // Did we find anything?
00429         if (minFaceI == -1)
00430         {
00431             break;
00432         }
00433 
00434         // From this nearest face see if needs to be flipped and then
00435         // go outwards.
00436         propagateOrientation
00437         (
00438             s,
00439             samplePoint,
00440             orientOutside,
00441             minFaceI,
00442             minPoint,
00443             flipState
00444         );
00445     }
00446 
00447     // Now finally flip triangles according to flipState.
00448     bool geomFlipped = flipSurface(s, flipState);
00449 
00450     return anyFlipped || geomFlipped;
00451 }
00452 
00453 
00454 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd